Skip to content

Commit

Permalink
browser backend todoed, raw frame format, threaded fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
l1npengtul committed Oct 24, 2022
1 parent 8f08c8a commit 4677ce0
Show file tree
Hide file tree
Showing 7 changed files with 269 additions and 174 deletions.
6 changes: 6 additions & 0 deletions nokhwa-core/src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ impl Buffer {
&self.buffer
}

/// Get a owned version of this buffer.
#[must_use]
pub fn buffer_bytes(&self) -> Bytes {
self.buffer.clone()
}

/// Get the [`FrameFormat`] of this buffer.
#[must_use]
pub fn source_frame_format(&self) -> FrameFormat {
Expand Down
45 changes: 45 additions & 0 deletions nokhwa-core/src/pixel_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ impl FormatDecoder for RgbFormat {
[pxv, pxv, pxv]
})
.collect()),
FrameFormat::RAWRGB => Ok(data.to_vec()),
}
}

Expand Down Expand Up @@ -92,6 +93,7 @@ impl FormatDecoder for RgbFormat {
});
Ok(())
}
FrameFormat::RAWRGB => {dest.copy_from_slice(data); Ok(())}
}
}
}
Expand Down Expand Up @@ -121,6 +123,13 @@ impl FormatDecoder for RgbAFormat {
[pxv, pxv, pxv, 255]
})
.collect()),
FrameFormat::RAWRGB => Ok(data
.chunks_exact(3)
.flat_map(|x| {
[x[0], x[1], x[2], 255]
})
.collect()
),
}
}

Expand Down Expand Up @@ -150,6 +159,20 @@ impl FormatDecoder for RgbAFormat {
});
Ok(())
}
FrameFormat::RAWRGB => {
data
.chunks_exact(3)
.enumerate()
.for_each(|(idx, px)| {
let index = idx * 4;
dest[index] = px[0];
dest[index + 1] = px[1];
dest[index + 2] = px[2];
dest[index + 3] = 255;

});
Ok(())
}
}
}
}
Expand Down Expand Up @@ -191,6 +214,11 @@ impl FormatDecoder for LumaFormat {
})
.collect()),
FrameFormat::GRAY => Ok(data.to_vec()),
FrameFormat::RAWRGB => {
Ok(data.chunks(3).map(|px| {
((px[0] as i32 + px[1] as i32 + px[2] as i32) / 3) as u8
}).collect())
},
}
}

Expand Down Expand Up @@ -219,6 +247,11 @@ impl FormatDecoder for LumaFormat {
});
Ok(())
}
FrameFormat::RAWRGB => Err(NokhwaError::ProcessFrameError {
src: fcc,
destination: "RGB => RGB".to_string(),
error: "Conversion Error".to_string(),
}),
}
}
}
Expand Down Expand Up @@ -260,6 +293,11 @@ impl FormatDecoder for LumaAFormat {
})
.collect()),
FrameFormat::GRAY => Ok(data.iter().flat_map(|x| [*x, 255]).collect()),
FrameFormat::RAWRGB => Err(NokhwaError::ProcessFrameError {
src: fcc,
destination: "RGB => RGB".to_string(),
error: "Conversion Error".to_string(),
}),
}
}

Expand Down Expand Up @@ -301,6 +339,13 @@ impl FormatDecoder for LumaAFormat {
});
Ok(())
}
FrameFormat::RAWRGB => {
Err(NokhwaError::ProcessFrameError {
src: fcc,
destination: "RGB => RGB".to_string(),
error: "Conversion Error".to_string(),
})
},
}
}
}
2 changes: 1 addition & 1 deletion nokhwa-core/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ pub trait CaptureBackendTrait {
let cfmt = self.camera_format();
let resolution = cfmt.resolution();
let pxwidth = match cfmt.format() {
FrameFormat::MJPEG | FrameFormat::YUYV => 3,
FrameFormat::MJPEG | FrameFormat::YUYV | FrameFormat::RAWRGB => 3,
FrameFormat::GRAY => 1,
};
if alpha {
Expand Down
8 changes: 6 additions & 2 deletions nokhwa-core/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ pub enum FrameFormat {
MJPEG,
YUYV,
GRAY,
RAWRGB,
}

impl Display for FrameFormat {
Expand All @@ -298,13 +299,16 @@ impl Display for FrameFormat {
FrameFormat::GRAY => {
write!(f, "GRAY")
}
FrameFormat::RAWRGB => {
write!(f, "RAWRGB")
},
}
}
}

#[must_use]
pub const fn frame_formats() -> [FrameFormat; 3] {
[FrameFormat::MJPEG, FrameFormat::YUYV, FrameFormat::GRAY]
pub const fn frame_formats() -> &'static [FrameFormat] {
&[FrameFormat::MJPEG, FrameFormat::YUYV, FrameFormat::GRAY, FrameFormat::RAWRGB]
}

/// Describes a Resolution.
Expand Down
Loading

0 comments on commit 4677ce0

Please sign in to comment.