use super::{
GeometryOverflow, InsufficientBuffer, MixedSinker, MixedSinkerError, RowIndexOutOfRange,
RowShapeMismatch, RowSlice, check_dimensions_match, check_frozen_alpha_mode,
deinterleave_y_high_bit_masked, packed_yuva444_filter_resample, packed_yuva444_resample,
reset_high_bit_yuva_streams, rgb_row_buf_or_scratch, rgba_plane_row_slice,
rgba_u16_plane_row_slice,
};
use crate::{PixelSink, row::*, source::*};
impl<'a, R> MixedSinker<'a, Yuva444p, R> {
#[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 = self.frame_pixels()?;
if buf.len() < expected {
return Err(MixedSinkerError::InsufficientLumaU16Buffer(
InsufficientBuffer::new(expected, buf.len()),
));
}
self.luma_u16 = Some(buf);
Ok(self)
}
}
impl<R> Yuva444pSink for MixedSinker<'_, Yuva444p, R> {}
impl<R> PixelSink for MixedSinker<'_, Yuva444p, R> {
type Input<'r> = Yuva444pRow<'r>;
type Error = MixedSinkerError;
#[cfg_attr(not(tarpaulin), inline(always))]
fn kernel_matrix(&self) -> crate::KernelMatrix {
self.kernel_matrix
}
fn begin_frame(&mut self, width: u32, height: u32) -> Result<(), Self::Error> {
check_dimensions_match(self.width, self.height, width, height)?;
if let Some(stream) = self.rgba_stream.as_mut() {
stream.reset();
}
if let Some(stream) = self.luma_stream_u16.as_mut() {
stream.reset();
}
if let Some(stream) = self.rgba_filter_stream.as_mut() {
stream.reset();
}
if let Some(stream) = self.luma_filter_stream.as_mut() {
stream.reset();
}
self.resample_outputs = None;
self.frozen_alpha_mode = Some(self.alpha_mode);
Ok(())
}
fn process(&mut self, row: Yuva444pRow<'_>) -> Result<(), Self::Error> {
let w = self.width;
let h = self.height;
let idx = row.row();
let use_simd = self.simd;
if row.y().len() != w {
return Err(MixedSinkerError::RowShapeMismatch(RowShapeMismatch::new(
RowSlice::Y,
idx,
w,
row.y().len(),
)));
}
if row.u().len() != w {
return Err(MixedSinkerError::RowShapeMismatch(RowShapeMismatch::new(
RowSlice::UFull,
idx,
w,
row.u().len(),
)));
}
if row.v().len() != w {
return Err(MixedSinkerError::RowShapeMismatch(RowShapeMismatch::new(
RowSlice::VFull,
idx,
w,
row.v().len(),
)));
}
if row.a().len() != w {
return Err(MixedSinkerError::RowShapeMismatch(RowShapeMismatch::new(
RowSlice::AFull,
idx,
w,
row.a().len(),
)));
}
if idx >= self.height {
return Err(MixedSinkerError::RowIndexOutOfRange(
RowIndexOutOfRange::new(idx, self.height),
));
}
if self.plan.is_some() {
let alpha_mode = self.alpha_mode;
let matrix = row.matrix();
let full_range = row.full_range();
let y = row.y();
let u = row.u();
let v = row.v();
let a = row.a();
let Self {
rgb,
rgba,
rgb_u16,
rgba_u16,
luma,
luma_u16,
hsv,
rgba_scratch,
rgb_scratch,
rgba_scratch_u16,
rgba_color_scratch_u16,
luma_scratch_u16,
plan,
rgba_stream,
rgba_stream_u16,
luma_stream_u16,
rgba_filter_stream,
rgba_filter_stream_u16,
luma_filter_stream,
luma_filter_stream_u16,
resample_outputs,
frozen_alpha_mode,
..
} = self;
let plan = plan.as_ref().expect("plan.is_some() checked above");
check_frozen_alpha_mode(*frozen_alpha_mode, alpha_mode, idx)?;
return match plan.kind() {
crate::resample::SpanKind::Area => packed_yuva444_resample::<8>(
rgba_stream,
rgba_stream_u16,
luma_stream_u16,
resample_outputs,
rgb,
rgba,
rgb_u16,
rgba_u16,
luma,
luma_u16,
hsv,
rgba_scratch,
rgb_scratch,
rgba_scratch_u16,
rgba_color_scratch_u16,
luma_scratch_u16,
w,
plan,
idx,
use_simd,
alpha_mode,
|dst| yuva444p_to_rgba_row(y, u, v, a, dst, w, matrix, full_range, use_simd),
|_dst: &mut [u16]| {},
|dst| {
for (d, &s) in dst.iter_mut().zip(y) {
*d = s as u16;
}
},
),
crate::resample::SpanKind::Filter if alpha_mode.is_premultiplied() => {
packed_yuva444_resample::<8>(
rgba_stream,
rgba_stream_u16,
luma_stream_u16,
resample_outputs,
rgb,
rgba,
rgb_u16,
rgba_u16,
luma,
luma_u16,
hsv,
rgba_scratch,
rgb_scratch,
rgba_scratch_u16,
rgba_color_scratch_u16,
luma_scratch_u16,
w,
plan,
idx,
use_simd,
alpha_mode,
|dst| yuva444p_to_rgba_row(y, u, v, a, dst, w, matrix, full_range, use_simd),
|_dst: &mut [u16]| {},
|dst| {
for (d, &s) in dst.iter_mut().zip(y) {
*d = s as u16;
}
},
)
}
crate::resample::SpanKind::Filter => packed_yuva444_filter_resample::<8, true, false>(
rgba_filter_stream,
rgba_filter_stream_u16,
luma_filter_stream,
luma_filter_stream_u16,
resample_outputs,
rgb,
rgba,
rgb_u16,
rgba_u16,
luma,
luma_u16,
hsv,
rgba_scratch,
rgb_scratch,
rgba_scratch_u16,
rgba_color_scratch_u16,
luma_scratch_u16,
w,
plan,
idx,
use_simd,
y,
None,
|dst| yuva444p_to_rgba_row(y, u, v, a, dst, w, matrix, full_range, use_simd),
|_dst: &mut [u16]| {},
|_dst: &mut [u16]| {},
|_dst: &mut [u8]| {},
),
};
}
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 want_rgb = rgb.is_some();
let want_rgba = rgba.is_some();
let want_hsv = hsv.is_some();
let want_hsv_direct = want_hsv && !want_rgb && !want_rgba;
let need_rgb_kernel = want_rgb || (want_hsv && want_rgba);
let rgb_row = if need_rgb_kernel {
Some(rgb_row_buf_or_scratch(
rgb.as_deref_mut(),
rgb_scratch,
one_plane_start,
one_plane_end,
w,
h,
)?)
} else {
None
};
if let Some(luma) = luma.as_deref_mut() {
luma[one_plane_start..one_plane_end].copy_from_slice(&row.y()[..w]);
}
if let Some(luma_u16) = luma_u16.as_deref_mut() {
for (d, &s) in luma_u16[one_plane_start..one_plane_end]
.iter_mut()
.zip(&row.y()[..w])
{
*d = s as u16;
}
}
if want_hsv_direct {
let hsv = hsv.as_mut().expect("want_hsv_direct implies hsv attached");
let (h, s, v) = hsv.hsv();
yuv_444_to_hsv_row(
row.y(),
row.u(),
row.v(),
&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,
row.matrix(),
row.full_range(),
use_simd,
);
return Ok(());
}
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)?;
yuva444p_to_rgba_row(
row.y(),
row.u(),
row.v(),
row.a(),
rgba_row,
w,
row.matrix(),
row.full_range(),
use_simd,
);
return Ok(());
}
let Some(rgb_row) = rgb_row else {
return Ok(());
};
yuv_444_to_rgb_row(
row.y(),
row.u(),
row.v(),
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 want_rgba {
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)?;
expand_rgb_to_rgba_row(rgb_row, rgba_row, w);
crate::row::alpha_extract::copy_alpha_plane_u8(row.a(), rgba_row, w, use_simd);
}
Ok(())
}
}
impl<'a, R, const BE: bool> MixedSinker<'a, Yuva444p9<BE>, R> {
#[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_rgba_u16(mut self, buf: &'a mut [u16]) -> Result<Self, MixedSinkerError> {
self.set_rgba_u16(buf)?;
Ok(self)
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn set_rgba_u16(&mut self, buf: &'a mut [u16]) -> Result<&mut Self, MixedSinkerError> {
let expected = self.frame_elems(4)?;
if buf.len() < expected {
return Err(MixedSinkerError::InsufficientRgbaU16Buffer(
InsufficientBuffer::new(expected, buf.len()),
));
}
self.rgba_u16 = Some(buf);
Ok(self)
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn with_rgb_u16(mut self, buf: &'a mut [u16]) -> Result<Self, MixedSinkerError> {
self.set_rgb_u16(buf)?;
Ok(self)
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn set_rgb_u16(&mut self, buf: &'a mut [u16]) -> Result<&mut Self, MixedSinkerError> {
let expected = self.frame_elems(3)?;
if buf.len() < expected {
return Err(MixedSinkerError::InsufficientRgbU16Buffer(
InsufficientBuffer::new(expected, buf.len()),
));
}
self.rgb_u16 = 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 = self.frame_pixels()?;
if buf.len() < expected {
return Err(MixedSinkerError::InsufficientLumaU16Buffer(
InsufficientBuffer::new(expected, buf.len()),
));
}
self.luma_u16 = Some(buf);
Ok(self)
}
}
impl<R, const BE: bool> Yuva444p9Sink<BE> for MixedSinker<'_, Yuva444p9<BE>, R> {}
impl<R, const BE: bool> PixelSink for MixedSinker<'_, Yuva444p9<BE>, R> {
type Input<'r> = Yuva444p9Row<'r>;
type Error = MixedSinkerError;
#[cfg_attr(not(tarpaulin), inline(always))]
fn kernel_matrix(&self) -> crate::KernelMatrix {
self.kernel_matrix
}
fn begin_frame(&mut self, width: u32, height: u32) -> Result<(), Self::Error> {
check_dimensions_match(self.width, self.height, width, height)?;
reset_high_bit_yuva_streams(self);
Ok(())
}
fn process(&mut self, row: Yuva444p9Row<'_>) -> Result<(), Self::Error> {
if self.plan.is_some() {
return yuva444p_high_bit_resample::<9, BE>(
self,
row.row(),
row.y(),
row.u(),
row.v(),
row.a(),
row.matrix(),
row.full_range(),
RowSlice::Y9,
RowSlice::UFull9,
RowSlice::VFull9,
RowSlice::AFull9,
yuva444p9_to_rgba_row_endian,
yuva444p9_to_rgba_u16_row_endian,
);
}
yuva444p_high_bit_process::<9, BE, _, _, _, _, _, _>(
self,
row.row(),
row.y(),
row.u(),
row.v(),
row.a(),
row.matrix(),
row.full_range(),
RowSlice::Y9,
RowSlice::UFull9,
RowSlice::VFull9,
RowSlice::AFull9,
yuv444p9_to_rgb_row_endian,
yuv444p9_to_rgb_u16_row_endian,
yuva444p9_to_rgba_row_endian,
yuv444p9_to_hsv_row_endian,
yuva444p9_to_rgba_u16_row_endian,
)
}
}
impl<'a, R, const BE: bool> MixedSinker<'a, Yuva444p10<BE>, R> {
#[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_rgba_u16(mut self, buf: &'a mut [u16]) -> Result<Self, MixedSinkerError> {
self.set_rgba_u16(buf)?;
Ok(self)
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn set_rgba_u16(&mut self, buf: &'a mut [u16]) -> Result<&mut Self, MixedSinkerError> {
let expected = self.frame_elems(4)?;
if buf.len() < expected {
return Err(MixedSinkerError::InsufficientRgbaU16Buffer(
InsufficientBuffer::new(expected, buf.len()),
));
}
self.rgba_u16 = Some(buf);
Ok(self)
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn with_rgb_u16(mut self, buf: &'a mut [u16]) -> Result<Self, MixedSinkerError> {
self.set_rgb_u16(buf)?;
Ok(self)
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn set_rgb_u16(&mut self, buf: &'a mut [u16]) -> Result<&mut Self, MixedSinkerError> {
let expected = self.frame_elems(3)?;
if buf.len() < expected {
return Err(MixedSinkerError::InsufficientRgbU16Buffer(
InsufficientBuffer::new(expected, buf.len()),
));
}
self.rgb_u16 = 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 = self.frame_pixels()?;
if buf.len() < expected {
return Err(MixedSinkerError::InsufficientLumaU16Buffer(
InsufficientBuffer::new(expected, buf.len()),
));
}
self.luma_u16 = Some(buf);
Ok(self)
}
}
impl<R, const BE: bool> Yuva444p10Sink<BE> for MixedSinker<'_, Yuva444p10<BE>, R> {}
impl<R, const BE: bool> PixelSink for MixedSinker<'_, Yuva444p10<BE>, R> {
type Input<'r> = Yuva444p10Row<'r>;
type Error = MixedSinkerError;
#[cfg_attr(not(tarpaulin), inline(always))]
fn kernel_matrix(&self) -> crate::KernelMatrix {
self.kernel_matrix
}
fn begin_frame(&mut self, width: u32, height: u32) -> Result<(), Self::Error> {
check_dimensions_match(self.width, self.height, width, height)?;
reset_high_bit_yuva_streams(self);
Ok(())
}
fn process(&mut self, row: Yuva444p10Row<'_>) -> Result<(), Self::Error> {
if self.plan.is_some() {
return yuva444p_high_bit_resample::<10, BE>(
self,
row.row(),
row.y(),
row.u(),
row.v(),
row.a(),
row.matrix(),
row.full_range(),
RowSlice::Y10,
RowSlice::UFull10,
RowSlice::VFull10,
RowSlice::AFull10,
yuva444p10_to_rgba_row_endian,
yuva444p10_to_rgba_u16_row_endian,
);
}
yuva444p_high_bit_process::<10, BE, _, _, _, _, _, _>(
self,
row.row(),
row.y(),
row.u(),
row.v(),
row.a(),
row.matrix(),
row.full_range(),
RowSlice::Y10,
RowSlice::UFull10,
RowSlice::VFull10,
RowSlice::AFull10,
yuv444p10_to_rgb_row_endian,
yuv444p10_to_rgb_u16_row_endian,
yuva444p10_to_rgba_row_endian,
yuv444p10_to_hsv_row_endian,
yuva444p10_to_rgba_u16_row_endian,
)
}
}
impl<'a, R, const BE: bool> MixedSinker<'a, Yuva444p12<BE>, R> {
#[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_rgba_u16(mut self, buf: &'a mut [u16]) -> Result<Self, MixedSinkerError> {
self.set_rgba_u16(buf)?;
Ok(self)
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn set_rgba_u16(&mut self, buf: &'a mut [u16]) -> Result<&mut Self, MixedSinkerError> {
let expected = self.frame_elems(4)?;
if buf.len() < expected {
return Err(MixedSinkerError::InsufficientRgbaU16Buffer(
InsufficientBuffer::new(expected, buf.len()),
));
}
self.rgba_u16 = Some(buf);
Ok(self)
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn with_rgb_u16(mut self, buf: &'a mut [u16]) -> Result<Self, MixedSinkerError> {
self.set_rgb_u16(buf)?;
Ok(self)
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn set_rgb_u16(&mut self, buf: &'a mut [u16]) -> Result<&mut Self, MixedSinkerError> {
let expected = self.frame_elems(3)?;
if buf.len() < expected {
return Err(MixedSinkerError::InsufficientRgbU16Buffer(
InsufficientBuffer::new(expected, buf.len()),
));
}
self.rgb_u16 = 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 = self.frame_pixels()?;
if buf.len() < expected {
return Err(MixedSinkerError::InsufficientLumaU16Buffer(
InsufficientBuffer::new(expected, buf.len()),
));
}
self.luma_u16 = Some(buf);
Ok(self)
}
}
impl<R, const BE: bool> Yuva444p12Sink<BE> for MixedSinker<'_, Yuva444p12<BE>, R> {}
impl<R, const BE: bool> PixelSink for MixedSinker<'_, Yuva444p12<BE>, R> {
type Input<'r> = Yuva444p12Row<'r>;
type Error = MixedSinkerError;
#[cfg_attr(not(tarpaulin), inline(always))]
fn kernel_matrix(&self) -> crate::KernelMatrix {
self.kernel_matrix
}
fn begin_frame(&mut self, width: u32, height: u32) -> Result<(), Self::Error> {
check_dimensions_match(self.width, self.height, width, height)?;
reset_high_bit_yuva_streams(self);
Ok(())
}
fn process(&mut self, row: Yuva444p12Row<'_>) -> Result<(), Self::Error> {
if self.plan.is_some() {
return yuva444p_high_bit_resample::<12, BE>(
self,
row.row(),
row.y(),
row.u(),
row.v(),
row.a(),
row.matrix(),
row.full_range(),
RowSlice::Y12,
RowSlice::UFull12,
RowSlice::VFull12,
RowSlice::AFull12,
yuva444p12_to_rgba_row_endian,
yuva444p12_to_rgba_u16_row_endian,
);
}
yuva444p_high_bit_process::<12, BE, _, _, _, _, _, _>(
self,
row.row(),
row.y(),
row.u(),
row.v(),
row.a(),
row.matrix(),
row.full_range(),
RowSlice::Y12,
RowSlice::UFull12,
RowSlice::VFull12,
RowSlice::AFull12,
yuv444p12_to_rgb_row_endian,
yuv444p12_to_rgb_u16_row_endian,
yuva444p12_to_rgba_row_endian,
yuv444p12_to_hsv_row_endian,
yuva444p12_to_rgba_u16_row_endian,
)
}
}
impl<'a, R, const BE: bool> MixedSinker<'a, Yuva444p14<BE>, R> {
#[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_rgba_u16(mut self, buf: &'a mut [u16]) -> Result<Self, MixedSinkerError> {
self.set_rgba_u16(buf)?;
Ok(self)
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn set_rgba_u16(&mut self, buf: &'a mut [u16]) -> Result<&mut Self, MixedSinkerError> {
let expected = self.frame_elems(4)?;
if buf.len() < expected {
return Err(MixedSinkerError::InsufficientRgbaU16Buffer(
InsufficientBuffer::new(expected, buf.len()),
));
}
self.rgba_u16 = Some(buf);
Ok(self)
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn with_rgb_u16(mut self, buf: &'a mut [u16]) -> Result<Self, MixedSinkerError> {
self.set_rgb_u16(buf)?;
Ok(self)
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn set_rgb_u16(&mut self, buf: &'a mut [u16]) -> Result<&mut Self, MixedSinkerError> {
let expected = self.frame_elems(3)?;
if buf.len() < expected {
return Err(MixedSinkerError::InsufficientRgbU16Buffer(
InsufficientBuffer::new(expected, buf.len()),
));
}
self.rgb_u16 = 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 = self.frame_pixels()?;
if buf.len() < expected {
return Err(MixedSinkerError::InsufficientLumaU16Buffer(
InsufficientBuffer::new(expected, buf.len()),
));
}
self.luma_u16 = Some(buf);
Ok(self)
}
}
impl<R, const BE: bool> Yuva444p14Sink<BE> for MixedSinker<'_, Yuva444p14<BE>, R> {}
impl<R, const BE: bool> PixelSink for MixedSinker<'_, Yuva444p14<BE>, R> {
type Input<'r> = Yuva444p14Row<'r>;
type Error = MixedSinkerError;
#[cfg_attr(not(tarpaulin), inline(always))]
fn kernel_matrix(&self) -> crate::KernelMatrix {
self.kernel_matrix
}
fn begin_frame(&mut self, width: u32, height: u32) -> Result<(), Self::Error> {
check_dimensions_match(self.width, self.height, width, height)?;
reset_high_bit_yuva_streams(self);
Ok(())
}
fn process(&mut self, row: Yuva444p14Row<'_>) -> Result<(), Self::Error> {
if self.plan.is_some() {
return yuva444p_high_bit_resample::<14, BE>(
self,
row.row(),
row.y(),
row.u(),
row.v(),
row.a(),
row.matrix(),
row.full_range(),
RowSlice::Y14,
RowSlice::UFull14,
RowSlice::VFull14,
RowSlice::AFull14,
yuva444p14_to_rgba_row_endian,
yuva444p14_to_rgba_u16_row_endian,
);
}
yuva444p_high_bit_process::<14, BE, _, _, _, _, _, _>(
self,
row.row(),
row.y(),
row.u(),
row.v(),
row.a(),
row.matrix(),
row.full_range(),
RowSlice::Y14,
RowSlice::UFull14,
RowSlice::VFull14,
RowSlice::AFull14,
yuv444p14_to_rgb_row_endian,
yuv444p14_to_rgb_u16_row_endian,
yuva444p14_to_rgba_row_endian,
yuv444p14_to_hsv_row_endian,
yuva444p14_to_rgba_u16_row_endian,
)
}
}
impl<'a, R, const BE: bool> MixedSinker<'a, Yuva444p16<BE>, R> {
#[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_rgba_u16(mut self, buf: &'a mut [u16]) -> Result<Self, MixedSinkerError> {
self.set_rgba_u16(buf)?;
Ok(self)
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn set_rgba_u16(&mut self, buf: &'a mut [u16]) -> Result<&mut Self, MixedSinkerError> {
let expected = self.frame_elems(4)?;
if buf.len() < expected {
return Err(MixedSinkerError::InsufficientRgbaU16Buffer(
InsufficientBuffer::new(expected, buf.len()),
));
}
self.rgba_u16 = Some(buf);
Ok(self)
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn with_rgb_u16(mut self, buf: &'a mut [u16]) -> Result<Self, MixedSinkerError> {
self.set_rgb_u16(buf)?;
Ok(self)
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub fn set_rgb_u16(&mut self, buf: &'a mut [u16]) -> Result<&mut Self, MixedSinkerError> {
let expected = self.frame_elems(3)?;
if buf.len() < expected {
return Err(MixedSinkerError::InsufficientRgbU16Buffer(
InsufficientBuffer::new(expected, buf.len()),
));
}
self.rgb_u16 = 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 = self.frame_pixels()?;
if buf.len() < expected {
return Err(MixedSinkerError::InsufficientLumaU16Buffer(
InsufficientBuffer::new(expected, buf.len()),
));
}
self.luma_u16 = Some(buf);
Ok(self)
}
}
impl<R, const BE: bool> Yuva444p16Sink<BE> for MixedSinker<'_, Yuva444p16<BE>, R> {}
impl<R, const BE: bool> PixelSink for MixedSinker<'_, Yuva444p16<BE>, R> {
type Input<'r> = Yuva444p16Row<'r>;
type Error = MixedSinkerError;
#[cfg_attr(not(tarpaulin), inline(always))]
fn kernel_matrix(&self) -> crate::KernelMatrix {
self.kernel_matrix
}
fn begin_frame(&mut self, width: u32, height: u32) -> Result<(), Self::Error> {
check_dimensions_match(self.width, self.height, width, height)?;
reset_high_bit_yuva_streams(self);
Ok(())
}
fn process(&mut self, row: Yuva444p16Row<'_>) -> Result<(), Self::Error> {
if self.plan.is_some() {
return yuva444p_high_bit_resample::<16, BE>(
self,
row.row(),
row.y(),
row.u(),
row.v(),
row.a(),
row.matrix(),
row.full_range(),
RowSlice::Y16,
RowSlice::UFull16,
RowSlice::VFull16,
RowSlice::AFull16,
yuva444p16_to_rgba_row_endian,
yuva444p16_to_rgba_u16_row_endian,
);
}
yuva444p_high_bit_process::<16, BE, _, _, _, _, _, _>(
self,
row.row(),
row.y(),
row.u(),
row.v(),
row.a(),
row.matrix(),
row.full_range(),
RowSlice::Y16,
RowSlice::UFull16,
RowSlice::VFull16,
RowSlice::AFull16,
yuv444p16_to_rgb_row_endian,
yuv444p16_to_rgb_u16_row_endian,
yuva444p16_to_rgba_row_endian,
yuv444p16_to_hsv_row_endian,
yuva444p16_to_rgba_u16_row_endian,
)
}
}
#[allow(clippy::too_many_arguments, clippy::type_complexity)]
#[cfg_attr(not(tarpaulin), inline(always))]
fn yuva444p_high_bit_process<
const BITS: u32,
const BE: bool,
F: crate::SourceFormat,
R,
RgbRowFn: Fn(&[u16], &[u16], &[u16], &mut [u8], usize, crate::KernelMatrix, bool, bool, bool),
RgbU16RowFn: Fn(&[u16], &[u16], &[u16], &mut [u16], usize, crate::KernelMatrix, bool, bool, bool),
RgbaRowFn: Fn(&[u16], &[u16], &[u16], &[u16], &mut [u8], usize, crate::KernelMatrix, bool, bool, bool),
HsvRowFn: Fn(
&[u16],
&[u16],
&[u16],
&mut [u8],
&mut [u8],
&mut [u8],
usize,
crate::KernelMatrix,
bool,
bool,
bool,
),
>(
sinker: &mut MixedSinker<'_, F, R>,
idx: usize,
y_row: &[u16],
u_row: &[u16],
v_row: &[u16],
a_row: &[u16],
matrix: crate::KernelMatrix,
full_range: bool,
y_slice: RowSlice,
u_slice: RowSlice,
v_slice: RowSlice,
a_slice: RowSlice,
rgb_dispatch: RgbRowFn,
rgb_u16_dispatch: RgbU16RowFn,
rgba_dispatch: RgbaRowFn,
hsv_dispatch: HsvRowFn,
rgba_u16_dispatch: fn(
&[u16],
&[u16],
&[u16],
&[u16],
&mut [u16],
usize,
crate::KernelMatrix,
bool,
bool,
bool,
),
) -> Result<(), MixedSinkerError> {
let w = sinker.width;
let h = sinker.height;
let use_simd = sinker.simd;
if y_row.len() != w {
return Err(MixedSinkerError::RowShapeMismatch(RowShapeMismatch::new(
y_slice,
idx,
w,
y_row.len(),
)));
}
if u_row.len() != w {
return Err(MixedSinkerError::RowShapeMismatch(RowShapeMismatch::new(
u_slice,
idx,
w,
u_row.len(),
)));
}
if v_row.len() != w {
return Err(MixedSinkerError::RowShapeMismatch(RowShapeMismatch::new(
v_slice,
idx,
w,
v_row.len(),
)));
}
if a_row.len() != w {
return Err(MixedSinkerError::RowShapeMismatch(RowShapeMismatch::new(
a_slice,
idx,
w,
a_row.len(),
)));
}
if idx >= sinker.height {
return Err(MixedSinkerError::RowIndexOutOfRange(
RowIndexOutOfRange::new(idx, sinker.height),
));
}
let MixedSinker {
rgb,
rgb_u16,
rgba,
rgba_u16,
luma,
luma_u16,
hsv,
rgb_scratch,
..
} = sinker;
let one_plane_start = idx * w;
let one_plane_end = one_plane_start + w;
let want_rgb = rgb.is_some();
let want_rgba = rgba.is_some();
let want_hsv = hsv.is_some();
let want_hsv_direct = want_hsv && !want_rgb && !want_rgba;
let need_rgb_kernel = want_rgb || (want_hsv && want_rgba);
let rgb_row = if need_rgb_kernel {
Some(rgb_row_buf_or_scratch(
rgb.as_deref_mut(),
rgb_scratch,
one_plane_start,
one_plane_end,
w,
h,
)?)
} else {
None
};
if luma.is_some() || luma_u16.is_some() {
let mut luma_row = luma
.as_deref_mut()
.map(|b| &mut b[one_plane_start..one_plane_end]);
let mut luma_u16_row = luma_u16
.as_deref_mut()
.map(|b| &mut b[one_plane_start..one_plane_end]);
let sample_mask = ((1u32 << BITS) - 1) as u16;
for (i, &s) in y_row.iter().enumerate().take(w) {
let logical = (if BE { u16::from_be(s) } else { u16::from_le(s) }) & sample_mask;
if let Some(row) = luma_row.as_deref_mut() {
row[i] = (logical >> (BITS - 8)) as u8;
}
if let Some(row) = luma_u16_row.as_deref_mut() {
row[i] = logical;
}
}
}
let want_rgb_u16 = rgb_u16.is_some();
let want_rgba_u16 = rgba_u16.is_some();
if want_rgb_u16 {
let buf = rgb_u16.as_deref_mut().unwrap();
let rgb_plane_end = one_plane_end
.checked_mul(3)
.ok_or(MixedSinkerError::GeometryOverflow(GeometryOverflow::new(
w, h, 3,
)))?;
let rgb_plane_start = one_plane_start * 3;
let rgb_u16_row = &mut buf[rgb_plane_start..rgb_plane_end];
rgb_u16_dispatch(
y_row,
u_row,
v_row,
rgb_u16_row,
w,
matrix,
full_range,
use_simd,
BE,
);
if want_rgba_u16 {
let rgba_buf = rgba_u16.as_deref_mut().unwrap();
let rgba_u16_row = rgba_u16_plane_row_slice(rgba_buf, one_plane_start, one_plane_end, w, h)?;
expand_rgb_u16_to_rgba_u16_row::<BITS>(rgb_u16_row, rgba_u16_row, w);
crate::row::alpha_extract::copy_alpha_plane_u16::<BITS, BE>(a_row, rgba_u16_row, w, use_simd);
}
} else if want_rgba_u16 {
let rgba_buf = rgba_u16.as_deref_mut().unwrap();
let rgba_u16_row = rgba_u16_plane_row_slice(rgba_buf, one_plane_start, one_plane_end, w, h)?;
rgba_u16_dispatch(
y_row,
u_row,
v_row,
a_row,
rgba_u16_row,
w,
matrix,
full_range,
use_simd,
BE,
);
}
if want_hsv_direct {
let hsv = hsv.as_mut().expect("want_hsv_direct implies hsv attached");
let (h, s, v) = hsv.hsv();
hsv_dispatch(
y_row,
u_row,
v_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,
matrix,
full_range,
use_simd,
BE,
);
return Ok(());
}
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)?;
rgba_dispatch(
y_row, u_row, v_row, a_row, rgba_row, w, matrix, full_range, use_simd, BE,
);
return Ok(());
}
let Some(rgb_row) = rgb_row else {
return Ok(());
};
rgb_dispatch(
y_row, u_row, v_row, rgb_row, w, matrix, full_range, use_simd, BE,
);
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 want_rgba {
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)?;
expand_rgb_to_rgba_row(rgb_row, rgba_row, w);
crate::row::alpha_extract::copy_alpha_plane_u16_to_u8::<BITS, BE>(a_row, rgba_row, w, use_simd);
}
Ok(())
}
#[allow(clippy::too_many_arguments, clippy::type_complexity)]
#[cfg_attr(not(tarpaulin), inline(always))]
fn yuva444p_high_bit_resample<const BITS: u32, const BE: bool>(
sinker: &mut MixedSinker<'_, impl crate::SourceFormat, impl Sized>,
idx: usize,
y_row: &[u16],
u_row: &[u16],
v_row: &[u16],
a_row: &[u16],
matrix: crate::KernelMatrix,
full_range: bool,
y_slice: RowSlice,
u_slice: RowSlice,
v_slice: RowSlice,
a_slice: RowSlice,
rgba_dispatch: fn(
&[u16],
&[u16],
&[u16],
&[u16],
&mut [u8],
usize,
crate::KernelMatrix,
bool,
bool,
bool,
),
rgba_u16_dispatch: fn(
&[u16],
&[u16],
&[u16],
&[u16],
&mut [u16],
usize,
crate::KernelMatrix,
bool,
bool,
bool,
),
) -> Result<(), MixedSinkerError> {
let w = sinker.width;
let use_simd = sinker.simd;
if y_row.len() != w {
return Err(MixedSinkerError::RowShapeMismatch(RowShapeMismatch::new(
y_slice,
idx,
w,
y_row.len(),
)));
}
if u_row.len() != w {
return Err(MixedSinkerError::RowShapeMismatch(RowShapeMismatch::new(
u_slice,
idx,
w,
u_row.len(),
)));
}
if v_row.len() != w {
return Err(MixedSinkerError::RowShapeMismatch(RowShapeMismatch::new(
v_slice,
idx,
w,
v_row.len(),
)));
}
if a_row.len() != w {
return Err(MixedSinkerError::RowShapeMismatch(RowShapeMismatch::new(
a_slice,
idx,
w,
a_row.len(),
)));
}
if idx >= sinker.height {
return Err(MixedSinkerError::RowIndexOutOfRange(
RowIndexOutOfRange::new(idx, sinker.height),
));
}
let alpha_mode = sinker.alpha_mode;
let MixedSinker {
rgb,
rgba,
rgb_u16,
rgba_u16,
luma,
luma_u16,
hsv,
rgba_scratch,
rgb_scratch,
rgba_scratch_u16,
rgba_color_scratch_u16,
luma_scratch_u16,
plan,
rgba_stream,
rgba_stream_u16,
luma_stream_u16,
rgba_filter_stream,
rgba_filter_stream_u16,
luma_filter_stream_u16,
resample_outputs,
frozen_alpha_mode,
..
} = sinker;
let plan = plan.as_ref().expect("plan.is_some() checked by the caller");
check_frozen_alpha_mode(*frozen_alpha_mode, alpha_mode, idx)?;
match plan.kind() {
crate::resample::SpanKind::Area => packed_yuva444_resample::<BITS>(
rgba_stream,
rgba_stream_u16,
luma_stream_u16,
resample_outputs,
rgb,
rgba,
rgb_u16,
rgba_u16,
luma,
luma_u16,
hsv,
rgba_scratch,
rgb_scratch,
rgba_scratch_u16,
rgba_color_scratch_u16,
luma_scratch_u16,
w,
plan,
idx,
use_simd,
alpha_mode,
|dst| {
rgba_dispatch(
y_row, u_row, v_row, a_row, dst, w, matrix, full_range, use_simd, BE,
)
},
|dst| {
rgba_u16_dispatch(
y_row, u_row, v_row, a_row, dst, w, matrix, full_range, use_simd, BE,
)
},
|dst| deinterleave_y_high_bit_masked::<BITS, BE>(y_row, dst, w),
),
crate::resample::SpanKind::Filter if alpha_mode.is_premultiplied() => {
packed_yuva444_resample::<BITS>(
rgba_stream,
rgba_stream_u16,
luma_stream_u16,
resample_outputs,
rgb,
rgba,
rgb_u16,
rgba_u16,
luma,
luma_u16,
hsv,
rgba_scratch,
rgb_scratch,
rgba_scratch_u16,
rgba_color_scratch_u16,
luma_scratch_u16,
w,
plan,
idx,
use_simd,
alpha_mode,
|dst| {
rgba_dispatch(
y_row, u_row, v_row, a_row, dst, w, matrix, full_range, use_simd, BE,
)
},
|dst| {
rgba_u16_dispatch(
y_row, u_row, v_row, a_row, dst, w, matrix, full_range, use_simd, BE,
)
},
|dst| deinterleave_y_high_bit_masked::<BITS, BE>(y_row, dst, w),
)
}
crate::resample::SpanKind::Filter => packed_yuva444_filter_resample::<BITS, false, false>(
rgba_filter_stream,
rgba_filter_stream_u16,
&mut None,
luma_filter_stream_u16,
resample_outputs,
rgb,
rgba,
rgb_u16,
rgba_u16,
luma,
luma_u16,
hsv,
rgba_scratch,
rgb_scratch,
rgba_scratch_u16,
rgba_color_scratch_u16,
luma_scratch_u16,
w,
plan,
idx,
use_simd,
&[],
None,
|dst| {
rgba_dispatch(
y_row, u_row, v_row, a_row, dst, w, matrix, full_range, use_simd, BE,
)
},
|dst| {
rgba_u16_dispatch(
y_row, u_row, v_row, a_row, dst, w, matrix, full_range, use_simd, BE,
)
},
|dst| deinterleave_y_high_bit_masked::<BITS, BE>(y_row, dst, w),
|_dst: &mut [u8]| {},
),
}
}