use crate::av1::ChromaSampling;
const KR: f32 = 0.299;
const KB: f32 = 0.114;
const KG: f32 = 1.0 - KR - KB;
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct Planes {
pub(crate) y: Vec<u8>,
pub(crate) u: Vec<u8>,
pub(crate) v: Vec<u8>,
}
fn to_byte(value: f32) -> u8 {
value.round().clamp(0.0, 255.0) as u8
}
fn yuv([r, g, b]: [u8; 3]) -> [f32; 3] {
let [r, g, b] = [r, g, b].map(|c| f32::from(c) / 255.0);
let y = KR * r + KG * g + KB * b;
[
y,
(b - y) / (2.0 * (1.0 - KB)),
(r - y) / (2.0 * (1.0 - KR)),
]
}
pub(crate) fn bt601(width: usize, height: usize, rgba: &[u8], sampling: ChromaSampling) -> Planes {
let pixel = |x: usize, y: usize| {
let i = (y * width + x) * 4;
yuv([rgba[i], rgba[i + 1], rgba[i + 2]])
};
let y_plane = (0..height)
.flat_map(|y| (0..width).map(move |x| (x, y)))
.map(|(x, y)| to_byte(pixel(x, y)[0] * 255.0))
.collect();
let chroma = |channel: usize| -> Vec<u8> {
match sampling {
ChromaSampling::Monochrome => Vec::new(),
ChromaSampling::Yuv444 => (0..height)
.flat_map(|y| (0..width).map(move |x| (x, y)))
.map(|(x, y)| to_byte(pixel(x, y)[channel] * 255.0 + 128.0))
.collect(),
ChromaSampling::Yuv420 => {
let (cw, ch) = (width.div_ceil(2), height.div_ceil(2));
(0..ch)
.flat_map(|cy| (0..cw).map(move |cx| (cx, cy)))
.map(|(cx, cy)| {
let xs = 2 * cx..(2 * cx + 2).min(width);
let ys = 2 * cy..(2 * cy + 2).min(height);
let count = (xs.len() * ys.len()) as f32;
let sum: f32 = ys
.flat_map(|y| xs.clone().map(move |x| (x, y)))
.map(|(x, y)| pixel(x, y)[channel])
.sum();
to_byte(sum / count * 255.0 + 128.0)
})
.collect()
}
}
};
Planes {
y: y_plane,
u: chroma(1),
v: chroma(2),
}
}
pub(crate) fn identity(rgba: &[u8]) -> Planes {
let channel = |c: usize| rgba.as_chunks::<4>().0.iter().map(|p| p[c]).collect();
Planes {
y: channel(1),
u: channel(2),
v: channel(0),
}
}
pub(crate) fn alpha(rgba: &[u8]) -> Vec<u8> {
rgba.as_chunks::<4>().0.iter().map(|p| p[3]).collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn primaries_and_greys() {
let rgba = [
255, 255, 255, 255, 0, 0, 0, 255, 255, 0, 0, 255, 0, 0, 255, 255,
];
let planes = bt601(4, 1, &rgba, ChromaSampling::Yuv444);
assert_eq!(planes.y, [255, 0, 76, 29]);
assert_eq!(planes.u, [128, 128, 85, 255]);
assert_eq!(planes.v, [128, 128, 255, 107]);
}
#[test]
fn chroma_averages_over_squares() {
let rgba = [255, 0, 0, 255, 0, 0, 255, 255, 0, 0, 255, 255];
let planes = bt601(3, 1, &rgba, ChromaSampling::Yuv420);
assert_eq!(planes.y.len(), 3);
assert_eq!(planes.u, [170, 255]);
assert_eq!(planes.v, [181, 107]);
let grey = bt601(3, 1, &rgba, ChromaSampling::Monochrome);
assert!(grey.u.is_empty() && grey.v.is_empty());
}
#[test]
fn identity_reorders_channels() {
let planes = identity(&[1, 2, 3, 4, 5, 6, 7, 8]);
assert_eq!(
(planes.y, planes.u, planes.v),
(vec![2, 6], vec![3, 7], vec![1, 5])
);
assert_eq!(alpha(&[1, 2, 3, 4, 5, 6, 7, 8]), [4, 8]);
}
}