use crate::encode_image::AlphaPlane;
use crate::image::Image3Si;
static NEAREST_ONES_2: [usize; 1] = [9];
static NEAREST_ONES_4: [usize; 3] = [19, 24, 49];
static NEAREST_ONES_8: [usize; 10] = [39, 44, 49, 54, 119, 124, 129, 174, 179, 204];
pub(crate) const fn weight_count(factor: u32) -> usize {
match factor {
2 => 15,
4 => 55,
_ => 210,
}
}
pub(crate) fn nearest_weights(factor: u32) -> Vec<f32> {
let mut weights = vec![0.0f32; weight_count(factor)];
let ones: &[usize] = match factor {
2 => &NEAREST_ONES_2,
4 => &NEAREST_ONES_4,
_ => &NEAREST_ONES_8,
};
for &i in ones {
weights[i] = 1.0;
}
weights
}
pub(crate) fn constant_cell_factor(image: &Image3Si, alpha: Option<&AlphaPlane>) -> u32 {
let xsize = image.xsize();
let ysize = image.ysize();
if xsize < 2 && ysize < 2 {
return 1;
}
for factor in [8u32, 4, 2] {
let n = factor as usize;
let planes_constant = (0..3).all(|c| cells_constant(|y| image.plane_row(c, y), ysize, n));
let alpha_constant = match alpha {
None => true,
Some(AlphaPlane::U8(data)) => {
cells_constant(|y| &data[y * xsize..(y + 1) * xsize], ysize, n)
}
Some(AlphaPlane::U16 { data, .. }) => {
cells_constant(|y| &data[y * xsize..(y + 1) * xsize], ysize, n)
}
Some(AlphaPlane::F32(data)) => {
cells_constant(|y| &data[y * xsize..(y + 1) * xsize], ysize, n)
}
};
if planes_constant && alpha_constant {
return factor;
}
}
1
}
fn cells_constant<'a, T: PartialEq + 'a>(
row: impl Fn(usize) -> &'a [T],
ysize: usize,
n: usize,
) -> bool {
for y in 0..ysize {
let current = row(y);
if y % n != 0 {
if current != row(y - y % n) {
return false;
}
continue;
}
if current
.chunks(n)
.any(|cell| cell[1..].iter().any(|v| *v != cell[0]))
{
return false;
}
}
true
}
pub(crate) fn downsample(
image: &Image3Si,
alpha: Option<&AlphaPlane>,
factor: u32,
) -> (Image3Si, Option<AlphaPlane>) {
let n = factor as usize;
let xsize = image.xsize();
let ysize = image.ysize();
let bx = xsize.div_ceil(n);
let by = ysize.div_ceil(n);
let mut base = Image3Si::new(bx, by);
for c in 0..3 {
for y in 0..by {
let src = image.plane_row(c, y * n);
let dst = base.plane_row_mut(c, y);
for (x, d) in dst.iter_mut().enumerate() {
*d = src[x * n];
}
}
}
fn pick<T: Copy>(data: &[T], xsize: usize, bx: usize, by: usize, n: usize) -> Vec<T> {
let mut out = Vec::with_capacity(bx * by);
for y in 0..by {
let src = &data[y * n * xsize..];
out.extend((0..bx).map(|x| src[x * n]));
}
out
}
let base_alpha = alpha.map(|alpha| match alpha {
AlphaPlane::U8(data) => AlphaPlane::U8(pick(data, xsize, bx, by, n)),
AlphaPlane::U16 { data, bits } => AlphaPlane::U16 {
data: pick(data, xsize, bx, by, n),
bits: *bits,
},
AlphaPlane::F32(data) => AlphaPlane::F32(pick(data, xsize, bx, by, n)),
});
(base, base_alpha)
}
#[cfg(test)]
mod tests {
use super::*;
fn cells(w: usize, h: usize, n: usize) -> Image3Si {
let mut image = Image3Si::new(w, h);
for c in 0..3 {
for y in 0..h {
for x in 0..w {
image.plane_row_mut(c, y)[x] = ((y / n) * 131 + (x / n) * 17 + c) as i32;
}
}
}
image
}
#[test]
fn detects_the_largest_constant_cell() {
assert_eq!(constant_cell_factor(&cells(64, 48, 8), None), 8);
assert_eq!(constant_cell_factor(&cells(64, 48, 4), None), 4);
assert_eq!(constant_cell_factor(&cells(64, 48, 2), None), 2);
assert_eq!(constant_cell_factor(&cells(64, 48, 1), None), 1);
assert_eq!(constant_cell_factor(&cells(61, 47, 4), None), 4);
let alpha = AlphaPlane::U8((0..64 * 48).map(|i| (i % 3) as u8).collect());
assert_eq!(constant_cell_factor(&cells(64, 48, 4), Some(&alpha)), 1);
let mut broken = cells(64, 48, 4);
broken.plane_row_mut(1, 45)[62] += 1;
assert_eq!(constant_cell_factor(&broken, None), 1);
}
#[test]
fn downsample_keeps_one_sample_per_cell() {
let image = cells(61, 47, 4);
let (base, _) = downsample(&image, None, 4);
assert_eq!((base.xsize(), base.ysize()), (16, 12));
for c in 0..3 {
for y in 0..12 {
for x in 0..16 {
assert_eq!(base.plane_row(c, y)[x], image.plane_row(c, y * 4)[x * 4]);
}
}
}
}
#[test]
fn lossless_codes_constant_cells_at_the_base_size() {
let (bw, bh) = (37usize, 29usize);
let base: Vec<u8> = (0..bw * bh * 3).map(|i| ((i * 7919) % 251) as u8).collect();
let config = crate::EncodeConfig::default()
.with_lossless(true)
.with_upsampling(true)
.with_speed(crate::Speed::Fast);
let base_bytes = crate::encode_image(&base, bw, bh, &config).unwrap();
for n in [2usize, 4, 8] {
let (w, h) = (bw * n - n / 2, bh * n - 1);
let mut up = vec![0u8; w * h * 3];
for y in 0..h {
for x in 0..w {
for c in 0..3 {
up[(y * w + x) * 3 + c] = base[((y / n) * bw + x / n) * 3 + c];
}
}
}
let bytes = crate::encode_image(&up, w, h, &config).unwrap();
let kernel_bytes = 2 * weight_count(n as u32);
assert!(
bytes.len() <= base_bytes.len() + kernel_bytes + 8,
"{n}x: {} bytes, base {} + kernel {kernel_bytes}",
bytes.len(),
base_bytes.len()
);
}
}
#[test]
fn nearest_weights_match_libjxl() {
let w2 = nearest_weights(2);
assert_eq!(w2.iter().filter(|w| **w == 1.0).count(), 1);
assert_eq!(w2[9], 1.0);
assert_eq!(nearest_weights(4).len(), 55);
assert_eq!(nearest_weights(8).len(), 210);
}
}