LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust
pipeline: bound dehaze veil by the pixel's own minimum channel (#2504)
Matt Markham9122443f227e
diff --git a/crates/gpu/src/wgsl/finish.wgsl b/crates/gpu/src/wgsl/finish.wgslindex d51308ee0..caf54a9b1 100644--- a/crates/gpu/src/wgsl/finish.wgsl+++ b/crates/gpu/src/wgsl/finish.wgsl@@ -324,8 +324,11 @@ fn main(@builtin(global_invocation_id) gid: vec3<u32>) {// --- dehaze (scene linear)let dz = pf(F_DEHAZE) + lt[10];if (dz != 0.0 && pu(F_HAS_DARK) != 0u) {- let d = clamp(dark[i] / pf(F_AIR_PRE), 0.0, 1.0);+ // the dark channel is a blur, not a patch minimum: never attribute more veil than the+ // pixel's own minimum channel, or dark pixels next to bright ones crush to black (#2504)let air = pf(F_AIR);+ let cmin = min(c.x, min(c.y, c.z));+ let d = clamp(min(select(0.0, dark[i] / pf(F_AIR_PRE), pf(F_AIR_PRE) > 0.0), select(0.0, cmin / air, air > 0.0)), 0.0, 1.0);if (dz > 0.0) {let t = max(1.0 - 0.95 * min(dz, 1.0) * d, 0.12);c = max((c - air * (1.0 - t)) / t, vec3<f32>(0.0));diff --git a/crates/pipeline/src/finish.rs b/crates/pipeline/src/finish.rsindex eb65d51a2..b61b6a62c 100644--- a/crates/pipeline/src/finish.rs+++ b/crates/pipeline/src/finish.rs@@ -450,7 +450,12 @@ pub(crate) fn finish_with<T: Copy + Default + Send>(if dz != 0.0&& let Some(dark) = &p.dark{- let d = (dark.data[i] / air_pre).clamp(0.0, 1.0);+ // The dark channel is a blur, not a patch minimum: near bright neighbours it credits+ // dark pixels with veil they don't hold (and a windowed render borrows the whole+ // frame's airlight), so the subtraction went past zero and crushed them to black+ // (#2504). Never attribute more veil than the pixel's own minimum channel.+ let c_min = c[0].min(c[1]).min(c[2]);+ let d = (if air_pre > 0.0 { dark.data[i] / air_pre } else { 0.0 }).min(if air > 0.0 { c_min / air } else { 0.0 }).clamp(0.0, 1.0);if dz > 0.0 {let t = (1.0 - 0.95 * dz.min(1.0) * d).max(0.12);c = c.map(|v| ((v - air * (1.0 - t)) / t).max(0.0));diff --git a/crates/pipeline/src/tests.rs b/crates/pipeline/src/tests.rsindex d7949315e..f31e96f59 100644--- a/crates/pipeline/src/tests.rs+++ b/crates/pipeline/src/tests.rs@@ -75,6 +75,36 @@ fn directional_sliders() {assert!(at("effects.dehaze", -100.0) != base);}+/// Dehaze crushed dark regions next to bright ones to black: the dark channel is a blur, not a+/// patch minimum, so near the boundary it credited the dark pixels with the bright side's veil and+/// the subtraction went past zero (#2504). The veil credited to a pixel is now bounded by its own+/// minimum channel, so a uniform dark region renders uniformly under dehaze.+#[test]+fn dehaze_does_not_crush_dark_regions_next_to_bright_ones() {+ // a dark field against a bright hazy sky; the blur bleeds the sky's dark channel ~10 px into+ // the field, which is where the blotches formed+ let src = Rgb32f::from_fn(240, 160, |x, _| if x < 120 { [0.02, 0.03, 0.012] } else { [0.45, 0.5, 0.55] });+ let mut s = DevelopSettings::default();+ s.effects.dehaze = 80.0;+ let r = render(&src, &SourceInfo::default(), &s, &RenderRequest::fit(240, 160)).image;+ for ch in 0..3 {+ let mut v: Vec<u8> = Vec::with_capacity(160 * 100);+ for y in 0..160 {+ for x in 0..100 {+ v.push(r.get(x, y)[ch]);+ }+ }+ v.sort_unstable();+ let mid = v[v.len() / 2] as i32;+ for y in 0..160 {+ for x in 0..120 {+ let p = r.get(x, y)[ch] as i32;+ assert!(p >= mid - 2, "crushed at {x},{y} ch {ch}: {p} vs field level {mid}");+ }+ }+ }+}+#[test]fn resolution_independence_of_local_contrast() {// Clarity at two preview sizes should give similar results after downscaling.