use crate::{
KernelGamut,
frame::Xyz12LeFrame,
resample::AreaResampler,
row::scalar::{xyz12::matmul3_xyz_rgb, xyz12_constants::xyz_to_rgb_matrix},
sinker::MixedSinker,
source::{Xyz12Le, xyz12_to},
};
const SRC: usize = 8;
const OUT: usize = 4;
const TOL: f32 = 1e-4;
fn pack12_le(code: u16) -> u16 {
u16::from_ne_bytes((code << 4).to_le_bytes())
}
fn constant_block_frame() -> Vec<u16> {
let blocks: [[(u16, u16, u16); OUT]; OUT] = [
[
(0x000, 0x000, 0x000),
(0x800, 0x800, 0x800),
(0xFFF, 0xFFF, 0xFFF),
(0x000, 0xFFF, 0x000),
],
[
(0x123, 0x456, 0x789),
(0xABC, 0x0DE, 0x0F0),
(0x400, 0x200, 0x600),
(0xFFF, 0x000, 0x000),
],
[
(0x000, 0x000, 0xFFF),
(0x333, 0x999, 0x222),
(0x700, 0x800, 0x900),
(0x0F0, 0x0FF, 0x00F),
],
[
(0xFFF, 0x800, 0x400),
(0x100, 0x900, 0x100),
(0x555, 0x555, 0x555),
(0xC00, 0x300, 0xE00),
],
];
let mut buf = vec![0u16; SRC * SRC * 3];
for sy in 0..SRC {
for sx in 0..SRC {
let (x, y, z) = blocks[sy / 2][sx / 2];
let i = (sy * SRC + sx) * 3;
buf[i] = pack12_le(x);
buf[i + 1] = pack12_le(y);
buf[i + 2] = pack12_le(z);
}
}
buf
}
fn varying_frame() -> Vec<u16> {
let mut buf = vec![0u16; SRC * SRC * 3];
for (i, c) in buf.iter_mut().enumerate() {
let code = ((i * 137) % 4096) as u16;
*c = pack12_le(code);
}
buf
}
fn full_res_linear_xyz(wire: &[u16], gamut: KernelGamut) -> Vec<f32> {
let src = Xyz12LeFrame::try_new(wire, SRC as u32, SRC as u32, (SRC * 3) as u32).unwrap();
let mut xyz = vec![0.0f32; SRC * SRC * 3];
let mut sink = MixedSinker::<Xyz12Le>::new(SRC, SRC)
.with_simd(false)
.with_xyz_f32(&mut xyz)
.unwrap();
xyz12_to(&src, sink.set_kernel_gamut(gamut)).unwrap();
xyz
}
fn block_mean_linear_xyz(src_xyz: &[f32], ox: usize, oy: usize, c: usize) -> f64 {
let mut acc = 0.0f64;
for dy in 0..2 {
for dx in 0..2 {
acc += src_xyz[((oy * 2 + dy) * SRC + ox * 2 + dx) * 3 + c] as f64;
}
}
acc / 4.0
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn xyz12_constant_block_outputs_match_direct_full_res() {
for &gamut in &[
KernelGamut::DciP3,
KernelGamut::Rec709,
KernelGamut::Rec2020,
] {
let wire = constant_block_frame();
let src = Xyz12LeFrame::try_new(&wire, SRC as u32, SRC as u32, (SRC * 3) as u32).unwrap();
let mut rgb = vec![0u8; OUT * OUT * 3];
let mut rgba = vec![0u8; OUT * OUT * 4];
let mut rgb_u16 = vec![0u16; OUT * OUT * 3];
let mut rgba_u16 = vec![0u16; OUT * OUT * 4];
let mut rgb_f32 = vec![0.0f32; OUT * OUT * 3];
let mut xyz_f32 = vec![0.0f32; OUT * OUT * 3];
let mut rgb_f16 = vec![half::f16::ZERO; OUT * OUT * 3];
let mut rgba_f16 = vec![half::f16::ZERO; OUT * OUT * 4];
let mut luma = vec![0u8; OUT * OUT];
let mut luma_u16 = vec![0u16; OUT * OUT];
let mut hh = vec![0u8; OUT * OUT];
let mut ss = vec![0u8; OUT * OUT];
let mut vv = vec![0u8; OUT * OUT];
{
let mut sink = MixedSinker::<Xyz12Le, AreaResampler>::with_resampler(
SRC,
SRC,
AreaResampler::to(OUT, OUT),
)
.unwrap()
.with_simd(false)
.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_rgb_f32(&mut rgb_f32)
.unwrap()
.with_xyz_f32(&mut xyz_f32)
.unwrap()
.with_rgb_f16(&mut rgb_f16)
.unwrap()
.with_rgba_f16(&mut rgba_f16)
.unwrap()
.with_luma(&mut luma)
.unwrap()
.with_luma_u16(&mut luma_u16)
.unwrap()
.with_hsv(&mut hh, &mut ss, &mut vv)
.unwrap();
xyz12_to(&src, sink.set_kernel_gamut(gamut)).unwrap();
}
let mut d_rgb = vec![0u8; SRC * SRC * 3];
let mut d_rgba = vec![0u8; SRC * SRC * 4];
let mut d_rgb_u16 = vec![0u16; SRC * SRC * 3];
let mut d_rgba_u16 = vec![0u16; SRC * SRC * 4];
let mut d_rgb_f32 = vec![0.0f32; SRC * SRC * 3];
let mut d_xyz_f32 = vec![0.0f32; SRC * SRC * 3];
let mut d_rgb_f16 = vec![half::f16::ZERO; SRC * SRC * 3];
let mut d_rgba_f16 = vec![half::f16::ZERO; SRC * SRC * 4];
let mut d_luma = vec![0u8; SRC * SRC];
let mut d_luma_u16 = vec![0u16; SRC * SRC];
let mut d_h = vec![0u8; SRC * SRC];
let mut d_s = vec![0u8; SRC * SRC];
let mut d_v = vec![0u8; SRC * SRC];
{
let mut sink = MixedSinker::<Xyz12Le>::new(SRC, SRC)
.with_simd(false)
.with_rgb(&mut d_rgb)
.unwrap()
.with_rgba(&mut d_rgba)
.unwrap()
.with_rgb_u16(&mut d_rgb_u16)
.unwrap()
.with_rgba_u16(&mut d_rgba_u16)
.unwrap()
.with_rgb_f32(&mut d_rgb_f32)
.unwrap()
.with_xyz_f32(&mut d_xyz_f32)
.unwrap()
.with_rgb_f16(&mut d_rgb_f16)
.unwrap()
.with_rgba_f16(&mut d_rgba_f16)
.unwrap()
.with_luma(&mut d_luma)
.unwrap()
.with_luma_u16(&mut d_luma_u16)
.unwrap()
.with_hsv(&mut d_h, &mut d_s, &mut d_v)
.unwrap();
xyz12_to(&src, sink.set_kernel_gamut(gamut)).unwrap();
}
for oy in 0..OUT {
for ox in 0..OUT {
let sp = (oy * 2) * SRC + ox * 2;
let op = oy * OUT + ox;
for c in 0..3 {
assert_eq!(
rgb[op * 3 + c],
d_rgb[sp * 3 + c],
"rgb ({ox},{oy}) c{c} gamut={gamut:?}"
);
assert_eq!(
rgb_u16[op * 3 + c],
d_rgb_u16[sp * 3 + c],
"rgb_u16 ({ox},{oy}) c{c}"
);
assert_eq!(
rgb_f32[op * 3 + c].to_bits(),
d_rgb_f32[sp * 3 + c].to_bits(),
"rgb_f32 ({ox},{oy}) c{c} gamut={gamut:?}"
);
assert_eq!(
xyz_f32[op * 3 + c].to_bits(),
d_xyz_f32[sp * 3 + c].to_bits(),
"xyz_f32 ({ox},{oy}) c{c} gamut={gamut:?}"
);
assert_eq!(
rgb_f16[op * 3 + c].to_bits(),
d_rgb_f16[sp * 3 + c].to_bits(),
"rgb_f16 ({ox},{oy}) c{c}"
);
}
for c in 0..4 {
assert_eq!(
rgba[op * 4 + c],
d_rgba[sp * 4 + c],
"rgba ({ox},{oy}) c{c}"
);
assert_eq!(
rgba_u16[op * 4 + c],
d_rgba_u16[sp * 4 + c],
"rgba_u16 ({ox},{oy}) c{c}"
);
assert_eq!(
rgba_f16[op * 4 + c].to_bits(),
d_rgba_f16[sp * 4 + c].to_bits(),
"rgba_f16 ({ox},{oy}) c{c}"
);
}
assert_eq!(luma[op], d_luma[sp], "luma ({ox},{oy}) gamut={gamut:?}");
assert_eq!(luma_u16[op], d_luma_u16[sp], "luma_u16 ({ox},{oy})");
assert_eq!(hh[op], d_h[sp], "hsv.h ({ox},{oy})");
assert_eq!(ss[op], d_s[sp], "hsv.s ({ox},{oy})");
assert_eq!(vv[op], d_v[sp], "hsv.v ({ox},{oy})");
}
}
}
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn xyz12_downscale_xyz_f32_is_linear_light_area_mean() {
let gamut = KernelGamut::Rec709;
let wire = varying_frame();
let src = Xyz12LeFrame::try_new(&wire, SRC as u32, SRC as u32, (SRC * 3) as u32).unwrap();
let src_xyz = full_res_linear_xyz(&wire, gamut);
let mut xyz_f32 = vec![0.0f32; OUT * OUT * 3];
{
let mut sink =
MixedSinker::<Xyz12Le, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap()
.with_simd(false)
.with_xyz_f32(&mut xyz_f32)
.unwrap();
xyz12_to(&src, sink.set_kernel_gamut(gamut)).unwrap();
}
for oy in 0..OUT {
for ox in 0..OUT {
for c in 0..3 {
let got = xyz_f32[(oy * OUT + ox) * 3 + c];
let want = block_mean_linear_xyz(&src_xyz, ox, oy, c) as f32;
assert!(
(got - want).abs() <= TOL,
"xyz_f32 ({ox},{oy}) c{c}: {got} vs {want}"
);
}
}
}
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn xyz12_downscale_rgb_f32_is_matrix_of_binned_xyz() {
let gamut = KernelGamut::DciP3;
let wire = varying_frame();
let src = Xyz12LeFrame::try_new(&wire, SRC as u32, SRC as u32, (SRC * 3) as u32).unwrap();
let src_xyz = full_res_linear_xyz(&wire, gamut);
let m = xyz_to_rgb_matrix(gamut);
let mut rgb_f32 = vec![0.0f32; OUT * OUT * 3];
{
let mut sink =
MixedSinker::<Xyz12Le, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap()
.with_simd(false)
.with_rgb_f32(&mut rgb_f32)
.unwrap();
xyz12_to(&src, sink.set_kernel_gamut(gamut)).unwrap();
}
for oy in 0..OUT {
for ox in 0..OUT {
let bx = block_mean_linear_xyz(&src_xyz, ox, oy, 0) as f32;
let by = block_mean_linear_xyz(&src_xyz, ox, oy, 1) as f32;
let bz = block_mean_linear_xyz(&src_xyz, ox, oy, 2) as f32;
let want = matmul3_xyz_rgb(&m, [bx, by, bz]);
for c in 0..3 {
let got = rgb_f32[(oy * OUT + ox) * 3 + c];
assert!(
(got - want[c]).abs() <= TOL,
"rgb_f32 ({ox},{oy}) c{c}: {got} vs {}",
want[c]
);
}
}
}
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn xyz12_downscale_preserves_hdr_and_out_of_gamut() {
let gamut = KernelGamut::Rec709;
let wire = constant_block_frame();
let src = Xyz12LeFrame::try_new(&wire, SRC as u32, SRC as u32, (SRC * 3) as u32).unwrap();
let mut xyz_f32 = vec![0.0f32; OUT * OUT * 3];
let mut rgb_f32 = vec![0.0f32; OUT * OUT * 3];
{
let mut sink =
MixedSinker::<Xyz12Le, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap()
.with_simd(false)
.with_xyz_f32(&mut xyz_f32)
.unwrap()
.with_rgb_f32(&mut rgb_f32)
.unwrap();
xyz12_to(&src, sink.set_kernel_gamut(gamut)).unwrap();
}
let op = 3;
assert!(
xyz_f32[op * 3 + 1] > 1.0,
"y-only-max binned Y should be HDR > 1, got {}",
xyz_f32[op * 3 + 1]
);
assert!(
rgb_f32.iter().any(|&v| v < 0.0),
"binned rgb_f32 clamped away out-of-gamut negatives"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn xyz12_simd_downscale_matches_scalar_on_constant_blocks() {
let gamut = KernelGamut::DciP3;
let wire = constant_block_frame();
let src = Xyz12LeFrame::try_new(&wire, SRC as u32, SRC as u32, (SRC * 3) as u32).unwrap();
let run = |simd: bool| {
let mut rgb_u16 = vec![0u16; OUT * OUT * 3];
let mut xyz_f32 = vec![0.0f32; OUT * OUT * 3];
let mut sink =
MixedSinker::<Xyz12Le, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap()
.with_simd(simd)
.with_rgb_u16(&mut rgb_u16)
.unwrap()
.with_xyz_f32(&mut xyz_f32)
.unwrap();
xyz12_to(&src, sink.set_kernel_gamut(gamut)).unwrap();
(rgb_u16, xyz_f32)
};
let (s_rgb_u16, s_xyz) = run(false);
let (v_rgb_u16, v_xyz) = run(true);
assert_eq!(s_rgb_u16, v_rgb_u16, "rgb_u16 scalar vs simd");
for (a, b) in s_xyz.iter().zip(v_xyz.iter()) {
assert_eq!(a.to_bits(), b.to_bits(), "xyz_f32 scalar vs simd");
}
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn xyz12_identity_plan_matches_new_sink() {
let gamut = KernelGamut::Rec709;
let wire = varying_frame();
let src = Xyz12LeFrame::try_new(&wire, SRC as u32, SRC as u32, (SRC * 3) as u32).unwrap();
let mut d_rgb = vec![0u8; SRC * SRC * 3];
let mut d_xyz = vec![0.0f32; SRC * SRC * 3];
{
let mut sink = MixedSinker::<Xyz12Le>::new(SRC, SRC)
.with_rgb(&mut d_rgb)
.unwrap()
.with_xyz_f32(&mut d_xyz)
.unwrap();
xyz12_to(&src, sink.set_kernel_gamut(gamut)).unwrap();
}
let mut a_rgb = vec![0u8; SRC * SRC * 3];
let mut a_xyz = vec![0.0f32; SRC * SRC * 3];
{
let mut sink =
MixedSinker::<Xyz12Le, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(SRC, SRC))
.unwrap()
.with_rgb(&mut a_rgb)
.unwrap()
.with_xyz_f32(&mut a_xyz)
.unwrap();
xyz12_to(&src, sink.set_kernel_gamut(gamut)).unwrap();
}
assert_eq!(d_rgb, a_rgb, "rgb identity");
for (a, b) in d_xyz.iter().zip(a_xyz.iter()) {
assert_eq!(a.to_bits(), b.to_bits(), "xyz_f32 identity");
}
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn xyz12_no_output_sink_is_a_noop() {
let wire = varying_frame();
let src = Xyz12LeFrame::try_new(&wire, SRC as u32, SRC as u32, (SRC * 3) as u32).unwrap();
let mut sink =
MixedSinker::<Xyz12Le, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap();
xyz12_to(&src, sink.set_kernel_gamut(KernelGamut::DciP3)).unwrap();
assert!(
!sink.xyz_stream_f32_allocated(),
"no-output sink allocated the linear-XYZ stream"
);
assert_eq!(
sink.xyz_scratch_f32_capacity(),
0,
"no-output sink grew the linear-XYZ scratch"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn xyz12_f32_only_downscale_does_not_size_the_narrow_scratch() {
let wire = varying_frame();
let src = Xyz12LeFrame::try_new(&wire, SRC as u32, SRC as u32, (SRC * 3) as u32).unwrap();
let mut xyz_f32 = vec![0.0f32; OUT * OUT * 3];
let mut rgb_f32 = vec![0.0f32; OUT * OUT * 3];
let mut sink =
MixedSinker::<Xyz12Le, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap()
.with_xyz_f32(&mut xyz_f32)
.unwrap()
.with_rgb_f32(&mut rgb_f32)
.unwrap();
xyz12_to(&src, sink.set_kernel_gamut(KernelGamut::DciP3)).unwrap();
assert_eq!(
sink.rgb_scratch_capacity(),
0,
"f32-only sink grew the u8 narrow scratch"
);
let mut rgb = vec![0u8; OUT * OUT * 3];
let mut sink2 =
MixedSinker::<Xyz12Le, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap()
.with_rgb(&mut rgb)
.unwrap();
xyz12_to(&src, sink2.set_kernel_gamut(KernelGamut::DciP3)).unwrap();
assert!(
sink2.rgb_scratch_capacity() >= OUT * 3,
"u8 output did not size the narrow scratch"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn xyz12_begin_frame_resets_stream_between_frames() {
let gamut = KernelGamut::Rec709;
let wire_a = constant_block_frame();
let wire_b = varying_frame();
let src_a = Xyz12LeFrame::try_new(&wire_a, SRC as u32, SRC as u32, (SRC * 3) as u32).unwrap();
let src_b = Xyz12LeFrame::try_new(&wire_b, SRC as u32, SRC as u32, (SRC * 3) as u32).unwrap();
let mut xyz_f32 = vec![0.0f32; OUT * OUT * 3];
let mut sink =
MixedSinker::<Xyz12Le, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap()
.with_simd(false)
.with_xyz_f32(&mut xyz_f32)
.unwrap();
xyz12_to(&src_a, sink.set_kernel_gamut(gamut)).unwrap();
xyz12_to(&src_b, sink.set_kernel_gamut(gamut)).unwrap();
let src_xyz_b = full_res_linear_xyz(&wire_b, gamut);
for oy in 0..OUT {
for ox in 0..OUT {
for c in 0..3 {
let got = xyz_f32[(oy * OUT + ox) * 3 + c];
let want = block_mean_linear_xyz(&src_xyz_b, ox, oy, c) as f32;
assert!(
(got - want).abs() <= TOL,
"frame B xyz_f32 ({ox},{oy}) c{c}: {got} vs {want}"
);
}
}
}
}
#[test]
#[cfg(feature = "std")]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn first_row_source_scratch_oom_leaves_stream_and_freeze_uncommitted_for_retry() {
let gamut = KernelGamut::Rec709;
let wire = varying_frame();
let src = Xyz12LeFrame::try_new(&wire, SRC as u32, SRC as u32, (SRC * 3) as u32).unwrap();
let mut xyz = std::vec![0f32; OUT * OUT * 3];
let mut rgb = std::vec![0u8; OUT * OUT * 3];
{
let mut sink =
MixedSinker::<Xyz12Le, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap()
.with_xyz_f32(&mut xyz)
.unwrap();
crate::sinker::mixed::arm_source_xyz_f32_scratch_failure();
let err = xyz12_to(&src, sink.set_kernel_gamut(gamut)).unwrap_err();
assert!(
matches!(
err,
crate::sinker::MixedSinkerError::Resample(
crate::resample::ResampleError::AllocationFailed(_)
)
),
"first-row source-XYZ scratch OOM must surface AllocationFailed, got {err:?}"
);
assert!(
!sink.xyz_stream_f32_allocated(),
"a scratch OOM must leave the xyz_stream_f32 field None (no partial commit)"
);
assert!(
!sink.resample_outputs_frozen(),
"a first-row scratch OOM must leave resample_outputs uncommitted (no partial commit)"
);
sink.set_rgb(&mut rgb).unwrap();
xyz12_to(&src, sink.set_kernel_gamut(gamut))
.expect("frame replay after a first-row source-XYZ scratch OOM must succeed");
}
assert!(
xyz.iter().any(|&v| v != 0.0),
"the recovered frame produced real xyz_f32 output (scratch OOM was recoverable)"
);
}
#[test]
#[cfg(feature = "std")]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn first_row_emit_narrow_scratch_oom_leaves_stream_and_freeze_uncommitted_for_retry() {
let gamut = KernelGamut::Rec709;
let wire = varying_frame();
let src = Xyz12LeFrame::try_new(&wire, SRC as u32, SRC as u32, (SRC * 3) as u32).unwrap();
let mut rgb = std::vec![0u8; OUT * OUT * 3];
let mut xyz = std::vec![0f32; OUT * OUT * 3];
{
let mut sink =
MixedSinker::<Xyz12Le, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap()
.with_rgb(&mut rgb)
.unwrap();
crate::sinker::mixed::arm_source_rgb_scratch_failure();
let err = xyz12_to(&src, sink.set_kernel_gamut(gamut)).unwrap_err();
assert!(
matches!(
err,
crate::sinker::MixedSinkerError::Resample(
crate::resample::ResampleError::AllocationFailed(_)
)
),
"first-row emit-narrow scratch OOM must surface AllocationFailed, got {err:?}"
);
assert!(
!sink.xyz_stream_f32_allocated(),
"an emit-owned scratch OOM must leave the xyz_stream_f32 field None (no partial commit)"
);
assert!(
!sink.resample_outputs_frozen(),
"a first-row scratch OOM must leave resample_outputs uncommitted (no partial commit)"
);
sink.set_xyz_f32(&mut xyz).unwrap();
xyz12_to(&src, sink.set_kernel_gamut(gamut))
.expect("frame replay after a first-row emit-narrow scratch OOM must succeed");
}
assert!(
rgb.iter().any(|&b| b != 0),
"the recovered frame produced real rgb output (scratch OOM was recoverable)"
);
}