use fast_image_resize::{ FilterType, PixelType, ResizeAlg, ResizeOptions, Resizer, images::{Image, ImageRef}, }; use yuv::{ YuvBiPlanarImage, YuvChromaSubsampling, YuvConversionMode, YuvPackedImage, YuvPlanarImage, YuvPlanarImageMut, YuvRange, YuvStandardMatrix, }; const BYTES_PER_PIXEL: u32 = 4; const RANGE: YuvRange = YuvRange::Limited; const MATRIX: YuvStandardMatrix = YuvStandardMatrix::Bt709; fn blank(width: u32, height: u32) -> Vec { vec![0; (width * height * BYTES_PER_PIXEL) as usize] } pub struct Bgra<'a> { pub data: &'a [u8], pub width: u32, pub height: u32, pub stride: u32, } pub struct I420 { pub y: Vec, pub u: Vec, pub v: Vec, pub width: u32, pub height: u32, } #[allow(clippy::too_many_arguments)] pub fn yuv_to_bgra( y: &[u8], y_stride: usize, u: &[u8], v: &[u8], uv_stride: usize, width: u32, height: u32, max_width: u32, ) -> Option<(u32, u32, Vec)> { if width == 0 || height == 0 { return None; } let image = YuvPlanarImage { y_plane: y, y_stride: y_stride as u32, u_plane: u, u_stride: uv_stride as u32, v_plane: v, v_stride: uv_stride as u32, width, height, }; let mut out = blank(width, height); yuv::yuv420_to_bgra(&image, &mut out, width * BYTES_PER_PIXEL, RANGE, MATRIX).ok()?; if width <= max_width { return Some((width, height, out)); } let out_height = (u64::from(height) * u64::from(max_width) / u64::from(width)).max(1) as u32; let source = Bgra { data: &out, width, height, stride: width * BYTES_PER_PIXEL, }; Some(( max_width, out_height, scale(&source, max_width, out_height)?, )) } pub fn yuyv_to_bgra(data: &[u8], width: u32, height: u32) -> Option> { if width == 0 || height == 0 || !width.is_multiple_of(2) { return None; } let stride = width * 2; if data.len() < (stride * height) as usize { return None; } let image = YuvPackedImage { yuy: data, yuy_stride: stride, width, height, }; let mut out = blank(width, height); yuv::yuyv422_to_bgra(&image, &mut out, width * BYTES_PER_PIXEL, RANGE, MATRIX).ok()?; Some(out) } pub fn nv12_to_bgra(data: &[u8], width: u32, height: u32) -> Option> { if width == 0 || height == 0 || !width.is_multiple_of(2) || !height.is_multiple_of(2) { return None; } let luma = (width * height) as usize; if data.len() < luma + luma / 2 { return None; } let image = YuvBiPlanarImage { y_plane: &data[..luma], y_stride: width, uv_plane: &data[luma..luma + luma / 2], uv_stride: width, width, height, }; let mut out = blank(width, height); yuv::yuv_nv12_to_bgra( &image, &mut out, width * BYTES_PER_PIXEL, RANGE, MATRIX, YuvConversionMode::Balanced, ) .ok()?; Some(out) } pub fn bgra_to_i420(source: &Bgra, width: u32, height: u32) -> Option { let scaled; let (data, stride) = if (source.width, source.height) == (width, height) { (source.data, source.stride) } else { scaled = scale(source, width, height)?; (scaled.as_slice(), width * BYTES_PER_PIXEL) }; let mut planes = YuvPlanarImageMut::alloc(width, height, YuvChromaSubsampling::Yuv420); yuv::bgra_to_yuv420( &mut planes, data, stride, RANGE, MATRIX, YuvConversionMode::Balanced, ) .ok()?; Some(I420 { y: planes.y_plane.borrow().to_vec(), u: planes.u_plane.borrow().to_vec(), v: planes.v_plane.borrow().to_vec(), width, height, }) } fn scale(source: &Bgra, width: u32, height: u32) -> Option> { let view = ImageRef::new( source.stride / BYTES_PER_PIXEL, source.height, source.data, PixelType::U8x4, ) .ok()?; let mut target = Image::new(width, height, PixelType::U8x4); let options = ResizeOptions::new() .resize_alg(ResizeAlg::Convolution(FilterType::Box)) .use_alpha(false) .crop(0.0, 0.0, f64::from(source.width), f64::from(source.height)); Resizer::new().resize(&view, &mut target, &options).ok()?; Some(target.into_vec()) } pub fn jpeg_to_bgra(data: &[u8]) -> Option<(u32, u32, Vec)> { let options = zune_core::options::DecoderOptions::default() .jpeg_set_out_colorspace(zune_core::colorspace::ColorSpace::BGRA); let mut decoder = zune_jpeg::JpegDecoder::new_with_options( zune_core::bytestream::ZCursor::new(data), options, ); let bgra = decoder.decode().ok()?; let (width, height) = decoder.dimensions()?; Some((width as u32, height as u32, bgra)) } #[cfg(test)] mod tests { use super::*; #[test] fn limited_range_maps_to_black_and_white() { let bgra = |luma| yuv_to_bgra(&[luma; 4], 2, &[128], &[128], 1, 2, 2, 1920); assert_eq!(bgra(235), Some((2, 2, [255, 255, 255, 255].repeat(4)))); assert_eq!(bgra(16), Some((2, 2, [0, 0, 0, 255].repeat(4)))); } #[test] fn yuyv_black_is_opaque_black() { assert_eq!( yuyv_to_bgra(&[16, 128, 16, 128], 2, 1), Some(vec![0, 0, 0, 255, 0, 0, 0, 255]) ); } }