use crate::{
KernelMatrix, PixelSink,
resample::{
AreaResampler, CatmullRom, FilterStream, FilteredResampler, Lanczos3, ResampleError, Resampler,
Triangle,
},
sinker::{AlphaMode, MixedSinker, MixedSinkerError},
source::{
Abgr, AbgrRow, Argb, ArgbRow, Bgra, BgraRow, Rgba, RgbaRow, abgr_to, argb_to, bgra_to, rgba_to,
},
};
use mediaframe::frame::{AbgrFrame, ArgbFrame, BgraFrame, RgbaFrame};
const SRC: usize = 8;
const OUT: usize = 4;
fn packed_frame(seed: u32) -> Vec<u8> {
let mut buf = std::vec![0u8; SRC * SRC * 4];
super::pseudo_random_u8(&mut buf, seed);
buf
}
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 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 rgb = std::vec![0u8; rgba.len() / 4 * 3];
for (o, i) in rgb.chunks_exact_mut(3).zip(rgba.chunks_exact(4)) {
o.copy_from_slice(&i[..3]);
}
rgb
}
#[allow(clippy::too_many_arguments)]
fn channel_plane_filter<K: crate::resample::FilterKernel>(
kernel: K,
canonical: &[u8],
c: usize,
sw: usize,
sh: usize,
ow: usize,
oh: usize,
use_simd: bool,
) -> Vec<u8> {
let mut plane = std::vec![0u8; sw * sh];
for (dst, px) in plane.iter_mut().zip(canonical.chunks_exact(4)) {
*dst = px[c];
}
let plan = FilteredResampler::new(ow, oh, kernel)
.plan(sw, sh)
.expect("valid filter plan")
.expect("non-identity");
let fh = plan.filter_h().expect("h windows");
let fv = plan.filter_v().expect("v windows");
let mut stream = FilterStream::<u8>::new(fh, fv, sw, sh, 1).expect("geometry");
let mut out = std::vec![0u8; ow * oh];
for y in 0..sh {
stream
.feed_row(y, &plane[y * sw..(y + 1) * sw], use_simd, |oy, fin| {
out[oy * ow..(oy + 1) * ow].copy_from_slice(fin);
})
.expect("rows in order");
}
out
}
#[allow(clippy::too_many_arguments)]
fn assert_rgba_is_per_channel_filter<K: crate::resample::FilterKernel + Copy>(
kernel: K,
resampled_rgba: &[u8],
canonical: &[u8],
sw: usize,
sh: usize,
ow: usize,
oh: usize,
ctx: &str,
) {
for c in 0..4 {
let plane = channel_plane_filter(kernel, canonical, c, sw, sh, ow, oh, true);
for (i, &p) in plane.iter().enumerate() {
assert_eq!(
resampled_rgba[i * 4 + c],
p,
"{ctx} channel {c} px {i}: rgba {} vs per-plane filter {p}",
resampled_rgba[i * 4 + c],
);
}
}
}
macro_rules! rgba8_resample_suite {
($modname:ident, $marker:ident, $row:ident, $walk:ident, $frame:ident, $perm:expr) => {
mod $modname {
use super::*;
const PERM: [usize; 4] = $perm;
fn encode(canonical: &[u8]) -> Vec<u8> {
let mut src = std::vec![0u8; canonical.len()];
for (s, c) in src.chunks_exact_mut(4).zip(canonical.chunks_exact(4)) {
for k in 0..4 {
s[k] = c[PERM[k]];
}
}
src
}
fn direct_rgba(pix: &[u8]) -> Vec<u8> {
let src = $frame::try_new(pix, SRC as u32, SRC as u32, (SRC * 4) as u32).unwrap();
let mut rgba = std::vec![0u8; SRC * SRC * 4];
let mut sink = MixedSinker::<$marker>::new(SRC, SRC)
.with_rgba(&mut rgba)
.unwrap();
$walk(&src, true, sink.set_kernel_matrix(KernelMatrix::Bt709)).unwrap();
rgba
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn straight_rgba_is_block_mean_of_direct_rgba() {
let pix = packed_frame(0x51A1 ^ stringify!($modname).len() as u32);
let src = $frame::try_new(&pix, SRC as u32, SRC as u32, (SRC * 4) as u32).unwrap();
let mut rgba = std::vec![0u8; OUT * OUT * 4];
{
let mut sink = MixedSinker::<$marker, AreaResampler>::with_resampler(
SRC,
SRC,
AreaResampler::to(OUT, OUT),
)
.unwrap()
.with_rgba(&mut rgba)
.unwrap();
$walk(&src, true, sink.set_kernel_matrix(KernelMatrix::Bt709)).unwrap();
}
assert_eq!(rgba, block_mean_rgba(&direct_rgba(&pix)));
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn straight_alpha_is_averaged_not_forced_opaque() {
let mut canonical = packed_frame(0x9E37 ^ stringify!($modname).len() as u32);
for (i, px) in canonical.chunks_exact_mut(4).enumerate() {
px[3] = (i as u8).wrapping_mul(3);
}
let pix = encode(&canonical);
let src = $frame::try_new(&pix, SRC as u32, SRC as u32, (SRC * 4) as u32).unwrap();
let mut rgba = std::vec![0u8; OUT * OUT * 4];
{
let mut sink = MixedSinker::<$marker, AreaResampler>::with_resampler(
SRC,
SRC,
AreaResampler::to(OUT, OUT),
)
.unwrap()
.with_rgba(&mut rgba)
.unwrap();
$walk(&src, true, sink.set_kernel_matrix(KernelMatrix::Bt709)).unwrap();
}
assert_eq!(rgba, block_mean_rgba(&direct_rgba(&pix)));
assert!(
rgba.chunks_exact(4).any(|px| px[3] != 0xFF),
"alpha was forced opaque — the bug is back"
);
for (oy, ox) in (0..OUT).flat_map(|y| (0..OUT).map(move |x| (y, x))) {
let mut acc = 0u32;
for dy in 0..2 {
for dx in 0..2 {
acc += canonical[((oy * 2 + dy) * SRC + ox * 2 + dx) * 4 + 3] as u32;
}
}
assert_eq!(rgba[(oy * OUT + ox) * 4 + 3], ((acc + 2) / 4) as u8, "alpha mean");
}
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn straight_rgb_luma_hsv_derive_from_binned_color() {
let pix = packed_frame(0xBEEF ^ stringify!($modname).len() as u32);
let src = $frame::try_new(&pix, SRC as u32, SRC as u32, (SRC * 4) as u32).unwrap();
let mut rgb = std::vec![0u8; OUT * OUT * 3];
let mut rgba = std::vec![0u8; OUT * OUT * 4];
let mut luma = std::vec![0u8; 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::<$marker, AreaResampler>::with_resampler(
SRC,
SRC,
AreaResampler::to(OUT, OUT),
)
.unwrap()
.with_rgb(&mut rgb)
.unwrap()
.with_rgba(&mut rgba)
.unwrap()
.with_luma(&mut luma)
.unwrap()
.with_hsv(&mut h, &mut s_, &mut v_)
.unwrap();
$walk(&src, true, sink.set_kernel_matrix(KernelMatrix::Bt709)).unwrap();
}
let binned = block_mean_rgba(&direct_rgba(&pix));
assert_eq!(rgb, drop_alpha(&binned), "rgb");
let binned_rgb = drop_alpha(&binned);
let rgb_src = mediaframe::frame::Rgb24Frame::new(
&binned_rgb,
OUT as u32,
OUT as u32,
(OUT * 3) as u32,
);
let mut ref_luma = std::vec![0u8; OUT * OUT];
let mut ref_h = std::vec![0u8; OUT * OUT];
let mut ref_s = std::vec![0u8; OUT * OUT];
let mut ref_v = std::vec![0u8; OUT * OUT];
{
let mut sink = MixedSinker::<crate::source::Rgb24>::new(OUT, OUT)
.with_luma(&mut ref_luma)
.unwrap()
.with_hsv(&mut ref_h, &mut ref_s, &mut ref_v)
.unwrap();
crate::source::rgb24_to(&rgb_src, true, sink.set_kernel_matrix(KernelMatrix::Bt709)).unwrap();
}
assert_eq!(luma, ref_luma, "luma");
assert_eq!(h, ref_h, "h");
assert_eq!(s_, ref_s, "s");
assert_eq!(v_, ref_v, "v");
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn premultiplied_rgba_matches_premult_bin_unpremult_oracle() {
let pix = packed_frame(0x1234 ^ stringify!($modname).len() as u32);
let src = $frame::try_new(&pix, SRC as u32, SRC as u32, (SRC * 4) as u32).unwrap();
let mut rgba = std::vec![0u8; OUT * OUT * 4];
let mut rgb = std::vec![0u8; OUT * OUT * 3];
{
let mut sink = MixedSinker::<$marker, 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();
$walk(&src, true, sink.set_kernel_matrix(KernelMatrix::Bt709)).unwrap();
}
let mut pm = direct_rgba(&pix);
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");
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn premultiplied_transparent_block_does_not_bleed() {
let mut canonical = packed_frame(0xABCD ^ stringify!($modname).len() as u32);
for &(sx, sy) in &[(0usize, 0usize), (1, 0), (0, 1), (1, 1)] {
let p = (sy * SRC + sx) * 4;
canonical[p] = 250;
canonical[p + 1] = 240;
canonical[p + 2] = 230;
canonical[p + 3] = 0; }
let pix = encode(&canonical);
let src = $frame::try_new(&pix, SRC as u32, SRC as u32, (SRC * 4) as u32).unwrap();
let mut rgba = std::vec![0u8; OUT * OUT * 4];
{
let mut sink = MixedSinker::<$marker, AreaResampler>::with_resampler(
SRC,
SRC,
AreaResampler::to(OUT, OUT),
)
.unwrap()
.with_alpha_mode(AlphaMode::Premultiplied)
.with_rgba(&mut rgba)
.unwrap();
$walk(&src, true, sink.set_kernel_matrix(KernelMatrix::Bt709)).unwrap();
}
assert_eq!(&rgba[..4], &[0, 0, 0, 0], "transparent block bled color");
let mut pm = direct_rgba(&pix);
premultiply(&mut pm);
let oracle = unpremultiply(&block_mean_rgba(&pm));
assert_eq!(rgba, oracle);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn straight_and_premult_differ_under_varying_alpha() {
let mut canonical = packed_frame(0x77AA ^ stringify!($modname).len() as u32);
for (i, px) in canonical.chunks_exact_mut(4).enumerate() {
px[3] = 16u8.wrapping_add((i as u8).wrapping_mul(5));
}
let pix = encode(&canonical);
let render = |mode: AlphaMode| {
let src = $frame::try_new(&pix, SRC as u32, SRC as u32, (SRC * 4) as u32).unwrap();
let mut rgba = std::vec![0u8; OUT * OUT * 4];
let mut sink = MixedSinker::<$marker, AreaResampler>::with_resampler(
SRC,
SRC,
AreaResampler::to(OUT, OUT),
)
.unwrap()
.with_alpha_mode(mode)
.with_rgba(&mut rgba)
.unwrap();
$walk(&src, true, sink.set_kernel_matrix(KernelMatrix::Bt709)).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::<$marker>::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_rgba() {
let pix = packed_frame(0x0F0F ^ stringify!($modname).len() as u32);
let src = $frame::try_new(&pix, SRC as u32, SRC as u32, (SRC * 4) as u32).unwrap();
let mut via_area = std::vec![0u8; SRC * SRC * 4];
{
let mut sink = MixedSinker::<$marker, AreaResampler>::with_resampler(
SRC,
SRC,
AreaResampler::to(SRC, SRC),
)
.unwrap()
.with_rgba(&mut via_area)
.unwrap();
$walk(&src, true, sink.set_kernel_matrix(KernelMatrix::Bt709)).unwrap();
}
assert_eq!(via_area, direct_rgba(&pix));
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn no_output_sink_is_a_noop() {
let pix = packed_frame(0x4242 ^ stringify!($modname).len() as u32);
let mut sink = MixedSinker::<$marker, AreaResampler>::with_resampler(
SRC,
SRC,
AreaResampler::to(OUT, OUT),
)
.unwrap();
sink.begin_frame(SRC as u32, SRC as u32).unwrap();
let row3 = &pix[3 * SRC * 4..4 * SRC * 4];
sink.process($row::for_tests(row3, 3, KernelMatrix::Bt709, true)).unwrap();
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn cross_frame_reset_reuses_streams() {
let pix = packed_frame(0x5151 ^ stringify!($modname).len() as u32);
let src = $frame::try_new(&pix, SRC as u32, SRC as u32, (SRC * 4) as u32).unwrap();
let first;
let mut rgba = std::vec![0u8; OUT * OUT * 4];
{
let mut sink = MixedSinker::<$marker, AreaResampler>::with_resampler(
SRC,
SRC,
AreaResampler::to(OUT, OUT),
)
.unwrap()
.with_rgba(&mut rgba)
.unwrap();
$walk(&src, true, sink.set_kernel_matrix(KernelMatrix::Bt709)).unwrap();
$walk(&src, true, sink.set_kernel_matrix(KernelMatrix::Bt709)).unwrap();
}
first = block_mean_rgba(&direct_rgba(&pix));
assert_eq!(rgba, first);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn out_of_sequence_first_row_rejected() {
let pix = packed_frame(0x6262 ^ stringify!($modname).len() as u32);
let row3 = &pix[3 * SRC * 4..4 * SRC * 4];
let mut rgba = std::vec![0u8; OUT * OUT * 4];
let mut sink = MixedSinker::<$marker, 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($row::for_tests(row3, 3, KernelMatrix::Bt709, true))
.unwrap_err();
assert!(matches!(
err,
MixedSinkerError::Resample(ResampleError::OutOfSequenceRow(_))
));
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn rejects_mid_frame_output_change() {
let pix = packed_frame(0x7373 ^ stringify!($modname).len() as u32);
let mut rgba = std::vec![0u8; OUT * OUT * 4];
let mut luma = std::vec![0u8; OUT * OUT];
let mut sink = MixedSinker::<$marker, 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($row::for_tests(&pix[..SRC * 4], 0, KernelMatrix::Bt709, true))
.unwrap();
sink.set_luma(&mut luma).unwrap();
let err = sink
.process($row::for_tests(&pix[SRC * 4..2 * SRC * 4], 1, KernelMatrix::Bt709, true))
.unwrap_err();
assert!(matches!(err, MixedSinkerError::ResampleOutputsChanged(_)));
assert!(luma.iter().all(|&b| b == 0), "rejected row mutated new output");
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn rejected_first_row_does_not_poison_output_retry() {
let pix = packed_frame(0x8484 ^ stringify!($modname).len() as u32);
let mut rgba = std::vec![0u8; OUT * OUT * 4];
let mut sink = MixedSinker::<$marker, 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 row3 = &pix[3 * SRC * 4..4 * SRC * 4];
let err = sink
.process($row::for_tests(row3, 3, KernelMatrix::Bt709, true))
.unwrap_err();
assert!(matches!(
err,
MixedSinkerError::Resample(ResampleError::OutOfSequenceRow(_))
));
let mut rgb = std::vec![0u8; OUT * OUT * 3];
sink.set_rgb(&mut rgb).unwrap();
sink
.process($row::for_tests(&pix[..SRC * 4], 0, KernelMatrix::Bt709, true))
.expect("row 0 must succeed after a rejected out-of-sequence first row");
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn rejects_mid_frame_alpha_mode_change() {
let pix = packed_frame(0x9595 ^ stringify!($modname).len() as u32);
let mut rgba = std::vec![0u8; OUT * OUT * 4];
let mut sink = MixedSinker::<$marker, 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($row::for_tests(&pix[..SRC * 4], 0, KernelMatrix::Bt709, true))
.unwrap();
sink.set_alpha_mode(AlphaMode::Premultiplied);
let err = sink
.process($row::for_tests(&pix[SRC * 4..2 * SRC * 4], 1, KernelMatrix::Bt709, true))
.unwrap_err();
assert!(
matches!(err, MixedSinkerError::ResampleOutputsChanged(_)),
"expected ResampleOutputsChanged on mid-frame alpha-mode flip, got {err:?}"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn rejects_mid_frame_alpha_mode_route_switch() {
let pix = packed_frame(0xA1A1 ^ stringify!($modname).len() as u32);
let mut rgb = std::vec![0u8; OUT * SRC * 3];
let mut sink = MixedSinker::<$marker, AreaResampler>::with_resampler(
SRC,
SRC,
AreaResampler::to(OUT, SRC),
)
.unwrap()
.with_alpha_mode(AlphaMode::Premultiplied)
.with_rgb(&mut rgb)
.unwrap();
sink.begin_frame(SRC as u32, SRC as u32).unwrap();
sink
.process($row::for_tests(&pix[..SRC * 4], 0, KernelMatrix::Bt709, true))
.unwrap();
sink.set_alpha_mode(AlphaMode::Straight);
let err = sink
.process($row::for_tests(&pix[..SRC * 4], 0, KernelMatrix::Bt709, true))
.unwrap_err();
assert!(
matches!(err, MixedSinkerError::ResampleOutputsChanged(_)),
"expected ResampleOutputsChanged on mid-frame alpha-mode route-switch, got {err:?}"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn accepts_alpha_mode_change_across_frames() {
let pix = packed_frame(0xB2B2 ^ stringify!($modname).len() as u32);
let mut rgba = std::vec![0u8; OUT * OUT * 4];
let mut sink = MixedSinker::<$marker, 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();
for y in 0..SRC {
sink
.process($row::for_tests(&pix[y * SRC * 4..(y + 1) * SRC * 4], y, KernelMatrix::Bt709, true))
.unwrap();
}
sink.set_alpha_mode(AlphaMode::Premultiplied);
sink.begin_frame(SRC as u32, SRC as u32).unwrap();
for y in 0..SRC {
sink
.process($row::for_tests(&pix[y * SRC * 4..(y + 1) * SRC * 4], y, KernelMatrix::Bt709, true))
.expect("a fresh frame must accept a different alpha mode");
}
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn rejects_straight_to_premultiplied_route_switch() {
let pix = packed_frame(0xC3C3 ^ stringify!($modname).len() as u32);
let mut rgb = std::vec![0u8; OUT * SRC * 3];
let mut sink = MixedSinker::<$marker, AreaResampler>::with_resampler(
SRC,
SRC,
AreaResampler::to(OUT, SRC),
)
.unwrap()
.with_rgb(&mut rgb)
.unwrap();
sink.begin_frame(SRC as u32, SRC as u32).unwrap();
sink
.process($row::for_tests(&pix[..SRC * 4], 0, KernelMatrix::Bt709, true))
.unwrap();
sink.set_alpha_mode(AlphaMode::Premultiplied);
let err = sink
.process($row::for_tests(&pix[..SRC * 4], 0, KernelMatrix::Bt709, true))
.unwrap_err();
assert!(
matches!(err, MixedSinkerError::ResampleOutputsChanged(_)),
"expected ResampleOutputsChanged on straight-to-premultiplied route-switch, got {err:?}"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn rejects_route_switch_after_out_of_sequence_first_row() {
let pix = packed_frame(0xD4D4 ^ stringify!($modname).len() as u32);
let mut rgb = std::vec![0u8; OUT * SRC * 3];
let mut sink = MixedSinker::<$marker, AreaResampler>::with_resampler(
SRC,
SRC,
AreaResampler::to(OUT, SRC),
)
.unwrap()
.with_rgb(&mut rgb)
.unwrap();
sink.begin_frame(SRC as u32, SRC as u32).unwrap();
sink
.process($row::for_tests(&pix[3 * SRC * 4..4 * SRC * 4], 3, KernelMatrix::Bt709, true))
.unwrap_err();
sink
.process($row::for_tests(&pix[..SRC * 4], 0, KernelMatrix::Bt709, true))
.unwrap();
sink.set_alpha_mode(AlphaMode::Premultiplied);
let err = sink
.process($row::for_tests(&pix[SRC * 4..2 * SRC * 4], 1, KernelMatrix::Bt709, true))
.unwrap_err();
assert!(
matches!(err, MixedSinkerError::ResampleOutputsChanged(_)),
"expected ResampleOutputsChanged after OOS first row + route-switch, got {err:?}"
);
}
fn filter_rgba_per_channel<K: crate::resample::FilterKernel + Copy>(
kernel: K,
sw: usize,
sh: usize,
ow: usize,
oh: usize,
seed: u32,
ctx: &str,
) {
let canonical = {
let mut buf = std::vec![0u8; sw * sh * 4];
crate::sinker::mixed::tests::pseudo_random_u8(&mut buf, seed);
buf
};
let pix = encode(&canonical);
let src = $frame::try_new(&pix, sw as u32, sh as u32, (sw * 4) as u32).unwrap();
let mut rgba = std::vec![0u8; ow * oh * 4];
{
let mut sink = MixedSinker::<$marker, FilteredResampler<K>>::with_resampler(
sw,
sh,
FilteredResampler::new(ow, oh, kernel),
)
.unwrap()
.with_rgba(&mut rgba)
.unwrap();
$walk(&src, true, sink.set_kernel_matrix(KernelMatrix::Bt709)).unwrap();
}
assert_rgba_is_per_channel_filter(kernel, &rgba, &canonical, sw, sh, ow, oh, ctx);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn filter_real_alpha_is_byte_exact_per_channel_downscale() {
let s = stringify!($modname).len() as u32;
filter_rgba_per_channel(Triangle, 8, 8, 4, 4, 0xF11A ^ s, "Triangle 8x8->4x4");
filter_rgba_per_channel(CatmullRom, 8, 8, 4, 4, 0xF22B ^ s, "CatmullRom 8x8->4x4");
filter_rgba_per_channel(Lanczos3, 8, 8, 4, 4, 0xF33C ^ s, "Lanczos3 8x8->4x4");
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn filter_real_alpha_is_byte_exact_per_channel_upscale() {
let s = stringify!($modname).len() as u32;
filter_rgba_per_channel(Triangle, 4, 4, 8, 8, 0xE11A ^ s, "Triangle 4x4->8x8");
filter_rgba_per_channel(CatmullRom, 4, 4, 8, 8, 0xE22B ^ s, "CatmullRom 4x4->8x8");
filter_rgba_per_channel(Lanczos3, 4, 4, 8, 8, 0xE33C ^ s, "Lanczos3 4x4->8x8");
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn filter_real_alpha_rgb_is_drop_alpha_of_filtered_rgba() {
let s = stringify!($modname).len() as u32;
let canonical = {
let mut buf = std::vec![0u8; 8 * 8 * 4];
crate::sinker::mixed::tests::pseudo_random_u8(&mut buf, 0xD0DA ^ s);
buf
};
let pix = encode(&canonical);
let src = $frame::try_new(&pix, 8, 8, 32).unwrap();
let mut rgba = std::vec![0u8; 4 * 4 * 4];
let mut rgb = std::vec![0u8; 4 * 4 * 3];
{
let mut sink = MixedSinker::<$marker, FilteredResampler<Triangle>>::with_resampler(
8,
8,
FilteredResampler::new(4, 4, Triangle),
)
.unwrap()
.with_rgba(&mut rgba)
.unwrap()
.with_rgb(&mut rgb)
.unwrap();
$walk(&src, true, sink.set_kernel_matrix(KernelMatrix::Bt709)).unwrap();
}
assert_eq!(rgb, drop_alpha(&rgba), "rgb is drop-alpha of filtered rgba");
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn filter_plan_is_accepted_not_unsupported() {
let pix = packed_frame(0xC0DE ^ stringify!($modname).len() as u32);
let src = $frame::try_new(&pix, SRC as u32, SRC as u32, (SRC * 4) as u32).unwrap();
let mut rgba = std::vec![0u8; OUT * OUT * 4];
{
let mut sink = MixedSinker::<$marker, FilteredResampler<Triangle>>::with_resampler(
SRC,
SRC,
FilteredResampler::new(OUT, OUT, Triangle),
)
.unwrap()
.with_rgba(&mut rgba)
.unwrap();
$walk(&src, true, sink.set_kernel_matrix(KernelMatrix::Bt709))
.expect("a filter plan must be accepted for real-alpha packed RGBA");
}
let mut rgb = std::vec![0u8; OUT * OUT * 3];
{
let mut sink = MixedSinker::<$marker, FilteredResampler<Triangle>>::with_resampler(
SRC,
SRC,
FilteredResampler::new(OUT, OUT, Triangle),
)
.unwrap()
.with_rgb(&mut rgb)
.unwrap();
$walk(&src, true, sink.set_kernel_matrix(KernelMatrix::Bt709))
.expect("a filter plan must be accepted for an rgb-only sink too");
}
}
}
};
}
#[test]
fn alpha_mode_str_and_predicates() {
assert_eq!(AlphaMode::Straight.as_str(), "straight");
assert_eq!(AlphaMode::Premultiplied.as_str(), "premultiplied");
assert!(AlphaMode::Straight.is_straight());
assert!(AlphaMode::Premultiplied.is_premultiplied());
assert_eq!(AlphaMode::default(), AlphaMode::Straight);
}
rgba8_resample_suite!(rgba, Rgba, RgbaRow, rgba_to, RgbaFrame, [0, 1, 2, 3]);
rgba8_resample_suite!(bgra, Bgra, BgraRow, bgra_to, BgraFrame, [2, 1, 0, 3]);
rgba8_resample_suite!(argb, Argb, ArgbRow, argb_to, ArgbFrame, [3, 0, 1, 2]);
rgba8_resample_suite!(abgr, Abgr, AbgrRow, abgr_to, AbgrFrame, [3, 2, 1, 0]);
#[test]
#[cfg(feature = "std")]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn rgba_first_row_stream_alloc_failure_leaves_freeze_uncommitted_for_retry() {
let pix = packed_frame(0xA70A);
let mut rgba = std::vec![0u8; OUT * OUT * 4];
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::<Rgba, 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();
crate::resample::arm_box_failure();
let err = sink
.process(RgbaRow::for_tests(
&pix[..SRC * 4],
0,
KernelMatrix::Bt709,
true,
))
.unwrap_err();
assert!(
matches!(
err,
MixedSinkerError::Resample(ResampleError::AllocationFailed(_))
),
"first-row 4-channel stream box-alloc refusal must surface AllocationFailed, got {err:?}"
);
assert!(
!sink.rgba_stream_allocated(),
"a box-alloc-refused first row must leave the rgba_stream field None"
);
sink.set_hsv(&mut h, &mut s, &mut v).unwrap();
for r in 0..SRC {
sink
.process(RgbaRow::for_tests(
&pix[r * SRC * 4..(r + 1) * SRC * 4],
r,
KernelMatrix::Bt709,
true,
))
.expect("frame replay after a first-row 4-channel stream-alloc OOM must succeed");
}
}
assert!(
rgba.iter().any(|&b| b != 0),
"the recovered frame produced real RGBA output (box-alloc refusal was recoverable)"
);
}
#[test]
#[cfg(feature = "std")]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn rgba_first_row_scratch_oom_leaves_stream_and_freeze_uncommitted_for_retry() {
let pix = packed_frame(0x5C0A);
let mut rgba = std::vec![0u8; OUT * OUT * 4];
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 luma = std::vec![0u8; OUT * OUT];
{
let mut sink =
MixedSinker::<Rgba, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap()
.with_rgba(&mut rgba)
.unwrap();
sink.set_hsv(&mut h, &mut s, &mut v).unwrap();
sink.begin_frame(SRC as u32, SRC as u32).unwrap();
crate::sinker::mixed::arm_source_rgb_scratch_failure();
let err = sink
.process(RgbaRow::for_tests(
&pix[..SRC * 4],
0,
KernelMatrix::Bt709,
true,
))
.unwrap_err();
assert!(
matches!(
err,
MixedSinkerError::Resample(ResampleError::AllocationFailed(_))
),
"first-row 4-channel drop-alpha scratch OOM must surface AllocationFailed, got {err:?}"
);
assert!(
!sink.rgba_stream_allocated(),
"a scratch OOM must leave the rgba_stream field None (no partial commit)"
);
sink.set_luma(&mut luma).unwrap();
for r in 0..SRC {
sink
.process(RgbaRow::for_tests(
&pix[r * SRC * 4..(r + 1) * SRC * 4],
r,
KernelMatrix::Bt709,
true,
))
.expect("frame replay after a first-row 4-channel scratch OOM must succeed");
}
}
assert!(
rgba.iter().any(|&b| b != 0),
"the recovered frame produced real RGBA output (scratch OOM was recoverable)"
);
}