use super::*;
use crate::ColorMatrix;
fn le_wire_u16(v: u16) -> u16 {
u16::from_ne_bytes(v.to_le_bytes())
}
fn assert_close(got: [f32; 3], want: [f32; 3], tol: f32, what: &str) {
for i in 0..3 {
assert!(
(got[i] - want[i]).abs() <= tol,
"{what}: channel {i} = {} (want {}, |Δ| = {})",
got[i],
want[i],
(got[i] - want[i]).abs()
);
}
}
fn encode_xyz_prime_to_norm(xyz_prime: [f32; 3]) -> [f32; 3] {
let [xp, yp, zp] = xyz_prime;
let dz = (WHITE_NORM_Z * zp - yp) / 2.0_f32;
let dx = (xp - WHITE_NORM_X * yp) / 2.0_f32;
[yp, dz, dx]
}
#[test]
fn for_transfer_selects_pq_only() {
use crate::Transfer;
assert_eq!(
Smpte2085Transfer::for_transfer(&Transfer::SmpteSt2084Pq),
Some(Smpte2085Transfer::Pq)
);
for t in [
Transfer::Unspecified,
Transfer::Bt709,
Transfer::Bt2020_10Bit,
Transfer::AribStdB67Hlg,
Transfer::SmpteSt428,
Transfer::other("unassigned-99"),
] {
assert_eq!(
Smpte2085Transfer::for_transfer(&t),
None,
"{t:?} must not select the SMPTE 2085 transfer"
);
}
assert_eq!(Smpte2085Transfer::Pq.as_str(), "pq");
}
#[test]
fn recover_xyz_prime_inverts_forward_eqs() {
let cases: &[[f32; 3]] = &[
[0.5, 0.5, 0.5],
[0.6, 0.55, 0.5],
[0.3, 0.4, 0.7],
[0.9, 0.75, 0.6],
[0.1, 0.2, 0.35],
];
for &xyz_prime in cases {
let norm = encode_xyz_prime_to_norm(xyz_prime);
let got = recover_xyz_prime(norm);
assert_close(got, xyz_prime, 1e-6, "recover_xyz_prime inverse");
}
}
#[test]
fn equal_xyz_prime_yields_small_chroma() {
for v in [0.1_f32, 0.3, 0.5, 0.75, 0.9] {
let norm = encode_xyz_prime_to_norm([v, v, v]);
assert!((norm[0] - v).abs() <= 1e-6, "Y' = {} (want {v})", norm[0]);
assert!(
norm[1].abs() <= 0.01,
"equal X'Y'Z' D'z = {} (want small)",
norm[1]
);
assert!(
norm[2].abs() <= 0.01,
"equal X'Y'Z' D'x = {} (want small)",
norm[2]
);
}
}
#[test]
fn dequant_matches_h273_convention() {
let n = dequant_smpte2085::<12>(2048, 2148, 2248, true);
assert!((n[0] - 2048.0 / 4095.0).abs() <= 1e-6);
assert!((n[1] - 100.0 / 4095.0).abs() <= 1e-6);
assert!((n[2] - 200.0 / 4095.0).abs() <= 1e-6);
let n = dequant_smpte2085::<12>(1800, 2048, 2148, false);
assert!((n[0] - (1800.0 - 256.0) / (219.0 * 16.0)).abs() <= 1e-6);
assert!((n[1] - 0.0).abs() <= 1e-6);
assert!((n[2] - (2148.0 - 2048.0) / (224.0 * 16.0)).abs() <= 1e-6);
}
fn encode_wire<const BITS: u32>(xyz_prime: [f32; 3], full_range: bool) -> [u16; 3] {
let norm = encode_xyz_prime_to_norm(xyz_prime);
let k: i32 = 1 << (BITS - 8);
let chroma_bias = (128 * k) as f32;
let (y_off, y_range, c_range): (f32, f32, f32) = if full_range {
let in_max = ((1u32 << BITS) - 1) as f32;
(0.0, in_max, in_max)
} else {
((16 * k) as f32, (219 * k) as f32, (224 * k) as f32)
};
let max = ((1u32 << BITS) - 1) as i32;
let q = |val: f32| -> u16 { (val.round() as i32).clamp(0, max) as u16 };
[
q(norm[0] * y_range + y_off),
q(norm[1] * c_range + chroma_bias),
q(norm[2] * c_range + chroma_bias),
]
}
#[cfg_attr(miri, ignore)]
#[test]
fn smpte2085_roundtrip_recovers_rgb_prime() {
let cases: &[([f32; 3], bool)] = &[
([0.6, 0.6, 0.6], true),
([0.6, 0.55, 0.5], true),
([0.65, 0.62, 0.6], false),
([0.55, 0.58, 0.62], false),
];
for &(xyz_prime, full) in cases {
let want = smpte2085_norm_to_rgb_prime(encode_xyz_prime_to_norm(xyz_prime));
let [y, dz, dx] = encode_wire::<12>(xyz_prime, full);
let dec = smpte2085_norm_to_rgb_prime(dequant_smpte2085::<12>(y, dz, dx, full));
assert_close(dec, want, 1e-3, "SMPTE 2085 R'G'B' round-trip");
}
}
#[cfg_attr(miri, ignore)]
#[test]
fn smpte2085_444p12_to_rgb_u8_roundtrip() {
let cases: &[([f32; 3], bool)] = &[
([0.6, 0.6, 0.6], true),
([0.6, 0.55, 0.5], true),
([0.65, 0.62, 0.6], false),
([0.55, 0.58, 0.62], false),
];
for &(xyz_prime, full) in cases {
let rgb_prime = smpte2085_norm_to_rgb_prime(encode_xyz_prime_to_norm(xyz_prime));
let want = [
narrow_unit_to_u8(rgb_prime[0]),
narrow_unit_to_u8(rgb_prime[1]),
narrow_unit_to_u8(rgb_prime[2]),
];
let [y, dz, dx] = encode_wire::<12>(xyz_prime, full);
let (yp, u, v) = (
[le_wire_u16(y); 2],
[le_wire_u16(dz); 2],
[le_wire_u16(dx); 2],
);
let mut out = [0_u8; 6];
smpte2085_444p_n_to_rgb_row::<12, false>(&yp, &u, &v, &mut out, 2, full);
for px in 0..2 {
for c in 0..3 {
let g = out[px * 3 + c] as i32;
assert!(
(g - want[c] as i32).abs() <= 1,
"{xyz_prime:?} full={full}: px{px} ch{c} = {g} (want {})",
want[c]
);
}
}
}
}
#[cfg_attr(miri, ignore)]
#[test]
fn smpte2085_444p12_to_rgb_u16_roundtrip() {
let cases: &[([f32; 3], bool)] = &[
([0.6, 0.6, 0.6], true),
([0.6, 0.55, 0.5], true),
([0.65, 0.62, 0.6], false),
([0.55, 0.58, 0.62], false),
];
for &(xyz_prime, full) in cases {
let rgb_prime = smpte2085_norm_to_rgb_prime(encode_xyz_prime_to_norm(xyz_prime));
let want = [
narrow_unit_to_u16_native::<12>(rgb_prime[0]),
narrow_unit_to_u16_native::<12>(rgb_prime[1]),
narrow_unit_to_u16_native::<12>(rgb_prime[2]),
];
let [y, dz, dx] = encode_wire::<12>(xyz_prime, full);
let (yp, u, v) = ([le_wire_u16(y)], [le_wire_u16(dz)], [le_wire_u16(dx)]);
let mut out = [0_u16; 3];
smpte2085_444p_n_to_rgb_u16_row::<12, false>(&yp, &u, &v, &mut out, 1, full);
for c in 0..3 {
let g = out[c] as i32;
assert!(
g <= 4095,
"{xyz_prime:?}: ch{c} = {g} over native 12-bit range"
);
assert!(
(g - want[c] as i32).abs() <= 2,
"{xyz_prime:?} full={full}: ch{c} = {g} (want {})",
want[c]
);
}
}
}
#[cfg_attr(miri, ignore)]
#[test]
fn rgba_kernels_match_rgb_plus_opaque_alpha() {
let (y, u, v) = ([2048_u16], [2148_u16], [2248_u16]);
let mut rgb = [0_u8; 3];
let mut rgba = [0_u8; 4];
smpte2085_444p_n_to_rgb_row::<12, false>(&y, &u, &v, &mut rgb, 1, true);
smpte2085_444p_n_to_rgba_row::<12, false>(&y, &u, &v, &mut rgba, 1, true);
assert_eq!(&rgba[..3], &rgb[..]);
assert_eq!(rgba[3], 0xFF);
let mut rgb16 = [0_u16; 3];
let mut rgba16 = [0_u16; 4];
smpte2085_444p_n_to_rgb_u16_row::<12, false>(&y, &u, &v, &mut rgb16, 1, true);
smpte2085_444p_n_to_rgba_u16_row::<12, false>(&y, &u, &v, &mut rgba16, 1, true);
assert_eq!(&rgba16[..3], &rgb16[..]);
assert_eq!(
rgba16[3], 4095,
"native 12-bit opaque alpha = (1 << BITS) - 1"
);
assert!(
rgb16.iter().all(|&c| c <= 4095),
"u16 RGB must be native 12-bit [0, 4095], got {rgb16:?}"
);
}
#[test]
fn big_endian_matches_swapped_little_endian() {
let le = ([2048_u16], [2148_u16], [2248_u16]);
let be = (
[2048_u16.swap_bytes()],
[2148_u16.swap_bytes()],
[2248_u16.swap_bytes()],
);
let mut out_le = [0_u8; 3];
let mut out_be = [0_u8; 3];
smpte2085_444p_n_to_rgb_row::<12, false>(&le.0, &le.1, &le.2, &mut out_le, 1, true);
smpte2085_444p_n_to_rgb_row::<12, true>(&be.0, &be.1, &be.2, &mut out_be, 1, true);
assert_eq!(out_le, out_be);
}
#[test]
#[should_panic(expected = "rgb_out row too short")]
fn smpte2085_rgb_u8_short_output_panics_on_length_check() {
use crate::{KernelMatrix, Primaries, Transfer, row::yuv444p12_to_rgb_row_smpte2085_endian};
let (y, u, v) = ([2048_u16; 2], [2048_u16; 2], [2048_u16; 2]);
let mut rgb = [0_u8; 3]; yuv444p12_to_rgb_row_smpte2085_endian(
&y,
&u,
&v,
&mut rgb,
2,
KernelMatrix::Bt709,
&ColorMatrix::Smpte2085,
&Primaries::Unspecified,
true,
&Transfer::SmpteSt2084Pq,
false,
false,
);
}
#[test]
#[should_panic(expected = "rgba_out row too short")]
fn smpte2085_rgba_u8_short_output_panics_on_length_check() {
use crate::{KernelMatrix, Primaries, Transfer, row::yuv444p12_to_rgba_row_smpte2085_endian};
let (y, u, v) = ([2048_u16; 2], [2048_u16; 2], [2048_u16; 2]);
let mut rgba = [0_u8; 4]; yuv444p12_to_rgba_row_smpte2085_endian(
&y,
&u,
&v,
&mut rgba,
2,
KernelMatrix::Bt709,
&ColorMatrix::Smpte2085,
&Primaries::Unspecified,
true,
&Transfer::SmpteSt2084Pq,
false,
false,
);
}
#[test]
#[should_panic(expected = "rgb_out row too short")]
fn smpte2085_rgb_u16_short_output_panics_on_length_check() {
use crate::{KernelMatrix, Primaries, Transfer, row::yuv444p12_to_rgb_u16_row_smpte2085_endian};
let (y, u, v) = ([2048_u16; 2], [2048_u16; 2], [2048_u16; 2]);
let mut rgb = [0_u16; 3]; yuv444p12_to_rgb_u16_row_smpte2085_endian(
&y,
&u,
&v,
&mut rgb,
2,
KernelMatrix::Bt709,
&ColorMatrix::Smpte2085,
&Primaries::Unspecified,
true,
&Transfer::SmpteSt2084Pq,
false,
false,
);
}
#[test]
#[should_panic(expected = "rgba_out row too short")]
fn smpte2085_rgba_u16_short_output_panics_on_length_check() {
use crate::{KernelMatrix, Primaries, Transfer, row::yuv444p12_to_rgba_u16_row_smpte2085_endian};
let (y, u, v) = ([2048_u16; 2], [2048_u16; 2], [2048_u16; 2]);
let mut rgba = [0_u16; 4]; yuv444p12_to_rgba_u16_row_smpte2085_endian(
&y,
&u,
&v,
&mut rgba,
2,
KernelMatrix::Bt709,
&ColorMatrix::Smpte2085,
&Primaries::Unspecified,
true,
&Transfer::SmpteSt2084Pq,
false,
false,
);
}