use super::{
GeometryOverflow, InsufficientBuffer, MixedSinker, MixedSinkerError, RowIndexOutOfRange,
RowShapeMismatch, RowSlice, WidthAlignment, check_dimensions_match, rgb_row_buf_or_scratch,
rgba_plane_row_slice,
};
use crate::{
PixelSink,
row::{
expand_rgb_to_rgba_row, rgb_to_hsv_row, uyyvyy411_to_luma_row, uyyvyy411_to_luma_u16_row,
uyyvyy411_to_rgb_row, uyyvyy411_to_rgba_row,
},
source::{Uyyvyy411, Uyyvyy411Row, Uyyvyy411Sink},
};
impl<'a> MixedSinker<'a, Uyyvyy411> {
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn with_rgba(mut self, buf: &'a mut [u8]) -> Result<Self, MixedSinkerError> {
self.set_rgba(buf)?;
Ok(self)
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn set_rgba(&mut self, buf: &'a mut [u8]) -> Result<&mut Self, MixedSinkerError> {
let expected = self.frame_elems(4)?;
if buf.len() < expected {
return Err(MixedSinkerError::InsufficientRgbaBuffer(
InsufficientBuffer::new(expected, buf.len()),
));
}
self.rgba = Some(buf);
Ok(self)
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn with_luma_u16(mut self, buf: &'a mut [u16]) -> Result<Self, MixedSinkerError> {
self.set_luma_u16(buf)?;
Ok(self)
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn set_luma_u16(&mut self, buf: &'a mut [u16]) -> Result<&mut Self, MixedSinkerError> {
let expected_elems = self.frame_pixels()?;
if buf.len() < expected_elems {
return Err(MixedSinkerError::InsufficientLumaU16Buffer(
InsufficientBuffer::new(expected_elems, buf.len()),
));
}
self.luma_u16 = Some(buf);
Ok(self)
}
}
impl Uyyvyy411Sink for MixedSinker<'_, Uyyvyy411> {}
impl PixelSink for MixedSinker<'_, Uyyvyy411> {
type Input<'r> = Uyyvyy411Row<'r>;
type Error = MixedSinkerError;
fn begin_frame(&mut self, width: u32, height: u32) -> Result<(), Self::Error> {
check_dimensions_match(self.width, self.height, width, height)?;
if self.width & 3 != 0 {
return Err(MixedSinkerError::WidthAlignment(
WidthAlignment::multiple_of_four(self.width),
));
}
Ok(())
}
fn process(&mut self, row: Uyyvyy411Row<'_>) -> Result<(), Self::Error> {
let w = self.width;
let h = self.height;
let idx = row.row();
let use_simd = self.simd;
if w & 3 != 0 {
return Err(MixedSinkerError::WidthAlignment(
WidthAlignment::multiple_of_four(w),
));
}
let packed_expected =
w.checked_mul(3)
.map(|n| n / 2)
.ok_or(MixedSinkerError::GeometryOverflow(GeometryOverflow::new(
w, h, 3,
)))?;
if row.uyyvyy().len() != packed_expected {
return Err(MixedSinkerError::RowShapeMismatch(RowShapeMismatch::new(
RowSlice::Uyyvyy411Packed,
idx,
packed_expected,
row.uyyvyy().len(),
)));
}
if idx >= self.height {
return Err(MixedSinkerError::RowIndexOutOfRange(
RowIndexOutOfRange::new(idx, self.height),
));
}
let Self {
rgb,
rgba,
luma,
luma_u16,
hsv,
rgb_scratch,
..
} = self;
let one_plane_start = idx * w;
let one_plane_end = one_plane_start + w;
let packed = row.uyyvyy();
if let Some(luma) = luma.as_deref_mut() {
uyyvyy411_to_luma_row(
packed,
&mut luma[one_plane_start..one_plane_end],
w,
use_simd,
);
}
if let Some(buf) = luma_u16.as_deref_mut() {
uyyvyy411_to_luma_u16_row(
packed,
&mut buf[one_plane_start..one_plane_end],
w,
use_simd,
);
}
let want_rgb = rgb.is_some();
let want_rgba = rgba.is_some();
let want_hsv = hsv.is_some();
let need_rgb_kernel = want_rgb || want_hsv;
if want_rgba && !need_rgb_kernel {
let rgba_buf = rgba.as_deref_mut().unwrap();
let rgba_row = rgba_plane_row_slice(rgba_buf, one_plane_start, one_plane_end, w, h)?;
uyyvyy411_to_rgba_row(
packed,
rgba_row,
w,
row.matrix(),
row.full_range(),
use_simd,
);
return Ok(());
}
if !need_rgb_kernel {
return Ok(());
}
let rgb_row = rgb_row_buf_or_scratch(
rgb.as_deref_mut(),
rgb_scratch,
one_plane_start,
one_plane_end,
w,
h,
)?;
uyyvyy411_to_rgb_row(packed, rgb_row, w, row.matrix(), row.full_range(), use_simd);
if let Some(hsv) = hsv.as_mut() {
let (h, s, v) = hsv.hsv();
rgb_to_hsv_row(
rgb_row,
&mut h[one_plane_start..one_plane_end],
&mut s[one_plane_start..one_plane_end],
&mut v[one_plane_start..one_plane_end],
w,
use_simd,
);
}
if let Some(buf) = rgba.as_deref_mut() {
let rgba_row = rgba_plane_row_slice(buf, one_plane_start, one_plane_end, w, h)?;
expand_rgb_to_rgba_row(rgb_row, rgba_row, w);
}
Ok(())
}
}