use thiserror::Error;
#[derive(Debug, Error, PartialEq)]
pub enum LensEffectError {
#[error("Empty image buffer")]
EmptyImage,
#[error(
"Image buffer {actual} bytes does not match {width}\u{d7}{height}\u{d7}4 = {expected}"
)]
InvalidDimensions {
actual: usize,
expected: usize,
width: u32,
height: u32,
},
#[error("Invalid aberration strength {strength}: must be in [0, 1]")]
InvalidAberrationStrength {
strength: f32,
},
#[error("Invalid distortion coefficient {k}: must be in [-1, 1]")]
InvalidDistortionCoeff {
k: f32,
},
}
#[derive(Debug, Clone, PartialEq)]
pub struct ChromAberrationConfig {
pub red_shift: f32,
pub green_shift: f32,
pub blue_shift: f32,
pub strength: f32,
}
impl Default for ChromAberrationConfig {
fn default() -> Self {
Self {
red_shift: 0.005,
green_shift: 0.0,
blue_shift: -0.005,
strength: 1.0,
}
}
}
impl ChromAberrationConfig {
pub fn validate(&self) -> Result<(), LensEffectError> {
if !(0.0..=1.0).contains(&self.strength) {
return Err(LensEffectError::InvalidAberrationStrength {
strength: self.strength,
});
}
Ok(())
}
pub fn subtle() -> Self {
Self {
red_shift: 0.002,
green_shift: 0.0,
blue_shift: -0.002,
strength: 1.0,
}
}
pub fn moderate() -> Self {
Self {
red_shift: 0.01,
green_shift: 0.0,
blue_shift: -0.01,
strength: 1.0,
}
}
pub fn strong() -> Self {
Self {
red_shift: 0.03,
green_shift: 0.0,
blue_shift: -0.03,
strength: 1.0,
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct DistortionConfig {
pub k1: f32,
pub k2: f32,
pub strength: f32,
}
impl Default for DistortionConfig {
fn default() -> Self {
Self {
k1: 0.0,
k2: 0.0,
strength: 1.0,
}
}
}
impl DistortionConfig {
pub fn validate(&self) -> Result<(), LensEffectError> {
if !(-1.0..=1.0).contains(&self.k1) {
return Err(LensEffectError::InvalidDistortionCoeff { k: self.k1 });
}
if !(-1.0..=1.0).contains(&self.k2) {
return Err(LensEffectError::InvalidDistortionCoeff { k: self.k2 });
}
if !(0.0..=1.0).contains(&self.strength) {
return Err(LensEffectError::InvalidAberrationStrength {
strength: self.strength,
});
}
Ok(())
}
pub fn slight_barrel() -> Self {
Self {
k1: -0.1,
k2: 0.0,
strength: 1.0,
}
}
pub fn slight_pincushion() -> Self {
Self {
k1: 0.1,
k2: 0.0,
strength: 1.0,
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct LensVignetteConfig {
pub strength: f32,
pub radius: f32,
pub falloff: f32,
pub color: [f32; 3],
}
impl Default for LensVignetteConfig {
fn default() -> Self {
Self {
strength: 0.5,
radius: 0.7,
falloff: 2.0,
color: [0.0, 0.0, 0.0],
}
}
}
impl LensVignetteConfig {
pub fn validate(&self) -> Result<(), LensEffectError> {
if !(0.0..=1.0).contains(&self.strength) {
return Err(LensEffectError::InvalidAberrationStrength {
strength: self.strength,
});
}
Ok(())
}
pub fn subtle() -> Self {
Self {
strength: 0.3,
..Self::default()
}
}
pub fn cinema() -> Self {
Self {
strength: 0.7,
radius: 0.5,
..Self::default()
}
}
}
#[derive(Debug, Clone, Default)]
pub struct LensEffectsConfig {
pub chromatic_aberration: Option<ChromAberrationConfig>,
pub distortion: Option<DistortionConfig>,
pub vignette: Option<LensVignetteConfig>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct LensEffectStats {
pub mean_shift_pixels: f32,
pub max_shift_pixels: f32,
pub vignette_factor_center: f32,
pub vignette_factor_corner: f32,
}
fn bilinear_sample_f32(img: &[u8], width: u32, height: u32, fx: f32, fy: f32) -> [f32; 3] {
if img.is_empty() || width == 0 || height == 0 {
return [0.0; 3];
}
let xc = fx.clamp(0.0, (width - 1) as f32);
let yc = fy.clamp(0.0, (height - 1) as f32);
let x0 = xc.floor() as usize;
let y0 = yc.floor() as usize;
let x1 = (x0 + 1).min((width - 1) as usize);
let y1 = (y0 + 1).min((height - 1) as usize);
let tx = xc - x0 as f32;
let ty = yc - y0 as f32;
let w = width as usize;
let sample = |row: usize, col: usize| -> [f32; 3] {
let base = (row * w + col) * 4;
[
img.get(base).copied().unwrap_or(0) as f32,
img.get(base + 1).copied().unwrap_or(0) as f32,
img.get(base + 2).copied().unwrap_or(0) as f32,
]
};
let p00 = sample(y0, x0);
let p10 = sample(y0, x1);
let p01 = sample(y1, x0);
let p11 = sample(y1, x1);
let mut out = [0.0_f32; 3];
for i in 0..3 {
let top = p00[i] * (1.0 - tx) + p10[i] * tx;
let bot = p01[i] * (1.0 - tx) + p11[i] * tx;
out[i] = top * (1.0 - ty) + bot * ty;
}
out
}
#[inline]
fn bilinear_sample_channel_f32(
img: &[u8],
width: u32,
height: u32,
fx: f32,
fy: f32,
channel: usize,
) -> f32 {
if img.is_empty() || width == 0 || height == 0 {
return 0.0;
}
let xc = fx.clamp(0.0, (width - 1) as f32);
let yc = fy.clamp(0.0, (height - 1) as f32);
let x0 = xc.floor() as usize;
let y0 = yc.floor() as usize;
let x1 = (x0 + 1).min((width - 1) as usize);
let y1 = (y0 + 1).min((height - 1) as usize);
let tx = xc - x0 as f32;
let ty = yc - y0 as f32;
let w = width as usize;
let sample = |row: usize, col: usize| -> f32 {
let idx = (row * w + col) * 4 + channel;
img.get(idx).copied().unwrap_or(0) as f32
};
let v00 = sample(y0, x0);
let v10 = sample(y0, x1);
let v01 = sample(y1, x0);
let v11 = sample(y1, x1);
let top = v00 * (1.0 - tx) + v10 * tx;
let bot = v01 * (1.0 - tx) + v11 * tx;
top * (1.0 - ty) + bot * ty
}
#[inline]
fn normalized_coords(px: u32, py: u32, width: u32, height: u32) -> (f32, f32) {
let scale = 2.0 / (width.min(height).max(1) as f32);
let cx = width as f32 * 0.5;
let cy = height as f32 * 0.5;
let nx = ((px as f32 + 0.5) - cx) * scale;
let ny = ((py as f32 + 0.5) - cy) * scale;
(nx, ny)
}
#[inline]
pub(crate) fn normalized_to_pixel(nx: f32, ny: f32, width: u32, height: u32) -> (f32, f32) {
let scale = 2.0 / (width.min(height).max(1) as f32);
let cx = width as f32 * 0.5;
let cy = height as f32 * 0.5;
let px = nx / scale + cx - 0.5;
let py = ny / scale + cy - 0.5;
(px, py)
}
#[inline]
pub(crate) fn apply_distortion_coords(nx: f32, ny: f32, k1: f32, k2: f32) -> (f32, f32) {
let r2 = nx * nx + ny * ny;
let rf = 1.0 + k1 * r2 + k2 * r2 * r2;
(nx * rf, ny * rf)
}
#[inline]
pub(crate) fn compute_vignette_factor(nx: f32, ny: f32, config: &LensVignetteConfig) -> f32 {
let r = (nx * nx + ny * ny).sqrt();
if r <= config.radius {
return 1.0;
}
let outer = (std::f32::consts::SQRT_2 - config.radius).max(1e-6);
let t = ((r - config.radius) / outer).clamp(0.0, 1.0);
(1.0 - config.strength * t.powf(config.falloff)).clamp(0.0, 1.0)
}
fn validate_rgba_buffer(img: &[u8], width: u32, height: u32) -> Result<(), LensEffectError> {
if img.is_empty() || width == 0 || height == 0 {
return Err(LensEffectError::EmptyImage);
}
let expected = (width as usize) * (height as usize) * 4;
if img.len() != expected {
return Err(LensEffectError::InvalidDimensions {
actual: img.len(),
expected,
width,
height,
});
}
Ok(())
}
pub fn apply_chromatic_aberration_rgba(
image_rgba: &[u8],
width: u32,
height: u32,
config: &ChromAberrationConfig,
) -> Result<Vec<u8>, LensEffectError> {
validate_rgba_buffer(image_rgba, width, height)?;
config.validate()?;
let eff_r = config.red_shift * config.strength;
let eff_g = config.green_shift * config.strength;
let eff_b = config.blue_shift * config.strength;
let n = (width * height) as usize;
let mut out = vec![0u8; n * 4];
for py in 0..height {
for px in 0..width {
let (nx, ny) = normalized_coords(px, py, width, height);
let r2 = nx * nx + ny * ny;
let idx = (py * width + px) as usize;
let base = idx * 4;
for (channel, shift) in [(0usize, eff_r), (1, eff_g), (2, eff_b)] {
let scale = 1.0 + shift * r2;
let src_nx = nx * scale;
let src_ny = ny * scale;
let (src_px, src_py) = normalized_to_pixel(src_nx, src_ny, width, height);
let v =
bilinear_sample_channel_f32(image_rgba, width, height, src_px, src_py, channel);
out[base + channel] = v.clamp(0.0, 255.0).round() as u8;
}
out[base + 3] = image_rgba.get(base + 3).copied().unwrap_or(255);
}
}
Ok(out)
}
pub fn apply_barrel_distortion(
image_rgba: &[u8],
width: u32,
height: u32,
config: &DistortionConfig,
) -> Result<Vec<u8>, LensEffectError> {
validate_rgba_buffer(image_rgba, width, height)?;
config.validate()?;
let k1 = config.k1 * config.strength;
let k2 = config.k2 * config.strength;
let n = (width * height) as usize;
let mut out = vec![0u8; n * 4];
for py in 0..height {
for px in 0..width {
let (nx, ny) = normalized_coords(px, py, width, height);
let (src_nx, src_ny) = apply_distortion_coords(nx, ny, k1, k2);
let (src_px, src_py) = normalized_to_pixel(src_nx, src_ny, width, height);
let rgb = bilinear_sample_f32(image_rgba, width, height, src_px, src_py);
let a = {
let ax = src_px.clamp(0.0, (width - 1) as f32) as usize;
let ay = src_py.clamp(0.0, (height - 1) as f32) as usize;
image_rgba
.get((ay * width as usize + ax) * 4 + 3)
.copied()
.unwrap_or(255)
};
let idx = (py * width + px) as usize;
if let Some(dst) = out.get_mut(idx * 4..idx * 4 + 4) {
dst[0] = rgb[0].clamp(0.0, 255.0).round() as u8;
dst[1] = rgb[1].clamp(0.0, 255.0).round() as u8;
dst[2] = rgb[2].clamp(0.0, 255.0).round() as u8;
dst[3] = a;
}
}
}
Ok(out)
}
pub fn apply_vignette_effect(
image_rgba: &[u8],
width: u32,
height: u32,
config: &LensVignetteConfig,
) -> Result<Vec<u8>, LensEffectError> {
validate_rgba_buffer(image_rgba, width, height)?;
config.validate()?;
let n = (width * height) as usize;
let mut out = vec![0u8; n * 4];
for py in 0..height {
for px in 0..width {
let (nx, ny) = normalized_coords(px, py, width, height);
let factor = compute_vignette_factor(nx, ny, config);
let inv = 1.0 - factor;
let idx = (py * width + px) as usize;
let base = idx * 4;
for ch in 0..3 {
let src = image_rgba.get(base + ch).copied().unwrap_or(0) as f32;
let color_val = config.color.get(ch).copied().unwrap_or(0.0) * 255.0;
let blended = (factor * src + inv * color_val).clamp(0.0, 255.0).round() as u8;
if let Some(dst) = out.get_mut(base + ch) {
*dst = blended;
}
}
if let Some(dst) = out.get_mut(base + 3) {
*dst = image_rgba.get(base + 3).copied().unwrap_or(255);
}
}
}
Ok(out)
}
pub fn apply_lens_effects(
image_rgba: &[u8],
width: u32,
height: u32,
config: &LensEffectsConfig,
) -> Result<Vec<u8>, LensEffectError> {
validate_rgba_buffer(image_rgba, width, height)?;
let mut current: Vec<u8> = image_rgba.to_vec();
if let Some(ref ca) = config.chromatic_aberration {
current = apply_chromatic_aberration_rgba(¤t, width, height, ca)?;
}
if let Some(ref dist) = config.distortion {
current = apply_barrel_distortion(¤t, width, height, dist)?;
}
if let Some(ref vig) = config.vignette {
current = apply_vignette_effect(¤t, width, height, vig)?;
}
Ok(current)
}
pub fn compute_lens_effect_stats(
width: u32,
height: u32,
ca_config: Option<&ChromAberrationConfig>,
vignette_config: Option<&LensVignetteConfig>,
) -> LensEffectStats {
let (mean_shift, max_shift) = if let Some(ca) = ca_config {
let max_sf = ca.red_shift.abs().max(ca.blue_shift.abs()) * ca.strength;
let half_diag = ((width * width + height * height) as f32).sqrt() * 0.5;
let mut sum = 0.0f32;
let mut max_s = 0.0f32;
let n = width * height;
for py in 0..height {
for px in 0..width {
let (nx, ny) = normalized_coords(px, py, width, height);
let r2 = nx * nx + ny * ny;
let shift_px = max_sf * r2 * half_diag;
sum += shift_px;
if shift_px > max_s {
max_s = shift_px;
}
}
}
let mean = if n > 0 { sum / n as f32 } else { 0.0 };
(mean, max_s)
} else {
(0.0, 0.0)
};
let (vf_center, vf_corner) = if let Some(vc) = vignette_config {
(
compute_vignette_factor(0.0, 0.0, vc),
compute_vignette_factor(1.0, 1.0, vc),
)
} else {
(1.0, 1.0)
};
LensEffectStats {
mean_shift_pixels: mean_shift,
max_shift_pixels: max_shift,
vignette_factor_center: vf_center,
vignette_factor_corner: vf_corner,
}
}
#[cfg(test)]
mod tests {
use super::*;
fn solid_rgba(w: u32, h: u32, r: u8, g: u8, b: u8, a: u8) -> Vec<u8> {
let n = (w * h) as usize;
let mut img = vec![0u8; n * 4];
for i in 0..n {
img[i * 4] = r;
img[i * 4 + 1] = g;
img[i * 4 + 2] = b;
img[i * 4 + 3] = a;
}
img
}
#[test]
fn test_chrom_aberration_config_defaults() {
let cfg = ChromAberrationConfig::default();
assert!((cfg.red_shift - 0.005).abs() < 1e-6);
assert!((cfg.green_shift - 0.0).abs() < 1e-6);
assert!((cfg.blue_shift + 0.005).abs() < 1e-6);
assert!((cfg.strength - 1.0).abs() < 1e-6);
}
#[test]
fn test_chrom_aberration_config_validate_bad_strength() {
let cfg = ChromAberrationConfig {
strength: 1.1,
..Default::default()
};
assert!(matches!(
cfg.validate(),
Err(LensEffectError::InvalidAberrationStrength { .. })
));
let cfg2 = ChromAberrationConfig {
strength: -0.1,
..Default::default()
};
assert!(cfg2.validate().is_err());
}
#[test]
fn test_chrom_aberration_config_validate_ok() {
assert!(ChromAberrationConfig::default().validate().is_ok());
}
#[test]
fn test_distortion_config_defaults() {
let cfg = DistortionConfig::default();
assert!((cfg.k1 - 0.0).abs() < 1e-6);
assert!((cfg.k2 - 0.0).abs() < 1e-6);
assert!((cfg.strength - 1.0).abs() < 1e-6);
}
#[test]
fn test_distortion_config_validate_bad_k1() {
let cfg = DistortionConfig {
k1: -1.5,
..DistortionConfig::default()
};
assert!(matches!(
cfg.validate(),
Err(LensEffectError::InvalidDistortionCoeff { .. })
));
}
#[test]
fn test_distortion_config_validate_bad_k2() {
let cfg = DistortionConfig {
k2: 1.5,
..DistortionConfig::default()
};
assert!(matches!(
cfg.validate(),
Err(LensEffectError::InvalidDistortionCoeff { .. })
));
}
#[test]
fn test_distortion_config_validate_bad_strength() {
let cfg = DistortionConfig {
strength: 2.0,
..DistortionConfig::default()
};
assert!(cfg.validate().is_err());
}
#[test]
fn test_lens_vignette_config_defaults() {
let cfg = LensVignetteConfig::default();
assert!((cfg.strength - 0.5).abs() < 1e-6);
assert!((cfg.radius - 0.7).abs() < 1e-6);
assert!((cfg.falloff - 2.0).abs() < 1e-6);
assert_eq!(cfg.color, [0.0, 0.0, 0.0]);
}
#[test]
fn test_lens_vignette_config_validate_bad_strength() {
let cfg = LensVignetteConfig {
strength: -0.1,
..LensVignetteConfig::default()
};
assert!(cfg.validate().is_err());
let cfg2 = LensVignetteConfig {
strength: 1.5,
..LensVignetteConfig::default()
};
assert!(cfg2.validate().is_err());
}
#[test]
fn test_normalized_coords_center() {
let (nx, ny) = normalized_coords(127, 127, 256, 256);
assert!(nx.abs() < 0.01, "Expected nx ≈ 0, got {nx}");
assert!(ny.abs() < 0.01, "Expected ny ≈ 0, got {ny}");
}
#[test]
fn test_normalized_to_pixel_inverse() {
for &(px, py, w, h) in &[(3u32, 5u32, 16u32, 16u32), (0, 0, 8, 8), (7, 3, 10, 10)] {
let (nx, ny) = normalized_coords(px, py, w, h);
let (rx, ry) = normalized_to_pixel(nx, ny, w, h);
assert!(
(rx - px as f32).abs() < 1e-4,
"px round-trip: {px} → {nx} → {rx}"
);
assert!(
(ry - py as f32).abs() < 1e-4,
"py round-trip: {py} → {ny} → {ry}"
);
}
}
#[test]
fn test_normalized_coords_isotropic_on_wide_image() {
let w = 16u32;
let h = 4u32;
let (nx_h, ny_h) = normalized_coords(w / 2 + 2, h / 2, w, h);
let (nx_v, ny_v) = normalized_coords(w / 2, h / 2 + 2, w, h);
let r_h = (nx_h * nx_h + ny_h * ny_h).sqrt();
let r_v = (nx_v * nx_v + ny_v * ny_v).sqrt();
assert!(
(r_h - r_v).abs() < 1e-4,
"radii should match for equal pixel offsets: r_h={r_h} r_v={r_v}"
);
}
#[test]
fn test_vignette_factor_equal_at_equal_pixel_radius_wide_image() {
let w = 16u32;
let h = 4u32;
let cfg = LensVignetteConfig {
strength: 0.6,
radius: 0.1,
..LensVignetteConfig::default()
};
let (nx_h, ny_h) = normalized_coords(w / 2 + 2, h / 2, w, h);
let (nx_v, ny_v) = normalized_coords(w / 2, h / 2 + 2, w, h);
let f_h = compute_vignette_factor(nx_h, ny_h, &cfg);
let f_v = compute_vignette_factor(nx_v, ny_v, &cfg);
assert!(
(f_h - f_v).abs() < 1e-4,
"vignette factor should be equal at equal pixel radius: f_h={f_h} f_v={f_v}"
);
}
#[test]
fn test_apply_distortion_coords_identity() {
let (ox, oy) = apply_distortion_coords(0.3, -0.4, 0.0, 0.0);
assert!((ox - 0.3).abs() < 1e-6);
assert!((oy + 0.4).abs() < 1e-6);
}
#[test]
fn test_apply_distortion_coords_barrel() {
let (ox, _) = apply_distortion_coords(0.5, 0.0, -0.5, 0.0);
assert!(
ox < 0.5,
"Barrel: distorted x should be less than original: {ox}"
);
}
#[test]
fn test_compute_vignette_factor_center() {
let cfg = LensVignetteConfig::default();
let f = compute_vignette_factor(0.0, 0.0, &cfg);
assert!((f - 1.0).abs() < 1e-6, "Expected 1.0 at centre, got {f}");
}
#[test]
fn test_compute_vignette_factor_corner_reduced() {
let cfg = LensVignetteConfig::default();
let corner = compute_vignette_factor(1.0, 1.0, &cfg);
assert!(
corner < 1.0,
"Vignette at corner should be < 1.0, got {corner}"
);
assert!(corner >= 0.0);
}
#[test]
fn test_compute_vignette_factor_corner_bounded_by_strength() {
let cfg = LensVignetteConfig::default(); let corner = compute_vignette_factor(1.0, 1.0, &cfg);
assert!(
corner >= 1.0 - cfg.strength - 1e-5,
"corner factor {corner} should be >= 1.0 - strength ({})",
1.0 - cfg.strength
);
}
#[test]
fn test_bilinear_sample_f32_in_bounds() {
let mut img = vec![0u8; 4 * 4]; img[0] = 200;
img[1] = 100;
img[2] = 50;
img[3] = 255;
let rgb = bilinear_sample_f32(&img, 2, 2, 0.0, 0.0);
assert!((rgb[0] - 200.0).abs() < 1.0);
assert!((rgb[1] - 100.0).abs() < 1.0);
assert!((rgb[2] - 50.0).abs() < 1.0);
}
#[test]
fn test_bilinear_sample_f32_clamped() {
let img = solid_rgba(4, 4, 128, 200, 60, 255);
let rgb = bilinear_sample_f32(&img, 4, 4, 100.0, 100.0);
assert!((rgb[0] - 128.0).abs() < 1.0);
}
#[test]
fn test_bilinear_sample_channel_matches_full_sample() {
let img = solid_rgba(6, 5, 37, 91, 214, 255);
let mut img = img;
img[0] = 250;
img[1] = 10;
img[2] = 5;
for &(fx, fy) in &[(0.0f32, 0.0f32), (2.7, 1.3), (5.0, 4.0), (3.5, 2.5)] {
let full = bilinear_sample_f32(&img, 6, 5, fx, fy);
for ch in 0..3 {
let single = bilinear_sample_channel_f32(&img, 6, 5, fx, fy, ch);
assert!(
(single - full[ch]).abs() < 1e-3,
"channel {ch} mismatch at ({fx},{fy}): single={single} full={full:?}"
);
}
}
}
#[test]
fn test_apply_chromatic_aberration_rgba_output_size() {
let img = solid_rgba(8, 6, 100, 120, 80, 255);
let cfg = ChromAberrationConfig::default();
let out =
apply_chromatic_aberration_rgba(&img, 8, 6, &cfg).expect("chromatic aberration failed");
assert_eq!(out.len(), 8 * 6 * 4);
}
#[test]
fn test_apply_chromatic_aberration_rgba_zero_shifts_identity() {
let img = solid_rgba(8, 8, 120, 80, 200, 255);
let cfg = ChromAberrationConfig {
red_shift: 0.0,
green_shift: 0.0,
blue_shift: 0.0,
strength: 1.0,
};
let out = apply_chromatic_aberration_rgba(&img, 8, 8, &cfg).expect("CA failed");
for i in 0..(8 * 8) {
assert_eq!(img[i * 4], out[i * 4]);
assert_eq!(img[i * 4 + 1], out[i * 4 + 1]);
assert_eq!(img[i * 4 + 2], out[i * 4 + 2]);
}
}
#[test]
fn test_apply_chromatic_aberration_rgba_empty_error() {
let cfg = ChromAberrationConfig::default();
let result = apply_chromatic_aberration_rgba(&[], 0, 0, &cfg);
assert!(matches!(result, Err(LensEffectError::EmptyImage)));
}
#[test]
fn test_chromatic_aberration_rgba_alpha_preserved() {
let img = solid_rgba(8, 8, 100, 150, 200, 77);
let cfg = ChromAberrationConfig::moderate();
let out = apply_chromatic_aberration_rgba(&img, 8, 8, &cfg).expect("CA failed");
for i in 0..(8 * 8) {
assert_eq!(out[i * 4 + 3], 77, "Alpha must equal 77 at pixel {i}");
}
}
#[test]
fn test_apply_barrel_distortion_output_size() {
let img = solid_rgba(6, 4, 100, 100, 100, 255);
let cfg = DistortionConfig::default();
let out = apply_barrel_distortion(&img, 6, 4, &cfg).expect("barrel distortion failed");
assert_eq!(out.len(), 6 * 4 * 4);
}
#[test]
fn test_apply_barrel_distortion_identity() {
let img = solid_rgba(8, 8, 100, 150, 200, 255);
let cfg = DistortionConfig::default();
let out = apply_barrel_distortion(&img, 8, 8, &cfg).expect("barrel distortion failed");
for i in 0..(8 * 8) {
assert!((img[i * 4] as i32 - out[i * 4] as i32).abs() <= 1);
}
}
#[test]
fn test_apply_barrel_distortion_invalid_buffer() {
let bad = vec![0u8; 5];
let cfg = DistortionConfig::default();
let result = apply_barrel_distortion(&bad, 2, 2, &cfg);
assert!(matches!(
result,
Err(LensEffectError::InvalidDimensions { .. })
));
}
#[test]
fn test_apply_vignette_effect_output_size() {
let img = solid_rgba(10, 8, 200, 200, 200, 255);
let cfg = LensVignetteConfig::default();
let out = apply_vignette_effect(&img, 10, 8, &cfg).expect("vignette failed");
assert_eq!(out.len(), 10 * 8 * 4);
}
#[test]
fn test_apply_vignette_effect_zero_strength_identity() {
let img = solid_rgba(8, 8, 200, 100, 50, 255);
let cfg = LensVignetteConfig {
strength: 0.0,
..LensVignetteConfig::default()
};
let out = apply_vignette_effect(&img, 8, 8, &cfg).expect("vignette failed");
assert_eq!(img, out);
}
#[test]
fn test_apply_vignette_effect_center_vs_corner() {
let w = 16u32;
let h = 16u32;
let img = solid_rgba(w, h, 200, 200, 200, 255);
let cfg = LensVignetteConfig {
strength: 0.8,
radius: 0.3,
..LensVignetteConfig::default()
};
let out = apply_vignette_effect(&img, w, h, &cfg).expect("vignette failed");
let cx = (h / 2 * w + w / 2) as usize;
let center_r = out[cx * 4] as f32;
let corner_r = out[0] as f32;
assert!(
center_r >= corner_r,
"Centre ({center_r}) should be >= corner ({corner_r})"
);
}
#[test]
fn test_apply_vignette_effect_alpha_preserved() {
let img = solid_rgba(4, 4, 128, 128, 128, 200);
let cfg = LensVignetteConfig::default();
let out = apply_vignette_effect(&img, 4, 4, &cfg).expect("vignette failed");
for i in 0..(4 * 4) {
assert_eq!(out[i * 4 + 3], 200, "Alpha must be preserved at pixel {i}");
}
}
#[test]
fn test_apply_vignette_effect_invalid_buffer() {
let bad = vec![0u8; 3];
let cfg = LensVignetteConfig::default();
let result = apply_vignette_effect(&bad, 1, 1, &cfg);
assert!(matches!(
result,
Err(LensEffectError::InvalidDimensions { .. })
));
}
#[test]
fn test_apply_lens_effects_all_none_passthrough() {
let img = solid_rgba(8, 8, 100, 150, 200, 255);
let cfg = LensEffectsConfig::default();
let out = apply_lens_effects(&img, 8, 8, &cfg).expect("lens effects failed");
assert_eq!(img, out);
}
#[test]
fn test_apply_lens_effects_all_some_applies() {
let img = solid_rgba(8, 8, 100, 150, 200, 255);
let cfg = LensEffectsConfig {
chromatic_aberration: Some(ChromAberrationConfig::default()),
distortion: Some(DistortionConfig::default()),
vignette: Some(LensVignetteConfig::default()),
};
let out = apply_lens_effects(&img, 8, 8, &cfg).expect("lens effects failed");
assert_eq!(out.len(), 8 * 8 * 4);
}
#[test]
fn test_apply_lens_effects_empty_buffer_error() {
let cfg = LensEffectsConfig::default();
let result = apply_lens_effects(&[], 0, 0, &cfg);
assert!(matches!(result, Err(LensEffectError::EmptyImage)));
}
#[test]
fn test_compute_lens_effect_stats_vignette_center() {
let vc = LensVignetteConfig::default();
let stats = compute_lens_effect_stats(128, 128, None, Some(&vc));
assert!(
(stats.vignette_factor_center - 1.0).abs() < 1e-5,
"Centre factor should be 1.0: {}",
stats.vignette_factor_center
);
}
#[test]
fn test_compute_lens_effect_stats_vignette_corner_less_than_center() {
let vc = LensVignetteConfig {
strength: 0.8,
radius: 0.3,
..LensVignetteConfig::default()
};
let stats = compute_lens_effect_stats(64, 64, None, Some(&vc));
assert!(
stats.vignette_factor_corner < stats.vignette_factor_center,
"Corner ({}) should be < centre ({})",
stats.vignette_factor_corner,
stats.vignette_factor_center
);
}
#[test]
fn test_compute_lens_effect_stats_none_effects() {
let stats = compute_lens_effect_stats(64, 64, None, None);
assert!((stats.mean_shift_pixels - 0.0).abs() < 1e-6);
assert!((stats.max_shift_pixels - 0.0).abs() < 1e-6);
assert!((stats.vignette_factor_center - 1.0).abs() < 1e-6);
assert!((stats.vignette_factor_corner - 1.0).abs() < 1e-6);
}
#[test]
fn test_compute_lens_effect_stats_ca_positive_shift() {
let ca = ChromAberrationConfig::strong();
let stats = compute_lens_effect_stats(128, 128, Some(&ca), None);
assert!(
stats.mean_shift_pixels > 0.0,
"Mean shift should be positive: {}",
stats.mean_shift_pixels
);
assert!(stats.max_shift_pixels >= stats.mean_shift_pixels);
}
#[test]
fn test_chrom_aberration_presets_ordering() {
let subtle = ChromAberrationConfig::subtle();
let moderate = ChromAberrationConfig::moderate();
let strong = ChromAberrationConfig::strong();
assert!(subtle.red_shift < moderate.red_shift);
assert!(moderate.red_shift < strong.red_shift);
assert!(subtle.blue_shift > moderate.blue_shift);
assert!(moderate.blue_shift > strong.blue_shift);
}
#[test]
fn test_distortion_presets_signs() {
let barrel = DistortionConfig::slight_barrel();
let pincushion = DistortionConfig::slight_pincushion();
assert!(barrel.k1 < 0.0, "Barrel k1 should be negative");
assert!(pincushion.k1 > 0.0, "Pincushion k1 should be positive");
}
#[test]
fn test_lens_vignette_presets_strength_ordering() {
let subtle = LensVignetteConfig::subtle();
let cinema = LensVignetteConfig::cinema();
assert!(subtle.strength < cinema.strength);
}
#[test]
fn test_lens_effect_error_display() {
let e1 = LensEffectError::EmptyImage;
assert!(!e1.to_string().is_empty());
let e2 = LensEffectError::InvalidDimensions {
actual: 10,
expected: 16,
width: 2,
height: 2,
};
assert!(e2.to_string().contains("10"));
let e3 = LensEffectError::InvalidAberrationStrength { strength: 1.5 };
assert!(e3.to_string().contains("1.5"));
let e4 = LensEffectError::InvalidDistortionCoeff { k: -2.0 };
assert!(e4.to_string().contains("-2"));
}
#[test]
fn test_lens_effects_config_fields() {
let cfg = LensEffectsConfig {
chromatic_aberration: Some(ChromAberrationConfig::default()),
distortion: None,
vignette: None,
};
assert!(cfg.chromatic_aberration.is_some());
assert!(cfg.distortion.is_none());
}
#[test]
fn test_4x4_chromatic_aberration_smoke() {
let img = solid_rgba(4, 4, 200, 100, 50, 255);
let cfg = ChromAberrationConfig {
strength: 0.5,
..ChromAberrationConfig::default()
};
let out = apply_chromatic_aberration_rgba(&img, 4, 4, &cfg).expect("4×4 CA failed");
assert_eq!(out.len(), 4 * 4 * 4);
for i in 0..(4 * 4) {
assert_eq!(out[i * 4 + 3], 255);
}
}
#[test]
fn test_4x4_barrel_distortion_smoke() {
let img = solid_rgba(4, 4, 128, 64, 32, 128);
let cfg = DistortionConfig::slight_barrel();
let out = apply_barrel_distortion(&img, 4, 4, &cfg).expect("4×4 barrel failed");
assert_eq!(out.len(), 4 * 4 * 4);
}
#[test]
fn test_4x4_vignette_smoke() {
let img = solid_rgba(4, 4, 200, 200, 200, 255);
let cfg = LensVignetteConfig::cinema();
let out = apply_vignette_effect(&img, 4, 4, &cfg).expect("4×4 vignette failed");
assert_eq!(out.len(), 4 * 4 * 4);
}
#[test]
fn test_lens_effect_stats_fields() {
let stats = LensEffectStats {
mean_shift_pixels: 1.5,
max_shift_pixels: 3.0,
vignette_factor_center: 1.0,
vignette_factor_corner: 0.6,
};
assert!((stats.mean_shift_pixels - 1.5).abs() < 1e-6);
assert!((stats.max_shift_pixels - 3.0).abs() < 1e-6);
assert!((stats.vignette_factor_center - 1.0).abs() < 1e-6);
assert!((stats.vignette_factor_corner - 0.6).abs() < 1e-5);
}
#[test]
fn test_lens_vignette_config_validate_ok() {
assert!(LensVignetteConfig::default().validate().is_ok());
assert!(LensVignetteConfig::subtle().validate().is_ok());
assert!(LensVignetteConfig::cinema().validate().is_ok());
}
#[test]
fn test_distortion_config_validate_ok() {
assert!(DistortionConfig::default().validate().is_ok());
assert!(DistortionConfig::slight_barrel().validate().is_ok());
assert!(DistortionConfig::slight_pincushion().validate().is_ok());
}
#[test]
fn test_chrom_aberration_presets_validate_ok() {
assert!(ChromAberrationConfig::subtle().validate().is_ok());
assert!(ChromAberrationConfig::moderate().validate().is_ok());
assert!(ChromAberrationConfig::strong().validate().is_ok());
}
#[test]
fn test_apply_lens_effects_only_vignette() {
let img = solid_rgba(8, 8, 200, 200, 200, 255);
let cfg = LensEffectsConfig {
chromatic_aberration: None,
distortion: None,
vignette: Some(LensVignetteConfig {
strength: 1.0,
radius: 0.0,
..LensVignetteConfig::default()
}),
};
let out = apply_lens_effects(&img, 8, 8, &cfg).expect("lens effects failed");
assert!(
out[0] < 200,
"Corner should be darkened by vignette, got {}",
out[0]
);
}
}