use thiserror::Error;
#[derive(Debug, Error)]
pub enum CalibrationError {
#[error("Empty image")]
EmptyImage,
#[error("Image buffer {actual} bytes does not match {width}×{height}×4 = {expected}")]
BufferSizeMismatch {
actual: usize,
expected: usize,
width: u32,
height: u32,
},
#[error("Invalid color temperature {kelvin}K: must be in [1000, 20000]")]
InvalidColorTemperature {
kelvin: f32,
},
#[error("Invalid gamma {gamma}: must be > 0")]
InvalidGamma {
gamma: f32,
},
#[error("Singular matrix: cannot invert color correction matrix")]
SingularMatrix,
#[error("Reference image has different dimensions from source")]
DimensionMismatch,
}
#[derive(Debug, Clone, Copy)]
pub struct ColorMatrix {
pub data: [[f32; 3]; 3],
}
impl ColorMatrix {
pub fn identity() -> Self {
Self {
data: [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]],
}
}
pub fn from_rows(r0: [f32; 3], r1: [f32; 3], r2: [f32; 3]) -> Self {
Self { data: [r0, r1, r2] }
}
pub fn apply(&self, rgb: [f32; 3]) -> [f32; 3] {
let m = &self.data;
[
m[0][0] * rgb[0] + m[0][1] * rgb[1] + m[0][2] * rgb[2],
m[1][0] * rgb[0] + m[1][1] * rgb[1] + m[1][2] * rgb[2],
m[2][0] * rgb[0] + m[2][1] * rgb[1] + m[2][2] * rgb[2],
]
}
pub fn mul(&self, other: &ColorMatrix) -> ColorMatrix {
let a = &self.data;
let b = &other.data;
let mut out = [[0.0f32; 3]; 3];
for i in 0..3 {
for j in 0..3 {
out[i][j] = a[i][0] * b[0][j] + a[i][1] * b[1][j] + a[i][2] * b[2][j];
}
}
ColorMatrix { data: out }
}
pub fn transpose(&self) -> ColorMatrix {
let m = &self.data;
ColorMatrix {
data: [
[m[0][0], m[1][0], m[2][0]],
[m[0][1], m[1][1], m[2][1]],
[m[0][2], m[1][2], m[2][2]],
],
}
}
pub fn determinant(&self) -> f32 {
let m = &self.data;
m[0][0] * (m[1][1] * m[2][2] - m[1][2] * m[2][1])
- m[0][1] * (m[1][0] * m[2][2] - m[1][2] * m[2][0])
+ m[0][2] * (m[1][0] * m[2][1] - m[1][1] * m[2][0])
}
pub fn inverse(&self) -> Option<ColorMatrix> {
let det = self.determinant();
if det.abs() < 1e-8 {
return None;
}
let inv_det = 1.0 / det;
let m = &self.data;
let c00 = m[1][1] * m[2][2] - m[1][2] * m[2][1];
let c01 = -(m[1][0] * m[2][2] - m[1][2] * m[2][0]);
let c02 = m[1][0] * m[2][1] - m[1][1] * m[2][0];
let c10 = -(m[0][1] * m[2][2] - m[0][2] * m[2][1]);
let c11 = m[0][0] * m[2][2] - m[0][2] * m[2][0];
let c12 = -(m[0][0] * m[2][1] - m[0][1] * m[2][0]);
let c20 = m[0][1] * m[1][2] - m[0][2] * m[1][1];
let c21 = -(m[0][0] * m[1][2] - m[0][2] * m[1][0]);
let c22 = m[0][0] * m[1][1] - m[0][1] * m[1][0];
Some(ColorMatrix {
data: [
[c00 * inv_det, c10 * inv_det, c20 * inv_det],
[c01 * inv_det, c11 * inv_det, c21 * inv_det],
[c02 * inv_det, c12 * inv_det, c22 * inv_det],
],
})
}
pub fn scale(r: f32, g: f32, b: f32) -> Self {
Self {
data: [[r, 0.0, 0.0], [0.0, g, 0.0], [0.0, 0.0, b]],
}
}
}
#[derive(Debug, Clone, Copy)]
pub struct WhiteBalance {
pub temperature: f32,
pub tint: f32,
pub exposure: f32,
}
impl Default for WhiteBalance {
fn default() -> Self {
WhiteBalance {
temperature: 6500.0,
tint: 0.0,
exposure: 0.0,
}
}
}
#[derive(Debug, Clone)]
pub struct ColorCalibrationConfig {
pub white_balance: WhiteBalance,
pub color_matrix: Option<ColorMatrix>,
pub gamma: f32,
pub saturation: f32,
pub brightness: f32,
}
impl Default for ColorCalibrationConfig {
fn default() -> Self {
ColorCalibrationConfig {
white_balance: WhiteBalance::default(),
color_matrix: None,
gamma: 1.0,
saturation: 1.0,
brightness: 1.0,
}
}
}
#[derive(Debug, Clone)]
pub struct ColorStats {
pub mean_r: f32,
pub mean_g: f32,
pub mean_b: f32,
pub std_r: f32,
pub std_g: f32,
pub std_b: f32,
pub white_point: [f32; 3],
pub gray_point: [f32; 3],
}
fn validate_buffer(pixels: &[u8], width: u32, height: u32) -> Result<(), CalibrationError> {
let expected = (width as usize) * (height as usize) * 4;
if pixels.is_empty() {
return Err(CalibrationError::EmptyImage);
}
if pixels.len() != expected {
return Err(CalibrationError::BufferSizeMismatch {
actual: pixels.len(),
expected,
width,
height,
});
}
Ok(())
}
pub fn kelvin_to_rgb_multipliers(kelvin: f32) -> Result<[f32; 3], CalibrationError> {
if !(1000.0..=20000.0).contains(&kelvin) {
return Err(CalibrationError::InvalidColorTemperature { kelvin });
}
let k = kelvin / 100.0;
let r = if k <= 66.0 {
255.0_f32
} else {
(329.698_73 * (k - 60.0).powf(-0.133_204_76)).clamp(0.0, 255.0)
};
let g = if k <= 66.0 {
(99.470_8 * k.ln() - 161.119_57).clamp(0.0, 255.0)
} else {
(288.122_16 * (k - 60.0).powf(-0.075_514_846)).clamp(0.0, 255.0)
};
let b = if k >= 66.0 {
255.0_f32
} else if k <= 19.0 {
0.0_f32
} else {
(138.517_73 * (k - 10.0).ln() - 305.044_8).clamp(0.0, 255.0)
};
let r_n = r / 255.0;
let g_n = g / 255.0;
let b_n = b / 255.0;
let max_val = r_n.max(g_n).max(b_n);
if max_val < 1e-9 {
return Ok([1.0, 1.0, 1.0]);
}
Ok([r_n / max_val, g_n / max_val, b_n / max_val])
}
pub fn white_balance_matrix(wb: &WhiteBalance) -> Result<ColorMatrix, CalibrationError> {
let [mut r, mut g, mut b] = kelvin_to_rgb_multipliers(wb.temperature)?;
g *= 1.0 + wb.tint;
let exposure_factor = (2.0_f32).powf(wb.exposure);
r *= exposure_factor;
g *= exposure_factor;
b *= exposure_factor;
Ok(ColorMatrix::scale(r, g, b))
}
#[inline]
pub fn rgb_to_luminance(r: f32, g: f32, b: f32) -> f32 {
0.2126 * r + 0.7152 * g + 0.0722 * b
}
pub fn saturation_matrix(saturation: f32) -> ColorMatrix {
let s = saturation;
let lum = [0.2126_f32, 0.7152_f32, 0.0722_f32];
let mut data = [[0.0f32; 3]; 3];
for (i, row) in data.iter_mut().enumerate() {
for (j, cell) in row.iter_mut().enumerate() {
let diag = if i == j { s } else { 0.0 };
*cell = lum[j] * (1.0 - s) + diag;
}
}
ColorMatrix { data }
}
#[inline]
pub fn apply_gamma_f32(x: f32, gamma: f32) -> f32 {
x.max(0.0).powf(1.0 / gamma)
}
pub fn compute_color_stats(
pixels: &[u8],
width: u32,
height: u32,
) -> Result<ColorStats, CalibrationError> {
validate_buffer(pixels, width, height)?;
let n = (width as usize) * (height as usize);
let mut sum_r = 0.0_f64;
let mut sum_g = 0.0_f64;
let mut sum_b = 0.0_f64;
let mut max_r = 0.0_f32;
let mut max_g = 0.0_f32;
let mut max_b = 0.0_f32;
for chunk in pixels.chunks_exact(4) {
let r = chunk[0] as f32 / 255.0;
let g = chunk[1] as f32 / 255.0;
let b = chunk[2] as f32 / 255.0;
sum_r += r as f64;
sum_g += g as f64;
sum_b += b as f64;
if r > max_r {
max_r = r;
}
if g > max_g {
max_g = g;
}
if b > max_b {
max_b = b;
}
}
let mean_r = (sum_r / n as f64) as f32;
let mean_g = (sum_g / n as f64) as f32;
let mean_b = (sum_b / n as f64) as f32;
let mut var_r = 0.0_f64;
let mut var_g = 0.0_f64;
let mut var_b = 0.0_f64;
for chunk in pixels.chunks_exact(4) {
let r = chunk[0] as f64 / 255.0;
let g = chunk[1] as f64 / 255.0;
let b = chunk[2] as f64 / 255.0;
var_r += (r - mean_r as f64).powi(2);
var_g += (g - mean_g as f64).powi(2);
var_b += (b - mean_b as f64).powi(2);
}
let std_r = ((var_r / n as f64) as f32).sqrt();
let std_g = ((var_g / n as f64) as f32).sqrt();
let std_b = ((var_b / n as f64) as f32).sqrt();
Ok(ColorStats {
mean_r,
mean_g,
mean_b,
std_r,
std_g,
std_b,
white_point: [max_r, max_g, max_b],
gray_point: [mean_r, mean_g, mean_b],
})
}
pub fn gray_world_white_balance(
pixels: &[u8],
width: u32,
height: u32,
) -> Result<ColorMatrix, CalibrationError> {
validate_buffer(pixels, width, height)?;
let n = (width as usize) * (height as usize);
let mut sum_r = 0.0_f64;
let mut sum_g = 0.0_f64;
let mut sum_b = 0.0_f64;
for chunk in pixels.chunks_exact(4) {
sum_r += chunk[0] as f64;
sum_g += chunk[1] as f64;
sum_b += chunk[2] as f64;
}
let mean_r = (sum_r / n as f64) as f32 / 255.0;
let mean_g = (sum_g / n as f64) as f32 / 255.0;
let mean_b = (sum_b / n as f64) as f32 / 255.0;
let gray = (mean_r + mean_g + mean_b) / 3.0;
if gray < 1e-9 {
return Ok(ColorMatrix::identity());
}
let wr = gray / mean_r.max(1e-9);
let wg = gray / mean_g.max(1e-9);
let wb = gray / mean_b.max(1e-9);
let max_w = wr.max(wg).max(wb);
Ok(ColorMatrix::scale(wr / max_w, wg / max_w, wb / max_w))
}
pub fn apply_white_balance_image(
pixels: &[u8],
width: u32,
height: u32,
wb: &WhiteBalance,
) -> Result<Vec<u8>, CalibrationError> {
validate_buffer(pixels, width, height)?;
let matrix = white_balance_matrix(wb)?;
apply_color_correction_matrix(pixels, width, height, &matrix)
}
pub fn apply_color_correction_matrix(
pixels: &[u8],
width: u32,
height: u32,
matrix: &ColorMatrix,
) -> Result<Vec<u8>, CalibrationError> {
validate_buffer(pixels, width, height)?;
let mut out = Vec::with_capacity(pixels.len());
for chunk in pixels.chunks_exact(4) {
let r = chunk[0] as f32 / 255.0;
let g = chunk[1] as f32 / 255.0;
let b = chunk[2] as f32 / 255.0;
let a = chunk[3];
let [nr, ng, nb] = matrix.apply([r, g, b]);
out.push((nr.clamp(0.0, 1.0) * 255.0).round() as u8);
out.push((ng.clamp(0.0, 1.0) * 255.0).round() as u8);
out.push((nb.clamp(0.0, 1.0) * 255.0).round() as u8);
out.push(a);
}
Ok(out)
}
pub fn apply_gamma_correction_image(
pixels: &[u8],
width: u32,
height: u32,
gamma: f32,
) -> Result<Vec<u8>, CalibrationError> {
if gamma <= 0.0 {
return Err(CalibrationError::InvalidGamma { gamma });
}
validate_buffer(pixels, width, height)?;
let mut out = Vec::with_capacity(pixels.len());
for chunk in pixels.chunks_exact(4) {
let r = chunk[0] as f32 / 255.0;
let g = chunk[1] as f32 / 255.0;
let b = chunk[2] as f32 / 255.0;
let a = chunk[3];
out.push((apply_gamma_f32(r, gamma) * 255.0).round() as u8);
out.push((apply_gamma_f32(g, gamma) * 255.0).round() as u8);
out.push((apply_gamma_f32(b, gamma) * 255.0).round() as u8);
out.push(a);
}
Ok(out)
}
pub fn apply_saturation_image(
pixels: &[u8],
width: u32,
height: u32,
saturation: f32,
) -> Result<Vec<u8>, CalibrationError> {
validate_buffer(pixels, width, height)?;
let matrix = saturation_matrix(saturation);
apply_color_correction_matrix(pixels, width, height, &matrix)
}
pub fn apply_calibration(
pixels: &[u8],
width: u32,
height: u32,
config: &ColorCalibrationConfig,
) -> Result<Vec<u8>, CalibrationError> {
validate_buffer(pixels, width, height)?;
if config.gamma <= 0.0 {
return Err(CalibrationError::InvalidGamma {
gamma: config.gamma,
});
}
let wb_matrix = white_balance_matrix(&config.white_balance)?;
let combined = match &config.color_matrix {
Some(cm) => cm.mul(&wb_matrix),
None => wb_matrix,
};
let sat_matrix = saturation_matrix(config.saturation);
let full_matrix = sat_matrix.mul(&combined);
let mut out = Vec::with_capacity(pixels.len());
let gamma = config.gamma;
let brightness = config.brightness;
for chunk in pixels.chunks_exact(4) {
let r = chunk[0] as f32 / 255.0;
let g = chunk[1] as f32 / 255.0;
let b = chunk[2] as f32 / 255.0;
let a = chunk[3];
let [mut nr, mut ng, mut nb] = full_matrix.apply([r, g, b]);
if (gamma - 1.0).abs() > 1e-6 {
nr = apply_gamma_f32(nr.clamp(0.0, 1.0), gamma);
ng = apply_gamma_f32(ng.clamp(0.0, 1.0), gamma);
nb = apply_gamma_f32(nb.clamp(0.0, 1.0), gamma);
}
nr *= brightness;
ng *= brightness;
nb *= brightness;
out.push((nr.clamp(0.0, 1.0) * 255.0).round() as u8);
out.push((ng.clamp(0.0, 1.0) * 255.0).round() as u8);
out.push((nb.clamp(0.0, 1.0) * 255.0).round() as u8);
out.push(a);
}
Ok(out)
}
pub fn compute_correction_matrix(
source: &[u8],
reference: &[u8],
width: u32,
height: u32,
) -> Result<ColorMatrix, CalibrationError> {
validate_buffer(source, width, height)?;
validate_buffer(reference, width, height)?;
if source.len() != reference.len() {
return Err(CalibrationError::DimensionMismatch);
}
let n = (width as usize) * (height as usize);
let mut src_r = 0.0_f64;
let mut src_g = 0.0_f64;
let mut src_b = 0.0_f64;
let mut ref_r = 0.0_f64;
let mut ref_g = 0.0_f64;
let mut ref_b = 0.0_f64;
for (s, r) in source.chunks_exact(4).zip(reference.chunks_exact(4)) {
src_r += s[0] as f64;
src_g += s[1] as f64;
src_b += s[2] as f64;
ref_r += r[0] as f64;
ref_g += r[1] as f64;
ref_b += r[2] as f64;
}
let mean_src_r = (src_r / n as f64) as f32 / 255.0;
let mean_src_g = (src_g / n as f64) as f32 / 255.0;
let mean_src_b = (src_b / n as f64) as f32 / 255.0;
let mean_ref_r = (ref_r / n as f64) as f32 / 255.0;
let mean_ref_g = (ref_g / n as f64) as f32 / 255.0;
let mean_ref_b = (ref_b / n as f64) as f32 / 255.0;
let scale_r = if mean_src_r < 0.01 {
1.0
} else {
mean_ref_r / mean_src_r
};
let scale_g = if mean_src_g < 0.01 {
1.0
} else {
mean_ref_g / mean_src_g
};
let scale_b = if mean_src_b < 0.01 {
1.0
} else {
mean_ref_b / mean_src_b
};
Ok(ColorMatrix::scale(scale_r, scale_g, scale_b))
}
#[inline]
pub fn linear_to_srgb_calib(x: f32) -> f32 {
if x <= 0.003_130_8 {
12.92 * x
} else {
1.055 * x.powf(1.0 / 2.4) - 0.055
}
}
#[inline]
pub fn srgb_to_linear_calib(x: f32) -> f32 {
if x <= 0.04045 {
x / 12.92
} else {
((x + 0.055) / 1.055).powf(2.4)
}
}
pub fn apply_srgb_encoding(
pixels: &[u8],
width: u32,
height: u32,
) -> Result<Vec<u8>, CalibrationError> {
validate_buffer(pixels, width, height)?;
let mut out = Vec::with_capacity(pixels.len());
for chunk in pixels.chunks_exact(4) {
let r = chunk[0] as f32 / 255.0;
let g = chunk[1] as f32 / 255.0;
let b = chunk[2] as f32 / 255.0;
let a = chunk[3];
out.push((linear_to_srgb_calib(r.clamp(0.0, 1.0)) * 255.0).round() as u8);
out.push((linear_to_srgb_calib(g.clamp(0.0, 1.0)) * 255.0).round() as u8);
out.push((linear_to_srgb_calib(b.clamp(0.0, 1.0)) * 255.0).round() as u8);
out.push(a);
}
Ok(out)
}
pub fn apply_srgb_decoding(
pixels: &[u8],
width: u32,
height: u32,
) -> Result<Vec<u8>, CalibrationError> {
validate_buffer(pixels, width, height)?;
let mut out = Vec::with_capacity(pixels.len());
for chunk in pixels.chunks_exact(4) {
let r = chunk[0] as f32 / 255.0;
let g = chunk[1] as f32 / 255.0;
let b = chunk[2] as f32 / 255.0;
let a = chunk[3];
out.push((srgb_to_linear_calib(r.clamp(0.0, 1.0)) * 255.0).round() as u8);
out.push((srgb_to_linear_calib(g.clamp(0.0, 1.0)) * 255.0).round() as u8);
out.push((srgb_to_linear_calib(b.clamp(0.0, 1.0)) * 255.0).round() as u8);
out.push(a);
}
Ok(out)
}
pub fn histogram_stretch_matrix(
pixels: &[u8],
width: u32,
height: u32,
) -> Result<ColorMatrix, CalibrationError> {
validate_buffer(pixels, width, height)?;
let mut min_r = f32::MAX;
let mut min_g = f32::MAX;
let mut min_b = f32::MAX;
let mut max_r = f32::MIN;
let mut max_g = f32::MIN;
let mut max_b = f32::MIN;
for chunk in pixels.chunks_exact(4) {
let r = chunk[0] as f32 / 255.0;
let g = chunk[1] as f32 / 255.0;
let b = chunk[2] as f32 / 255.0;
if r < min_r {
min_r = r;
}
if g < min_g {
min_g = g;
}
if b < min_b {
min_b = b;
}
if r > max_r {
max_r = r;
}
if g > max_g {
max_g = g;
}
if b > max_b {
max_b = b;
}
}
let range_r = max_r - min_r;
let range_g = max_g - min_g;
let range_b = max_b - min_b;
let scale_r = if range_r < 1e-6 { 1.0 } else { 1.0 / range_r };
let scale_g = if range_g < 1e-6 { 1.0 } else { 1.0 / range_g };
let scale_b = if range_b < 1e-6 { 1.0 } else { 1.0 / range_b };
Ok(ColorMatrix::scale(scale_r, scale_g, scale_b))
}
pub fn d65_white_balance() -> ColorMatrix {
let wb = WhiteBalance {
temperature: 6504.0,
tint: 0.0,
exposure: 0.0,
};
match white_balance_matrix(&wb) {
Ok(m) => m,
Err(_) => ColorMatrix::identity(),
}
}
#[cfg(test)]
mod tests {
use super::*;
fn approx(a: f32, b: f32, eps: f32) -> bool {
(a - b).abs() <= eps
}
fn uniform_image(w: u32, h: u32, r: u8, g: u8, b: u8, a: u8) -> Vec<u8> {
let n = (w * h) as usize;
let mut v = Vec::with_capacity(n * 4);
for _ in 0..n {
v.push(r);
v.push(g);
v.push(b);
v.push(a);
}
v
}
#[test]
fn test_color_matrix_identity_apply() {
let m = ColorMatrix::identity();
let rgb = [0.3, 0.5, 0.8];
let out = m.apply(rgb);
assert!(approx(out[0], 0.3, 1e-6));
assert!(approx(out[1], 0.5, 1e-6));
assert!(approx(out[2], 0.8, 1e-6));
}
#[test]
fn test_color_matrix_from_rows() {
let m = ColorMatrix::from_rows([1.0, 0.0, 0.0], [0.0, 2.0, 0.0], [0.0, 0.0, 3.0]);
let out = m.apply([1.0, 1.0, 1.0]);
assert!(approx(out[0], 1.0, 1e-6));
assert!(approx(out[1], 2.0, 1e-6));
assert!(approx(out[2], 3.0, 1e-6));
}
#[test]
fn test_color_matrix_mul_identity() {
let m = ColorMatrix::scale(2.0, 3.0, 4.0);
let result = m.mul(&ColorMatrix::identity());
let out = result.apply([1.0, 1.0, 1.0]);
assert!(approx(out[0], 2.0, 1e-5));
assert!(approx(out[1], 3.0, 1e-5));
assert!(approx(out[2], 4.0, 1e-5));
}
#[test]
fn test_color_matrix_transpose() {
let m = ColorMatrix::from_rows([1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]);
let t = m.transpose();
assert!(approx(t.data[0][1], 4.0, 1e-6));
assert!(approx(t.data[1][0], 2.0, 1e-6));
assert!(approx(t.data[2][0], 3.0, 1e-6));
}
#[test]
fn test_color_matrix_scale() {
let m = ColorMatrix::scale(0.5, 2.0, 1.0);
let out = m.apply([1.0, 1.0, 1.0]);
assert!(approx(out[0], 0.5, 1e-6));
assert!(approx(out[1], 2.0, 1e-6));
assert!(approx(out[2], 1.0, 1e-6));
}
#[test]
fn test_color_matrix_determinant_identity() {
let det = ColorMatrix::identity().determinant();
assert!(approx(det, 1.0, 1e-6));
}
#[test]
fn test_color_matrix_inverse_identity() {
let inv = ColorMatrix::identity().inverse();
assert!(inv.is_some());
let out = inv.unwrap().apply([1.0, 0.5, 0.25]);
assert!(approx(out[0], 1.0, 1e-5));
}
#[test]
fn test_color_matrix_inverse_singular() {
let m = ColorMatrix::from_rows([1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]);
assert!(m.inverse().is_none());
}
#[test]
fn test_color_matrix_inverse_roundtrip() {
let m = ColorMatrix::scale(2.0, 0.5, 4.0);
let inv = m.inverse().unwrap();
let composed = m.mul(&inv);
let out = composed.apply([0.7, 0.4, 0.9]);
assert!(approx(out[0], 0.7, 1e-5));
assert!(approx(out[1], 0.4, 1e-5));
assert!(approx(out[2], 0.9, 1e-5));
}
#[test]
fn test_kelvin_daylight_6500() {
let rgb = kelvin_to_rgb_multipliers(6500.0).unwrap();
let max = rgb[0].max(rgb[1]).max(rgb[2]);
assert!(approx(max, 1.0, 1e-5));
for &v in &rgb {
assert!((0.0..=1.0).contains(&v));
}
}
#[test]
fn test_kelvin_warm_2700() {
let rgb = kelvin_to_rgb_multipliers(2700.0).unwrap();
assert!(rgb[0] > rgb[2]);
let max = rgb[0].max(rgb[1]).max(rgb[2]);
assert!(approx(max, 1.0, 1e-5));
}
#[test]
fn test_kelvin_cool_10000() {
let rgb = kelvin_to_rgb_multipliers(10000.0).unwrap();
assert!(rgb[2] > rgb[0]);
let max = rgb[0].max(rgb[1]).max(rgb[2]);
assert!(approx(max, 1.0, 1e-5));
}
#[test]
fn test_kelvin_boundary_valid() {
assert!(kelvin_to_rgb_multipliers(1000.0).is_ok());
assert!(kelvin_to_rgb_multipliers(20000.0).is_ok());
}
#[test]
fn test_kelvin_invalid() {
assert!(kelvin_to_rgb_multipliers(500.0).is_err());
assert!(kelvin_to_rgb_multipliers(25000.0).is_err());
}
#[test]
fn test_wb_matrix_neutral_near_identity() {
let wb = WhiteBalance::default();
let m = white_balance_matrix(&wb).unwrap();
let out = m.apply([1.0, 1.0, 1.0]);
let max_out = out[0].max(out[1]).max(out[2]);
assert!(max_out <= 1.01);
}
#[test]
fn test_wb_matrix_warm_temperature() {
let wb = WhiteBalance {
temperature: 3000.0,
tint: 0.0,
exposure: 0.0,
};
let m = white_balance_matrix(&wb).unwrap();
let out = m.apply([1.0, 1.0, 1.0]);
assert!(out[0] >= out[2]);
}
#[test]
fn test_wb_matrix_exposure_plus_one() {
let wb_base = WhiteBalance {
temperature: 6500.0,
tint: 0.0,
exposure: 0.0,
};
let wb_exp = WhiteBalance {
temperature: 6500.0,
tint: 0.0,
exposure: 1.0,
};
let m_base = white_balance_matrix(&wb_base).unwrap();
let m_exp = white_balance_matrix(&wb_exp).unwrap();
let out_base = m_base.apply([0.5, 0.5, 0.5]);
let out_exp = m_exp.apply([0.5, 0.5, 0.5]);
assert!(approx(out_exp[0], out_base[0] * 2.0, 1e-4));
}
#[test]
fn test_wb_matrix_invalid_temperature() {
let wb = WhiteBalance {
temperature: 0.0,
tint: 0.0,
exposure: 0.0,
};
assert!(white_balance_matrix(&wb).is_err());
}
#[test]
fn test_luminance_white() {
assert!(approx(rgb_to_luminance(1.0, 1.0, 1.0), 1.0, 1e-5));
}
#[test]
fn test_luminance_black() {
assert!(approx(rgb_to_luminance(0.0, 0.0, 0.0), 0.0, 1e-5));
}
#[test]
fn test_luminance_pure_red() {
assert!(approx(rgb_to_luminance(1.0, 0.0, 0.0), 0.2126, 1e-5));
}
#[test]
fn test_saturation_zero_is_grayscale() {
let m = saturation_matrix(0.0);
let rgb = [0.8, 0.3, 0.1];
let out = m.apply(rgb);
let lum = rgb_to_luminance(rgb[0], rgb[1], rgb[2]);
assert!(approx(out[0], lum, 1e-5));
assert!(approx(out[1], lum, 1e-5));
assert!(approx(out[2], lum, 1e-5));
}
#[test]
fn test_saturation_one_is_identity() {
let m = saturation_matrix(1.0);
let rgb = [0.4, 0.6, 0.9];
let out = m.apply(rgb);
assert!(approx(out[0], rgb[0], 1e-5));
assert!(approx(out[1], rgb[1], 1e-5));
assert!(approx(out[2], rgb[2], 1e-5));
}
#[test]
fn test_saturation_half() {
let m = saturation_matrix(0.5);
let rgb = [1.0, 0.0, 0.0];
let out = m.apply(rgb);
let lum = rgb_to_luminance(1.0, 0.0, 0.0);
assert!(out[0] >= lum && out[0] <= 1.0);
}
#[test]
fn test_gamma_one_is_identity() {
assert!(approx(apply_gamma_f32(0.5, 1.0), 0.5, 1e-6));
assert!(approx(apply_gamma_f32(0.0, 1.0), 0.0, 1e-6));
assert!(approx(apply_gamma_f32(1.0, 1.0), 1.0, 1e-6));
}
#[test]
fn test_gamma_two_is_sqrt() {
let val = 0.25_f32;
assert!(approx(apply_gamma_f32(val, 2.0), val.sqrt(), 1e-6));
}
#[test]
fn test_gamma_negative_clamped_to_zero() {
assert!(approx(apply_gamma_f32(-0.5, 2.0), 0.0, 1e-6));
}
#[test]
fn test_srgb_encode_decode_roundtrip() {
for &v in &[0.0f32, 0.01, 0.1, 0.5, 0.9, 1.0] {
let encoded = linear_to_srgb_calib(v);
let decoded = srgb_to_linear_calib(encoded);
assert!(approx(decoded, v, 1e-5), "roundtrip failed at {}", v);
}
}
#[test]
fn test_srgb_decode_encode_roundtrip() {
for &v in &[0.0f32, 0.04, 0.2, 0.5, 0.8, 1.0] {
let decoded = srgb_to_linear_calib(v);
let encoded = linear_to_srgb_calib(decoded);
assert!(approx(encoded, v, 1e-5), "roundtrip failed at {}", v);
}
}
#[test]
fn test_srgb_known_values() {
assert!(approx(linear_to_srgb_calib(1.0), 1.0, 1e-5));
assert!(approx(srgb_to_linear_calib(0.0), 0.0, 1e-5));
}
#[test]
fn test_srgb_midpoint_lt_linear() {
assert!(linear_to_srgb_calib(0.5) > 0.5);
assert!(srgb_to_linear_calib(0.5) < 0.5);
}
#[test]
fn test_color_stats_uniform() {
let img = uniform_image(4, 4, 128, 64, 200, 255);
let stats = compute_color_stats(&img, 4, 4).unwrap();
assert!(approx(stats.mean_r, 128.0 / 255.0, 1e-4));
assert!(approx(stats.mean_g, 64.0 / 255.0, 1e-4));
assert!(approx(stats.mean_b, 200.0 / 255.0, 1e-4));
assert!(approx(stats.std_r, 0.0, 1e-4));
}
#[test]
fn test_color_stats_two_color() {
let mut img = uniform_image(2, 1, 255, 0, 0, 255);
img.extend_from_slice(&[0, 0, 255, 255]);
let stats = compute_color_stats(&img, 3, 1).unwrap();
assert!(stats.mean_r > 0.0);
assert!(stats.std_r > 0.0);
}
#[test]
fn test_color_stats_empty_error() {
assert!(compute_color_stats(&[], 2, 2).is_err());
}
#[test]
fn test_gray_world_gray_image_near_identity() {
let img = uniform_image(4, 4, 128, 128, 128, 255);
let m = gray_world_white_balance(&img, 4, 4).unwrap();
let out = m.apply([0.5, 0.5, 0.5]);
assert!(approx(out[0], out[1], 1e-4));
assert!(approx(out[1], out[2], 1e-4));
}
#[test]
fn test_gray_world_red_tinted() {
let img = uniform_image(4, 4, 200, 128, 128, 255);
let m = gray_world_white_balance(&img, 4, 4).unwrap();
assert!(m.data[0][0] <= m.data[1][1]);
assert!(m.data[0][0] <= m.data[2][2]);
}
#[test]
fn test_gray_world_all_black_returns_identity() {
let img = uniform_image(4, 4, 0, 0, 0, 255);
let m = gray_world_white_balance(&img, 4, 4).unwrap();
let out = m.apply([0.5, 0.5, 0.5]);
assert!(approx(out[0], 0.5, 1e-5));
}
#[test]
fn test_apply_white_balance_neutral_unchanged() {
let img = uniform_image(2, 2, 128, 128, 128, 255);
let wb = WhiteBalance::default();
let out = apply_white_balance_image(&img, 2, 2, &wb).unwrap();
assert_eq!(out[3], 255);
let diff_r = (out[0] as i32 - 128).abs();
let diff_g = (out[1] as i32 - 128).abs();
let diff_b = (out[2] as i32 - 128).abs();
assert!(diff_r <= 4, "R diff too large: {}", diff_r);
assert!(diff_g <= 4, "G diff too large: {}", diff_g);
assert!(diff_b <= 4, "B diff too large: {}", diff_b);
}
#[test]
fn test_apply_white_balance_warm_shift() {
let img = uniform_image(2, 2, 200, 200, 200, 255);
let wb = WhiteBalance {
temperature: 3000.0,
tint: 0.0,
exposure: 0.0,
};
let out = apply_white_balance_image(&img, 2, 2, &wb).unwrap();
assert!(out[0] >= out[2]);
}
#[test]
fn test_apply_white_balance_invalid_temp() {
let img = uniform_image(1, 1, 128, 128, 128, 255);
let wb = WhiteBalance {
temperature: 50.0,
tint: 0.0,
exposure: 0.0,
};
assert!(apply_white_balance_image(&img, 1, 1, &wb).is_err());
}
#[test]
fn test_apply_ccm_identity_unchanged() {
let img = uniform_image(2, 2, 100, 150, 200, 255);
let m = ColorMatrix::identity();
let out = apply_color_correction_matrix(&img, 2, 2, &m).unwrap();
assert_eq!(out[0], 100);
assert_eq!(out[1], 150);
assert_eq!(out[2], 200);
assert_eq!(out[3], 255);
}
#[test]
fn test_apply_ccm_scale_doubles_red() {
let img = uniform_image(2, 2, 50, 100, 100, 255);
let m = ColorMatrix::scale(2.0, 1.0, 1.0);
let out = apply_color_correction_matrix(&img, 2, 2, &m).unwrap();
assert!(out[0] >= 98 && out[0] <= 102);
assert_eq!(out[1], 100);
}
#[test]
fn test_apply_ccm_clamping() {
let img = uniform_image(1, 1, 200, 200, 200, 255);
let m = ColorMatrix::scale(2.0, 2.0, 2.0);
let out = apply_color_correction_matrix(&img, 1, 1, &m).unwrap();
assert_eq!(out[0], 255);
assert_eq!(out[1], 255);
assert_eq!(out[2], 255);
}
#[test]
fn test_apply_gamma_one_unchanged() {
let img = uniform_image(2, 2, 128, 64, 200, 255);
let out = apply_gamma_correction_image(&img, 2, 2, 1.0).unwrap();
assert_eq!(out[0], 128);
assert_eq!(out[1], 64);
assert_eq!(out[2], 200);
}
#[test]
fn test_apply_gamma_invalid() {
let img = uniform_image(1, 1, 128, 128, 128, 255);
assert!(apply_gamma_correction_image(&img, 1, 1, 0.0).is_err());
assert!(apply_gamma_correction_image(&img, 1, 1, -1.0).is_err());
}
#[test]
fn test_apply_saturation_one_unchanged() {
let img = uniform_image(2, 2, 100, 150, 200, 255);
let out = apply_saturation_image(&img, 2, 2, 1.0).unwrap();
assert_eq!(out[0], 100);
assert_eq!(out[1], 150);
assert_eq!(out[2], 200);
}
#[test]
fn test_apply_saturation_zero_grayscale() {
let img = uniform_image(2, 2, 200, 100, 50, 255);
let out = apply_saturation_image(&img, 2, 2, 0.0).unwrap();
assert_eq!(out[0], out[1]);
assert_eq!(out[1], out[2]);
}
#[test]
fn test_apply_saturation_alpha_preserved() {
let img = uniform_image(2, 2, 100, 100, 100, 200);
let out = apply_saturation_image(&img, 2, 2, 0.5).unwrap();
assert_eq!(out[3], 200);
}
#[test]
fn test_apply_calibration_default_near_unchanged() {
let img = uniform_image(2, 2, 128, 128, 128, 255);
let config = ColorCalibrationConfig::default();
let out = apply_calibration(&img, 2, 2, &config).unwrap();
let diff_r = (out[0] as i32 - 128).abs();
let diff_g = (out[1] as i32 - 128).abs();
let diff_b = (out[2] as i32 - 128).abs();
assert!(diff_r <= 4, "R diff too large: {}", diff_r);
assert!(diff_g <= 4, "G diff too large: {}", diff_g);
assert!(diff_b <= 4, "B diff too large: {}", diff_b);
}
#[test]
fn test_apply_calibration_with_gamma() {
let img = uniform_image(2, 2, 128, 128, 128, 255);
let config = ColorCalibrationConfig {
gamma: 2.0,
..Default::default()
};
let out = apply_calibration(&img, 2, 2, &config).unwrap();
assert!(out[0] > 128);
}
#[test]
fn test_apply_calibration_brightness() {
let img = uniform_image(2, 2, 128, 128, 128, 255);
let config = ColorCalibrationConfig {
brightness: 0.5,
..Default::default()
};
let out = apply_calibration(&img, 2, 2, &config).unwrap();
assert!(out[0] < 128);
}
#[test]
fn test_apply_calibration_pipeline_order_saturation_then_gamma() {
let config = ColorCalibrationConfig {
white_balance: WhiteBalance::default(),
color_matrix: None,
gamma: 2.2,
saturation: 0.4,
brightness: 1.0,
};
let img = vec![200u8, 80, 40, 255]; let out = apply_calibration(&img, 1, 1, &config).unwrap();
let wb_matrix = white_balance_matrix(&config.white_balance).unwrap();
let sat_matrix = saturation_matrix(config.saturation);
let r = img[0] as f32 / 255.0;
let g = img[1] as f32 / 255.0;
let b = img[2] as f32 / 255.0;
let full_matrix = sat_matrix.mul(&wb_matrix);
let [mr, mg, mb] = full_matrix.apply([r, g, b]);
let expected = [mr, mg, mb].map(|c| {
(apply_gamma_f32(c.clamp(0.0, 1.0), config.gamma) * config.brightness).clamp(0.0, 1.0)
});
for (i, &e) in expected.iter().enumerate() {
let got = out[i] as f32 / 255.0;
assert!(
(got - e).abs() < 1.0 / 255.0 + 1e-4,
"channel {i}: expected {e}, got {got} (saturation-then-gamma order)"
);
}
let [wr, wg, wb_] = wb_matrix.apply([r, g, b]);
let gamma_first = [wr, wg, wb_].map(|c| apply_gamma_f32(c.clamp(0.0, 1.0), config.gamma));
let [ar, ag, ab] = sat_matrix.apply(gamma_first);
let alt = [ar, ag, ab].map(|c| (c * config.brightness).clamp(0.0, 1.0));
assert!(
(alt[0] - expected[0]).abs() > 1e-3 || (alt[1] - expected[1]).abs() > 1e-3,
"test input should distinguish saturation-then-gamma from gamma-then-saturation"
);
}
#[test]
fn test_correction_matrix_identical_images_identity() {
let img = uniform_image(4, 4, 100, 150, 200, 255);
let m = compute_correction_matrix(&img, &img, 4, 4).unwrap();
assert!(approx(m.data[0][0], 1.0, 1e-4));
assert!(approx(m.data[1][1], 1.0, 1e-4));
assert!(approx(m.data[2][2], 1.0, 1e-4));
}
#[test]
fn test_correction_matrix_scaled_reference() {
let src = uniform_image(4, 4, 100, 100, 100, 255);
let ref_img = uniform_image(4, 4, 200, 100, 100, 255);
let _ = ref_img.len();
let m = compute_correction_matrix(&src, &ref_img, 4, 4).unwrap();
assert!(approx(m.data[0][0], 2.0, 0.05));
assert!(approx(m.data[1][1], 1.0, 0.05));
assert!(approx(m.data[2][2], 1.0, 0.05));
}
#[test]
fn test_correction_matrix_dimension_mismatch() {
let src = uniform_image(2, 2, 100, 100, 100, 255);
let reference = uniform_image(4, 4, 100, 100, 100, 255);
assert!(compute_correction_matrix(&src, &reference, 2, 2).is_err());
}
#[test]
fn test_srgb_encoding_roundtrip_image() {
let img = uniform_image(2, 2, 100, 150, 200, 255);
let encoded = apply_srgb_encoding(&img, 2, 2).unwrap();
let decoded = apply_srgb_decoding(&encoded, 2, 2).unwrap();
let diff_r = (decoded[0] as i32 - img[0] as i32).abs();
assert!(
diff_r <= 2,
"R channel roundtrip diff too large: {}",
diff_r
);
}
#[test]
fn test_srgb_decoding_roundtrip_image() {
let img = uniform_image(2, 2, 80, 120, 180, 200);
let decoded = apply_srgb_decoding(&img, 2, 2).unwrap();
let reencoded = apply_srgb_encoding(&decoded, 2, 2).unwrap();
let diff_r = (reencoded[0] as i32 - img[0] as i32).abs();
assert!(
diff_r <= 2,
"R channel roundtrip diff too large: {}",
diff_r
);
}
#[test]
fn test_srgb_alpha_preserved() {
let img = uniform_image(1, 1, 100, 100, 100, 128);
let enc = apply_srgb_encoding(&img, 1, 1).unwrap();
assert_eq!(enc[3], 128);
let dec = apply_srgb_decoding(&img, 1, 1).unwrap();
assert_eq!(dec[3], 128);
}
#[test]
fn test_histogram_stretch_full_range_scale_one() {
let mut img = Vec::with_capacity(8);
img.extend_from_slice(&[0, 0, 0, 255]);
img.extend_from_slice(&[255, 255, 255, 255]);
let m = histogram_stretch_matrix(&img, 1, 2).unwrap();
assert!(approx(m.data[0][0], 1.0, 1e-4));
assert!(approx(m.data[1][1], 1.0, 1e-4));
assert!(approx(m.data[2][2], 1.0, 1e-4));
}
#[test]
fn test_histogram_stretch_half_range() {
let mut img = Vec::with_capacity(8);
img.extend_from_slice(&[0, 0, 0, 255]);
img.extend_from_slice(&[127, 127, 127, 255]);
let m = histogram_stretch_matrix(&img, 1, 2).unwrap();
assert!(m.data[0][0] > 1.9);
}
#[test]
fn test_d65_white_balance_near_identity() {
let m = d65_white_balance();
let out = m.apply([1.0, 1.0, 1.0]);
let max_out = out[0].max(out[1]).max(out[2]);
assert!(max_out <= 1.01);
assert!(out[0] > 0.0 && out[1] > 0.0 && out[2] > 0.0);
}
#[test]
fn test_buffer_size_mismatch_error() {
let bad_buf = vec![0u8; 10];
assert!(compute_color_stats(&bad_buf, 4, 4).is_err());
}
#[test]
fn test_empty_buffer_error() {
assert!(compute_color_stats(&[], 2, 2).is_err());
}
}