use crate::{Transfer, hdr};
use super::bits_mask;
use crate::ColorMatrix;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub(crate) enum IptC2Transfer {
Pq,
}
impl IptC2Transfer {
#[allow(dead_code)]
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) const fn as_str(self) -> &'static str {
match self {
Self::Pq => "pq",
}
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) const fn for_transfer(transfer: &Transfer) -> Option<Self> {
match transfer {
Transfer::SmpteSt2084Pq => Some(Self::Pq),
_ => None,
}
}
}
const IPTC2_IPT_TO_LMSP: [[f32; 3]; 3] = [
[1.0_f32, 0.097_557_89_f32, 0.205_382_93_f32],
[1.0_f32, -0.113_883_62_f32, 0.133_378_28_f32],
[1.0_f32, 0.032_611_65_f32, -0.676_696_2_f32],
];
const IPTC2_LMS_TO_RGB: [[f32; 3]; 3] = [
[3.237_466_f32, -2.324_205_8_f32, 0.086_739_56_f32],
[-0.718_875_47_f32, 1.877_899_9_f32, -0.159_024_48_f32],
[-0.003_403_51_f32, -0.070_985_93_f32, 1.074_389_5_f32],
];
#[cfg_attr(not(tarpaulin), inline(always))]
fn matmul3(m: &[[f32; 3]; 3], v: [f32; 3]) -> [f32; 3] {
[
m[0][0] * v[0] + m[0][1] * v[1] + m[0][2] * v[2],
m[1][0] * v[0] + m[1][1] * v[1] + m[1][2] * v[2],
m[2][0] * v[0] + m[2][1] * v[1] + m[2][2] * v[2],
]
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn load_sample<const BITS: u32, const BE: bool>(s: u16) -> u16 {
let raw = if BE { u16::from_be(s) } else { u16::from_le(s) };
raw & bits_mask::<BITS>()
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn dequant_iptc2<const BITS: u32>(i: u16, p: u16, t: u16, full_range: bool) -> [f32; 3] {
let k: i32 = 1 << (BITS - 8);
let chroma_bias = 128 * k; let (y_off, y_range, c_range): (i32, f32, f32) = if full_range {
let in_max = ((1u32 << BITS) - 1) as f32;
(0, in_max, in_max)
} else {
(16 * k, (219 * k) as f32, (224 * k) as f32)
};
[
(i as i32 - y_off) as f32 / y_range,
(p as i32 - chroma_bias) as f32 / c_range,
(t as i32 - chroma_bias) as f32 / c_range,
]
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn iptc2_norm_to_rgb_linear(norm: [f32; 3]) -> [f32; 3] {
let lms_p = matmul3(&IPTC2_IPT_TO_LMSP, norm);
let lms = [
hdr::pq_eotf(lms_p[0]),
hdr::pq_eotf(lms_p[1]),
hdr::pq_eotf(lms_p[2]),
];
matmul3(&IPTC2_LMS_TO_RGB, lms)
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn iptc2_norm_to_rgb_prime(norm: [f32; 3]) -> [f32; 3] {
let lin = iptc2_norm_to_rgb_linear(norm);
[
hdr::pq_inverse_eotf(lin[0]),
hdr::pq_inverse_eotf(lin[1]),
hdr::pq_inverse_eotf(lin[2]),
]
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn iptc2_pixel_to_rgb_prime<const BITS: u32, const BE: bool>(
i: u16,
p: u16,
t: u16,
full_range: bool,
) -> [f32; 3] {
let norm = dequant_iptc2::<BITS>(
load_sample::<BITS, BE>(i),
load_sample::<BITS, BE>(p),
load_sample::<BITS, BE>(t),
full_range,
);
iptc2_norm_to_rgb_prime(norm)
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn narrow_unit_to_u8(c: f32) -> u8 {
let scaled = c.clamp(0.0_f32, 1.0_f32) * 255.0_f32 + 0.5_f32;
scaled.clamp(0.0_f32, 255.0_f32) as u8
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn narrow_unit_to_u16_native<const BITS: u32>(c: f32) -> u16 {
let out_max = ((1u32 << BITS) - 1) as f32;
let scaled = c.clamp(0.0_f32, 1.0_f32) * out_max + 0.5_f32;
scaled.clamp(0.0_f32, out_max) as u16
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn iptc2_444p_n_to_rgb_or_rgba_row<const BITS: u32, const ALPHA: bool, const BE: bool>(
y: &[u16],
u: &[u16],
v: &[u16],
out: &mut [u8],
width: usize,
full_range: bool,
) {
let bpp: usize = if ALPHA { 4 } else { 3 };
debug_assert!(y.len() >= width, "y row too short");
debug_assert!(u.len() >= width, "u row too short");
debug_assert!(v.len() >= width, "v row too short");
debug_assert!(out.len() >= width * bpp, "out row too short");
for x in 0..width {
let rgb = iptc2_pixel_to_rgb_prime::<BITS, BE>(y[x], u[x], v[x], full_range);
out[x * bpp] = narrow_unit_to_u8(rgb[0]);
out[x * bpp + 1] = narrow_unit_to_u8(rgb[1]);
out[x * bpp + 2] = narrow_unit_to_u8(rgb[2]);
if ALPHA {
out[x * bpp + 3] = 0xFF;
}
}
}
#[cfg_attr(not(tarpaulin), inline(always))]
fn iptc2_444p_n_to_rgb_or_rgba_u16_row<const BITS: u32, const ALPHA: bool, const BE: bool>(
y: &[u16],
u: &[u16],
v: &[u16],
out: &mut [u16],
width: usize,
full_range: bool,
) {
let bpp: usize = if ALPHA { 4 } else { 3 };
debug_assert!(y.len() >= width, "y row too short");
debug_assert!(u.len() >= width, "u row too short");
debug_assert!(v.len() >= width, "v row too short");
debug_assert!(out.len() >= width * bpp, "out row too short");
for x in 0..width {
let rgb = iptc2_pixel_to_rgb_prime::<BITS, BE>(y[x], u[x], v[x], full_range);
out[x * bpp] = narrow_unit_to_u16_native::<BITS>(rgb[0]);
out[x * bpp + 1] = narrow_unit_to_u16_native::<BITS>(rgb[1]);
out[x * bpp + 2] = narrow_unit_to_u16_native::<BITS>(rgb[2]);
if ALPHA {
out[x * bpp + 3] = ((1u32 << BITS) - 1) as u16;
}
}
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn iptc2_444p_n_to_rgb_row<const BITS: u32, const BE: bool>(
y: &[u16],
u: &[u16],
v: &[u16],
rgb_out: &mut [u8],
width: usize,
full_range: bool,
) {
iptc2_444p_n_to_rgb_or_rgba_row::<BITS, false, BE>(y, u, v, rgb_out, width, full_range);
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn iptc2_444p_n_to_rgba_row<const BITS: u32, const BE: bool>(
y: &[u16],
u: &[u16],
v: &[u16],
rgba_out: &mut [u8],
width: usize,
full_range: bool,
) {
iptc2_444p_n_to_rgb_or_rgba_row::<BITS, true, BE>(y, u, v, rgba_out, width, full_range);
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn iptc2_444p_n_to_rgb_u16_row<const BITS: u32, const BE: bool>(
y: &[u16],
u: &[u16],
v: &[u16],
rgb_out: &mut [u16],
width: usize,
full_range: bool,
) {
iptc2_444p_n_to_rgb_or_rgba_u16_row::<BITS, false, BE>(y, u, v, rgb_out, width, full_range);
}
#[cfg_attr(not(tarpaulin), inline(always))]
pub(crate) fn iptc2_444p_n_to_rgba_u16_row<const BITS: u32, const BE: bool>(
y: &[u16],
u: &[u16],
v: &[u16],
rgba_out: &mut [u16],
width: usize,
full_range: bool,
) {
iptc2_444p_n_to_rgb_or_rgba_u16_row::<BITS, true, BE>(y, u, v, rgba_out, width, full_range);
}
#[cfg(all(test, feature = "std"))]
mod tests;