PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Raw tone curve: no NaN between a knot at black and the next one (#7416)
Unemars65778c1f7e93
diff --git a/crates/raw/src/develop.rs b/crates/raw/src/develop.rsindex e3ab70b0c..5d2850875 100644--- a/crates/raw/src/develop.rs+++ b/crates/raw/src/develop.rs@@ -535,7 +535,8 @@ pub(crate) fn tone(curve: &[[f32; 2]], v: f32) -> f32 {}let i = curve.partition_point(|p| p[0] <= v).clamp(1, curve.len() - 1);let ([x0, y0], [x1, y1]) = (curve[i - 1], curve[i]);- let f = (v.log2() - x0.log2()) / (x1.log2() - x0.log2());+ // Logarithmic between knots; linear from a knot at black, where log₂ has no value.+ let f = if x0 > 0.0 { (v.log2() - x0.log2()) / (x1.log2() - x0.log2()) } else { (v - x0) / (x1 - x0) };y0 + (y1 - y0) * f}diff --git a/crates/raw/src/tone.rs b/crates/raw/src/tone.rsindex 80a80bfdd..1c2730bd2 100644--- a/crates/raw/src/tone.rs+++ b/crates/raw/src/tone.rs@@ -710,6 +710,19 @@ mod tests {assert!(Haze::new(w, h, |i| px[i], 0.0).is_none());}+ #[test]+ fn retone_lifts_shadows_with_a_curve_from_black() {+ // A curve whose first knot is black: its first segment must not divide by log₂ 0.+ let (w, h) = (96, 64);+ let curve = [[0.0, 0.0], [0.05, 0.12], [0.3, 0.6], [1.0, 1.0]];+ let px: Vec<[f32; 4]> = (0..w * h).map(|i| if i % w < 48 { [0.12, 0.11, 0.1, 1.0] } else { [0.7, 0.63, 0.56, 1.0] }).collect();+ let mut out = px.clone();+ retone(&mut out, w, h, &curve, SHADOWS);+ assert!(out.iter().flatten().all(|v| v.is_finite()));+ assert!(out[30 * w + 10][0] > px[30 * w + 10][0] + 0.05, "{:?}", out[30 * w + 10]);+ assert!((out[30 * w + 80][0] - px[30 * w + 80][0]).abs() < 0.03, "{:?}", out[30 * w + 80]);+ }+#[test]fn retone_changes_nothing_at_zero_and_matches_the_develop_stage() {let (w, h) = (300, 120);