use thiserror::Error;
#[derive(Debug, Error, PartialEq)]
pub enum ChromAberrationError {
#[error("Image dimensions mismatch: buffer size {got} != {width}×{height}×{channels}")]
SizeMismatch {
got: usize,
width: usize,
height: usize,
channels: usize,
},
#[error("Invalid strength value {0}: must be in [0, 1]")]
InvalidStrength(f32),
#[error("Invalid parameter: {0}")]
InvalidParam(String),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ChromInterpolation {
Nearest,
Bilinear,
}
#[derive(Debug, Clone, PartialEq)]
pub struct ChromAberrationConfig {
pub strength: f32,
pub red_shift: [f32; 2],
pub blue_shift: [f32; 2],
pub barrel_distortion: bool,
pub barrel_k1: f32,
pub barrel_k2: f32,
pub interpolation: ChromInterpolation,
}
impl Default for ChromAberrationConfig {
fn default() -> Self {
Self {
strength: 0.02,
red_shift: [2.0, 0.0],
blue_shift: [-2.0, 0.0],
barrel_distortion: false,
barrel_k1: -0.1,
barrel_k2: 0.0,
interpolation: ChromInterpolation::Bilinear,
}
}
}
#[inline]
fn check_rgb_buffer(img: &[f32], width: usize, height: usize) -> Result<(), ChromAberrationError> {
let expected = width * height * 3;
if img.len() != expected {
return Err(ChromAberrationError::SizeMismatch {
got: img.len(),
width,
height,
channels: 3,
});
}
Ok(())
}
pub fn bilinear_sample_channel(
img: &[f32],
width: usize,
height: usize,
channels: usize,
channel: usize,
x: f32,
y: f32,
) -> f32 {
if width == 0 || height == 0 || channels == 0 || channel >= channels {
return 0.0;
}
let xc = x.clamp(0.0, width as f32 - 1.0);
let yc = y.clamp(0.0, height as f32 - 1.0);
let x0 = xc.floor() as usize;
let y0 = yc.floor() as usize;
let x1 = (x0 + 1).min(width - 1);
let y1 = (y0 + 1).min(height - 1);
let fx = xc - x0 as f32;
let fy = yc - y0 as f32;
let get = |row: usize, col: usize| -> f32 {
img.get(row * width * channels + col * channels + channel)
.copied()
.unwrap_or(0.0)
};
let c00 = get(y0, x0);
let c10 = get(y0, x1);
let c01 = get(y1, x0);
let c11 = get(y1, x1);
let top = c00 * (1.0 - fx) + c10 * fx;
let bot = c01 * (1.0 - fx) + c11 * fx;
top * (1.0 - fy) + bot * fy
}
pub fn nearest_sample_channel(
img: &[f32],
width: usize,
height: usize,
channels: usize,
channel: usize,
x: f32,
y: f32,
) -> f32 {
if width == 0 || height == 0 || channels == 0 || channel >= channels {
return 0.0;
}
let xi = (x.round() as isize).clamp(0, width as isize - 1) as usize;
let yi = (y.round() as isize).clamp(0, height as isize - 1) as usize;
img.get(yi * width * channels + xi * channels + channel)
.copied()
.unwrap_or(0.0)
}
pub fn shift_channel(
img: &[f32],
width: usize,
height: usize,
channel: usize,
dx: f32,
dy: f32,
interp: ChromInterpolation,
) -> Result<Vec<f32>, ChromAberrationError> {
check_rgb_buffer(img, width, height)?;
if channel >= 3 {
return Err(ChromAberrationError::InvalidParam(format!(
"channel index {channel} out of range (0..2)"
)));
}
let mut out = img.to_vec();
for py in 0..height {
for px in 0..width {
let src_x = px as f32 - dx;
let src_y = py as f32 - dy;
let val = match interp {
ChromInterpolation::Bilinear => {
bilinear_sample_channel(img, width, height, 3, channel, src_x, src_y)
}
ChromInterpolation::Nearest => {
nearest_sample_channel(img, width, height, 3, channel, src_x, src_y)
}
};
if let Some(slot) = out.get_mut((py * width + px) * 3 + channel) {
*slot = val;
}
}
}
Ok(out)
}
pub fn apply_barrel_distortion(
img: &[f32],
width: usize,
height: usize,
k1: f32,
k2: f32,
) -> Result<Vec<f32>, ChromAberrationError> {
check_rgb_buffer(img, width, height)?;
if width == 0 || height == 0 {
return Ok(img.to_vec());
}
let cx = (width as f32 - 1.0) * 0.5;
let cy = (height as f32 - 1.0) * 0.5;
let norm_r = (cx * cx + cy * cy).sqrt().max(1.0);
let mut out = vec![0.0f32; width * height * 3];
for py in 0..height {
for px in 0..width {
let nx = (px as f32 - cx) / norm_r;
let ny = (py as f32 - cy) / norm_r;
let r2 = nx * nx + ny * ny;
let factor = 1.0 + k1 * r2 + k2 * r2 * r2;
let safe_factor = if factor.abs() < 1e-6 {
1e-6f32.copysign(factor)
} else {
factor
};
let src_nx = nx / safe_factor;
let src_ny = ny / safe_factor;
let src_x = src_nx * norm_r + cx;
let src_y = src_ny * norm_r + cy;
let pixel_out = (py * width + px) * 3;
for ch in 0..3 {
let val = bilinear_sample_channel(img, width, height, 3, ch, src_x, src_y);
if let Some(slot) = out.get_mut(pixel_out + ch) {
*slot = val;
}
}
}
}
Ok(out)
}
pub fn apply_chrom_aberration(
img: &[f32],
width: usize,
height: usize,
config: &ChromAberrationConfig,
) -> Result<Vec<f32>, ChromAberrationError> {
check_rgb_buffer(img, width, height)?;
if !(0.0..=1.0).contains(&config.strength) {
return Err(ChromAberrationError::InvalidStrength(config.strength));
}
let base: Vec<f32>;
let src = if config.barrel_distortion {
base = apply_barrel_distortion(img, width, height, config.barrel_k1, config.barrel_k2)?;
&base
} else {
img
};
let rdx = config.red_shift[0] * config.strength;
let rdy = config.red_shift[1] * config.strength;
let bdx = config.blue_shift[0] * config.strength;
let bdy = config.blue_shift[1] * config.strength;
let mut out = src.to_vec();
for py in 0..height {
for px in 0..width {
let src_x = px as f32 - rdx;
let src_y = py as f32 - rdy;
let val = match config.interpolation {
ChromInterpolation::Bilinear => {
bilinear_sample_channel(src, width, height, 3, 0, src_x, src_y)
}
ChromInterpolation::Nearest => {
nearest_sample_channel(src, width, height, 3, 0, src_x, src_y)
}
};
if let Some(slot) = out.get_mut((py * width + px) * 3) {
*slot = val;
}
}
}
for py in 0..height {
for px in 0..width {
let src_x = px as f32 - bdx;
let src_y = py as f32 - bdy;
let val = match config.interpolation {
ChromInterpolation::Bilinear => {
bilinear_sample_channel(src, width, height, 3, 2, src_x, src_y)
}
ChromInterpolation::Nearest => {
nearest_sample_channel(src, width, height, 3, 2, src_x, src_y)
}
};
if let Some(slot) = out.get_mut((py * width + px) * 3 + 2) {
*slot = val;
}
}
}
Ok(out)
}
pub fn apply_radial_chromatic_aberration(
img: &[f32],
width: usize,
height: usize,
strength: f32,
center: [f32; 2],
) -> Result<Vec<f32>, ChromAberrationError> {
check_rgb_buffer(img, width, height)?;
if !(0.0..=1.0).contains(&strength) {
return Err(ChromAberrationError::InvalidStrength(strength));
}
if width == 0 || height == 0 {
return Ok(img.to_vec());
}
let cx = center[0] * (width as f32 - 1.0);
let cy = center[1] * (height as f32 - 1.0);
let hnorm = (cx * cx + cy * cy).sqrt().max(1.0);
let mut out = img.to_vec();
for py in 0..height {
for px in 0..width {
let nx = (px as f32 - cx) / hnorm;
let ny = (py as f32 - cy) / hnorm;
let r = (nx * nx + ny * ny).sqrt();
let r_scale = 1.0 + strength * r;
let r_src_x = cx + nx * r_scale * hnorm;
let r_src_y = cy + ny * r_scale * hnorm;
let r_val = bilinear_sample_channel(img, width, height, 3, 0, r_src_x, r_src_y);
let b_scale = (1.0 - strength * r).max(0.0);
let b_src_x = cx + nx * b_scale * hnorm;
let b_src_y = cy + ny * b_scale * hnorm;
let b_val = bilinear_sample_channel(img, width, height, 3, 2, b_src_x, b_src_y);
let pixel_out = (py * width + px) * 3;
if let Some(slot) = out.get_mut(pixel_out) {
*slot = r_val;
}
if let Some(slot) = out.get_mut(pixel_out + 2) {
*slot = b_val;
}
}
}
Ok(out)
}
pub fn compute_radial_distances(width: usize, height: usize, center: [f32; 2]) -> Vec<f32> {
if width == 0 || height == 0 {
return Vec::new();
}
let cx = center[0] * (width as f32 - 1.0);
let cy = center[1] * (height as f32 - 1.0);
let corners = [
(0.0f32, 0.0f32),
(width as f32 - 1.0, 0.0),
(0.0, height as f32 - 1.0),
(width as f32 - 1.0, height as f32 - 1.0),
];
let max_dist = corners
.iter()
.map(|(cx2, cy2)| ((cx2 - cx).powi(2) + (cy2 - cy).powi(2)).sqrt())
.fold(0.0f32, f32::max)
.max(1.0);
let mut out = vec![0.0f32; width * height];
for py in 0..height {
for px in 0..width {
let dx = px as f32 - cx;
let dy = py as f32 - cy;
let dist = (dx * dx + dy * dy).sqrt() / max_dist;
if let Some(slot) = out.get_mut(py * width + px) {
*slot = dist;
}
}
}
out
}
pub fn apply_vignetting_f32(
img: &[f32],
width: usize,
height: usize,
strength: f32,
falloff: f32,
) -> Result<Vec<f32>, ChromAberrationError> {
check_rgb_buffer(img, width, height)?;
if !(0.0..=1.0).contains(&strength) {
return Err(ChromAberrationError::InvalidStrength(strength));
}
if falloff <= 0.0 {
return Err(ChromAberrationError::InvalidParam(format!(
"falloff must be > 0, got {falloff}"
)));
}
let mask = compute_vignette_mask(width, height, strength, falloff);
let mut out = img.to_vec();
for py in 0..height {
for px in 0..width {
let m = mask.get(py * width + px).copied().unwrap_or(1.0);
let pixel = (py * width + px) * 3;
for ch in 0..3 {
if let Some(slot) = out.get_mut(pixel + ch) {
*slot *= m;
}
}
}
}
Ok(out)
}
pub fn compute_vignette_mask(width: usize, height: usize, strength: f32, falloff: f32) -> Vec<f32> {
if width == 0 || height == 0 {
return Vec::new();
}
let dists = compute_radial_distances(width, height, [0.5, 0.5]);
dists
.iter()
.map(|&d| {
let factor = 1.0 - strength * d.powf(falloff);
factor.clamp(0.0, 1.0)
})
.collect()
}
pub fn estimate_aberration_strength(
img: &[f32],
width: usize,
height: usize,
) -> Result<f32, ChromAberrationError> {
check_rgb_buffer(img, width, height)?;
let n_px = width * height;
if n_px == 0 {
return Ok(0.0);
}
let mut sum_rg = 0.0f64;
let mut sum_bg = 0.0f64;
let mut sum_var = 0.0f64;
let mut mean = [0.0f64; 3];
for i in 0..n_px {
let r = img.get(i * 3).copied().unwrap_or(0.0) as f64;
let g = img.get(i * 3 + 1).copied().unwrap_or(0.0) as f64;
let b = img.get(i * 3 + 2).copied().unwrap_or(0.0) as f64;
mean[0] += r;
mean[1] += g;
mean[2] += b;
sum_rg += (r - g).abs();
sum_bg += (b - g).abs();
}
for m in mean.iter_mut() {
*m /= n_px as f64;
}
for i in 0..n_px {
let r = img.get(i * 3).copied().unwrap_or(0.0) as f64;
let g = img.get(i * 3 + 1).copied().unwrap_or(0.0) as f64;
let b = img.get(i * 3 + 2).copied().unwrap_or(0.0) as f64;
sum_var += (r - mean[0]).powi(2) + (g - mean[1]).powi(2) + (b - mean[2]).powi(2);
}
let variance = sum_var / (n_px as f64 * 3.0);
let mean_diff = (sum_rg + sum_bg) / (2.0 * n_px as f64);
let normalised = if variance > 1e-10 {
mean_diff / (variance.sqrt() + 1e-10)
} else {
0.0
};
Ok(normalised as f32)
}
pub fn format_chrom_config(config: &ChromAberrationConfig) -> String {
format!(
"ChromAberrationConfig {{ strength: {:.4}, red_shift: [{:.2}, {:.2}], \
blue_shift: [{:.2}, {:.2}], barrel_distortion: {}, barrel_k1: {:.4}, \
barrel_k2: {:.4}, interpolation: {} }}",
config.strength,
config.red_shift[0],
config.red_shift[1],
config.blue_shift[0],
config.blue_shift[1],
config.barrel_distortion,
config.barrel_k1,
config.barrel_k2,
match config.interpolation {
ChromInterpolation::Nearest => "Nearest",
ChromInterpolation::Bilinear => "Bilinear",
},
)
}
#[cfg(test)]
mod tests {
use super::*;
fn approx_eq(a: f32, b: f32, tol: f32) -> bool {
(a - b).abs() <= tol
}
fn make_rgb_gradient(w: usize, h: usize) -> Vec<f32> {
let mut out = vec![0.0f32; w * h * 3];
for py in 0..h {
for px in 0..w {
let r = px as f32 / w.max(1) as f32;
let g = py as f32 / h.max(1) as f32;
let b = 1.0 - r;
let idx = (py * w + px) * 3;
out[idx] = r;
out[idx + 1] = g;
out[idx + 2] = b;
}
}
out
}
#[test]
fn test_radial_distances_center_is_zero() {
let w = 11usize;
let h = 11usize;
let dists = compute_radial_distances(w, h, [0.5, 0.5]);
assert_eq!(dists.len(), w * h);
let cx = w / 2;
let cy = h / 2;
let centre = dists[cy * w + cx];
assert!(
approx_eq(centre, 0.0, 1e-5),
"Centre distance should be 0.0, got {centre}"
);
}
#[test]
fn test_radial_distances_corners_near_one() {
let w = 11usize;
let h = 11usize;
let dists = compute_radial_distances(w, h, [0.5, 0.5]);
let corner = dists[0]; assert!(
corner > 0.9 && corner <= 1.0,
"Corner distance should be near 1.0, got {corner}"
);
}
#[test]
fn test_radial_distances_in_range() {
let dists = compute_radial_distances(20, 15, [0.5, 0.5]);
for &d in &dists {
assert!((0.0..=1.0).contains(&d), "Distance out of range: {d}");
}
}
#[test]
fn test_radial_distances_zero_dims() {
assert!(compute_radial_distances(0, 10, [0.5, 0.5]).is_empty());
assert!(compute_radial_distances(10, 0, [0.5, 0.5]).is_empty());
}
#[test]
fn test_bilinear_sample_integer_exact() {
let img = vec![
1.0f32, 0.5, 0.0, 0.2, 0.4, 0.6, 0.3, 0.7, 0.1, 0.9, 0.0, 0.5,
];
let val = bilinear_sample_channel(&img, 2, 2, 3, 0, 0.0, 0.0);
assert!(approx_eq(val, 1.0, 1e-6), "Expected 1.0, got {val}");
let val2 = bilinear_sample_channel(&img, 2, 2, 3, 1, 1.0, 0.0);
assert!(approx_eq(val2, 0.4, 1e-6), "Expected 0.4, got {val2}");
}
#[test]
fn test_bilinear_sample_midpoint_average() {
let img = vec![0.0f32, 0.0, 0.0, 1.0, 0.0, 0.0];
let val = bilinear_sample_channel(&img, 2, 1, 3, 0, 0.5, 0.0);
assert!(
approx_eq(val, 0.5, 1e-5),
"Midpoint should be 0.5, got {val}"
);
}
#[test]
fn test_bilinear_sample_clamping() {
let img = vec![0.5f32; 4 * 4 * 3];
let val = bilinear_sample_channel(&img, 4, 4, 3, 0, -1.0, -1.0);
assert!(
approx_eq(val, 0.5, 1e-5),
"Clamped sample should be 0.5, got {val}"
);
let val2 = bilinear_sample_channel(&img, 4, 4, 3, 0, 100.0, 100.0);
assert!(
approx_eq(val2, 0.5, 1e-5),
"Clamped sample out-of-bounds should be 0.5, got {val2}"
);
}
#[test]
fn test_nearest_sample_channel_basic() {
let img = vec![0.2f32, 0.0, 0.0, 0.8, 0.0, 0.0];
let v0 = nearest_sample_channel(&img, 2, 1, 3, 0, 0.1, 0.0);
assert!(approx_eq(v0, 0.2, 1e-6), "Should round to px 0: got {v0}");
let v1 = nearest_sample_channel(&img, 2, 1, 3, 0, 0.9, 0.0);
assert!(approx_eq(v1, 0.8, 1e-6), "Should round to px 1: got {v1}");
}
#[test]
fn test_nearest_sample_channel_clamp() {
let img = vec![0.3f32; 3 * 3 * 3];
let val = nearest_sample_channel(&img, 3, 3, 3, 0, -5.0, -5.0);
assert!(approx_eq(val, 0.3, 1e-6), "Clamped nearest sample: {val}");
}
#[test]
fn test_shift_channel_zero_shift_identical() {
let img = make_rgb_gradient(8, 8);
let out = shift_channel(&img, 8, 8, 0, 0.0, 0.0, ChromInterpolation::Bilinear)
.expect("shift_channel failed");
for (a, b) in img.iter().zip(out.iter()) {
assert!(
approx_eq(*a, *b, 1e-5),
"Zero shift should be identity: {a} vs {b}"
);
}
}
#[test]
fn test_shift_channel_one_pixel() {
let w = 4usize;
let h = 4usize;
let img = make_rgb_gradient(w, h);
let out = shift_channel(&img, w, h, 0, 1.0, 0.0, ChromInterpolation::Bilinear)
.expect("shift_channel failed");
let original_px0_r = img[0];
let shifted_px1_r = out[3];
assert!(
approx_eq(original_px0_r, shifted_px1_r, 1e-4),
"Shifted R at px=1 should match original at px=0: {original_px0_r} vs {shifted_px1_r}"
);
}
#[test]
fn test_shift_channel_size_mismatch() {
let err = shift_channel(&[0.5f32; 5], 4, 4, 0, 0.0, 0.0, ChromInterpolation::Nearest);
assert!(matches!(
err,
Err(ChromAberrationError::SizeMismatch { .. })
));
}
#[test]
fn test_shift_channel_invalid_channel() {
let img = vec![0.5f32; 4 * 4 * 3];
let err = shift_channel(&img, 4, 4, 5, 0.0, 0.0, ChromInterpolation::Nearest);
assert!(matches!(err, Err(ChromAberrationError::InvalidParam(_))));
}
#[test]
fn test_barrel_distortion_identity() {
let img = make_rgb_gradient(10, 10);
let out =
apply_barrel_distortion(&img, 10, 10, 0.0, 0.0).expect("barrel_distortion failed");
for (a, b) in img.iter().zip(out.iter()) {
assert!(
approx_eq(*a, *b, 1e-4),
"k1=k2=0 should be identity: {a} vs {b}"
);
}
}
#[test]
fn test_barrel_distortion_nonzero_changes_image() {
let img = make_rgb_gradient(12, 12);
let out =
apply_barrel_distortion(&img, 12, 12, -0.2, 0.0).expect("barrel_distortion failed");
let n_diff = img
.iter()
.zip(out.iter())
.filter(|&(a, b)| (a - b).abs() > 1e-4)
.count();
assert!(n_diff > 0, "Non-zero k1 should change some pixels");
}
#[test]
fn test_barrel_distortion_size_mismatch() {
let err = apply_barrel_distortion(&[0.5f32; 7], 4, 4, 0.0, 0.0);
assert!(matches!(
err,
Err(ChromAberrationError::SizeMismatch { .. })
));
}
#[test]
fn test_barrel_distortion_output_size() {
let img = make_rgb_gradient(8, 6);
let out = apply_barrel_distortion(&img, 8, 6, -0.1, 0.0).expect("barrel_distortion failed");
assert_eq!(out.len(), img.len());
}
#[test]
fn test_barrel_distortion_center_unchanged() {
let w = 9usize;
let h = 9usize;
let img = make_rgb_gradient(w, h);
let out = apply_barrel_distortion(&img, w, h, -0.2, 0.0).expect("barrel distortion failed");
let cx = w / 2;
let cy = h / 2;
let orig = &img[(cy * w + cx) * 3..(cy * w + cx) * 3 + 3];
let result = &out[(cy * w + cx) * 3..(cy * w + cx) * 3 + 3];
for (a, b) in orig.iter().zip(result.iter()) {
assert!(
approx_eq(*a, *b, 1e-3),
"Centre pixel should be near-unchanged: {a} vs {b}"
);
}
}
#[test]
fn test_vignetting_zero_strength_unchanged() {
let img = make_rgb_gradient(8, 8);
let out = apply_vignetting_f32(&img, 8, 8, 0.0, 2.0).expect("vignetting failed");
for (a, b) in img.iter().zip(out.iter()) {
assert!(
approx_eq(*a, *b, 1e-5),
"Strength=0 should be identity: {a} vs {b}"
);
}
}
#[test]
fn test_vignetting_full_strength_corners_dark() {
let w = 11usize;
let h = 11usize;
let img = vec![1.0f32; w * h * 3];
let out = apply_vignetting_f32(&img, w, h, 1.0, 2.0).expect("vignetting failed");
let corner_r = out[0];
let cx = w / 2;
let cy = h / 2;
let centre_r = out[(cy * w + cx) * 3];
assert!(
centre_r > corner_r,
"Centre ({centre_r}) should be brighter than corner ({corner_r})"
);
assert!(
approx_eq(centre_r, 1.0, 1e-5),
"Centre should be 1.0 at strength=1: {centre_r}"
);
}
#[test]
fn test_vignette_mask_center_is_one() {
let w = 9usize;
let h = 9usize;
let mask = compute_vignette_mask(w, h, 0.8, 2.0);
let cx = w / 2;
let cy = h / 2;
let val = mask[cy * w + cx];
assert!(approx_eq(val, 1.0, 1e-5), "Centre should be 1.0, got {val}");
}
#[test]
fn test_vignette_mask_corners_less_than_one() {
let mask = compute_vignette_mask(10, 10, 0.8, 2.0);
let corner = mask[0];
assert!(corner < 1.0, "Corner should be < 1.0, got {corner}");
}
#[test]
fn test_vignette_mask_values_in_range() {
let mask = compute_vignette_mask(16, 12, 0.7, 2.5);
for &v in &mask {
assert!((0.0..=1.0).contains(&v), "Mask value out of range: {v}");
}
}
#[test]
fn test_vignetting_invalid_falloff() {
let img = vec![0.5f32; 4 * 4 * 3];
let err = apply_vignetting_f32(&img, 4, 4, 0.5, 0.0);
assert!(matches!(err, Err(ChromAberrationError::InvalidParam(_))));
}
#[test]
fn test_vignetting_size_mismatch() {
let err = apply_vignetting_f32(&[0.5f32; 5], 4, 4, 0.5, 2.0);
assert!(matches!(
err,
Err(ChromAberrationError::SizeMismatch { .. })
));
}
#[test]
fn test_chrom_aberration_zero_strength() {
let img = make_rgb_gradient(10, 10);
let config = ChromAberrationConfig {
strength: 0.0,
..ChromAberrationConfig::default()
};
let out =
apply_chrom_aberration(&img, 10, 10, &config).expect("apply_chrom_aberration failed");
for (a, b) in img.iter().zip(out.iter()) {
assert!(
approx_eq(*a, *b, 1e-5),
"Zero strength should preserve image: {a} vs {b}"
);
}
}
#[test]
fn test_chrom_aberration_nonzero_differs() {
let img = make_rgb_gradient(16, 16);
let config = ChromAberrationConfig {
strength: 1.0,
..ChromAberrationConfig::default()
};
let out =
apply_chrom_aberration(&img, 16, 16, &config).expect("apply_chrom_aberration failed");
let r_changed = (0..16 * 16).any(|i| (out[i * 3] - img[i * 3]).abs() > 1e-5);
let b_changed = (0..16 * 16).any(|i| (out[i * 3 + 2] - img[i * 3 + 2]).abs() > 1e-5);
assert!(r_changed, "Red channel should be shifted");
assert!(b_changed, "Blue channel should be shifted");
}
#[test]
fn test_chrom_aberration_invalid_strength() {
let img = vec![0.5f32; 4 * 4 * 3];
let config = ChromAberrationConfig {
strength: 1.5,
..ChromAberrationConfig::default()
};
let err = apply_chrom_aberration(&img, 4, 4, &config);
assert!(matches!(err, Err(ChromAberrationError::InvalidStrength(_))));
}
#[test]
fn test_chrom_aberration_size_mismatch() {
let config = ChromAberrationConfig::default();
let err = apply_chrom_aberration(&[0.5f32; 5], 4, 4, &config);
assert!(matches!(
err,
Err(ChromAberrationError::SizeMismatch { .. })
));
}
#[test]
fn test_chrom_aberration_one_by_one() {
let img = vec![0.7f32, 0.3, 0.5];
let config = ChromAberrationConfig {
strength: 1.0,
..ChromAberrationConfig::default()
};
let out = apply_chrom_aberration(&img, 1, 1, &config).expect("1×1 chrom aberration failed");
assert_eq!(out.len(), 3);
for (a, b) in img.iter().zip(out.iter()) {
assert!(approx_eq(*a, *b, 1e-5), "1×1 should clamp: {a} vs {b}");
}
}
#[test]
fn test_chrom_aberration_with_barrel() {
let img = make_rgb_gradient(12, 12);
let config = ChromAberrationConfig {
strength: 0.5,
barrel_distortion: true,
barrel_k1: -0.1,
..ChromAberrationConfig::default()
};
let out = apply_chrom_aberration(&img, 12, 12, &config)
.expect("apply_chrom_aberration with barrel failed");
assert_eq!(out.len(), img.len());
}
#[test]
fn test_chrom_aberration_nearest_interp() {
let img = make_rgb_gradient(8, 8);
let config = ChromAberrationConfig {
strength: 0.5,
interpolation: ChromInterpolation::Nearest,
..ChromAberrationConfig::default()
};
let out = apply_chrom_aberration(&img, 8, 8, &config).expect("nearest interp failed");
assert_eq!(out.len(), img.len());
}
#[test]
fn test_radial_ca_center_least_affected() {
let w = 13usize;
let h = 13usize;
let img = make_rgb_gradient(w, h);
let out = apply_radial_chromatic_aberration(&img, w, h, 0.5, [0.5, 0.5])
.expect("radial CA failed");
let cx = w / 2;
let cy = h / 2;
let center_diff = (0..3)
.map(|ch| (out[(cy * w + cx) * 3 + ch] - img[(cy * w + cx) * 3 + ch]).abs())
.fold(0.0f32, f32::max);
let corner_diff = (0..3)
.map(|ch| (out[ch] - img[ch]).abs())
.fold(0.0f32, f32::max);
assert!(
center_diff <= corner_diff + 1e-5,
"Centre ({center_diff}) should be less affected than corner ({corner_diff})"
);
}
#[test]
fn test_radial_ca_invalid_strength() {
let img = vec![0.5f32; 4 * 4 * 3];
let err = apply_radial_chromatic_aberration(&img, 4, 4, -0.1, [0.5, 0.5]);
assert!(matches!(err, Err(ChromAberrationError::InvalidStrength(_))));
}
#[test]
fn test_radial_ca_output_size() {
let img = make_rgb_gradient(10, 8);
let out = apply_radial_chromatic_aberration(&img, 10, 8, 0.1, [0.5, 0.5])
.expect("radial CA failed");
assert_eq!(out.len(), img.len());
}
#[test]
fn test_radial_ca_corner_reaches_full_normalized_radius() {
let w = 11usize;
let h = 7usize;
let img = make_rgb_gradient(w, h);
let out = apply_radial_chromatic_aberration(&img, w, h, 1.0, [0.5, 0.5])
.expect("radial CA failed");
let center_blue = img[((h / 2) * w + (w / 2)) * 3 + 2];
let corner_out_blue = out[2]; assert!(
(corner_out_blue - center_blue).abs() < 1e-4,
"at strength=1.0 the farthest corner's blue channel should be \
pulled exactly to the image centre's blue value ({center_blue}), \
got {corner_out_blue}"
);
}
#[test]
fn test_radial_ca_green_unchanged_at_center() {
let w = 11usize;
let h = 11usize;
let img = make_rgb_gradient(w, h);
let out = apply_radial_chromatic_aberration(&img, w, h, 0.5, [0.5, 0.5])
.expect("radial CA failed");
let cx = w / 2;
let cy = h / 2;
let orig_g = img[(cy * w + cx) * 3 + 1];
let out_g = out[(cy * w + cx) * 3 + 1];
assert!(
approx_eq(orig_g, out_g, 1e-5),
"Green at centre should be unchanged: {orig_g} vs {out_g}"
);
}
#[test]
fn test_estimate_aberration_strength_positive() {
let w = 20usize;
let h = 20usize;
let base = make_rgb_gradient(w, h);
let config = ChromAberrationConfig {
strength: 1.0,
red_shift: [2.0, 0.0],
blue_shift: [-2.0, 0.0],
..ChromAberrationConfig::default()
};
let aberrated =
apply_chrom_aberration(&base, w, h, &config).expect("apply_chrom_aberration failed");
let score = estimate_aberration_strength(&aberrated, w, h).expect("estimate failed");
assert!(
score > 0.0,
"Aberrated image should have positive score, got {score}"
);
}
#[test]
fn test_estimate_aberration_strength_size_mismatch() {
let err = estimate_aberration_strength(&[0.5f32; 5], 4, 4);
assert!(matches!(
err,
Err(ChromAberrationError::SizeMismatch { .. })
));
}
#[test]
fn test_estimate_aberration_strength_uniform_zero() {
let img = vec![0.5f32; 8 * 8 * 3];
let score = estimate_aberration_strength(&img, 8, 8).expect("estimate failed");
assert!(
score <= 1e-5,
"Uniform image should have near-zero aberration score, got {score}"
);
}
#[test]
fn test_format_chrom_config_non_empty() {
let config = ChromAberrationConfig::default();
let s = format_chrom_config(&config);
assert!(
!s.is_empty(),
"format_chrom_config should return a non-empty string"
);
assert!(
s.contains("strength"),
"String should mention 'strength': {s}"
);
}
#[test]
fn test_format_chrom_config_contains_interp() {
let config = ChromAberrationConfig {
interpolation: ChromInterpolation::Nearest,
..ChromAberrationConfig::default()
};
let s = format_chrom_config(&config);
assert!(s.contains("Nearest"), "Should mention 'Nearest': {s}");
}
#[test]
fn test_chrom_aberration_error_display() {
let err = ChromAberrationError::SizeMismatch {
got: 5,
width: 4,
height: 4,
channels: 3,
};
let s = err.to_string();
assert!(
s.contains("5"),
"Error message should mention actual size: {s}"
);
}
#[test]
fn test_shift_channel_large_shift_clamps() {
let w = 4usize;
let h = 4usize;
let img = make_rgb_gradient(w, h);
let out = shift_channel(&img, w, h, 0, -1000.0, 0.0, ChromInterpolation::Bilinear)
.expect("shift failed");
let last_px_r = img[(h / 2 * w + (w - 1)) * 3];
let shifted_mid_r = out[(h / 2 * w) * 3];
assert!(
approx_eq(shifted_mid_r, last_px_r, 1e-4),
"Large shift should clamp: expected {last_px_r}, got {shifted_mid_r}"
);
}
#[test]
fn test_nearest_sample_one_by_one() {
let img = vec![0.42f32, 0.0, 0.0];
let val = nearest_sample_channel(&img, 1, 1, 3, 0, 0.0, 0.0);
assert!(approx_eq(val, 0.42, 1e-6));
let val2 = nearest_sample_channel(&img, 1, 1, 3, 0, 5.0, 5.0);
assert!(approx_eq(val2, 0.42, 1e-6));
}
#[test]
fn test_radial_distances_off_center() {
let w = 10usize;
let h = 10usize;
let dists = compute_radial_distances(w, h, [0.0, 0.0]);
assert_eq!(dists.len(), w * h);
let top_left = dists[0];
assert!(
approx_eq(top_left, 0.0, 1e-5),
"Top-left should be 0.0 for center=[0,0]: {top_left}"
);
let bottom_right = dists[(h - 1) * w + (w - 1)];
assert!(
bottom_right > 0.9,
"Bottom-right should be far from [0,0] center: {bottom_right}"
);
}
#[test]
fn test_radial_ca_size_mismatch() {
let err = apply_radial_chromatic_aberration(&[0.5f32; 5], 4, 4, 0.1, [0.5, 0.5]);
assert!(matches!(
err,
Err(ChromAberrationError::SizeMismatch { .. })
));
}
#[test]
fn test_vignetting_invalid_strength() {
let img = vec![0.5f32; 4 * 4 * 3];
let err = apply_vignetting_f32(&img, 4, 4, -0.1, 2.0);
assert!(matches!(err, Err(ChromAberrationError::InvalidStrength(_))));
}
#[test]
fn test_chrom_aberration_output_size() {
let img = make_rgb_gradient(15, 10);
let config = ChromAberrationConfig::default();
let out =
apply_chrom_aberration(&img, 15, 10, &config).expect("apply_chrom_aberration failed");
assert_eq!(out.len(), img.len());
}
#[test]
fn test_bilinear_sample_invalid_channel() {
let img = vec![0.5f32; 4 * 4 * 3];
let val = bilinear_sample_channel(&img, 4, 4, 3, 5, 0.0, 0.0);
assert!(
approx_eq(val, 0.0, 1e-6),
"Invalid channel should return 0.0: {val}"
);
}
#[test]
fn test_nearest_sample_invalid_channel() {
let img = vec![0.5f32; 4 * 4 * 3];
let val = nearest_sample_channel(&img, 4, 4, 3, 5, 0.0, 0.0);
assert!(
approx_eq(val, 0.0, 1e-6),
"Invalid channel should return 0.0: {val}"
);
}
#[test]
fn test_vignette_mask_zero_dims() {
assert!(compute_vignette_mask(0, 10, 0.5, 2.0).is_empty());
assert!(compute_vignette_mask(10, 0, 0.5, 2.0).is_empty());
}
#[test]
fn test_barrel_distortion_with_k2() {
let img = make_rgb_gradient(10, 10);
let out = apply_barrel_distortion(&img, 10, 10, -0.1, 0.05)
.expect("barrel_distortion with k2 failed");
assert_eq!(out.len(), img.len());
let n_diff = img
.iter()
.zip(out.iter())
.filter(|&(a, b)| (a - b).abs() > 1e-4)
.count();
assert!(n_diff > 0, "k2 should also produce changes");
}
#[test]
fn test_chrom_aberration_boundary_strength() {
let img = make_rgb_gradient(8, 8);
for s in &[0.0f32, 1.0f32] {
let config = ChromAberrationConfig {
strength: *s,
..ChromAberrationConfig::default()
};
let out =
apply_chrom_aberration(&img, 8, 8, &config).expect("boundary strength failed");
assert_eq!(out.len(), img.len());
}
}
#[test]
fn test_estimate_aberration_strength_finite() {
let img = make_rgb_gradient(12, 12);
let score = estimate_aberration_strength(&img, 12, 12).expect("estimate failed");
assert!(score.is_finite(), "Score should be finite: {score}");
assert!(score >= 0.0, "Score should be non-negative: {score}");
}
#[test]
fn test_shift_all_channels() {
let img = make_rgb_gradient(6, 6);
for ch in 0..3 {
let out = shift_channel(&img, 6, 6, ch, 1.0, 0.0, ChromInterpolation::Bilinear)
.unwrap_or_else(|_| panic!("shift_channel failed for channel {ch}"));
assert_eq!(out.len(), img.len());
}
}
#[test]
fn test_format_chrom_config_barrel_flag() {
let config = ChromAberrationConfig {
barrel_distortion: true,
..ChromAberrationConfig::default()
};
let s = format_chrom_config(&config);
assert!(
s.contains("true"),
"Should show barrel_distortion=true: {s}"
);
}
#[test]
fn test_radial_distances_one_by_one() {
let dists = compute_radial_distances(1, 1, [0.5, 0.5]);
assert_eq!(dists.len(), 1);
assert!(
approx_eq(dists[0], 0.0, 1e-5),
"1×1 centre should be 0.0: {}",
dists[0]
);
}
#[test]
fn test_barrel_distortion_one_by_one() {
let img = vec![0.4f32, 0.6, 0.8];
let out = apply_barrel_distortion(&img, 1, 1, -0.5, 0.1).expect("barrel 1×1 failed");
assert_eq!(out.len(), 3);
for (a, b) in img.iter().zip(out.iter()) {
assert!(
approx_eq(*a, *b, 1e-5),
"1×1 barrel should be identity: {a} vs {b}"
);
}
}
#[test]
fn test_radial_ca_zero_strength_unchanged() {
let img = make_rgb_gradient(8, 8);
let out = apply_radial_chromatic_aberration(&img, 8, 8, 0.0, [0.5, 0.5])
.expect("radial CA zero strength failed");
for (a, b) in img.iter().zip(out.iter()) {
assert!(
approx_eq(*a, *b, 1e-5),
"Zero strength should preserve image: {a} vs {b}"
);
}
}
#[test]
fn test_vignetting_falloff_differences() {
let w = 21usize;
let h = 21usize;
let img = vec![1.0f32; w * h * 3];
let out2 = apply_vignetting_f32(&img, w, h, 0.8, 1.0).expect("linear vignetting failed");
let out4 = apply_vignetting_f32(&img, w, h, 0.8, 3.0).expect("cubic vignetting failed");
let half_px = (h / 2 * w + w / 4) * 3; let v1 = out2.get(half_px).copied().unwrap_or(0.0);
let v3 = out4.get(half_px).copied().unwrap_or(0.0);
assert!(
(v1 - v3).abs() > 1e-5,
"Different falloff should produce different mid-image values: {v1} vs {v3}"
);
}
}