Skip to content

Commit

Permalink
examples: glium: Add MJPG format support
Browse files Browse the repository at this point in the history
Most consumer class hardware supports Motion-JPEG (MJPG) capture,
but not raw RGB3 formats.

Signed-off-by: Christopher N. Hesse <[email protected]>
  • Loading branch information
raymanfx committed Feb 18, 2022
1 parent 7bc4cd9 commit e46d0bd
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Multi-planar capture will not be targeted in the near future unless someone else
## [0.13.0]
### Added
- Handling of boolean and button controls
- MJPG (consumer class hardware) support in `glium` example
### Changed
- Simplified examples, removing clap argument parsing
- Unified Value/Value64 control types into a single Integer enum variant
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ v4l2-sys = { path = "v4l2-sys", version = "0.2.0", package="v4l2-sys-mit", optio

[dev-dependencies]
glium = "0.27.0"
jpeg-decoder = "0.2.1"

[features]
default = ["v4l2"]
Expand Down
30 changes: 24 additions & 6 deletions examples/glium.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ use std::time::Instant;
use glium::index::PrimitiveType;
use glium::{glutin, Surface};
use glium::{implement_vertex, program, uniform};

use jpeg_decoder as jpeg;

use v4l::buffer::Type;
use v4l::io::traits::CaptureStream;
use v4l::prelude::*;
Expand All @@ -29,15 +32,21 @@ fn main() -> io::Result<()> {
format = dev.format()?;
params = dev.params()?;

// enforce RGB3
// try RGB3 first
format.fourcc = FourCC::new(b"RGB3");
format = dev.set_format(&format)?;

if format.fourcc != FourCC::new(b"RGB3") {
return Err(io::Error::new(
io::ErrorKind::Other,
"RGB3 not supported by the device, but required by this example!",
));
// fallback to Motion-JPEG
format.fourcc = FourCC::new(b"MJPG");
format = dev.set_format(&format)?;

if format.fourcc != FourCC::new(b"MJPG") {
return Err(io::Error::new(
io::ErrorKind::Other,
"neither RGB3 nor MJPG supported by the device, but required by this example!",
));
}
}
}

Expand Down Expand Up @@ -128,7 +137,16 @@ fn main() -> io::Result<()> {

loop {
let (buf, _) = stream.next().unwrap();
let data = buf.to_vec();
let data = match &format.fourcc.repr {
b"RGB3" => buf.to_vec(),
b"MJPG" => {
// Decode the JPEG frame to RGB
let mut decoder = jpeg::Decoder::new(buf);
let pixels = decoder.decode().expect("failed to decode JPEG");
pixels
}
_ => panic!("invalid buffer pixelformat"),
};
tx.send(data).unwrap();
}
});
Expand Down

0 comments on commit e46d0bd

Please sign in to comment.