yumush
Clone
https://git.tahinli.com/Tahinli/yumush.git
0 watching·0 forks·Rust·AGPL-3.0·6.8 MiB·27ceb05·Tahinli·8 hours ago·399 commits
| 1 | use fast_image_resize::{ |
| 2 | FilterType, PixelType, ResizeAlg, ResizeOptions, Resizer, |
| 3 | images::{Image, ImageRef}, |
| 4 | }; |
| 5 | use yuv::{ |
| 6 | YuvBiPlanarImage, YuvChromaSubsampling, YuvConversionMode, YuvPackedImage, YuvPlanarImage, |
| 7 | YuvPlanarImageMut, YuvRange, YuvStandardMatrix, |
| 8 | }; |
| 9 | |
| 10 | const BYTES_PER_PIXEL: u32 = 4; |
| 11 | const RANGE: YuvRange = YuvRange::Limited; |
| 12 | const MATRIX: YuvStandardMatrix = YuvStandardMatrix::Bt709; |
| 13 | |
| 14 | fn blank(width: u32, height: u32) -> Vec<u8> { |
| 15 | vec![0; (width * height * BYTES_PER_PIXEL) as usize] |
| 16 | } |
| 17 | |
| 18 | pub struct Bgra<'a> { |
| 19 | pub data: &'a [u8], |
| 20 | pub width: u32, |
| 21 | pub height: u32, |
| 22 | pub stride: u32, |
| 23 | } |
| 24 | |
| 25 | pub struct I420 { |
| 26 | pub y: Vec<u8>, |
| 27 | pub u: Vec<u8>, |
| 28 | pub v: Vec<u8>, |
| 29 | pub width: u32, |
| 30 | pub height: u32, |
| 31 | } |
| 32 | |
| 33 | #[allow(clippy::too_many_arguments)] |
| 34 | pub fn yuv_to_bgra( |
| 35 | y: &[u8], |
| 36 | y_stride: usize, |
| 37 | u: &[u8], |
| 38 | v: &[u8], |
| 39 | uv_stride: usize, |
| 40 | width: u32, |
| 41 | height: u32, |
| 42 | max_width: u32, |
| 43 | ) -> Option<(u32, u32, Vec<u8>)> { |
| 44 | if width == 0 || height == 0 { |
| 45 | return None; |
| 46 | } |
| 47 | let image = YuvPlanarImage { |
| 48 | y_plane: y, |
| 49 | y_stride: y_stride as u32, |
| 50 | u_plane: u, |
| 51 | u_stride: uv_stride as u32, |
| 52 | v_plane: v, |
| 53 | v_stride: uv_stride as u32, |
| 54 | width, |
| 55 | height, |
| 56 | }; |
| 57 | let mut out = blank(width, height); |
| 58 | yuv::yuv420_to_bgra(&image, &mut out, width * BYTES_PER_PIXEL, RANGE, MATRIX).ok()?; |
| 59 | if width <= max_width { |
| 60 | return Some((width, height, out)); |
| 61 | } |
| 62 | let out_height = (u64::from(height) * u64::from(max_width) / u64::from(width)).max(1) as u32; |
| 63 | let source = Bgra { |
| 64 | data: &out, |
| 65 | width, |
| 66 | height, |
| 67 | stride: width * BYTES_PER_PIXEL, |
| 68 | }; |
| 69 | Some(( |
| 70 | max_width, |
| 71 | out_height, |
| 72 | scale(&source, max_width, out_height)?, |
| 73 | )) |
| 74 | } |
| 75 | |
| 76 | pub fn yuyv_to_bgra(data: &[u8], width: u32, height: u32) -> Option<Vec<u8>> { |
| 77 | if width == 0 || height == 0 || !width.is_multiple_of(2) { |
| 78 | return None; |
| 79 | } |
| 80 | let stride = width * 2; |
| 81 | if data.len() < (stride * height) as usize { |
| 82 | return None; |
| 83 | } |
| 84 | let image = YuvPackedImage { |
| 85 | yuy: data, |
| 86 | yuy_stride: stride, |
| 87 | width, |
| 88 | height, |
| 89 | }; |
| 90 | let mut out = blank(width, height); |
| 91 | yuv::yuyv422_to_bgra(&image, &mut out, width * BYTES_PER_PIXEL, RANGE, MATRIX).ok()?; |
| 92 | Some(out) |
| 93 | } |
| 94 | |
| 95 | pub fn nv12_to_bgra(data: &[u8], width: u32, height: u32) -> Option<Vec<u8>> { |
| 96 | if width == 0 || height == 0 || !width.is_multiple_of(2) || !height.is_multiple_of(2) { |
| 97 | return None; |
| 98 | } |
| 99 | let luma = (width * height) as usize; |
| 100 | if data.len() < luma + luma / 2 { |
| 101 | return None; |
| 102 | } |
| 103 | let image = YuvBiPlanarImage { |
| 104 | y_plane: &data[..luma], |
| 105 | y_stride: width, |
| 106 | uv_plane: &data[luma..luma + luma / 2], |
| 107 | uv_stride: width, |
| 108 | width, |
| 109 | height, |
| 110 | }; |
| 111 | let mut out = blank(width, height); |
| 112 | yuv::yuv_nv12_to_bgra( |
| 113 | &image, |
| 114 | &mut out, |
| 115 | width * BYTES_PER_PIXEL, |
| 116 | RANGE, |
| 117 | MATRIX, |
| 118 | YuvConversionMode::Balanced, |
| 119 | ) |
| 120 | .ok()?; |
| 121 | Some(out) |
| 122 | } |
| 123 | |
| 124 | pub fn bgra_to_i420(source: &Bgra, width: u32, height: u32) -> Option<I420> { |
| 125 | let resized = if (source.width, source.height) != (width, height) { |
| 126 | Some(scale(source, width, height)?) |
| 127 | } else { |
| 128 | None |
| 129 | }; |
| 130 | let (data, stride) = match &resized { |
| 131 | Some(pixels) => (pixels.as_slice(), width * BYTES_PER_PIXEL), |
| 132 | None => (source.data, source.stride), |
| 133 | }; |
| 134 | let mut planes = YuvPlanarImageMut::alloc(width, height, YuvChromaSubsampling::Yuv420); |
| 135 | yuv::bgra_to_yuv420( |
| 136 | &mut planes, |
| 137 | data, |
| 138 | stride, |
| 139 | RANGE, |
| 140 | MATRIX, |
| 141 | YuvConversionMode::Balanced, |
| 142 | ) |
| 143 | .ok()?; |
| 144 | Some(I420 { |
| 145 | y: planes.y_plane.borrow().to_vec(), |
| 146 | u: planes.u_plane.borrow().to_vec(), |
| 147 | v: planes.v_plane.borrow().to_vec(), |
| 148 | width, |
| 149 | height, |
| 150 | }) |
| 151 | } |
| 152 | |
| 153 | fn scale(source: &Bgra, width: u32, height: u32) -> Option<Vec<u8>> { |
| 154 | let view = ImageRef::new( |
| 155 | source.stride / BYTES_PER_PIXEL, |
| 156 | source.height, |
| 157 | source.data, |
| 158 | PixelType::U8x4, |
| 159 | ) |
| 160 | .ok()?; |
| 161 | let mut target = Image::new(width, height, PixelType::U8x4); |
| 162 | let options = ResizeOptions::new() |
| 163 | .resize_alg(ResizeAlg::Convolution(FilterType::Box)) |
| 164 | .use_alpha(false) |
| 165 | .crop(0.0, 0.0, f64::from(source.width), f64::from(source.height)); |
| 166 | Resizer::new().resize(&view, &mut target, &options).ok()?; |
| 167 | Some(target.into_vec()) |
| 168 | } |
| 169 | |
| 170 | pub fn jpeg_to_bgra(data: &[u8]) -> Option<(u32, u32, Vec<u8>)> { |
| 171 | let options = zune_core::options::DecoderOptions::default() |
| 172 | .jpeg_set_out_colorspace(zune_core::colorspace::ColorSpace::BGRA); |
| 173 | let mut decoder = zune_jpeg::JpegDecoder::new_with_options( |
| 174 | zune_core::bytestream::ZCursor::new(data), |
| 175 | options, |
| 176 | ); |
| 177 | let bgra = decoder.decode().ok()?; |
| 178 | let (width, height) = decoder.dimensions()?; |
| 179 | Some((width as u32, height as u32, bgra)) |
| 180 | } |
| 181 | |
| 182 | #[cfg(test)] |
| 183 | mod tests { |
| 184 | use super::*; |
| 185 | |
| 186 | #[test] |
| 187 | fn limited_range_maps_to_black_and_white() { |
| 188 | let bgra = |luma| yuv_to_bgra(&[luma; 4], 2, &[128], &[128], 1, 2, 2, 1920); |
| 189 | assert_eq!(bgra(235), Some((2, 2, [255, 255, 255, 255].repeat(4)))); |
| 190 | assert_eq!(bgra(16), Some((2, 2, [0, 0, 0, 255].repeat(4)))); |
| 191 | } |
| 192 | |
| 193 | #[test] |
| 194 | fn yuyv_black_is_opaque_black() { |
| 195 | assert_eq!( |
| 196 | yuyv_to_bgra(&[16, 128, 16, 128], 2, 1), |
| 197 | Some(vec![0, 0, 0, 255, 0, 0, 0, 255]) |
| 198 | ); |
| 199 | } |
| 200 | } |