const SCALE_BITS: u32 = 16;
const ONE_HALF: i32 = 1 << (SCALE_BITS - 1);
const FIX_1_40200: i32 = 91_881;
const FIX_1_77200: i32 = 116_130;
const FIX_0_71414: i32 = 46_802;
const FIX_0_34414: i32 = 22_554;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum ColorSpace {
Gray,
YCbCr,
Rgb,
Cmyk,
Ycck,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub(super) struct ColorHints {
pub(super) jfif: bool,
pub(super) adobe: Option<u8>,
}
impl ColorSpace {
pub(super) fn guess(ids: &[u8], hints: ColorHints) -> Self {
match ids {
[_] => Self::Gray,
[_, _, _] if hints.jfif => Self::YCbCr,
[_, _, _] => match hints.adobe {
Some(0) => Self::Rgb,
None if ids == b"RGB" => Self::Rgb,
_ => Self::YCbCr,
},
_ => match hints.adobe {
None | Some(0) => Self::Cmyk,
Some(_) => Self::Ycck,
},
}
}
}
#[derive(Debug)]
struct YccTables {
cr_r: [i32; 256],
cb_b: [i32; 256],
cr_g: [i32; 256],
cb_g: [i32; 256],
}
impl YccTables {
fn new() -> Self {
let x = |i: usize| i32::try_from(i).expect("the tables have 256 entries") - 128;
Self {
cr_r: std::array::from_fn(|i| (FIX_1_40200 * x(i) + ONE_HALF) >> SCALE_BITS),
cb_b: std::array::from_fn(|i| (FIX_1_77200 * x(i) + ONE_HALF) >> SCALE_BITS),
cr_g: std::array::from_fn(|i| -FIX_0_71414 * x(i)),
cb_g: std::array::from_fn(|i| -FIX_0_34414 * x(i) + ONE_HALF),
}
}
fn rgb(&self, y: u8, cb: u8, cr: u8) -> [u8; 3] {
let (y, cb, cr) = (i32::from(y), usize::from(cb), usize::from(cr));
let green = (self.cb_g[cb] + self.cr_g[cr]) >> SCALE_BITS;
[y + self.cr_r[cr], y + green, y + self.cb_b[cb]].map(clamp)
}
}
fn clamp(value: i32) -> u8 {
u8::try_from(value.clamp(0, 255)).expect("clamped to a byte")
}
pub(super) fn ycc_to_rgb(y: &[u8], cb: &[u8], cr: &[u8]) -> Vec<[u8; 3]> {
let tables = YccTables::new();
y.iter()
.zip(cb)
.zip(cr)
.map(|((&y, &cb), &cr)| tables.rgb(y, cb, cr))
.collect()
}
pub(super) fn ycck_to_cmyk(planes: [&[u8]; 4]) -> Vec<[u8; 4]> {
let tables = YccTables::new();
(0..planes[0].len())
.map(|i| {
let [r, g, b] = tables.rgb(planes[0][i], planes[1][i], planes[2][i]);
[255 - r, 255 - g, 255 - b, planes[3][i]]
})
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn colour_spaces_are_guessed_like_libjpeg() {
let none = ColorHints::default();
let jfif = ColorHints {
jfif: true,
adobe: Some(0),
};
let adobe = |t| ColorHints {
jfif: false,
adobe: Some(t),
};
assert_eq!(ColorSpace::guess(&[1], none), ColorSpace::Gray);
assert_eq!(ColorSpace::guess(&[1, 2, 3], none), ColorSpace::YCbCr);
assert_eq!(ColorSpace::guess(b"RGB", none), ColorSpace::Rgb);
assert_eq!(ColorSpace::guess(b"RGB", jfif), ColorSpace::YCbCr);
assert_eq!(ColorSpace::guess(&[1, 2, 3], adobe(0)), ColorSpace::Rgb);
assert_eq!(ColorSpace::guess(&[1, 2, 3], adobe(7)), ColorSpace::YCbCr);
assert_eq!(ColorSpace::guess(&[1, 2, 3, 4], none), ColorSpace::Cmyk);
assert_eq!(ColorSpace::guess(&[1, 2, 3, 4], adobe(2)), ColorSpace::Ycck);
assert_eq!(ColorSpace::guess(&[1, 2, 3, 4], adobe(5)), ColorSpace::Ycck);
}
#[test]
fn ycc_converts_with_libjpeg_tables() {
assert_eq!(ycc_to_rgb(&[128], &[128], &[128]), [[128, 128, 128]]);
assert_eq!(ycc_to_rgb(&[76], &[85], &[255]), [[254, 0, 0]]);
assert_eq!(ycc_to_rgb(&[255], &[255], &[255]), [[255, 121, 255]]);
assert_eq!(
ycck_to_cmyk([&[128], &[128], &[128], &[9]]),
[[127, 127, 127, 9]]
);
}
}