use std::collections::HashMap;
use thiserror::Error;
#[derive(Debug, Error)]
pub enum ColorGradingError {
#[error("Invalid image: {0}")]
InvalidImage(String),
#[error("Invalid LUT: {0}")]
InvalidLut(String),
#[error("Invalid config: {0}")]
InvalidConfig(String),
}
#[inline]
pub fn srgb_to_linear(v: f32) -> f32 {
v.clamp(0.0, 1.0).powf(2.2)
}
#[inline]
pub fn linear_to_srgb(v: f32) -> f32 {
v.clamp(0.0, 1.0).powf(1.0 / 2.2)
}
#[inline]
pub fn luminance(r: f32, g: f32, b: f32) -> f32 {
0.2126 * r + 0.7152 * g + 0.0722 * b
}
pub fn rgb_to_hsv_grading(r: f32, g: f32, b: f32) -> (f32, f32, f32) {
let max = r.max(g).max(b);
let min = r.min(g).min(b);
let delta = max - min;
let v = max;
let s = if max > 1e-7 { delta / max } else { 0.0 };
let h = if delta < 1e-7 {
0.0
} else if max == r {
let h = 60.0 * ((g - b) / delta);
if h < 0.0 {
h + 360.0
} else {
h
}
} else if max == g {
60.0 * ((b - r) / delta) + 120.0
} else {
60.0 * ((r - g) / delta) + 240.0
};
(h, s, v)
}
pub fn hsv_to_rgb_grading(h: f32, s: f32, v: f32) -> (f32, f32, f32) {
if s < 1e-7 {
let c = v.clamp(0.0, 1.0);
return (c, c, c);
}
let h = ((h % 360.0) + 360.0) % 360.0; let sector = (h / 60.0) as i32;
let f = h / 60.0 - sector as f32;
let p = v * (1.0 - s);
let q = v * (1.0 - s * f);
let t = v * (1.0 - s * (1.0 - f));
let (r, g, b) = match sector {
0 => (v, t, p),
1 => (q, v, p),
2 => (p, v, t),
3 => (p, q, v),
4 => (t, p, v),
_ => (v, p, q),
};
(r.clamp(0.0, 1.0), g.clamp(0.0, 1.0), b.clamp(0.0, 1.0))
}
fn validate_rgb_image(image: &[f32]) -> Result<(), ColorGradingError> {
if !image.len().is_multiple_of(3) {
return Err(ColorGradingError::InvalidImage(format!(
"image length {} is not a multiple of 3",
image.len()
)));
}
Ok(())
}
#[derive(Debug, Clone)]
pub struct ExposureAdjust {
pub stops: f32,
}
impl ExposureAdjust {
pub fn new(stops: f32) -> Self {
Self { stops }
}
#[inline]
pub fn apply_pixel(&self, r: f32, g: f32, b: f32) -> (f32, f32, f32) {
let scale = (self.stops * std::f32::consts::LN_2).exp();
(
(r * scale).clamp(0.0, 1.0),
(g * scale).clamp(0.0, 1.0),
(b * scale).clamp(0.0, 1.0),
)
}
pub fn apply_image(&self, image: &[f32]) -> Result<Vec<f32>, ColorGradingError> {
validate_rgb_image(image)?;
let scale = (self.stops * std::f32::consts::LN_2).exp();
let out = image.iter().map(|&v| (v * scale).clamp(0.0, 1.0)).collect();
Ok(out)
}
}
#[derive(Debug, Clone)]
pub struct ContrastAdjust {
pub contrast: f32,
}
impl ContrastAdjust {
pub fn new(contrast: f32) -> Result<Self, ColorGradingError> {
if contrast <= 0.0 {
return Err(ColorGradingError::InvalidConfig(format!(
"contrast must be positive, got {}",
contrast
)));
}
Ok(Self { contrast })
}
#[inline]
pub fn apply_pixel(&self, v: f32) -> f32 {
((v - 0.5) * self.contrast + 0.5).clamp(0.0, 1.0)
}
pub fn apply_image(&self, image: &[f32]) -> Result<Vec<f32>, ColorGradingError> {
validate_rgb_image(image)?;
let out = image.iter().map(|&v| self.apply_pixel(v)).collect();
Ok(out)
}
}
#[derive(Debug, Clone)]
pub struct SaturationAdjust {
pub saturation: f32,
}
impl SaturationAdjust {
pub fn new(saturation: f32) -> Result<Self, ColorGradingError> {
if saturation < 0.0 {
return Err(ColorGradingError::InvalidConfig(format!(
"saturation must be >= 0, got {}",
saturation
)));
}
Ok(Self { saturation })
}
pub fn apply_pixel(&self, r: f32, g: f32, b: f32) -> (f32, f32, f32) {
let (h, s, v) = rgb_to_hsv_grading(r, g, b);
let new_s = (s * self.saturation).clamp(0.0, 1.0);
hsv_to_rgb_grading(h, new_s, v)
}
pub fn apply_image(&self, image: &[f32]) -> Result<Vec<f32>, ColorGradingError> {
validate_rgb_image(image)?;
let mut out = Vec::with_capacity(image.len());
for chunk in image.chunks_exact(3) {
let (r, g, b) = self.apply_pixel(chunk[0], chunk[1], chunk[2]);
out.push(r);
out.push(g);
out.push(b);
}
Ok(out)
}
}
#[derive(Debug, Clone)]
pub struct ToneCurve {
pub control_points: Vec<(f32, f32)>,
}
impl ToneCurve {
pub fn identity() -> Self {
Self {
control_points: vec![(0.0, 0.0), (1.0, 1.0)],
}
}
pub fn s_curve() -> Self {
Self {
control_points: vec![
(0.0, 0.0),
(0.25, 0.18),
(0.5, 0.5),
(0.75, 0.82),
(1.0, 1.0),
],
}
}
pub fn film() -> Self {
Self {
control_points: vec![
(0.0, 0.02),
(0.25, 0.22),
(0.5, 0.5),
(0.75, 0.78),
(1.0, 0.97),
],
}
}
pub fn evaluate(&self, x: f32) -> f32 {
let x = x.clamp(0.0, 1.0);
let pts = &self.control_points;
if pts.is_empty() {
return x;
}
if pts.len() == 1 {
return pts[0].1;
}
if x <= pts[0].0 {
return pts[0].1;
}
if x >= pts[pts.len() - 1].0 {
return pts[pts.len() - 1].1;
}
let idx = pts.partition_point(|&(px, _)| px <= x);
let idx = idx.min(pts.len() - 1).max(1);
let (x0, y0) = pts[idx - 1];
let (x1, y1) = pts[idx];
if (x1 - x0).abs() < 1e-9 {
return y0;
}
let t = (x - x0) / (x1 - x0);
y0 + t * (y1 - y0)
}
#[inline]
pub fn apply_pixel(&self, r: f32, g: f32, b: f32) -> (f32, f32, f32) {
(self.evaluate(r), self.evaluate(g), self.evaluate(b))
}
pub fn apply_image(&self, image: &[f32]) -> Result<Vec<f32>, ColorGradingError> {
validate_rgb_image(image)?;
let out = image.iter().map(|&v| self.evaluate(v)).collect();
Ok(out)
}
pub fn add_point(&mut self, x: f32, y: f32) {
let idx = self.control_points.partition_point(|&(px, _)| px < x);
self.control_points.insert(idx, (x, y));
}
}
#[derive(Debug, Clone)]
pub struct Lut3D {
pub size: usize,
pub table: Vec<f32>,
}
impl Lut3D {
pub fn identity(size: usize) -> Result<Self, ColorGradingError> {
if size < 2 {
return Err(ColorGradingError::InvalidLut(format!(
"LUT size must be >= 2, got {}",
size
)));
}
let n = size * size * size * 3;
let mut table = vec![0.0f32; n];
let scale = 1.0 / (size - 1) as f32;
for ri in 0..size {
for gi in 0..size {
for bi in 0..size {
let base = (ri * size * size + gi * size + bi) * 3;
table[base] = ri as f32 * scale;
table[base + 1] = gi as f32 * scale;
table[base + 2] = bi as f32 * scale;
}
}
}
Ok(Self { size, table })
}
pub fn validate(&self) -> Result<(), ColorGradingError> {
if self.size < 2 {
return Err(ColorGradingError::InvalidLut(format!(
"LUT size must be >= 2, got {}",
self.size
)));
}
let expected = self.size * self.size * self.size * 3;
if self.table.len() != expected {
return Err(ColorGradingError::InvalidLut(format!(
"LUT table has {} entries, expected size^3*3 = {} for size {}",
self.table.len(),
expected,
self.size
)));
}
Ok(())
}
pub fn lookup(&self, r: f32, g: f32, b: f32) -> (f32, f32, f32) {
if self.size == 0 {
return (0.0, 0.0, 0.0);
}
let s = (self.size - 1) as f32;
let lr = (r.clamp(0.0, 1.0) * s).min(s);
let lg = (g.clamp(0.0, 1.0) * s).min(s);
let lb = (b.clamp(0.0, 1.0) * s).min(s);
let r0 = lr.floor() as usize;
let g0 = lg.floor() as usize;
let b0 = lb.floor() as usize;
let r1 = (r0 + 1).min(self.size - 1);
let g1 = (g0 + 1).min(self.size - 1);
let b1 = (b0 + 1).min(self.size - 1);
let fr = lr - r0 as f32;
let fg = lg - g0 as f32;
let fb = lb - b0 as f32;
let fetch = |ri: usize, gi: usize, bi: usize, ch: usize| -> f32 {
let base = (ri * self.size * self.size + gi * self.size + bi) * 3;
self.table.get(base + ch).copied().unwrap_or(0.0)
};
let interp = |ch: usize| -> f32 {
let c000 = fetch(r0, g0, b0, ch);
let c001 = fetch(r0, g0, b1, ch);
let c010 = fetch(r0, g1, b0, ch);
let c011 = fetch(r0, g1, b1, ch);
let c100 = fetch(r1, g0, b0, ch);
let c101 = fetch(r1, g0, b1, ch);
let c110 = fetch(r1, g1, b0, ch);
let c111 = fetch(r1, g1, b1, ch);
let c00 = c000 * (1.0 - fb) + c001 * fb;
let c01 = c010 * (1.0 - fb) + c011 * fb;
let c10 = c100 * (1.0 - fb) + c101 * fb;
let c11 = c110 * (1.0 - fb) + c111 * fb;
let c0 = c00 * (1.0 - fg) + c01 * fg;
let c1 = c10 * (1.0 - fg) + c11 * fg;
c0 * (1.0 - fr) + c1 * fr
};
(interp(0), interp(1), interp(2))
}
pub fn apply_image(&self, image: &[f32]) -> Result<Vec<f32>, ColorGradingError> {
validate_rgb_image(image)?;
self.validate()?;
let mut out = Vec::with_capacity(image.len());
for chunk in image.chunks_exact(3) {
let (r, g, b) = self.lookup(chunk[0], chunk[1], chunk[2]);
out.push(r);
out.push(g);
out.push(b);
}
Ok(out)
}
pub fn from_tone_curve(size: usize, curve: &ToneCurve) -> Result<Self, ColorGradingError> {
if size < 2 {
return Err(ColorGradingError::InvalidLut(format!(
"LUT size must be >= 2, got {}",
size
)));
}
let n = size * size * size * 3;
let mut table = vec![0.0f32; n];
let scale = 1.0 / (size - 1) as f32;
for ri in 0..size {
for gi in 0..size {
for bi in 0..size {
let base = (ri * size * size + gi * size + bi) * 3;
table[base] = curve.evaluate(ri as f32 * scale);
table[base + 1] = curve.evaluate(gi as f32 * scale);
table[base + 2] = curve.evaluate(bi as f32 * scale);
}
}
}
Ok(Self { size, table })
}
}
#[derive(Debug, Clone)]
pub enum GradingStep {
Exposure(f32),
Contrast(f32),
Saturation(f32),
ToneCurve(ToneCurve),
Custom(String),
}
type CustomStepHandler = Box<dyn Fn(&[f32]) -> Result<Vec<f32>, ColorGradingError> + Send + Sync>;
pub struct ColorGradingPipeline {
steps: Vec<GradingStep>,
pub lut: Option<Lut3D>,
custom_handlers: HashMap<String, CustomStepHandler>,
}
impl ColorGradingPipeline {
pub fn new() -> Self {
Self {
steps: Vec::new(),
lut: None,
custom_handlers: HashMap::new(),
}
}
pub fn push(mut self, step: GradingStep) -> Self {
self.steps.push(step);
self
}
pub fn with_lut(mut self, lut: Lut3D) -> Self {
self.lut = Some(lut);
self
}
pub fn with_custom_handler<F>(mut self, name: impl Into<String>, handler: F) -> Self
where
F: Fn(&[f32]) -> Result<Vec<f32>, ColorGradingError> + Send + Sync + 'static,
{
self.custom_handlers.insert(name.into(), Box::new(handler));
self
}
pub fn len(&self) -> usize {
self.steps.len()
}
pub fn is_empty(&self) -> bool {
self.steps.is_empty() && self.lut.is_none()
}
pub fn steps(&self) -> &[GradingStep] {
&self.steps
}
pub fn apply(&self, image: &[f32]) -> Result<Vec<f32>, ColorGradingError> {
validate_rgb_image(image)?;
let mut current: Vec<f32> = image.to_vec();
for step in &self.steps {
current = match step {
GradingStep::Exposure(stops) => {
ExposureAdjust::new(*stops).apply_image(¤t)?
}
GradingStep::Contrast(c) => {
ContrastAdjust::new(*c)?.apply_image(¤t)?
}
GradingStep::Saturation(s) => SaturationAdjust::new(*s)?.apply_image(¤t)?,
GradingStep::ToneCurve(curve) => curve.apply_image(¤t)?,
GradingStep::Custom(name) => match self.custom_handlers.get(name) {
Some(handler) => handler(¤t)?,
None => {
return Err(ColorGradingError::InvalidConfig(format!(
"no handler registered for custom grading step {name:?} \
(register one with ColorGradingPipeline::with_custom_handler)"
)))
}
},
};
}
if let Some(lut) = &self.lut {
current = lut.apply_image(¤t)?;
}
Ok(current)
}
pub fn standard_preset() -> Self {
Self::new()
.push(GradingStep::ToneCurve(ToneCurve::s_curve()))
.push(GradingStep::Saturation(1.1))
.push(GradingStep::Exposure(-0.1))
}
pub fn cinematic_preset() -> Self {
Self::new()
.push(GradingStep::ToneCurve(ToneCurve::film()))
.push(GradingStep::Saturation(0.85))
.push(GradingStep::Contrast(1.15))
}
}
impl Default for ColorGradingPipeline {
fn default() -> Self {
Self::new()
}
}
pub struct RgbHistogram {
pub bins: usize,
pub r_counts: Vec<u32>,
pub g_counts: Vec<u32>,
pub b_counts: Vec<u32>,
pub total_pixels: usize,
}
impl RgbHistogram {
pub fn compute(image: &[f32], bins: usize) -> Result<Self, ColorGradingError> {
validate_rgb_image(image)?;
if bins == 0 {
return Err(ColorGradingError::InvalidConfig(
"bins must be > 0".to_string(),
));
}
let mut r_counts = vec![0u32; bins];
let mut g_counts = vec![0u32; bins];
let mut b_counts = vec![0u32; bins];
let to_bin = |v: f32| -> usize {
let idx = (v.clamp(0.0, 1.0) * bins as f32) as usize;
idx.min(bins - 1)
};
for chunk in image.chunks_exact(3) {
r_counts[to_bin(chunk[0])] += 1;
g_counts[to_bin(chunk[1])] += 1;
b_counts[to_bin(chunk[2])] += 1;
}
let total_pixels = image.len() / 3;
Ok(Self {
bins,
r_counts,
g_counts,
b_counts,
total_pixels,
})
}
fn weighted_mean(counts: &[u32], bins: usize, total: usize) -> f32 {
if total == 0 {
return 0.0;
}
let scale = 1.0 / bins as f32;
counts
.iter()
.enumerate()
.map(|(i, &c)| (i as f32 + 0.5) * scale * c as f32)
.sum::<f32>()
/ total as f32
}
pub fn mean_r(&self) -> f32 {
Self::weighted_mean(&self.r_counts, self.bins, self.total_pixels)
}
pub fn mean_g(&self) -> f32 {
Self::weighted_mean(&self.g_counts, self.bins, self.total_pixels)
}
pub fn mean_b(&self) -> f32 {
Self::weighted_mean(&self.b_counts, self.bins, self.total_pixels)
}
pub fn peak_bin_r(&self) -> usize {
self.r_counts
.iter()
.enumerate()
.max_by_key(|&(_, &c)| c)
.map(|(i, _)| i)
.unwrap_or(0)
}
pub fn format_ascii(&self, height: usize) -> String {
if self.bins == 0 || height == 0 {
return String::new();
}
let max_count = self.r_counts.iter().copied().max().unwrap_or(0);
if max_count == 0 {
return "(empty)\n".to_string();
}
let mut rows: Vec<String> = Vec::with_capacity(height + 2);
for row in (0..height).rev() {
let threshold = max_count as f32 * (row as f32 + 1.0) / height as f32;
let line: String = self
.r_counts
.iter()
.map(|&c| if c as f32 >= threshold { '#' } else { ' ' })
.collect();
rows.push(line);
}
rows.push("-".repeat(self.bins));
rows.join("\n") + "\n"
}
}
#[cfg(test)]
mod tests {
use super::*;
const EPSILON: f32 = 1e-4;
fn approx_eq(a: f32, b: f32, eps: f32) -> bool {
(a - b).abs() <= eps
}
#[test]
fn test_srgb_to_linear_boundaries() {
assert!(approx_eq(srgb_to_linear(0.0), 0.0, EPSILON));
assert!(approx_eq(srgb_to_linear(1.0), 1.0, EPSILON));
}
#[test]
fn test_linear_to_srgb_roundtrip() {
for v in [0.0f32, 0.25, 0.5, 0.75, 1.0] {
let roundtripped = linear_to_srgb(srgb_to_linear(v));
assert!(
approx_eq(v, roundtripped, 1e-3),
"roundtrip failed for v={}: got {}",
v,
roundtripped
);
}
}
#[test]
fn test_rgb_to_hsv_red() {
let (h, s, v) = rgb_to_hsv_grading(1.0, 0.0, 0.0);
assert!(approx_eq(h, 0.0, EPSILON), "hue: {}", h);
assert!(approx_eq(s, 1.0, EPSILON), "sat: {}", s);
assert!(approx_eq(v, 1.0, EPSILON), "val: {}", v);
}
#[test]
fn test_rgb_to_hsv_white() {
let (_, s, v) = rgb_to_hsv_grading(1.0, 1.0, 1.0);
assert!(approx_eq(s, 0.0, EPSILON), "sat should be 0: {}", s);
assert!(approx_eq(v, 1.0, EPSILON), "val should be 1: {}", v);
}
#[test]
fn test_hsv_to_rgb_black() {
let (r, g, b) = hsv_to_rgb_grading(0.0, 0.0, 0.0);
assert!(approx_eq(r, 0.0, EPSILON));
assert!(approx_eq(g, 0.0, EPSILON));
assert!(approx_eq(b, 0.0, EPSILON));
}
#[test]
fn test_hsv_rgb_roundtrip() {
let cases = [(0.8, 0.3, 0.6f32), (0.1, 0.9, 0.7), (0.5, 0.5, 0.5)];
for (r, g, b) in cases {
let (h, s, v) = rgb_to_hsv_grading(r, g, b);
let (r2, g2, b2) = hsv_to_rgb_grading(h, s, v);
assert!(approx_eq(r, r2, 1e-3), "r roundtrip: {} → {}", r, r2);
assert!(approx_eq(g, g2, 1e-3), "g roundtrip: {} → {}", g, g2);
assert!(approx_eq(b, b2, 1e-3), "b roundtrip: {} → {}", b, b2);
}
}
#[test]
fn test_luminance_red_and_weights() {
assert!(approx_eq(luminance(1.0, 0.0, 0.0), 0.2126, EPSILON));
let total = 0.2126_f32 + 0.7152 + 0.0722;
assert!(approx_eq(total, 1.0, EPSILON));
}
#[test]
fn test_exposure_zero_stops() {
let ea = ExposureAdjust::new(0.0);
let (r, g, b) = ea.apply_pixel(0.5, 0.3, 0.8);
assert!(approx_eq(r, 0.5, EPSILON));
assert!(approx_eq(g, 0.3, EPSILON));
assert!(approx_eq(b, 0.8, EPSILON));
}
#[test]
fn test_exposure_one_stop() {
let ea = ExposureAdjust::new(1.0);
let (r, _g, _b) = ea.apply_pixel(0.3, 0.0, 0.0);
assert!(approx_eq(r, (0.3 * 2.0f32).min(1.0), EPSILON));
let (r2, _, _) = ea.apply_pixel(0.8, 0.0, 0.0);
assert!(approx_eq(r2, 1.0, EPSILON));
}
#[test]
fn test_exposure_wrong_length() {
let ea = ExposureAdjust::new(0.0);
let bad = vec![1.0f32, 0.5];
assert!(ea.apply_image(&bad).is_err());
}
#[test]
fn test_contrast_one_no_change() {
let ca = ContrastAdjust::new(1.0).expect("valid contrast");
assert!(approx_eq(ca.apply_pixel(0.5), 0.5, EPSILON));
}
#[test]
fn test_contrast_spreads_values() {
let ca = ContrastAdjust::new(2.0).expect("valid contrast");
assert!(ca.apply_pixel(0.7) > 0.7);
assert!(ca.apply_pixel(0.3) < 0.3);
}
#[test]
fn test_saturation_zero_grayscale() {
let sa = SaturationAdjust::new(0.0).expect("valid");
let (r, g, b) = sa.apply_pixel(0.8, 0.3, 0.1);
assert!(approx_eq(r, g, 1e-3), "r={} g={}", r, g);
assert!(approx_eq(g, b, 1e-3), "g={} b={}", g, b);
}
#[test]
fn test_saturation_one_unchanged() {
let sa = SaturationAdjust::new(1.0).expect("valid");
let (r, g, b) = sa.apply_pixel(0.8, 0.3, 0.1);
assert!(approx_eq(r, 0.8, 1e-3));
assert!(approx_eq(g, 0.3, 1e-3));
assert!(approx_eq(b, 0.1, 1e-3));
}
#[test]
fn test_tone_curve_identity_endpoints() {
let tc = ToneCurve::identity();
assert!(approx_eq(tc.evaluate(0.0), 0.0, EPSILON));
assert!(approx_eq(tc.evaluate(1.0), 1.0, EPSILON));
}
#[test]
fn test_tone_curve_interpolation() {
let tc = ToneCurve {
control_points: vec![(0.0, 0.0), (0.5, 0.25), (1.0, 1.0)],
};
let y = tc.evaluate(0.25);
assert!(approx_eq(y, 0.125, EPSILON), "got {}", y);
}
#[test]
fn test_s_curve_midpoint() {
let tc = ToneCurve::s_curve();
assert!(approx_eq(tc.evaluate(0.5), 0.5, EPSILON));
}
#[test]
fn test_lut3d_identity_midpoint() {
let lut = Lut3D::identity(5).expect("valid size");
let (r, g, b) = lut.lookup(0.5, 0.5, 0.5);
assert!(approx_eq(r, 0.5, 0.05), "r={}", r);
assert!(approx_eq(g, 0.5, 0.05), "g={}", g);
assert!(approx_eq(b, 0.5, 0.05), "b={}", b);
}
#[test]
fn test_lut3d_identity_corners() {
let lut = Lut3D::identity(5).expect("valid size");
let (r, g, b) = lut.lookup(0.0, 0.0, 0.0);
assert!(approx_eq(r, 0.0, EPSILON));
assert!(approx_eq(g, 0.0, EPSILON));
assert!(approx_eq(b, 0.0, EPSILON));
let (r, g, b) = lut.lookup(1.0, 1.0, 1.0);
assert!(approx_eq(r, 1.0, EPSILON));
assert!(approx_eq(g, 1.0, EPSILON));
assert!(approx_eq(b, 1.0, EPSILON));
}
#[test]
fn test_lut3d_from_identity_curve() {
let curve = ToneCurve::identity();
let lut = Lut3D::from_tone_curve(9, &curve).expect("valid");
let (r, g, b) = lut.lookup(0.3, 0.6, 0.9);
assert!(approx_eq(r, 0.3, 0.02), "r={}", r);
assert!(approx_eq(g, 0.6, 0.02), "g={}", g);
assert!(approx_eq(b, 0.9, 0.02), "b={}", b);
}
#[test]
fn test_lut3d_lookup_zero_size_no_panic() {
let lut = Lut3D {
size: 0,
table: Vec::new(),
};
assert_eq!(lut.lookup(0.5, 0.5, 0.5), (0.0, 0.0, 0.0));
}
#[test]
fn test_lut3d_lookup_truncated_table_no_panic() {
let lut = Lut3D {
size: 2,
table: vec![0.5f32; 4],
};
let (r, g, b) = lut.lookup(1.0, 1.0, 1.0);
assert!(
r.is_finite() && g.is_finite() && b.is_finite(),
"missing table entries should read as 0.0, not panic: ({r}, {g}, {b})"
);
}
#[test]
fn test_lut3d_validate_rejects_invalid_luts() {
assert!(Lut3D {
size: 0,
table: Vec::new(),
}
.validate()
.is_err());
assert!(Lut3D {
size: 2,
table: vec![0.5f32; 4], }
.validate()
.is_err());
assert!(Lut3D::identity(4).expect("valid").validate().is_ok());
}
#[test]
fn test_lut3d_apply_image_rejects_invalid_lut() {
let lut = Lut3D {
size: 0,
table: Vec::new(),
};
let image = vec![0.5f32, 0.5, 0.5];
assert!(matches!(
lut.apply_image(&image),
Err(ColorGradingError::InvalidLut(_))
));
}
#[test]
fn test_pipeline_empty_unchanged() {
let pipeline = ColorGradingPipeline::new();
let image = vec![0.2f32, 0.5, 0.8, 0.1, 0.9, 0.4];
let out = pipeline.apply(&image).expect("should succeed");
for (a, b) in image.iter().zip(out.iter()) {
assert!(approx_eq(*a, *b, EPSILON));
}
}
#[test]
fn test_pipeline_noop_steps() {
let pipeline = ColorGradingPipeline::new()
.push(GradingStep::Exposure(0.0))
.push(GradingStep::Saturation(1.0));
let image = vec![0.4f32, 0.6, 0.2, 0.9, 0.1, 0.5];
let out = pipeline.apply(&image).expect("should succeed");
for (a, b) in image.iter().zip(out.iter()) {
assert!(approx_eq(*a, *b, 2e-3), "a={} b={}", a, b);
}
}
#[test]
fn test_pipeline_unregistered_custom_step_errors() {
let pipeline = ColorGradingPipeline::new().push(GradingStep::Custom("my_lut".into()));
let image = vec![0.4f32, 0.6, 0.2];
let result = pipeline.apply(&image);
assert!(matches!(result, Err(ColorGradingError::InvalidConfig(_))));
}
#[test]
fn test_pipeline_registered_custom_handler_is_invoked() {
let pipeline = ColorGradingPipeline::new()
.with_custom_handler("invert", |img| Ok(img.iter().map(|&v| 1.0 - v).collect()))
.push(GradingStep::Custom("invert".to_string()));
let image = vec![0.2f32, 0.5, 0.9];
let out = pipeline
.apply(&image)
.expect("registered handler should run");
for (a, b) in image.iter().zip(out.iter()) {
assert!(
approx_eq(1.0 - *a, *b, EPSILON),
"expected inverted value: a={a} b={b}"
);
}
}
#[test]
fn test_pipeline_custom_handler_name_must_match() {
let pipeline = ColorGradingPipeline::new()
.with_custom_handler("invert", |img| Ok(img.to_vec()))
.push(GradingStep::Custom("not_invert".to_string()));
let image = vec![0.4f32, 0.6, 0.2];
let result = pipeline.apply(&image);
assert!(matches!(result, Err(ColorGradingError::InvalidConfig(_))));
}
#[test]
fn test_histogram_uniform() {
const BINS: usize = 4;
const PIXELS: usize = 100;
let mut image = Vec::with_capacity(PIXELS * 3);
for i in 0..PIXELS {
let v = i as f32 / PIXELS as f32;
image.push(v);
image.push(v);
image.push(v);
}
let hist = RgbHistogram::compute(&image, BINS).expect("valid");
let expected = PIXELS as f32 / BINS as f32;
for &c in &hist.r_counts {
let diff = (c as f32 - expected).abs();
assert!(
diff <= expected * 0.5 + 2.0,
"count {} far from {}",
c,
expected
);
}
}
#[test]
fn test_histogram_mean_r() {
let image = vec![0.75f32, 0.0, 0.0];
let hist = RgbHistogram::compute(&image, 10).expect("valid");
let mean = hist.mean_r();
assert!(approx_eq(mean, 0.75, 0.1), "mean_r={}", mean);
}
}