use crate::props::colr::{Nclx, Range};
pub(crate) const fn shift_for(wide: bool) -> u32 {
if wide { 13 } else { 16 }
}
const ALPHA_SHIFT: u32 = 14;
fn q(v: f32) -> i32 {
if v < 0.0 {
(v - 0.5) as i32
} else {
(v + 0.5) as i32
}
}
fn sample_max(depth: u8) -> i32 {
((1u32 << depth) - 1) as i32
}
pub(crate) struct Coeffs {
pub shift: u32,
pub round: i32,
pub max_out: i32,
pub y_offset: i32,
pub c_offset: i32,
pub ky: i32,
pub rv: i32,
pub gu: i32,
pub gv: i32,
pub bu: i32,
}
impl Coeffs {
pub fn new(nclx: Nclx, depth: u8, wide: bool) -> Coeffs {
let shift = shift_for(wide);
let max_out: i32 = if wide { 65_535 } else { 255 };
let unit = (max_out as f32) * ((1u32 << shift) as f32);
let max = sample_max(depth) as f32;
let (y_offset, y_gain, c_offset, c_gain) = match nclx.range {
Range::Full => (0.0, 1.0 / max, f32::from(1u16 << (depth - 1)), 1.0 / max),
Range::Limited => {
let step = (1u32 << (depth - 8)) as f32;
(
16.0 * step,
1.0 / (219.0 * step),
128.0 * step,
1.0 / (224.0 * step),
)
}
};
let (kr, kb) = nclx.matrix.luma_weights();
let kg = 1.0 - kr - kb;
let (rv, bu) = (2.0 * (1.0 - kr), 2.0 * (1.0 - kb));
let (gu, gv) = (-2.0 * kb * (1.0 - kb) / kg, -2.0 * kr * (1.0 - kr) / kg);
Coeffs {
shift,
round: 1 << (shift - 1),
max_out,
y_offset: y_offset as i32,
c_offset: c_offset as i32,
ky: q(y_gain * unit),
rv: q(rv * c_gain * unit),
gu: q(gu * c_gain * unit),
gv: q(gv * c_gain * unit),
bu: q(bu * c_gain * unit),
}
}
#[inline(always)]
pub fn finish(&self, acc: i32) -> i32 {
((acc + self.round) >> self.shift).clamp(0, self.max_out)
}
}
pub(crate) struct AlphaScale {
gain: i32,
max_in: i32,
max_out: i32,
}
impl AlphaScale {
pub fn new(depth: u8, max_out: i32) -> AlphaScale {
let max_in = sample_max(depth);
let gain = q((max_out as f32) * ((1u32 << ALPHA_SHIFT) as f32) / (max_in as f32));
AlphaScale {
gain,
max_in,
max_out,
}
}
#[inline(always)]
pub fn apply(&self, v: u16) -> i32 {
let v = core::cmp::min(i32::from(v), self.max_in);
((v * self.gain + (1 << (ALPHA_SHIFT - 1))) >> ALPHA_SHIFT).clamp(0, self.max_out)
}
}