#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub enum CodingTransform {
#[default]
Xyb,
YCbCr,
Rgb,
}
impl CodingTransform {
#[inline]
pub(crate) fn is_xyb(self) -> bool {
self == Self::Xyb
}
#[inline]
pub(crate) fn base_correlation_x(self) -> f32 {
match self {
Self::Rgb => 1.0,
Self::Xyb | Self::YCbCr => 0.0,
}
}
#[inline]
pub(crate) fn base_correlation_b(self) -> f32 {
match self {
Self::Xyb | Self::Rgb => 1.0,
Self::YCbCr => 0.0,
}
}
#[inline]
pub(crate) fn dc_step_base(self) -> [f32; 3] {
match self {
Self::Xyb => [1.0; 3],
Self::YCbCr => [8.0 * 2.07, 1.0, 0.5 * 3.27],
Self::Rgb => [8.0 * 2.32, 1.0, 0.5 * 3.68],
}
}
#[inline]
pub(crate) fn table_scale(self) -> [f32; 3] {
match self {
Self::Xyb => [1.0; 3],
Self::YCbCr => [0.321, 1.0, 0.487],
Self::Rgb => [0.486, 1.0, 0.271],
}
}
#[inline]
pub(crate) fn table_hf_tilt(self) -> [f32; 3] {
match self {
Self::Xyb => [1.0; 3],
Self::YCbCr => [0.768, 0.609, 1.561],
Self::Rgb => [0.546, 1.542, 0.766],
}
}
pub(crate) fn plane_scale(self, coded: &crate::image::Image3F) -> [f32; 3] {
let (red_full, blue_full, start, full) = match self {
Self::Xyb => return [1.0; 3],
Self::Rgb => ([1.375, 1.0, 1.0], [1.0, 1.0, 2.0], 0.04, 0.20),
Self::YCbCr => ([1.375, 2.0, 1.375], [1.375, 2.8125, 1.0], 0.30, 0.80),
};
let [red, blue] = dominant_gradient_shares(coded, |p| self.decode(p))
.map(|share: f32| ((share - start) / (full - start)).clamp(0.0, 1.0));
std::array::from_fn(|c| {
let m = (1.0 + (red_full[c] - 1.0) * red).max(1.0 + (blue_full[c] - 1.0) * blue);
(16.0 * m).round() / 16.0
})
}
#[inline]
fn decode(self, [p0, p1, p2]: [f32; 3]) -> [f32; 3] {
match self {
Self::YCbCr => {
let y = p1 + 128.0 / 255.0;
[
y + 2.0 * (1.0 - KR) * p2,
y - 2.0 * (1.0 - KB) * KB / KG * p0 - 2.0 * (1.0 - KR) * KR / KG * p2,
y + 2.0 * (1.0 - KB) * p0,
]
}
Self::Rgb | Self::Xyb => [p0, p1, p2],
}
}
#[inline]
pub(crate) fn channel_weights(self) -> [f32; 3] {
match self {
Self::Xyb => [0.30, 1.0, 0.28],
Self::YCbCr => [0.125, 1.0, 0.255],
Self::Rgb => [1.1, 1.0, 0.159],
}
}
#[inline]
pub(crate) fn epf_channel_scale(self) -> Option<[f32; 3]> {
match self {
Self::Xyb => None,
Self::YCbCr | Self::Rgb => Some([5.0; 3]),
}
}
#[inline]
pub(crate) fn code(self, r: f32, g: f32, b: f32) -> [f32; 3] {
match self {
Self::YCbCr => ycbcr_triple(r, g, b),
Self::Rgb => [r, g, b],
Self::Xyb => unreachable!("XYB planes come from the opsin transform"),
}
}
}
pub(crate) fn fit_cfl_frame(
coding: CodingTransform,
coded: &crate::image::Image3F,
) -> crate::color_correlation::CflFrame {
const LIMIT: f32 = 4.0;
const TILE: usize = 64;
const REACH: f32 = 1.4;
const TEXTURED: f64 = 0.05;
const OUT_OF_REACH: f32 = 0.1;
const STEP: f32 = 1.0 / 64.0;
let (w, h) = (coded.xsize(), coded.ysize());
let tiles_x = w.div_ceil(TILE);
let mut all = [0.0f64; 3];
let mut tiles = vec![[0.0f64; 3]; tiles_x * h.div_ceil(TILE)];
for y in 0..h {
let rows = [0, 1, 2].map(|c| coded.plane_row(c, y));
let below = (y + 1 < h).then(|| [0, 1, 2].map(|c| coded.plane_row(c, y + 1)));
let tile_row = &mut tiles[(y / TILE) * tiles_x..][..tiles_x];
for x in 0..w {
let tile = &mut tile_row[x / TILE];
let mut add = |d: [f32; 3], within_tile: bool| {
let dy = f64::from(d[1]);
let terms = [dy * dy, f64::from(d[0]) * dy, f64::from(d[2]) * dy];
for i in 0..3 {
all[i] += terms[i];
if within_tile {
tile[i] += terms[i];
}
}
};
if x + 1 < w {
add(
[0, 1, 2].map(|c| rows[c][x + 1] - rows[c][x]),
(x + 1) % TILE != 0,
);
}
if let Some(below) = below {
add(
[0, 1, 2].map(|c| below[c][x] - rows[c][x]),
(y + 1) % TILE != 0,
);
}
}
}
let yy = all[0];
let textured = TEXTURED * tiles.iter().map(|t| t[0]).sum::<f64>() / tiles.len().max(1) as f64;
let fit = |plane: usize| -> f32 {
if yy <= 0.0 {
return 0.0;
}
let least_squares = ((all[plane] / yy) as f32).clamp(-LIMIT, LIMIT);
let slopes: Vec<f32> = tiles
.iter()
.filter(|t| t[0] > textured)
.map(|t| (t[plane] / t[0]) as f32)
.collect();
let out_of_reach = |base: f32| slopes.iter().filter(|&&s| (s - base).abs() > REACH).count();
let at_least_squares = out_of_reach(least_squares);
if coding != CodingTransform::YCbCr
|| (at_least_squares as f32) <= OUT_OF_REACH * slopes.len() as f32
{
return least_squares;
}
let mut best = (at_least_squares, 0.0f32, least_squares);
for i in 0..=(2.0 * LIMIT / STEP) as i32 {
let base = -LIMIT + i as f32 * STEP;
let key = (out_of_reach(base), (base - least_squares).abs());
if (key.0, key.1) < (best.0, best.1) {
best = (key.0, key.1, base);
}
}
best.2
};
let [base_x, base_b] = [fit(1), fit(2)]
.map(|slope| crate::util::f16_bits_to_f32(crate::util::f32_to_f16_bits(slope)));
crate::color_correlation::CflFrame {
base_x,
base_b,
color_factor: crate::color_correlation::K_COLOR_FACTOR,
}
}
fn dominant_gradient_shares(
coded: &crate::image::Image3F,
decode: impl Fn([f32; 3]) -> [f32; 3],
) -> [f32; 2] {
const FLOOR: f32 = 0.15;
const RATIO: f32 = 1.6;
let (w, h) = (coded.xsize(), coded.ysize());
let decode_row = |y: usize, out: &mut Vec<[f32; 3]>| {
let rows = [0, 1, 2].map(|c| coded.plane_row(c, y));
out.clear();
out.extend((0..w).map(|x| decode([rows[0][x], rows[1][x], rows[2][x]])));
};
let (mut row, mut below) = (Vec::with_capacity(w), Vec::with_capacity(w));
if h > 0 {
decode_row(0, &mut row);
}
let (mut total, mut dominant) = (0.0f64, [0.0f64; 2]);
for y in 0..h {
if y + 1 < h {
decode_row(y + 1, &mut below);
}
for x in 0..w {
let px = row[x];
let mut energy = [0.0f32; 3];
for c in 0..3 {
if x + 1 < w {
let d = row[x + 1][c] - px[c];
energy[c] += d * d;
}
if y + 1 < h {
let d = below[x][c] - px[c];
energy[c] += d * d;
}
}
total += f64::from(energy[0] + energy[1] + energy[2]);
let [r, g, b] = px;
if r > FLOOR && r > RATIO * g.max(b) {
dominant[0] += f64::from(energy[0]);
}
if b > FLOOR && b > RATIO * r.max(g) {
dominant[1] += f64::from(energy[2]);
}
}
std::mem::swap(&mut row, &mut below);
}
if total > 0.0 {
dominant.map(|e| (e / total) as f32)
} else {
[0.0; 2]
}
}
const KR: f32 = 0.299;
const KG: f32 = 0.587;
const KB: f32 = 0.114;
#[inline]
fn ycbcr_triple(r: f32, g: f32, b: f32) -> [f32; 3] {
let luma = KR * r + KG * g + KB * b;
[
(b - luma) / (2.0 * (1.0 - KB)),
luma - 128.0 / 255.0,
(r - luma) / (2.0 * (1.0 - KR)),
]
}
#[inline]
pub(crate) fn srgb_eotf(v: f32) -> f32 {
if v <= 0.040_45 {
v / 12.92
} else {
((v + 0.055) / 1.055).powf(2.4)
}
}
#[cfg(test)]
mod tests {
use super::*;
fn decode_ycbcr([cb, y, cr]: [f32; 3]) -> [f32; 3] {
let y = y + 128.0 / 255.0;
[
y + 1.402 * cr,
y - 0.114 * 1.772 / 0.587 * cb - 0.299 * 1.402 / 0.587 * cr,
y + 1.772 * cb,
]
}
#[test]
fn dominant_color_refines_its_plane() {
let textured = |coding: CodingTransform, dominant: usize| {
let mut img = crate::image::Image3F::new(32, 32);
for y in 0..32 {
let [p0, p1, p2] = img.all_plane_rows_mut(y);
for x in 0..32 {
let rgb: [f32; 3] = std::array::from_fn(|c| {
if c == dominant {
0.5 + 0.4 * (((x + y) % 4) as f32 / 3.0 - 0.5)
} else {
0.05
}
});
[p0[x], p1[x], p2[x]] = coding.code(rgb[0], rgb[1], rgb[2]);
}
}
img
};
let scale = |coding: CodingTransform, dominant: usize| {
coding.plane_scale(&textured(coding, dominant))
};
assert_eq!(scale(CodingTransform::Rgb, 0), [1.375, 1.0, 1.0]);
assert_eq!(scale(CodingTransform::Rgb, 2), [1.0, 1.0, 2.0]);
assert_eq!(scale(CodingTransform::Rgb, 1), [1.0; 3]);
assert_eq!(scale(CodingTransform::YCbCr, 0), [1.375, 2.0, 1.375]);
assert_eq!(scale(CodingTransform::YCbCr, 2), [1.375, 2.8125, 1.0]);
assert_eq!(scale(CodingTransform::YCbCr, 1), [1.0; 3]);
}
#[test]
fn cfl_base_reaches_mixed_hue_tiles() {
let coding = CodingTransform::YCbCr;
let mut img = crate::image::Image3F::new(128, 64);
for y in 0..64 {
let [p0, p1, p2] = img.all_plane_rows_mut(y);
for x in 0..128 {
let t = 0.5 + 0.3 * (((x * 7 + y * 3) % 5) as f32 / 4.0 - 0.5);
let [r, g, b] = if x < 64 { [t, 0.0, 0.0] } else { [0.2, t, 0.2] };
[p0[x], p1[x], p2[x]] = coding.code(r, g, b);
}
}
let base = fit_cfl_frame(coding, &img).base_b;
assert!(
(base - 1.67).abs() <= 1.4 && (base + 0.71).abs() <= 1.4,
"{base}"
);
let red = |x: usize, y: usize| 0.5 + 0.3 * (((x * 7 + y * 3) % 5) as f32 / 4.0 - 0.5);
for y in 0..64 {
let [p0, p1, p2] = img.all_plane_rows_mut(y);
for x in 0..128 {
[p0[x], p1[x], p2[x]] = coding.code(red(x, y), 0.0, 0.0);
}
}
let base = fit_cfl_frame(coding, &img).base_b;
assert!((base - 0.5 / 0.299).abs() < 0.02, "{base}");
}
#[test]
fn ycbcr_round_trips_through_the_decoder_matrix() {
for &(r, g, b) in &[
(0.0, 0.0, 0.0),
(1.0, 1.0, 1.0),
(0.9, 0.2, 0.4),
(0.1, 0.7, 0.95),
] {
let got = decode_ycbcr(CodingTransform::YCbCr.code(r, g, b));
for (a, e) in got.iter().zip([r, g, b]) {
assert!((a - e).abs() < 1e-5, "{got:?} vs {:?}", [r, g, b]);
}
}
assert_eq!(
CodingTransform::Rgb.code(0.25, 0.5, 0.75),
[0.25, 0.5, 0.75]
);
}
}