use crate::{
PixelSink,
frame::Pal8Frame,
resample::{AreaResampler, ResampleError},
sinker::{AlphaMode, MixedSinker, MixedSinkerError},
source::{Pal8, Pal8Row, pal8_to},
};
const SRC: usize = 8;
const OUT: usize = 4;
fn fill_pseudo_random(buf: &mut [u8], seed: u32) {
let mut state = seed;
for b in buf {
state = state.wrapping_mul(1_664_525).wrapping_add(1_013_904_223);
*b = (state >> 16) as u8;
}
}
fn varied_palette(seed: u32) -> [[u8; 4]; 256] {
let mut p = [[0u8; 4]; 256];
for (i, entry) in p.iter_mut().enumerate() {
let i = i as u32;
entry[0] = ((i.wrapping_mul(97).wrapping_add(seed)) ^ 0x5A) as u8; entry[1] = ((i.wrapping_mul(57).wrapping_add(seed)) ^ 0x3C) as u8; entry[2] = ((i.wrapping_mul(193).wrapping_add(seed)) ^ 0xA5) as u8; entry[3] = ((i.wrapping_mul(151).wrapping_add(seed)) ^ 0xF0) as u8; }
p
}
fn index_plane(seed: u32) -> Vec<u8> {
let mut buf = std::vec![0u8; SRC * SRC];
fill_pseudo_random(&mut buf, seed);
buf
}
fn direct_rgba(indices: &[u8], palette: &[[u8; 4]; 256]) -> Vec<u8> {
let frame = Pal8Frame::new(indices, palette, SRC as u32, SRC as u32, SRC as u32);
let mut rgba = std::vec![0u8; SRC * SRC * 4];
let mut sink = MixedSinker::<Pal8>::new(SRC, SRC)
.with_rgba(&mut rgba)
.unwrap();
pal8_to(&frame, &mut sink).unwrap();
rgba
}
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 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 expand_u16(plane: &[u8]) -> Vec<u16> {
plane
.iter()
.map(|&v| {
let v = v as u16;
(v << 8) | v
})
.collect()
}
fn per_pixel_palette(binned_rgba: &[u8]) -> (Vec<u8>, [[u8; 4]; 256]) {
let n = binned_rgba.len() / 4;
let mut palette = [[0u8; 4]; 256];
let mut indices = std::vec![0u8; n];
for (i, px) in binned_rgba.chunks_exact(4).enumerate() {
palette[i] = [px[2], px[1], px[0], px[3]];
indices[i] = i as u8;
}
(indices, palette)
}
fn direct_luma_of_binned(binned_rgba: &[u8]) -> (Vec<u8>, Vec<u16>) {
let n = binned_rgba.len() / 4;
let (indices, palette) = per_pixel_palette(binned_rgba);
let frame = Pal8Frame::new(&indices, &palette, n as u32, 1, n as u32);
let mut luma = std::vec![0u8; n];
let mut lu16 = std::vec![0u16; n];
let mut sink = MixedSinker::<Pal8>::new(n, 1)
.with_luma(&mut luma)
.unwrap()
.with_luma_u16(&mut lu16)
.unwrap();
pal8_to(&frame, &mut sink).unwrap();
(luma, lu16)
}
fn direct_hsv_of_binned(binned_rgba: &[u8]) -> (Vec<u8>, Vec<u8>, Vec<u8>) {
let n = binned_rgba.len() / 4;
let (indices, palette) = per_pixel_palette(binned_rgba);
let frame = Pal8Frame::new(&indices, &palette, n as u32, 1, n as u32);
let mut h = std::vec![0u8; n];
let mut s = std::vec![0u8; n];
let mut v = std::vec![0u8; n];
let mut sink = MixedSinker::<Pal8>::new(n, 1)
.with_hsv(&mut h, &mut s, &mut v)
.unwrap();
pal8_to(&frame, &mut sink).unwrap();
(h, s, v)
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn straight_rgba_is_block_mean_of_direct() {
let palette = varied_palette(0x11);
let indices = index_plane(0x51A1);
let frame = Pal8Frame::new(&indices, &palette, SRC as u32, SRC as u32, SRC as u32);
let mut rgba = std::vec![0u8; OUT * OUT * 4];
{
let mut sink =
MixedSinker::<Pal8, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap()
.with_rgba(&mut rgba)
.unwrap();
pal8_to(&frame, &mut sink).unwrap();
}
assert_eq!(rgba, block_mean_rgba(&direct_rgba(&indices, &palette)));
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn color_average_not_index_average() {
let mut palette = [[0u8; 4]; 256];
for entry in palette.iter_mut() {
*entry = [0, 0, 0, 255];
}
palette[0] = [0, 0, 0, 255]; palette[255] = [254, 254, 254, 255]; palette[127] = [0, 0, 255, 255];
let mut indices = std::vec![0u8; SRC * SRC];
for (i, ix) in indices.iter_mut().enumerate() {
let col = i % SRC;
*ix = if col % 2 == 1 { 255 } else { 0 };
}
let frame = Pal8Frame::new(&indices, &palette, SRC as u32, SRC as u32, SRC as u32);
let mut rgba = std::vec![0u8; OUT * OUT * 4];
{
let mut sink =
MixedSinker::<Pal8, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap()
.with_rgba(&mut rgba)
.unwrap();
pal8_to(&frame, &mut sink).unwrap();
}
let oracle = block_mean_rgba(&direct_rgba(&indices, &palette));
assert_eq!(rgba, oracle, "binned color != expand-then-bin oracle");
for px in rgba.chunks_exact(4) {
assert_eq!(
px,
[127, 127, 127, 255],
"expected the COLOR-average gray 127, got {px:?}"
);
}
assert_eq!(palette[127], [0, 0, 255, 255], "fixture: index-avg entry");
assert!(
rgba.chunks_exact(4).all(|px| px[0] != 255 || px[1] != 0),
"binned result is the INDEX-average (pure red) — bin-then-expand bug"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn straight_alpha_is_averaged_not_forced_opaque() {
let palette = varied_palette(0x7E);
let indices = index_plane(0x9E37);
let frame = Pal8Frame::new(&indices, &palette, SRC as u32, SRC as u32, SRC as u32);
let mut rgba = std::vec![0u8; OUT * OUT * 4];
{
let mut sink =
MixedSinker::<Pal8, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap()
.with_rgba(&mut rgba)
.unwrap();
pal8_to(&frame, &mut sink).unwrap();
}
let oracle = block_mean_rgba(&direct_rgba(&indices, &palette));
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 palette = varied_palette(0x42);
let indices = index_plane(0xBEEF);
let frame = Pal8Frame::new(&indices, &palette, SRC as u32, SRC as u32, SRC 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::<Pal8, 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();
pal8_to(&frame, &mut sink).unwrap();
}
let binned = block_mean_rgba(&direct_rgba(&indices, &palette));
assert_eq!(rgba, binned, "rgba == block mean");
assert_eq!(rgb, drop_alpha(&binned), "rgb == drop-alpha(binned)");
assert_eq!(
rgba_u16,
expand_u16(&binned),
"rgba_u16 == (x<<8)|x of binned"
);
assert_eq!(
rgb_u16,
expand_u16(&drop_alpha(&binned)),
"rgb_u16 == (x<<8)|x of drop-alpha(binned)"
);
let (luma_ref, lu16_ref) = direct_luma_of_binned(&binned);
let (h_ref, s_ref, v_ref) = direct_hsv_of_binned(&binned);
assert_eq!(luma, luma_ref, "luma (Q8 BT.709 of binned RGB)");
assert_eq!(lu16, lu16_ref, "luma_u16 ((y<<8)|y of binned RGB)");
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 palette = varied_palette(0x33);
let indices = index_plane(0x1234);
let frame = Pal8Frame::new(&indices, &palette, SRC as u32, SRC as u32, SRC 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 luma = std::vec![0u8; OUT * OUT];
{
let mut sink =
MixedSinker::<Pal8, 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(&mut luma)
.unwrap();
pal8_to(&frame, &mut sink).unwrap();
}
let mut pm = direct_rgba(&indices, &palette);
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, expand_u16(&oracle), "premult rgba_u16");
let (luma_ref, _) = direct_luma_of_binned(&oracle);
assert_eq!(luma, luma_ref, "premult luma (Q8 of un-premult color)");
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn premultiplied_transparent_block_does_not_bleed() {
let mut palette = varied_palette(0xAB);
let mut indices = index_plane(0xABCD);
palette[7] = [200, 210, 220, 0]; for off in [(0, 0), (1, 0), (0, 1), (1, 1)] {
indices[off.1 * SRC + off.0] = 7;
}
let frame = Pal8Frame::new(&indices, &palette, SRC as u32, SRC as u32, SRC as u32);
let mut rgba = std::vec![0u8; OUT * OUT * 4];
{
let mut sink =
MixedSinker::<Pal8, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap()
.with_alpha_mode(AlphaMode::Premultiplied)
.with_rgba(&mut rgba)
.unwrap();
pal8_to(&frame, &mut sink).unwrap();
}
assert_eq!(&rgba[..4], &[0, 0, 0, 0], "transparent block bled color");
let mut pm = direct_rgba(&indices, &palette);
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 straight_and_premult_differ_under_varying_alpha() {
let palette = varied_palette(0x77);
let indices = index_plane(0x77AA);
let render = |mode: AlphaMode| {
let frame = Pal8Frame::new(&indices, &palette, SRC as u32, SRC as u32, SRC as u32);
let mut rgba = std::vec![0u8; OUT * OUT * 4];
let mut sink =
MixedSinker::<Pal8, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap()
.with_alpha_mode(mode)
.with_rgba(&mut rgba)
.unwrap();
pal8_to(&frame, &mut sink).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::<Pal8>::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 palette = varied_palette(0x0F);
let indices = index_plane(0x0F0F);
let frame = Pal8Frame::new(&indices, &palette, SRC as u32, SRC as u32, SRC as u32);
let mut rgba = std::vec![0u8; SRC * SRC * 4];
{
let mut sink =
MixedSinker::<Pal8, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(SRC, SRC))
.unwrap()
.with_rgba(&mut rgba)
.unwrap();
pal8_to(&frame, &mut sink).unwrap();
}
assert_eq!(
rgba,
direct_rgba(&indices, &palette),
"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 palette = varied_palette(0xAC);
let indices = index_plane(0xF2AC);
let frame = Pal8Frame::new(&indices, &palette, SRC as u32, SRC as u32, SRC as u32);
let mut rgba = std::vec![0u8; F * F * 4];
{
let mut sink =
MixedSinker::<Pal8, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(F, F))
.unwrap()
.with_rgba(&mut rgba)
.unwrap();
pal8_to(&frame, &mut sink).unwrap();
}
let canonical = direct_rgba(&indices, &palette);
let mut rgba_ref = std::vec![0u8; F * F * 4];
{
use crate::{KernelMatrix, 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, true, sink.set_kernel_matrix(KernelMatrix::Bt709)).unwrap();
}
assert_eq!(rgba, rgba_ref, "Pal8 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 palette = varied_palette(0x51);
let indices = index_plane(0x5151);
let frame = Pal8Frame::new(&indices, &palette, SRC as u32, SRC as u32, SRC as u32);
let mut rgba = std::vec![0u8; OUT * OUT * 4];
{
let mut sink =
MixedSinker::<Pal8, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap()
.with_rgba(&mut rgba)
.unwrap();
pal8_to(&frame, &mut sink).unwrap();
pal8_to(&frame, &mut sink).unwrap();
}
assert_eq!(rgba, block_mean_rgba(&direct_rgba(&indices, &palette)));
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn accepts_alpha_mode_change_across_frames() {
let palette = varied_palette(0xB2);
let indices = index_plane(0xB2B2);
let frame = Pal8Frame::new(&indices, &palette, SRC as u32, SRC as u32, SRC as u32);
let mut rgba = std::vec![0u8; OUT * OUT * 4];
{
let mut sink =
MixedSinker::<Pal8, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap()
.with_rgba(&mut rgba)
.unwrap();
pal8_to(&frame, &mut sink).unwrap();
sink.set_alpha_mode(AlphaMode::Premultiplied);
pal8_to(&frame, &mut sink).expect("a fresh frame must accept a different alpha mode");
}
let mut pm = direct_rgba(&indices, &palette);
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 palette = varied_palette(0x3A);
let indices = index_plane(0x33AA);
let mut rgba = std::vec![0u8; OUT * OUT * 4];
let mut sink =
MixedSinker::<Pal8, 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(Pal8Row::for_tests(&indices[..SRC], &palette, 0))
.unwrap();
sink.set_alpha_mode(AlphaMode::Premultiplied);
let err = sink
.process(Pal8Row::for_tests(&indices[SRC..2 * SRC], &palette, 1))
.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 palette = varied_palette(0x4B);
let indices = index_plane(0x44BB);
let mut rgba = std::vec![0u8; OUT * OUT * 4];
let mut sink =
MixedSinker::<Pal8, 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(Pal8Row::for_tests(&indices[SRC..2 * SRC], &palette, 1))
.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 palette = varied_palette(0x24);
let indices = index_plane(0x4242);
let frame = Pal8Frame::new(&indices, &palette, SRC as u32, SRC as u32, SRC as u32);
let mut sink =
MixedSinker::<Pal8, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap();
pal8_to(&frame, &mut sink).unwrap();
}
#[test]
#[cfg(feature = "std")]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn area_first_row_scratch_oom_leaves_stream_and_freeze_uncommitted_for_retry() {
let palette = varied_palette(0x71);
let indices = index_plane(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::<Pal8, 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(Pal8Row::for_tests(&indices[..SRC], &palette, 0))
.unwrap_err();
assert!(
matches!(
err,
MixedSinkerError::Resample(ResampleError::AllocationFailed(_))
),
"first-row 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)"
);
assert!(
!sink.resample_outputs_frozen(),
"a first-row scratch OOM must leave resample_outputs uncommitted (no partial commit)"
);
sink.set_luma(&mut luma).unwrap();
for r in 0..SRC {
sink
.process(Pal8Row::for_tests(
&indices[r * SRC..(r + 1) * SRC],
&palette,
r,
))
.expect("frame replay after a first-row scratch OOM must succeed");
}
}
assert!(
rgba.iter().any(|&b| b != 0),
"the recovered frame produced real RGBA output (scratch OOM was recoverable)"
);
assert!(
luma.iter().any(|&b| b != 0),
"the changed-output retry drove the newly-attached luma"
);
}