use crate::{
ChromaLocation, ColorInfo, ColorMatrix, ColorSpec, DynamicRange, PixelFormat, Primaries,
Transfer, sinker::MixedSinker,
};
fn le_wire_u16(v: u16) -> u16 {
u16::from_ne_bytes(v.to_le_bytes())
}
fn decode_rgb(
yc: u16,
cbc: u16,
crc: u16,
full_range: bool,
matrix: ColorMatrix,
primaries: Primaries,
transfer: Transfer,
) -> std::vec::Vec<u8> {
let (w, h) = (4usize, 2usize);
let n = w * h;
let (y, u, v) = (
std::vec![le_wire_u16(yc); n],
std::vec![le_wire_u16(cbc); n],
std::vec![le_wire_u16(crc); n],
);
let src = crate::frame::Yuv444pFrame16::<12>::new(
&y, &u, &v, w as u32, h as u32, w as u32, w as u32, w as u32,
);
let mut rgb = std::vec![0u8; n * 3];
let range = if full_range {
DynamicRange::Full
} else {
DynamicRange::Limited
};
let spec = ColorSpec::from_info(
PixelFormat::Yuv444p12Le,
ColorInfo::new(primaries, transfer, matrix, range, ChromaLocation::Left),
);
{
let mut sink = MixedSinker::<crate::source::Yuv444p12>::new(w, h)
.with_rgb(&mut rgb)
.unwrap()
.with_color_spec(&spec)
.unwrap();
crate::source::yuv444p12_to(
&src,
full_range,
sink.set_kernel_matrix(spec.kernel_matrix().unwrap()),
)
.unwrap();
}
rgb
}
fn decode_rgb_u16(
yc: u16,
cbc: u16,
crc: u16,
full_range: bool,
matrix: ColorMatrix,
primaries: Primaries,
transfer: Transfer,
) -> std::vec::Vec<u16> {
let (w, h) = (4usize, 2usize);
let n = w * h;
let (y, u, v) = (
std::vec![le_wire_u16(yc); n],
std::vec![le_wire_u16(cbc); n],
std::vec![le_wire_u16(crc); n],
);
let src = crate::frame::Yuv444pFrame16::<12>::new(
&y, &u, &v, w as u32, h as u32, w as u32, w as u32, w as u32,
);
let mut rgb = std::vec![0u16; n * 3];
let range = if full_range {
DynamicRange::Full
} else {
DynamicRange::Limited
};
let spec = ColorSpec::from_info(
PixelFormat::Yuv444p12Le,
ColorInfo::new(primaries, transfer, matrix, range, ChromaLocation::Left),
);
{
let mut sink = MixedSinker::<crate::source::Yuv444p12>::new(w, h)
.with_rgb_u16(&mut rgb)
.unwrap()
.with_color_spec(&spec)
.unwrap();
crate::source::yuv444p12_to(
&src,
full_range,
sink.set_kernel_matrix(spec.kernel_matrix().unwrap()),
)
.unwrap();
}
rgb
}
fn decode_rgba_u16(
yc: u16,
cbc: u16,
crc: u16,
full_range: bool,
transfer: Transfer,
also_rgb_u16: bool,
) -> std::vec::Vec<u16> {
let (w, h) = (4usize, 2usize);
let n = w * h;
let (y, u, v) = (
std::vec![le_wire_u16(yc); n],
std::vec![le_wire_u16(cbc); n],
std::vec![le_wire_u16(crc); n],
);
let src = crate::frame::Yuv444pFrame16::<12>::new(
&y, &u, &v, w as u32, h as u32, w as u32, w as u32, w as u32,
);
let range = if full_range {
DynamicRange::Full
} else {
DynamicRange::Limited
};
let spec = ColorSpec::from_info(
PixelFormat::Yuv444p12Le,
ColorInfo::new(
Primaries::Bt2020,
transfer,
ColorMatrix::ChromaDerivedCl,
range,
ChromaLocation::Left,
),
);
let mut rgba = std::vec![0u16; n * 4];
let mut rgb = std::vec![0u16; n * 3];
if also_rgb_u16 {
let mut sink = MixedSinker::<crate::source::Yuv444p12>::new(w, h)
.with_rgba_u16(&mut rgba)
.unwrap()
.with_rgb_u16(&mut rgb)
.unwrap()
.with_color_spec(&spec)
.unwrap();
crate::source::yuv444p12_to(
&src,
full_range,
sink.set_kernel_matrix(spec.kernel_matrix().unwrap()),
)
.unwrap();
} else {
let mut sink = MixedSinker::<crate::source::Yuv444p12>::new(w, h)
.with_rgba_u16(&mut rgba)
.unwrap()
.with_color_spec(&spec)
.unwrap();
crate::source::yuv444p12_to(
&src,
full_range,
sink.set_kernel_matrix(spec.kernel_matrix().unwrap()),
)
.unwrap();
}
rgba
}
fn assert_all_pixels_u8(rgb: &[u8], want: [u8; 3], tol: i32, what: &str) {
for (px, chunk) in rgb.chunks_exact(3).enumerate() {
for c in 0..3 {
assert!(
(chunk[c] as i32 - want[c] as i32).abs() <= tol,
"{what}: px{px} ch{c} = {} (want {})",
chunk[c],
want[c]
);
}
}
}
fn assert_all_pixels_u16(rgb: &[u16], want: [u16; 3], tol: i32, what: &str) {
for (px, chunk) in rgb.chunks_exact(3).enumerate() {
for c in 0..3 {
assert!(
chunk[c] <= 4095,
"{what}: px{px} ch{c} over native 12-bit range"
);
assert!(
(chunk[c] as i32 - want[c] as i32).abs() <= tol,
"{what}: px{px} ch{c} = {} (want {})",
chunk[c],
want[c]
);
}
}
}
#[test]
fn sink_routes_bt2020_chroma_derived_cl_through_non_affine_decode() {
let rgb = decode_rgb(
2048,
2148,
1948,
false,
ColorMatrix::ChromaDerivedCl,
Primaries::Bt2020,
Transfer::Bt2020_12Bit,
);
assert_all_pixels_u8(&rgb, [118, 134, 142], 1, "CL sink RGB (studio 12-bit)");
let rgb_u16 = decode_rgb_u16(
2048,
2148,
1948,
false,
ColorMatrix::ChromaDerivedCl,
Primaries::Bt2020,
Transfer::Bt2020_12Bit,
);
assert_all_pixels_u16(
&rgb_u16,
[1898, 2149, 2275],
2,
"CL sink u16 (studio 12-bit)",
);
}
#[test]
fn sink_full_range_chroma_derived_cl_matches_reference() {
let rgb = decode_rgb(
2048,
2148,
1948,
true,
ColorMatrix::ChromaDerivedCl,
Primaries::Bt2020,
Transfer::Bt2020_12Bit,
);
assert_all_pixels_u8(&rgb, [117, 131, 137], 1, "CL sink RGB (full 12-bit)");
}
#[test]
fn sink_gray_axis_is_neutral_and_round_trips() {
let rgb = decode_rgb(
2048,
2048,
2048,
true,
ColorMatrix::ChromaDerivedCl,
Primaries::Bt2020,
Transfer::Bt2020_12Bit,
);
assert_all_pixels_u8(&rgb, [128, 128, 128], 0, "CL gray u8");
let rgb_u16 = decode_rgb_u16(
2048,
2048,
2048,
true,
ColorMatrix::ChromaDerivedCl,
Primaries::Bt2020,
Transfer::Bt2020_12Bit,
);
assert_all_pixels_u16(&rgb_u16, [2048, 2048, 2048], 0, "CL gray u16 round-trip");
}
#[test]
fn ten_bit_and_twelve_bit_oetf_differ_end_to_end() {
let ten = decode_rgb_u16(
293,
2600,
1500,
false,
ColorMatrix::ChromaDerivedCl,
Primaries::Bt2020,
Transfer::Bt2020_10Bit,
);
let twelve = decode_rgb_u16(
293,
2600,
1500,
false,
ColorMatrix::ChromaDerivedCl,
Primaries::Bt2020,
Transfer::Bt2020_12Bit,
);
assert_ne!(ten, twelve, "10-bit vs 12-bit OETF must decode differently");
}
#[test]
fn chroma_derived_cl_without_bt2020_primaries_falls_back_to_affine() {
let bt709 = decode_rgb(
2048,
2148,
1948,
false,
ColorMatrix::ChromaDerivedCl,
Primaries::Bt709,
Transfer::Bt2020_12Bit,
);
let bt2020 = decode_rgb(
2048,
2148,
1948,
false,
ColorMatrix::ChromaDerivedCl,
Primaries::Bt2020,
Transfer::Bt2020_12Bit,
);
assert_ne!(
bt709, bt2020,
"ChromaDerivedCl + non-BT.2020 primaries must fall back to affine (≠ CL decode)"
);
}
#[test]
fn chroma_derived_cl_bt2020_pq_transfer_falls_back_to_affine() {
let pq = decode_rgb(
2048,
2148,
1948,
false,
ColorMatrix::ChromaDerivedCl,
Primaries::Bt2020,
Transfer::SmpteSt2084Pq,
);
let affine_ref = decode_rgb(
2048,
2148,
1948,
false,
ColorMatrix::ChromaDerivedCl,
Primaries::Bt709,
Transfer::Bt2020_12Bit,
);
let cl = decode_rgb(
2048,
2148,
1948,
false,
ColorMatrix::ChromaDerivedCl,
Primaries::Bt2020,
Transfer::Bt2020_12Bit,
);
assert_eq!(
pq, affine_ref,
"BT.2020 + PQ ChromaDerivedCl must take the affine fallback, like a non-BT.2020 source"
);
assert_ne!(
pq, cl,
"BT.2020 + PQ must NOT decode through the CL camera inverse-OETF"
);
}
#[test]
fn non_chroma_derived_cl_matrix_ignores_primaries() {
let bt709_with_2020 = decode_rgb(
2048,
2148,
1948,
false,
ColorMatrix::Bt709,
Primaries::Bt2020,
Transfer::Bt2020_12Bit,
);
let bt709_with_709 = decode_rgb(
2048,
2148,
1948,
false,
ColorMatrix::Bt709,
Primaries::Bt709,
Transfer::Bt2020_12Bit,
);
assert_eq!(
bt709_with_2020, bt709_with_709,
"non-CL matrix must not route on primaries"
);
}
#[test]
fn chroma_derived_cl_u16_rgba_route_consistent_and_native_depth() {
for &full in &[true, false] {
let only = decode_rgba_u16(2048, 2148, 1948, full, Transfer::Bt2020_12Bit, false);
let with_rgb = decode_rgba_u16(2048, 2148, 1948, full, Transfer::Bt2020_12Bit, true);
assert_eq!(
only, with_rgb,
"rgba_u16-only must equal rgb_u16+rgba_u16 (full={full})"
);
for px in only.chunks_exact(4) {
assert!(
px[..3].iter().all(|&c| c <= 4095),
"rgba_u16 RGB over native 12-bit range: {px:?}"
);
assert_eq!(
px[3], 4095,
"native 12-bit opaque alpha must be 4095, got {}",
px[3]
);
}
}
}
fn resample_rgb(
yc: u16,
cbc: u16,
crc: u16,
primaries: Primaries,
transfer: Transfer,
) -> Result<(), crate::sinker::MixedSinkerError> {
use crate::resample::AreaResampler;
const SRC: usize = 4;
const OUT: usize = 2;
let n = SRC * SRC;
let (y, u, v) = (
std::vec![le_wire_u16(yc); n],
std::vec![le_wire_u16(cbc); n],
std::vec![le_wire_u16(crc); n],
);
let src = crate::frame::Yuv444pFrame16::<12>::new(
&y, &u, &v, SRC as u32, SRC as u32, SRC as u32, SRC as u32, SRC as u32,
);
let spec = ColorSpec::from_info(
PixelFormat::Yuv444p12Le,
ColorInfo::new(
primaries,
transfer,
ColorMatrix::ChromaDerivedCl,
DynamicRange::Limited,
ChromaLocation::Left,
),
);
let mut rgb = std::vec![0u8; OUT * OUT * 3];
let mut sink = MixedSinker::<crate::source::Yuv444p12, AreaResampler>::with_resampler(
SRC,
SRC,
AreaResampler::to(OUT, OUT),
)
.unwrap()
.with_rgb(&mut rgb)
.unwrap()
.with_color_spec(&spec)
.unwrap();
crate::source::yuv444p12_to(
&src,
false,
sink.set_kernel_matrix(spec.kernel_matrix().unwrap()),
)
}
#[test]
fn chroma_derived_cl_bt2020_resample_returns_typed_error() {
let err = resample_rgb(2048, 2148, 1948, Primaries::Bt2020, Transfer::Bt2020_12Bit)
.expect_err("CL + BT.2020 + resample must be rejected");
match err {
crate::sinker::MixedSinkerError::UnsupportedMatrixResample(e) => {
assert_eq!(
e.matrix(),
"ChromaDerivedCl",
"error names the offending matrix"
);
}
other => panic!("expected UnsupportedMatrixResample, got {other:?}"),
}
}
#[test]
fn chroma_derived_cl_unresolved_resample_is_affine_ok() {
resample_rgb(2048, 2148, 1948, Primaries::Bt709, Transfer::Bt2020_12Bit)
.expect("unresolved CL (non-BT.2020) must resample affinely, no error");
}
#[test]
fn chroma_derived_cl_bt2020_unsupported_transfer_resample_is_affine_ok() {
resample_rgb(2048, 2148, 1948, Primaries::Bt2020, Transfer::SmpteSt2084Pq)
.expect("BT.2020 + PQ (unresolved CL) must resample affinely, no error");
}