use crate::{
KernelMatrix, PixelSink,
frame::Ya8Frame,
resample::{AreaResampler, ResampleError},
sinker::{AlphaMode, MixedSinker, MixedSinkerError},
source::{Ya8, Ya8Row, ya8_to},
};
const SRC: usize = 8;
const OUT: usize = 4;
const FR: bool = true;
const FR_LIMITED: bool = false;
const M: KernelMatrix = KernelMatrix::Bt709;
fn packed_frame(seed: u32) -> Vec<u8> {
let mut buf = std::vec![0u8; SRC * SRC * 2];
super::pseudo_random_u8(&mut buf, seed);
buf
}
#[cfg(feature = "rgb")]
fn canonical_from_packed(packed: &[u8], n: usize) -> Vec<u8> {
let mut out = std::vec![0u8; n * 4];
for i in 0..n {
let y = packed[i * 2];
let a = packed[i * 2 + 1];
out[i * 4] = y;
out[i * 4 + 1] = y;
out[i * 4 + 2] = y;
out[i * 4 + 3] = a;
}
out
}
fn block_mean_rgba(src: &[u8]) -> Vec<u8> {
let mut out = std::vec![0u8; OUT * OUT * 4];
for oy in 0..OUT {
for ox in 0..OUT {
for c in 0..4 {
let mut acc = 0u32;
for dy in 0..2 {
for dx in 0..2 {
acc += src[((oy * 2 + dy) * SRC + ox * 2 + dx) * 4 + c] as u32;
}
}
out[(oy * OUT + ox) * 4 + c] = ((acc + 2) / 4) as u8;
}
}
}
out
}
fn block_mean_native_y(packed: &[u8]) -> Vec<u8> {
let mut out = std::vec![0u8; OUT * OUT];
for oy in 0..OUT {
for ox in 0..OUT {
let mut acc = 0u32;
for dy in 0..2 {
for dx in 0..2 {
acc += packed[((oy * 2 + dy) * SRC + ox * 2 + dx) * 2] as u32;
}
}
out[oy * OUT + ox] = ((acc + 2) / 4) as u8;
}
}
out
}
fn premultiply(plane: &mut [u8]) {
for px in plane.chunks_exact_mut(4) {
let a = px[3] as u32;
for c in &mut px[..3] {
*c = ((*c as u32 * a + 127) / 255) as u8;
}
}
}
fn unpremultiply(plane: &[u8]) -> Vec<u8> {
let mut out = std::vec![0u8; plane.len()];
for (o, i) in out.chunks_exact_mut(4).zip(plane.chunks_exact(4)) {
let a = i[3] as u32;
for c in 0..3 {
o[c] = (i[c] as u32 * 255 + a / 2)
.checked_div(a)
.map_or(0, |q| q.min(255)) as u8;
}
o[3] = i[3];
}
out
}
fn drop_alpha(rgba: &[u8]) -> Vec<u8> {
let mut out = std::vec![0u8; rgba.len() / 4 * 3];
for (o, i) in out.chunks_exact_mut(3).zip(rgba.chunks_exact(4)) {
o.copy_from_slice(&i[..3]);
}
out
}
fn zero_extend(plane: &[u8]) -> Vec<u16> {
plane.iter().map(|&v| v as u16).collect()
}
fn direct_rgba(packed: &[u8]) -> Vec<u8> {
let frame = Ya8Frame::new(packed, SRC as u32, SRC as u32, (SRC * 2) as u32);
let mut rgba = std::vec![0u8; SRC * SRC * 4];
let mut sink = MixedSinker::<Ya8>::new(SRC, SRC)
.with_rgba(&mut rgba)
.unwrap();
ya8_to(&frame, FR, sink.set_kernel_matrix(M)).unwrap();
rgba
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn straight_rgba_is_block_mean_of_direct() {
let packed = packed_frame(0x51A1);
let frame = Ya8Frame::new(&packed, SRC as u32, SRC as u32, (SRC * 2) as u32);
let mut rgba = std::vec![0u8; OUT * OUT * 4];
{
let mut sink =
MixedSinker::<Ya8, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap()
.with_rgba(&mut rgba)
.unwrap();
ya8_to(&frame, FR, sink.set_kernel_matrix(M)).unwrap();
}
assert_eq!(rgba, block_mean_rgba(&direct_rgba(&packed)));
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn straight_alpha_is_averaged_not_forced_opaque() {
let mut packed = packed_frame(0x9E37);
for (i, px) in packed.chunks_exact_mut(2).enumerate() {
px[1] = (i as u8).wrapping_mul(3);
}
let frame = Ya8Frame::new(&packed, SRC as u32, SRC as u32, (SRC * 2) as u32);
let mut rgba = std::vec![0u8; OUT * OUT * 4];
{
let mut sink =
MixedSinker::<Ya8, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap()
.with_rgba(&mut rgba)
.unwrap();
ya8_to(&frame, FR, sink.set_kernel_matrix(M)).unwrap();
}
let oracle = block_mean_rgba(&direct_rgba(&packed));
assert_eq!(rgba, oracle, "straight rgba == block mean");
assert!(
rgba.chunks_exact(4).any(|px| px[3] != 0xFF),
"resampled alpha was forced opaque — area-mean alpha lost"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn straight_all_outputs_derive_from_binned_color() {
let packed = packed_frame(0xBEEF);
let frame = Ya8Frame::new(&packed, SRC as u32, SRC as u32, (SRC * 2) as u32);
let mut rgb = std::vec![0u8; OUT * OUT * 3];
let mut rgba = std::vec![0u8; OUT * OUT * 4];
let mut rgb_u16 = std::vec![0u16; OUT * OUT * 3];
let mut rgba_u16 = std::vec![0u16; OUT * OUT * 4];
let mut luma = std::vec![0u8; OUT * OUT];
let mut lu16 = std::vec![0u16; OUT * OUT];
let mut h = std::vec![0u8; OUT * OUT];
let mut s = std::vec![0u8; OUT * OUT];
let mut v = std::vec![0u8; OUT * OUT];
{
let mut sink =
MixedSinker::<Ya8, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap()
.with_rgb(&mut rgb)
.unwrap()
.with_rgba(&mut rgba)
.unwrap()
.with_rgb_u16(&mut rgb_u16)
.unwrap()
.with_rgba_u16(&mut rgba_u16)
.unwrap()
.with_luma(&mut luma)
.unwrap()
.with_luma_u16(&mut lu16)
.unwrap()
.with_hsv(&mut h, &mut s, &mut v)
.unwrap();
ya8_to(&frame, FR, sink.set_kernel_matrix(M)).unwrap();
}
let binned = block_mean_rgba(&direct_rgba(&packed));
assert_eq!(rgba, binned, "rgba == block mean");
assert_eq!(rgb, drop_alpha(&binned), "rgb == drop-alpha(binned)");
assert_eq!(
rgba_u16,
zero_extend(&binned),
"rgba_u16 == zero-extend(binned)"
);
assert_eq!(
rgb_u16,
zero_extend(&drop_alpha(&binned)),
"rgb_u16 == zero-extend(drop-alpha(binned))"
);
let binned_rgb = drop_alpha(&binned);
let mut binned_packed = std::vec![0u8; OUT * OUT * 2];
for i in 0..OUT * OUT {
binned_packed[i * 2] = binned_rgb[i * 3]; binned_packed[i * 2 + 1] = 0xFF;
}
let binned_frame = Ya8Frame::new(&binned_packed, OUT as u32, OUT as u32, (OUT * 2) as u32);
let mut luma_ref = std::vec![0u8; OUT * OUT];
let mut lu16_ref = std::vec![0u16; OUT * OUT];
let mut h_ref = std::vec![0u8; OUT * OUT];
let mut s_ref = std::vec![0u8; OUT * OUT];
let mut v_ref = std::vec![0u8; OUT * OUT];
{
let mut sink = MixedSinker::<Ya8>::new(OUT, OUT)
.with_luma(&mut luma_ref)
.unwrap()
.with_luma_u16(&mut lu16_ref)
.unwrap()
.with_hsv(&mut h_ref, &mut s_ref, &mut v_ref)
.unwrap();
ya8_to(&binned_frame, FR, sink.set_kernel_matrix(M)).unwrap();
}
assert_eq!(luma, luma_ref, "luma (native Y)");
assert_eq!(lu16, lu16_ref, "luma_u16 (native Y zero-extended)");
assert_eq!(h, h_ref, "hsv H");
assert_eq!(s, s_ref, "hsv S");
assert_eq!(v, v_ref, "hsv V");
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn premultiplied_matches_premult_bin_unpremult_oracle() {
let packed = packed_frame(0x1234);
let frame = Ya8Frame::new(&packed, SRC as u32, SRC as u32, (SRC * 2) as u32);
let mut rgba = std::vec![0u8; OUT * OUT * 4];
let mut rgb = std::vec![0u8; OUT * OUT * 3];
let mut rgba_u16 = std::vec![0u16; OUT * OUT * 4];
let mut lu16 = std::vec![0u16; OUT * OUT];
{
let mut sink =
MixedSinker::<Ya8, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap()
.with_alpha_mode(AlphaMode::Premultiplied)
.with_rgba(&mut rgba)
.unwrap()
.with_rgb(&mut rgb)
.unwrap()
.with_rgba_u16(&mut rgba_u16)
.unwrap()
.with_luma_u16(&mut lu16)
.unwrap();
ya8_to(&frame, FR, sink.set_kernel_matrix(M)).unwrap();
}
let mut pm = direct_rgba(&packed);
premultiply(&mut pm);
let binned = block_mean_rgba(&pm);
let oracle = unpremultiply(&binned);
assert_eq!(rgba, oracle, "premult rgba");
assert_eq!(rgb, drop_alpha(&oracle), "premult rgb");
assert_eq!(rgba_u16, zero_extend(&oracle), "premult rgba_u16");
let binned_y = block_mean_native_y(&packed);
let (_, lu16_ref) = direct_luma_of_binned_y(&binned_y, FR);
assert_eq!(lu16, lu16_ref, "premult luma_u16 (native-Y bin oracle)");
assert_eq!(
lu16,
zero_extend(&binned_y),
"premult luma_u16 == native-Y bin zero-extended"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn premultiplied_transparent_block_does_not_bleed() {
let mut packed = packed_frame(0xABCD);
for off in [(0, 0), (1, 0), (0, 1), (1, 1)] {
let i = off.1 * SRC + off.0;
packed[i * 2] = 250; packed[i * 2 + 1] = 0; }
let frame = Ya8Frame::new(&packed, SRC as u32, SRC as u32, (SRC * 2) as u32);
let mut rgba = std::vec![0u8; OUT * OUT * 4];
{
let mut sink =
MixedSinker::<Ya8, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap()
.with_alpha_mode(AlphaMode::Premultiplied)
.with_rgba(&mut rgba)
.unwrap();
ya8_to(&frame, FR, sink.set_kernel_matrix(M)).unwrap();
}
assert_eq!(&rgba[..4], &[0, 0, 0, 0], "transparent block bled color");
let mut pm = direct_rgba(&packed);
premultiply(&mut pm);
let oracle = unpremultiply(&block_mean_rgba(&pm));
assert_eq!(rgba, oracle, "premult output != oracle");
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn premultiplied_nonuniform_alpha_luma_is_native_y_bin_not_color() {
let mut packed = std::vec![0u8; SRC * SRC * 2];
for (i, px) in packed.chunks_exact_mut(2).enumerate() {
let col = i % SRC;
let odd = col % 2 == 1;
px[0] = if odd { 255 } else { 0 }; px[1] = if odd { 0 } else { 255 }; }
let frame = Ya8Frame::new(&packed, SRC as u32, SRC as u32, (SRC * 2) as u32);
let mut luma = std::vec![0u8; OUT * OUT];
let mut lu16 = std::vec![0u16; OUT * OUT];
{
let mut sink =
MixedSinker::<Ya8, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap()
.with_alpha_mode(AlphaMode::Premultiplied)
.with_luma(&mut luma)
.unwrap()
.with_luma_u16(&mut lu16)
.unwrap();
ya8_to(&frame, FR, sink.set_kernel_matrix(M)).unwrap();
}
assert!(
luma.iter().all(|&y| y == 128),
"premult non-uniform-alpha luma must be native-Y mean 128, got {luma:?}"
);
assert!(
lu16.iter().all(|&y| y == 128),
"premult non-uniform-alpha luma_u16 must be native-Y mean 128, got {lu16:?}"
);
let binned_y = block_mean_native_y(&packed);
assert!(binned_y.iter().all(|&y| y == 128), "native-Y bin sanity");
let (luma_ref, lu16_ref) = direct_luma_of_binned_y(&binned_y, FR);
assert_eq!(luma, luma_ref, "premult luma == native-Y bin oracle");
assert_eq!(lu16, lu16_ref, "premult luma_u16 == native-Y bin oracle");
let mut pm = direct_rgba(&packed);
premultiply(&mut pm);
let color_oracle = unpremultiply(&block_mean_rgba(&pm));
let color_luma_r: Vec<u8> = color_oracle.chunks_exact(4).map(|px| px[0]).collect();
assert!(
color_luma_r.iter().all(|&r| r == 0),
"fixture failed to exercise the color-vs-native-Y divergence"
);
assert_ne!(luma, color_luma_r, "luma must NOT be the color-derived R");
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn straight_and_premult_differ_under_varying_alpha() {
let mut packed = packed_frame(0x77AA);
for (i, px) in packed.chunks_exact_mut(2).enumerate() {
px[1] = 16u8.wrapping_add((i as u8).wrapping_mul(5));
}
let render = |mode: AlphaMode| {
let frame = Ya8Frame::new(&packed, SRC as u32, SRC as u32, (SRC * 2) as u32);
let mut rgba = std::vec![0u8; OUT * OUT * 4];
let mut sink =
MixedSinker::<Ya8, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap()
.with_alpha_mode(mode)
.with_rgba(&mut rgba)
.unwrap();
ya8_to(&frame, FR, sink.set_kernel_matrix(M)).unwrap();
rgba
};
assert_ne!(
render(AlphaMode::Straight),
render(AlphaMode::Premultiplied),
"alpha mode had no effect"
);
}
#[test]
fn default_alpha_mode_is_straight() {
let sink = MixedSinker::<Ya8>::new(SRC, SRC);
assert_eq!(sink.alpha_mode(), AlphaMode::Straight);
assert!(sink.alpha_mode().is_straight());
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn identity_plan_matches_direct() {
let packed = packed_frame(0x0F0F);
let frame = Ya8Frame::new(&packed, SRC as u32, SRC as u32, (SRC * 2) as u32);
let mut rgba = std::vec![0u8; SRC * SRC * 4];
{
let mut sink =
MixedSinker::<Ya8, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(SRC, SRC))
.unwrap()
.with_rgba(&mut rgba)
.unwrap();
ya8_to(&frame, FR, sink.set_kernel_matrix(M)).unwrap();
}
assert_eq!(rgba, direct_rgba(&packed), "identity plan == direct");
}
#[cfg(feature = "rgb")]
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn fractional_ratio_matches_direct_then_bin() {
const F: usize = 3;
let packed = packed_frame(0xF2AC);
let frame = Ya8Frame::new(&packed, SRC as u32, SRC as u32, (SRC * 2) as u32);
let mut rgba = std::vec![0u8; F * F * 4];
{
let mut sink =
MixedSinker::<Ya8, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(F, F))
.unwrap()
.with_rgba(&mut rgba)
.unwrap();
ya8_to(&frame, FR, sink.set_kernel_matrix(M)).unwrap();
}
let canonical = canonical_from_packed(&packed, SRC * SRC);
let mut rgba_ref = std::vec![0u8; F * F * 4];
{
use crate::{frame::RgbaFrame, source::rgba_to};
let rsrc = RgbaFrame::new(&canonical, SRC as u32, SRC as u32, (SRC * 4) as u32);
let mut sink = MixedSinker::<crate::source::Rgba, AreaResampler>::with_resampler(
SRC,
SRC,
AreaResampler::to(F, F),
)
.unwrap()
.with_rgba(&mut rgba_ref)
.unwrap();
rgba_to(&rsrc, FR, sink.set_kernel_matrix(M)).unwrap();
}
assert_eq!(rgba, rgba_ref, "Ya8 8->3 != packed-RGBA 8->3 of canonical");
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn cross_frame_reset_reuses_streams() {
let packed = packed_frame(0x5151);
let frame = Ya8Frame::new(&packed, SRC as u32, SRC as u32, (SRC * 2) as u32);
let mut rgba = std::vec![0u8; OUT * OUT * 4];
{
let mut sink =
MixedSinker::<Ya8, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap()
.with_rgba(&mut rgba)
.unwrap();
ya8_to(&frame, FR, sink.set_kernel_matrix(M)).unwrap();
ya8_to(&frame, FR, sink.set_kernel_matrix(M)).unwrap();
}
assert_eq!(rgba, block_mean_rgba(&direct_rgba(&packed)));
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn accepts_alpha_mode_change_across_frames() {
let packed = packed_frame(0xB2B2);
let frame = Ya8Frame::new(&packed, SRC as u32, SRC as u32, (SRC * 2) as u32);
let mut rgba = std::vec![0u8; OUT * OUT * 4];
{
let mut sink =
MixedSinker::<Ya8, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap()
.with_rgba(&mut rgba)
.unwrap();
ya8_to(&frame, FR, sink.set_kernel_matrix(M)).unwrap();
sink.set_alpha_mode(AlphaMode::Premultiplied);
ya8_to(&frame, FR, sink.set_kernel_matrix(M))
.expect("a fresh frame must accept a different alpha mode");
}
let mut pm = direct_rgba(&packed);
premultiply(&mut pm);
let oracle = unpremultiply(&block_mean_rgba(&pm));
assert_eq!(rgba, oracle, "premult frame 2 output");
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn mid_frame_alpha_mode_flip_is_rejected() {
let packed = packed_frame(0x33AA);
let row_bytes = SRC * 2;
let mut rgba = std::vec![0u8; OUT * OUT * 4];
let mut sink =
MixedSinker::<Ya8, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap()
.with_rgba(&mut rgba)
.unwrap();
sink.begin_frame(SRC as u32, SRC as u32).unwrap();
sink
.process(Ya8Row::for_tests(&packed[..row_bytes], 0, M, FR))
.unwrap();
sink.set_alpha_mode(AlphaMode::Premultiplied);
let err = sink
.process(Ya8Row::for_tests(
&packed[row_bytes..2 * row_bytes],
1,
M,
FR,
))
.unwrap_err();
assert!(
matches!(err, MixedSinkerError::ResampleOutputsChanged(_)),
"mid-frame alpha flip not rejected: {err:?}"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn out_of_sequence_first_row_is_rejected() {
let packed = packed_frame(0x44BB);
let row_bytes = SRC * 2;
let mut rgba = std::vec![0u8; OUT * OUT * 4];
let mut sink =
MixedSinker::<Ya8, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap()
.with_rgba(&mut rgba)
.unwrap();
sink.begin_frame(SRC as u32, SRC as u32).unwrap();
let err = sink
.process(Ya8Row::for_tests(
&packed[row_bytes..2 * row_bytes],
1,
M,
FR,
))
.unwrap_err();
assert!(
matches!(
err,
MixedSinkerError::Resample(ResampleError::OutOfSequenceRow(_))
),
"out-of-sequence first row not rejected: {err:?}"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn no_output_sink_is_a_noop() {
let packed = packed_frame(0x4242);
let frame = Ya8Frame::new(&packed, SRC as u32, SRC as u32, (SRC * 2) as u32);
let mut sink =
MixedSinker::<Ya8, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap();
ya8_to(&frame, FR, sink.set_kernel_matrix(M)).unwrap();
}
fn direct_luma_of_binned_y(binned_y: &[u8], full_range: bool) -> (Vec<u8>, Vec<u16>) {
let n = binned_y.len();
let mut packed = std::vec![0u8; n * 2];
for (i, &y) in binned_y.iter().enumerate() {
packed[i * 2] = y;
packed[i * 2 + 1] = 0xFF;
}
let frame = Ya8Frame::new(&packed, n as u32, 1, (n * 2) as u32);
let mut luma = std::vec![0u8; n];
let mut lu16 = std::vec![0u16; n];
let mut sink = MixedSinker::<Ya8>::new(n, 1)
.with_luma(&mut luma)
.unwrap()
.with_luma_u16(&mut lu16)
.unwrap();
ya8_to(&frame, full_range, sink.set_kernel_matrix(M)).unwrap();
(luma, lu16)
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn limited_range_luma_is_native_y_not_rgb_derived() {
let packed = packed_frame(0xCAFE);
let frame = Ya8Frame::new(&packed, SRC as u32, SRC as u32, (SRC * 2) as u32);
let mut luma = std::vec![0u8; OUT * OUT];
let mut lu16 = std::vec![0u16; OUT * OUT];
{
let mut sink =
MixedSinker::<Ya8, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap()
.with_luma(&mut luma)
.unwrap()
.with_luma_u16(&mut lu16)
.unwrap();
ya8_to(&frame, FR_LIMITED, sink.set_kernel_matrix(M)).unwrap();
}
let binned_y = block_mean_native_y(&packed);
let (luma_ref, lu16_ref) = direct_luma_of_binned_y(&binned_y, FR_LIMITED);
assert_eq!(luma, luma_ref, "limited-range luma must be native Y");
assert_eq!(luma, binned_y, "limited-range luma == binned Y byte");
assert_eq!(
lu16, lu16_ref,
"limited-range luma_u16 must be native Y zero-extended"
);
assert_eq!(
lu16,
zero_extend(&binned_y),
"limited-range luma_u16 == binned Y zero-extended"
);
let mut luma_fr = std::vec![0u8; OUT * OUT];
let mut lu16_fr = std::vec![0u16; OUT * OUT];
{
let mut sink =
MixedSinker::<Ya8, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap()
.with_luma(&mut luma_fr)
.unwrap()
.with_luma_u16(&mut lu16_fr)
.unwrap();
ya8_to(&frame, FR, sink.set_kernel_matrix(M)).unwrap();
}
assert_eq!(luma, luma_fr, "native-Y luma must be range-independent");
assert_eq!(lu16, lu16_fr, "native-Y luma_u16 must be range-independent");
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn limited_range_y16_luma_is_16_not_rgb_scaled() {
let mut packed = std::vec![0u8; SRC * SRC * 2];
for px in packed.chunks_exact_mut(2) {
px[0] = 16; px[1] = 0xFF; }
let frame = Ya8Frame::new(&packed, SRC as u32, SRC as u32, (SRC * 2) as u32);
let mut luma = std::vec![0u8; OUT * OUT];
let mut lu16 = std::vec![0u16; OUT * OUT];
{
let mut sink =
MixedSinker::<Ya8, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap()
.with_luma(&mut luma)
.unwrap()
.with_luma_u16(&mut lu16)
.unwrap();
ya8_to(&frame, FR_LIMITED, sink.set_kernel_matrix(M)).unwrap();
}
assert!(
luma.iter().all(|&y| y == 16),
"limited-range Y=16 luma must stay native 16, got {luma:?}"
);
assert!(
lu16.iter().all(|&y| y == 16),
"limited-range Y=16 luma_u16 must stay native 16, got {lu16:?}"
);
}