LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust
Merge PR #628 (Auto renders under the photo's process, as #533 does)
Brandon Thomas3539de95ca2a
diff --cc crates/pipeline/src/auto.rsindex cb7a3f475,59af5fe33..f3259b5d9--- a/crates/pipeline/src/auto.rs+++ b/crates/pipeline/src/auto.rs@@@ -21,41 -22,83 +22,83 @@@ pub struct AutoTonepub saturation: f64,}- fn percentile(sorted: &[f32], q: f32) -> f32 {- if sorted.is_empty() {- return 0.0;+ /// Exposure in EV that Auto adds per EV of the scene's log-average luminance (the slope is negative:+ /// darker scenes get more). The slope is Lightroom's: regressing the Exposure its Auto chose on 24+ /// raw photos against our own [`scene_log_mean`] of each gives -0.41 EV/EV (r = -0.92).+ const EXPOSURE_SLOPE: f64 = -0.41;+ /// Exposure for a scene whose log-average luminance is middle grey: fitted, with [`CONTRAST`] and+ /// [`VIBRANCE`], so our renders match Lightroom's Auto results on those photos (see [`auto_tone`]).+ const EXPOSURE_AT_GREY: f64 = -0.58;+ /// Highlights and Shadows are Lightroom's own: the average its Auto chose on the 24 photos (our+ /// sliders aim at the same as its). Freeing them as well matched no better on held-out photos and+ /// left them poorly determined.+ const HIGHLIGHTS: f64 = -66.0;+ const SHADOWS: f64 = 52.0;+ /// Contrast and Vibrance are fitted: our slider scales differ from Lightroom's, and our base tone is+ /// flatter, so Contrast is far above its +6. Whites, Blacks and Saturation stay at 0 (Whites and+ /// Blacks made no difference to the match; Lightroom leaves Saturation at about 0).+ const CONTRAST: f64 = 68.0;+ const VIBRANCE: f64 = 30.0;++ /// The scene's log-average luminance under the current white balance, in EV relative to middle grey+ /// (the mean of log2(Y / 0.18) over a 512 px proxy, scene-linear, before any tone curve). Pixels that+ /// aren't finite (a damaged decode) are left out: they get no weight when the proxy is made, and+ /// proxy pixels made only of them are skipped. `None` when no pixel is valid.+ pub fn scene_log_mean(src: &Rgb32f, info: &SourceInfo, s: &DevelopSettings) -> Option<f64> {+ let (mut clean, mut weight) = (src.clone(), Rgb32f::new(src.width, src.height));+ let mut any = false;+ for (p, w) in clean.data.iter_mut().zip(weight.data.iter_mut()) {+ if p.iter().all(|v| v.is_finite()) {+ *w = [1.0; 3];+ any = true;+ } else {+ *p = [0.0; 3];+ }+ }+ if !any {+ return None;}- sorted[((sorted.len() - 1) as f32 * q.clamp(0.0, 1.0)) as usize]+ let fit = |img: &Rgb32f| lightcraft_raster::resample::fit(img, 512, 512, lightcraft_raster::resample::Filter::Box);+ let (img, weight) = (fit(&clean), fit(&weight));+ // the mean of the valid pixels under each proxy pixel+ let kept: Vec<[f32; 3]> = img.data.iter().zip(&weight.data).filter(|(_, w)| w[0] > 1e-6).map(|(p, w)| p.map(|v| v / w[0])).collect();+ let mut kept = Rgb32f { width: kept.len(), height: 1, data: kept };- let base = DevelopSettings { wb: s.wb, ..DevelopSettings::default() };++ let base = DevelopSettings { wb: s.wb, process: s.process, ..DevelopSettings::default() };+ // (white balance is per pixel: the valid pixels can be balanced in a row of their own)+ crate::local::scene_linear_pre(&mut kept, info, &base);+ let (mut sum, mut n) = (0f64, 0usize);+ for c in &kept.data {+ let ev = (luminance_2020(*c).max(1e-6) / 0.18).log2();+ if ev.is_finite() {+ sum += ev as f64;+ n += 1;+ }+ }+ (n > 0).then(|| sum / n as f64)}- /// Compute auto tone values for `src` under the current white balance and rendering process- /// (ignores current tone values).+ /// Auto settings for `src` under the current white balance (ignores current tone values), aiming at+ /// what Lightroom's Auto does: the Exposure a scene needs follows its log-average luminance+ /// ([`scene_log_mean`], scene-linear, so the rule doesn't depend on the base tone curve), and the+ /// other sliders are one fixed recipe. Calibrated black-box against Lightroom's observed behaviour+ /// (the values its Auto chose and its exported results) on 24 Sony raw photos, of which 16 have a+ /// starting look that Sony's in-camera DRO doesn't brighten. Held out one photo at a time, those 16+ /// come out at a median dE76 of 6.4 from Lightroom's Auto results (8.3 unedited; 18.8 with the+ /// previous percentile rule, which ended about 16 L* too bright); all 24 at 7.7 (7.9 unedited).+ /// Rendered sources (JPEGs) are already toned, so their log-average reads brighter and Auto changes+ /// their exposure less. A source without a single valid pixel gets Exposure 0 (and the rest of the+ /// recipe).pub fn auto_tone(src: &Rgb32f, info: &SourceInfo, s: &DevelopSettings) -> AutoTone {- let mut img = lightcraft_raster::resample::fit(src, 512, 512, lightcraft_raster::resample::Filter::Box);- let mut base = DevelopSettings { wb: s.wb, process: s.process, ..DevelopSettings::default() };- base.light.exposure = 0.0;- crate::local::scene_linear_pre(&mut img, info, &base);- let mut ev: Vec<f32> = img.data.iter().map(|c| (luminance_2020(*c).max(1e-6) / 0.18).log2()).collect();- ev.sort_by(|a, b| a.total_cmp(b));- let median = percentile(&ev, 0.5);- let exposure = (-median * 0.85 - 0.1).clamp(-4.0, 4.0);- let (p01, p05, p95, p995) =- (percentile(&ev, 0.01) + exposure, percentile(&ev, 0.05) + exposure, percentile(&ev, 0.95) + exposure, percentile(&ev, 0.995) + exposure);- let highlights = if p995 > 2.2 { -((p995 - 2.2) * 38.0).min(90.0) } else { 0.0 };- let shadows = if p05 < -4.0 { ((-4.0 - p05) * 22.0).min(70.0) } else { 0.0 };- let spread = p95 - p05;- let contrast = ((6.5 - spread) * 6.0).clamp(-20.0, 30.0);- let whites = if p995 < 1.8 { ((1.8 - p995) * 25.0).min(40.0) } else { -((p995 - 3.5).max(0.0) * 10.0).min(30.0) };- let blacks = if p01 > -5.0 { -((p01 + 5.0) * 10.0).min(35.0) } else { ((-7.0 - p01).max(0.0) * 8.0).min(20.0) };+ let exposure = scene_log_mean(src, info, s).map_or(0.0, |m| (EXPOSURE_AT_GREY + EXPOSURE_SLOPE * m).clamp(-2.0, 3.0));AutoTone {- exposure: (exposure as f64 * 100.0).round() / 100.0,- contrast: contrast.round() as f64,- highlights: highlights.round() as f64,- shadows: shadows.round() as f64,- whites: whites.round() as f64,- blacks: blacks.round() as f64,- vibrance: 12.0,- saturation: 3.0,+ exposure: (exposure * 100.0).round() / 100.0,+ contrast: CONTRAST,+ highlights: HIGHLIGHTS,+ shadows: SHADOWS,+ whites: 0.0,+ blacks: 0.0,+ vibrance: VIBRANCE,+ saturation: 0.0,}}