Skip to content

Commit 7e7174b

Browse files
authored
Merge pull request #14 from joelgallant/fourcc-size
feat: restrict the size of format using [u8; 4] instead of runtime check
2 parents 5ee6541 + 0ff456e commit 7e7174b

File tree

3 files changed

+9
-21
lines changed

3 files changed

+9
-21
lines changed

examples/complex.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ fn main() {
77
for wformat in camera.formats() {
88
let format = wformat.unwrap();
99
println!("{:?}", format);
10-
println!(" {:?}", camera.resolutions(&format.format).unwrap());
10+
println!(" {:?}", camera.resolutions(format.format).unwrap());
1111
}
1212

1313
camera

examples/formats.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ fn main() {
77
let format = wformat.unwrap();
88
println!("{:?}", format);
99

10-
let resolutions = camera.resolutions(&format.format).unwrap();
10+
let resolutions = camera.resolutions(format.format).unwrap();
1111

1212
if let ResolutionInfo::Discretes(d) = resolutions {
1313
for resol in &d {
1414
println!(
1515
" {}x{} {:?}",
1616
resol.0,
1717
resol.1,
18-
camera.intervals(&format.format, *resol).unwrap()
18+
camera.intervals(format.format, *resol).unwrap()
1919
);
2020
}
2121
} else {

src/lib.rs

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ pub struct Config<'a> {
6868
pub resolution: (u32, u32),
6969
/// FourCC of format (e.g. `b"RGB3"`). Note that case matters.
7070
/// Default is `b"YUYV"`.
71-
pub format: &'a [u8],
71+
pub format: &'a [u8; 4],
7272
/// Storage method of interlaced video. See `FIELD_*` constants.
7373
/// [Details](http://linuxtv.org/downloads/v4l-dvb-apis/uapi/v4l/field-order.html#v4l2-field).
7474
/// Default is `FIELD_NONE` (progressive).
@@ -116,7 +116,7 @@ impl FormatInfo {
116116
}
117117
}
118118

119-
fn fourcc(fmt: &[u8]) -> u32 {
119+
fn fourcc(fmt: [u8; 4]) -> u32 {
120120
u32::from(fmt[0])
121121
| (u32::from(fmt[1])) << 8
122122
| (u32::from(fmt[2])) << 16
@@ -271,11 +271,7 @@ impl Camera {
271271
}
272272

273273
/// Get detailed info about the available resolutions.
274-
pub fn resolutions(&self, format: &[u8]) -> Result<ResolutionInfo> {
275-
if format.len() != 4 {
276-
return Err(Error::BadFormat);
277-
}
278-
274+
pub fn resolutions(&self, format: [u8; 4]) -> Result<ResolutionInfo> {
279275
let fourcc = FormatInfo::fourcc(format);
280276
let mut size = v4l2::Frmsizeenum::new(fourcc);
281277

@@ -310,11 +306,7 @@ impl Camera {
310306
}
311307

312308
/// Get detailed info about the available intervals.
313-
pub fn intervals(&self, format: &[u8], resolution: (u32, u32)) -> Result<IntervalInfo> {
314-
if format.len() != 4 {
315-
return Err(Error::BadFormat);
316-
}
317-
309+
pub fn intervals(&self, format: [u8; 4], resolution: (u32, u32)) -> Result<IntervalInfo> {
318310
let fourcc = FormatInfo::fourcc(format);
319311
let mut ival = v4l2::Frmivalenum::new(fourcc, resolution);
320312

@@ -515,7 +507,7 @@ impl Camera {
515507
pub fn start(&mut self, config: &Config<'_>) -> Result<()> {
516508
assert_eq!(self.state, State::Idle);
517509

518-
self.tune_format(config.resolution, config.format, config.field)?;
510+
self.tune_format(config.resolution, *config.format, config.field)?;
519511
self.tune_stream(config.interval)?;
520512
self.alloc_buffers(config.nbuffers)?;
521513

@@ -575,11 +567,7 @@ impl Camera {
575567
Ok(())
576568
}
577569

578-
fn tune_format(&self, resolution: (u32, u32), format: &[u8], field: u32) -> Result<()> {
579-
if format.len() != 4 {
580-
return Err(Error::BadFormat);
581-
}
582-
570+
fn tune_format(&self, resolution: (u32, u32), format: [u8; 4], field: u32) -> Result<()> {
583571
let fourcc = FormatInfo::fourcc(format);
584572
let mut fmt = v4l2::Format::new(resolution, fourcc, field as u32);
585573

0 commit comments

Comments
 (0)