use crate::{
KernelMatrix,
frame::Yaf32Frame,
resample::{AreaResampler, FilteredResampler, ResampleError, Triangle},
sinker::{AlphaMode, MixedSinker, MixedSinkerError},
source::{Yaf32, yaf32_to},
};
const SRC: usize = 8;
const OUT: usize = 4;
const FR: bool = true;
const M: KernelMatrix = KernelMatrix::Bt709;
fn y_block(oy: usize, ox: usize) -> f32 {
(oy * OUT + ox) as f32 / 16.0
}
fn a_block(oy: usize, ox: usize) -> f32 {
((oy + ox) % 4) as f32 / 4.0
}
fn source() -> Vec<f32> {
let mut pix = vec![0.0f32; SRC * SRC * 2];
for y in 0..SRC {
for x in 0..SRC {
let yv = y_block(y / 2, x / 2);
let av = a_block(y / 2, x / 2);
pix[(y * SRC + x) * 2] = f32::from_bits(yv.to_bits().to_le());
pix[(y * SRC + x) * 2 + 1] = f32::from_bits(av.to_bits().to_le());
}
}
pix
}
fn f32_to_u8(v: f32) -> u8 {
(v.clamp(0.0, 1.0) * 255.0 + 0.5) as u8
}
fn frame(pix: &[f32]) -> Yaf32Frame<'_> {
Yaf32Frame::new(pix, SRC as u32, SRC as u32, (SRC * 2) as u32)
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn yaf32_downscale_luma_f32_is_native_block_mean() {
let pix = source();
let src = frame(&pix);
let mut luma_f32 = vec![0.0f32; OUT * OUT];
{
let mut sink =
MixedSinker::<Yaf32, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap()
.with_luma_f32(&mut luma_f32)
.unwrap();
yaf32_to(&src, FR, sink.set_kernel_matrix(M)).unwrap();
}
for oy in 0..OUT {
for ox in 0..OUT {
assert_eq!(
luma_f32[oy * OUT + ox],
y_block(oy, ox),
"luma_f32 must be the native Y block mean at ({oy},{ox})"
);
}
}
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn yaf32_downscale_rgba_straight() {
let pix = source();
let src = frame(&pix);
let mut rgba = vec![0u8; OUT * OUT * 4];
{
let mut sink =
MixedSinker::<Yaf32, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap()
.with_rgba(&mut rgba)
.unwrap();
yaf32_to(&src, FR, sink.set_kernel_matrix(M)).unwrap();
}
for oy in 0..OUT {
for ox in 0..OUT {
let y8 = f32_to_u8(y_block(oy, ox));
let a8 = f32_to_u8(a_block(oy, ox));
let p = (oy * OUT + ox) * 4;
assert_eq!(
&rgba[p..p + 4],
&[y8, y8, y8, a8],
"straight rgba at ({oy},{ox})"
);
}
}
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn yaf32_downscale_rgba_premultiplied_unpremultiplies_and_keeps_native_luma() {
let pix = source();
let src = frame(&pix);
let mut rgba = vec![0u8; OUT * OUT * 4];
let mut luma_f32 = vec![0.0f32; OUT * OUT];
{
let mut sink =
MixedSinker::<Yaf32, AreaResampler>::with_resampler(SRC, SRC, AreaResampler::to(OUT, OUT))
.unwrap()
.with_alpha_mode(AlphaMode::Premultiplied)
.with_rgba(&mut rgba)
.unwrap()
.with_luma_f32(&mut luma_f32)
.unwrap();
yaf32_to(&src, FR, sink.set_kernel_matrix(M)).unwrap();
}
for oy in 0..OUT {
for ox in 0..OUT {
let a = a_block(oy, ox);
let color_y = if a == 0.0 { 0.0 } else { y_block(oy, ox) };
let y8 = f32_to_u8(color_y);
let a8 = f32_to_u8(a);
let p = (oy * OUT + ox) * 4;
assert_eq!(
&rgba[p..p + 4],
&[y8, y8, y8, a8],
"premultiplied rgba at ({oy},{ox})"
);
assert_eq!(
luma_f32[oy * OUT + ox],
y_block(oy, ox),
"premultiplied native luma must stay mean(Y) at ({oy},{ox})"
);
}
}
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn yaf32_premultiplied_filter_is_unsupported() {
let pix = source();
let src = frame(&pix);
let mut rgba = vec![0u8; OUT * OUT * 4];
let mut sink = MixedSinker::<Yaf32, FilteredResampler<Triangle>>::with_resampler(
SRC,
SRC,
FilteredResampler::new(OUT, OUT, Triangle),
)
.unwrap()
.with_alpha_mode(AlphaMode::Premultiplied)
.with_rgba(&mut rgba)
.unwrap();
let err = yaf32_to(&src, FR, sink.set_kernel_matrix(M)).unwrap_err();
assert!(
matches!(
err,
MixedSinkerError::Resample(ResampleError::UnsupportedFilter(_))
),
"premultiplied filter must surface UnsupportedFilter, got {err:?}"
);
}
#[test]
#[cfg_attr(
miri,
ignore = "SIMD-dispatched row kernels use intrinsics unsupported by Miri"
)]
fn yaf32_straight_filter_populates_rgba_and_luma() {
let pix = source();
let src = frame(&pix);
let mut rgba = vec![0xABu8; OUT * OUT * 4];
let mut luma = vec![0xCDu8; OUT * OUT];
{
let mut sink = MixedSinker::<Yaf32, FilteredResampler<Triangle>>::with_resampler(
SRC,
SRC,
FilteredResampler::new(OUT, OUT, Triangle),
)
.unwrap()
.with_rgba(&mut rgba)
.unwrap()
.with_luma(&mut luma)
.unwrap();
yaf32_to(&src, FR, sink.set_kernel_matrix(M)).unwrap();
}
assert!(rgba.iter().any(|&b| b != 0xAB), "rgba must be filtered");
assert!(luma.iter().any(|&b| b != 0xCD), "luma must be filtered");
}