Skip to content

Commit

Permalink
Update ffimage to v0.10.0
Browse files Browse the repository at this point in the history
Signed-off-by: Christopher N. Hesse <raymanfx@gmail.com>
  • Loading branch information
raymanfx committed Oct 28, 2023
1 parent 24324eb commit c11737e
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 44 deletions.
4 changes: 2 additions & 2 deletions eye/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ jpeg = ["jpeg-decoder"]

[dependencies]
eye-hal = { version = "0.1.0", path = "../eye-hal" }
ffimage = "0.9.0"
ffimage_yuv = "0.9.0"
ffimage = "0.10.0"
ffimage_yuv = "0.10.0"

jpeg-decoder = { version = "0.2.1", optional = true }

Expand Down
36 changes: 15 additions & 21 deletions eye/src/colorconvert/codec/rgb.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use ffimage::color::{Bgr, Rgb};
use ffimage::packed::{ImageBuffer, ImageView};
use ffimage::traits::{Convert, Pixel};
use ffimage::{
color::{Bgr, Rgb},
iter::{BytesExt, ColorConvertExt, PixelsExt},
};

use eye_hal::format::{ImageFormat, PixelFormat};

Expand Down Expand Up @@ -81,26 +82,19 @@ impl Codec for Instance {
}

pub fn convert_to_bgr(src: &[u8], src_fmt: &ImageFormat, dst: &mut Vec<u8>) -> Result<()> {
let mut bgr = ImageBuffer::<Bgr<u8>>::new(src_fmt.width, src_fmt.height, 0u8);
match convert(src, src_fmt, &mut bgr) {
Ok(()) => {
*dst = bgr.into_buf();
Ok(())
}
Err(e) => Err(e),
let src_len = (src_fmt.width * src_fmt.height * 3) as usize;
let dst_len = (src_fmt.width * src_fmt.height * 3) as usize;
if src_len != src.len() {
return Err(Error::from(ErrorKind::InvalidBuffer));
}
}

fn convert<DP>(src: &[u8], src_fmt: &ImageFormat, dst: &mut ImageBuffer<DP>) -> Result<()>
where
DP: Pixel + From<Rgb<u8>> + Copy + Send,
DP::T: Default + Clone + Send,
{
let rgb = match ImageView::<Rgb<u8>>::from_buf(src, src_fmt.width, src_fmt.height) {
Some(view) => view,
None => return Err(Error::from(ErrorKind::InvalidBuffer)),
};
dst.resize(dst_len, 0);
src.iter()
.copied()
.pixels::<Rgb<u8>>()
.colorconvert::<Bgr<u8>>()
.bytes()
.write(dst);

rgb.convert(dst);
Ok(())
}
56 changes: 35 additions & 21 deletions eye/src/colorconvert/codec/yuv.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use ffimage::color::Rgb;
use ffimage::packed::{ImageBuffer, ImageView};
use ffimage::traits::Convert;
use ffimage_yuv::{yuv::Yuv, yuyv::Yuyv};
use ffimage::{
color::Rgb,
iter::{BytesExt, ColorConvertExt, PixelsExt},
};
use ffimage_yuv::{yuv::Yuv, yuv422::Yuv422};

use eye_hal::format::{ImageFormat, PixelFormat};

Expand Down Expand Up @@ -86,26 +87,39 @@ impl Codec for Instance {
}

pub fn yuv444_to_rgb(src: &[u8], src_fmt: &ImageFormat, dst: &mut Vec<u8>) -> Result<()> {
let yuv444 = match ImageView::<Yuv<u8>>::from_buf(src, src_fmt.width, src_fmt.height) {
Some(view) => view,
None => return Err(Error::from(ErrorKind::InvalidBuffer)),
};
let mut rgb = ImageBuffer::<Rgb<u8>>::new(src_fmt.width, src_fmt.height, 0u8);
yuv444.convert(&mut rgb);
*dst = rgb.into_buf();
let src_len = (src_fmt.width * src_fmt.height * 3) as usize;
let dst_len = (src_fmt.width * src_fmt.height * 3) as usize;
if src_len != src.len() {
return Err(Error::from(ErrorKind::InvalidBuffer));
}

dst.resize(dst_len, 0);
src.iter()
.copied()
.pixels::<Yuv<u8>>()
.colorconvert::<Rgb<u8>>()
.bytes()
.write(dst);

Ok(())
}

pub fn yuv422_to_rgb(src: &[u8], src_fmt: &ImageFormat, dst: &mut Vec<u8>) -> Result<()> {
// First stage conversion: YUV 4:2:0 --> YUV 4:4:4
let yuv422 = match ImageView::<Yuyv<u8>>::from_buf(src, src_fmt.width, src_fmt.height) {
Some(view) => view,
None => return Err(Error::from(ErrorKind::InvalidBuffer)),
};
let mut yuv444 = ImageBuffer::<Yuv<u8>>::new(src_fmt.width, src_fmt.height, 0u8);
yuv422.convert(&mut yuv444);

// Second stage conversion: YUV 4:4:4 --> RGB
yuv444_to_rgb(yuv444.as_ref(), src_fmt, dst)
let src_len = (src_fmt.width * src_fmt.height * 2) as usize;
let dst_len = (src_fmt.width * src_fmt.height * 3) as usize;
if src_len != src.len() {
return Err(Error::from(ErrorKind::InvalidBuffer));
}

dst.resize(dst_len, 0);
src.iter()
.copied()
.pixels::<Yuv422<u8, 0, 2, 1, 3>>()
.colorconvert::<[Yuv<u8>; 2]>()
.flatten()
.colorconvert::<Rgb<u8>>()
.bytes()
.write(dst);

Ok(())
}

0 comments on commit c11737e

Please sign in to comment.