use super::frame::{Block, Component, Frame};
const SAVED_COEFS: usize = 10;
const POSITIONS: [usize; SAVED_COEFS] = [0, 1, 8, 16, 9, 2, 3, 10, 17, 24];
struct Estimate {
index: usize,
weights: [[i64; 5]; 5],
}
const ESTIMATES: [Estimate; 5] = [
Estimate {
index: 1,
weights: [[0; 5], [0; 5], [-7, 50, 0, -50, 7], [0; 5], [0; 5]],
},
Estimate {
index: 2,
weights: [
[0, 0, -7, 0, 0],
[0, 0, 50, 0, 0],
[0; 5],
[0, 0, -50, 0, 0],
[0, 0, 7, 0, 0],
],
},
Estimate {
index: 3,
weights: [
[0, 0, -1, 0, 0],
[0, 0, 13, 0, 0],
[0, 0, -24, 0, 0],
[0, 0, 13, 0, 0],
[0, 0, -1, 0, 0],
],
},
Estimate {
index: 4,
weights: [
[0, -1, 0, 1, 0],
[-1, 10, 0, -10, 1],
[0; 5],
[1, -10, 0, 10, -1],
[0, 1, 0, -1, 0],
],
},
Estimate {
index: 5,
weights: [[0; 5], [0; 5], [-1, 13, -24, 13, -1], [0; 5], [0; 5]],
},
];
const INTERPOLATIONS: [Estimate; 9] = [
Estimate {
index: 1,
weights: [
[-1, -1, 0, 1, 1],
[-3, 13, 0, -13, 3],
[-3, 38, 0, -38, 3],
[-3, 13, 0, -13, 3],
[-1, -1, 0, 1, 1],
],
},
Estimate {
index: 2,
weights: [
[-1, -3, -3, -3, -1],
[-1, 13, 38, 13, -1],
[0; 5],
[1, -13, -38, -13, 1],
[1, 3, 3, 3, 1],
],
},
Estimate {
index: 3,
weights: [
[0, 0, 1, 0, 0],
[0, 2, 7, 2, 0],
[0, -5, -14, -5, 0],
[0, 2, 7, 2, 0],
[0, 0, 1, 0, 0],
],
},
Estimate {
index: 4,
weights: [
[-1, 0, 0, 0, 1],
[0, 9, 0, -9, 0],
[0; 5],
[0, -9, 0, 9, 0],
[1, 0, 0, 0, -1],
],
},
Estimate {
index: 5,
weights: [
[0; 5],
[0, 2, -5, 2, 0],
[1, 7, -14, 7, 1],
[0, 2, -5, 2, 0],
[0; 5],
],
},
Estimate {
index: 6,
weights: [
[0; 5],
[0, 1, 0, -1, 0],
[0, 2, 0, -2, 0],
[0, 1, 0, -1, 0],
[0; 5],
],
},
Estimate {
index: 7,
weights: [[0; 5], [0, 1, -3, 1, 0], [0; 5], [0, -1, 3, -1, 0], [0; 5]],
},
Estimate {
index: 8,
weights: [
[0; 5],
[0, 1, 0, -1, 0],
[0, -3, 0, 3, 0],
[0, 1, 0, -1, 0],
[0; 5],
],
},
Estimate {
index: 9,
weights: [[0; 5], [0, 1, 2, 1, 0], [0; 5], [0, -1, -2, -1, 0], [0; 5]],
},
];
const DC_WEIGHTS: [[i64; 5]; 5] = [
[-2, -6, -8, -6, -2],
[-6, 6, 42, 6, -6],
[-8, 42, 152, 42, -8],
[-6, 6, 42, 6, -6],
[-2, -6, -8, -6, -2],
];
pub(super) fn applies(frame: &Frame) -> bool {
frame.progressive
&& frame.components.iter().all(|c| {
c.quant
.is_some_and(|quant| POSITIONS.iter().all(|&p| quant[p] != 0))
&& c.coef_bits[0] >= 0
})
&& frame
.components
.iter()
.any(|c| c.coef_bits[1..SAVED_COEFS].iter().any(|&bits| bits != 0))
}
pub(super) fn smoothed_block(
component: &Component,
imcu_rows: usize,
row: usize,
column: usize,
) -> Block {
let quant = component.quant.unwrap_or([1; 64]);
let bits = &component.coef_bits;
let dc = neighbourhood(component, imcu_rows, row, column);
let weigh = |weights: &[[i64; 5]; 5]| -> i64 {
let sum: i64 = (0..25)
.map(|i| weights[i / 5][i % 5] * dc[i / 5][i % 5])
.sum();
i64::from(quant[0]) * sum
};
let mut block = *component.block(row, column);
let interpolate = bits[1..SAVED_COEFS].iter().all(|&b| b == -1);
let estimates: &[Estimate] = if interpolate {
&INTERPOLATIONS
} else {
&ESTIMATES
};
for estimate in estimates {
let position = POSITIONS[estimate.index];
let al = bits[estimate.index];
if al != 0 && block[position] == 0 {
let step = i64::from(quant[position]);
block[position] = predict(weigh(&estimate.weights), step, Some(al));
}
}
if interpolate {
block[0] = predict(weigh(&DC_WEIGHTS), i64::from(quant[0]), None);
}
block
}
fn predict(num: i64, step: i64, al: Option<i8>) -> i16 {
let limit = |prediction: i64| match al {
Some(al) if al > 0 && prediction >= 1 << al => (1 << al) - 1,
_ => prediction,
};
let magnitude = limit(((step << 7) + num.abs()) / (step << 8));
(if num >= 0 { magnitude } else { -magnitude }) as i16
}
fn neighbourhood(
component: &Component,
imcu_rows: usize,
row: usize,
column: usize,
) -> [[i64; 5]; 5] {
let v = component.spec.v;
let (imcu, offset) = (row / v, row % v);
let block_rows = if imcu + 1 < imcu_rows {
v
} else {
match component.height_in_blocks % v {
0 => v,
rows => rows,
}
};
let image_row = imcu * block_rows + offset;
let image_rows = block_rows * imcu_rows;
let previous = if image_row > 0 { row - 1 } else { row };
let before_previous = if image_row > 1 { row - 2 } else { previous };
let next = if image_row + 1 < image_rows {
row + 1
} else {
row
};
let after_next = if image_row + 2 < image_rows {
row + 2
} else {
next
};
let rows = [before_previous, previous, row, next, after_next];
let last_column = component.width_in_blocks - 1;
std::array::from_fn(|r| {
std::array::from_fn(|c| {
let column = (column + c).saturating_sub(2).min(last_column);
i64::from(component.block(rows[r], column)[0])
})
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn predictions_round_and_limit_like_libjpeg() {
assert_eq!(predict(1000, 2, None), 2);
assert_eq!(predict(-1000, 2, None), -2);
assert_eq!(predict(100_000, 1, Some(2)), 3, "limited below 2^2");
assert_eq!(
predict(100_000, 1, Some(-1)),
391,
"unknown bits are not limited"
);
}
#[test]
fn weights_are_antisymmetric_where_libjpeg_s_are() {
let sum: i64 = DC_WEIGHTS.iter().flatten().sum();
assert_eq!(sum, 256);
for estimate in ESTIMATES.iter().chain(&INTERPOLATIONS) {
let total: i64 = estimate.weights.iter().flatten().sum();
assert_eq!(total, 0, "estimate {}", estimate.index);
}
}
}