Repository navigation
Dehaze: process V3 with a patch dark channel, eased response and no black halos #1283
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
6b7b686
62acd27
c77afaf
f4694ee
7033afb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -243,6 +243,69 @@ fn median_row(src: &[f32], out: &mut [f32], r: usize) { | |
| } | ||
| } | ||
|
|
||
| /// Separable minimum (`2r+1` px along each row, then each column; clamped edges): a square | ||
| /// erosion. Dehaze's patch dark channel from process V3 (#1237). | ||
| pub fn min_hv(p: &Plane, r: usize) -> Plane { | ||
| if r == 0 || p.data.is_empty() { | ||
| return p.clone(); | ||
| } | ||
| let rows = |src: &Plane| { | ||
| let mut out = Plane::new(src.width, src.height); | ||
| for_rows(&mut out.data, src.width, |y, row| min_row(src.row(y), row, r)); | ||
| out | ||
| }; | ||
| transpose(&rows(&transpose(&rows(p)))) | ||
| } | ||
|
|
||
| /// Minimum of `src` over `2r+1` taps (clamped to the row) into `out`, in O(1) per pixel (van Herk / | ||
| /// Gil–Werman: block-wise running minima from both ends). | ||
| fn min_row(src: &[f32], out: &mut [f32], r: usize) { | ||
| let n = src.len(); | ||
| let k = 2 * r + 1; | ||
| // padded with +inf: r before, enough after to complete the last block | ||
| let len = (n + 2 * r).div_ceil(k) * k; | ||
| let at = |i: usize| if i >= r { src.get(i - r).copied().unwrap_or(f32::INFINITY) } else { f32::INFINITY }; | ||
| let mut fwd = vec![f32::INFINITY; len]; | ||
| let mut bwd = vec![f32::INFINITY; len]; | ||
| for i in 0..len { | ||
| let v = at(i); | ||
| fwd[i] = if i % k == 0 { v } else { v.min(fwd[i - 1]) }; | ||
| } | ||
| for i in (0..len).rev() { | ||
| let v = at(i); | ||
| bwd[i] = if i % k == k - 1 || i + 1 == len { v } else { v.min(bwd[i + 1]) }; | ||
| } | ||
| for (x, o) in out.iter_mut().enumerate().take(n) { | ||
| // window [x, x + 2r] in padded coordinates | ||
| *o = bwd.get(x).copied().unwrap_or(f32::INFINITY).min(fwd.get(x + 2 * r).copied().unwrap_or(f32::INFINITY)); | ||
| } | ||
| } | ||
|
|
||
| /// Patch radius of dehaze's dark channel from process V3, as a fraction of the long edge. | ||
| pub const DEHAZE_PATCH: f64 = 0.005; | ||
| /// Largest [`dehaze_patch_radius`] (px). | ||
| pub const DEHAZE_PATCH_MAX: usize = 64; | ||
|
|
||
| /// Patch (minimum) radius of the dehaze dark channel of `s`'s process at `px_per_long`: 0 (the | ||
| /// per-pixel minimum) before V3. | ||
| pub fn dehaze_patch_radius(s: &DevelopSettings, px_per_long: f64) -> usize { | ||
| match s.process.process() { | ||
| Process::V1 | Process::V2 => 0, | ||
| Process::V3 => ((DEHAZE_PATCH * px_per_long).round().max(1.0) as usize).min(DEHAZE_PATCH_MAX), | ||
| } | ||
| } | ||
|
|
||
| /// Cache key of the dark-channel plane: its blur radius and patch radius. | ||
| pub fn dark_key(sigma: f32, patch: usize) -> u64 { | ||
| u64::from(sigma.to_bits()) | ((patch as u64) << 32) | ||
| } | ||
|
|
||
| /// The dehaze dark channel: the Gaussian (`sigma`) of the `patch`-px minimum of each pixel's | ||
| /// darkest channel. | ||
| pub fn dark_plane(img: &Rgb32f, sigma: f32, patch: usize) -> Plane { | ||
| gaussian(&min_hv(&img.map(dark_of), patch), sigma) | ||
| } | ||
|
|
||
| fn transpose(p: &Plane) -> Plane { | ||
| let (w, h) = (p.width, p.height); | ||
| let mut t = Plane::new(h, w); | ||
|
|
@@ -273,7 +336,7 @@ pub fn base_detail_radius(px_per_long: f64) -> usize { | |
| pub fn tone_base_detail(s: &DevelopSettings, px_per_long: f64) -> usize { | ||
| match s.process.process() { | ||
| Process::V1 => 0, | ||
| Process::V2 => base_detail_radius(px_per_long), | ||
| Process::V2 | Process::V3 => base_detail_radius(px_per_long), | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -420,7 +483,7 @@ pub(crate) struct Planes { | |
| pub clarity: Option<(u32, Arc<Plane>)>, | ||
| pub texture: Option<(u32, Arc<Plane>)>, | ||
| /// Dark channel and its airlight. | ||
| pub dark: Option<(u32, Arc<Plane>, f32)>, | ||
| pub dark: Option<(u64, Arc<Plane>, f32)>, | ||
| /// Blurred chromaticity (local Moiré / Noise). | ||
| pub chroma: Option<(u32, Arc<Rgb32f>)>, | ||
| } | ||
|
|
@@ -484,6 +547,8 @@ pub struct PlaneSigmas { | |
| pub texture: Option<f32>, | ||
| /// Dehaze dark channel (Gaussian). | ||
| pub dark: Option<f32>, | ||
| /// Patch radius (px) of that dark channel ([`dehaze_patch_radius`]: 0 before V3). | ||
| pub dark_patch: usize, | ||
| /// Chromaticity blur for local Moiré / colour noise (Gaussian). | ||
| pub chroma: Option<f32>, | ||
| } | ||
|
|
@@ -512,7 +577,8 @@ pub fn plane_sigmas(s: &DevelopSettings, px_per_long: f64, q: Quality) -> PlaneS | |
| let dark = (s.effects.dehaze != 0.0 || local_any(|a| a.dehaze)).then(|| (0.02 * ppl).max(1.0)); | ||
| let chroma = (local_any(|a| a.moire) || s.masks.iter().any(|m| m.adjust.noise > 0.0)).then(|| (CHROMA_SIGMA * ppl).max(1.0)); | ||
| let base_detail = if base.is_some() { tone_base_detail(s, px_per_long) } else { 0 }; | ||
| PlaneSigmas { base, base_detail, clarity, texture, dark, chroma } | ||
| let dark_patch = if dark.is_some() { dehaze_patch_radius(s, px_per_long) } else { 0 }; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟠 Major | 🏗️ Heavy lift 🔎 Supported by static analysis🏁 Script executed: sed -n '440,500p' crates/pipeline/src/finish.rs
sed -n '315,350p' crates/gpu/src/wgsl/finish.wgsl
sed -n '284,310p;560,670p' crates/pipeline/src/local.rs
sed -n '1,30p' docs/process-versions.mdRepository: storytold/lightcraft Length of output: 13872 🏁 Script executed: set -o pipefail
for spec in \
'crates/pipeline/src/local.rs:560,665' \
'crates/pipeline/src/local.rs:250,315' \
'crates/pipeline/src/finish.rs:468,490' \
'crates/gpu/src/wgsl/finish.wgsl:315,345' \
'docs/process-versions.md:1,22'; do
file=${spec%%:*}; range=${spec#*:}; start=${range%,*}; end=${range#*,}
echo "===== $file:$start-$end ====="
nl -ba "$file" | sed -n "${start},${end}p"
done
echo "===== dehaze accumulation and local adjustment references ====="
rg -n -F --glob '*.rs' -e 'l_dehaze' -e 'dehaze + l_dehaze' -e 'local_any(|a| a.dehaze)' crates/pipeline/src crates/develop/src || test "$?" -eq 1Repository: storytold/lightcraft Length of output: 14984 🏁 Script executed: set -o pipefail
echo '===== CPU finish air bindings ====='
rg -n -F --glob '*.rs' -e 'fn finish' -e 'air_pre' -e 'p.air' -e 'Prepared' crates/pipeline/src/finish.rs crates/pipeline/src/*.rs || test "$?" -eq 1
echo '===== CPU finish surrounding declaration/call ====='
nl -ba crates/pipeline/src/finish.rs | sed -n '250,330p'
nl -ba crates/pipeline/src/finish.rs | sed -n '400,455p'Repository: storytold/lightcraft Length of output: 11026 Preserve V1 dark-plane inputs for negative V3 Dehaze. The negative formula reads both Use the V1 plane for negative-only settings. If local adjustments can make 🤖 Prompt for AI Agents |
||
| PlaneSigmas { base, base_detail, clarity, texture, dark, dark_patch, chroma } | ||
| } | ||
|
|
||
| /// The spatial planes the per-pixel stage needs for `img` (white-balanced, before exposure), | ||
|
|
@@ -529,8 +595,15 @@ pub(crate) fn prepare( | |
| mattes: Option<&masks::Mattes>, | ||
| ) -> Prepared { | ||
| let log_l = planes.log_l.get_or_insert_with(|| Arc::new(timed("log_l", || img.map(log_lum)))).clone(); | ||
| let PlaneSigmas { base: base_sigma, base_detail, clarity: clarity_sigma, texture: texture_sigma, dark: dark_sigma, chroma: chroma_sigma } = | ||
| plane_sigmas(s, px_per_long, q); | ||
| let PlaneSigmas { | ||
| base: base_sigma, | ||
| base_detail, | ||
| clarity: clarity_sigma, | ||
| texture: texture_sigma, | ||
| dark: dark_sigma, | ||
| dark_patch, | ||
| chroma: chroma_sigma, | ||
| } = plane_sigmas(s, px_per_long, q); | ||
|
|
||
| let Planes { base: sb, clarity: sc, texture: st, dark: sd, chroma: sch, .. } = planes; | ||
| let chroma_blur = chroma_sigma.map(|sg| match sch { | ||
|
|
@@ -552,11 +625,11 @@ pub(crate) fn prepare( | |
| || texture_sigma.map(|sg| plane_at(st, sg.to_bits(), || timed("texture", || gaussian(l, sg)))), | ||
| || { | ||
| dark_sigma.map(|sg| match sd { | ||
| Some((k, d, air)) if *k == sg.to_bits() => (d.clone(), *air), | ||
| Some((k, d, air)) if *k == dark_key(sg, dark_patch) => (d.clone(), *air), | ||
| _ => { | ||
| let d = timed("dehaze", || Arc::new(gaussian(&img.map(dark_of), sg))); | ||
| let d = timed("dehaze", || Arc::new(dark_plane(&img, sg, dark_patch))); | ||
| let air = airlight(&d); | ||
| *sd = Some((sg.to_bits(), d.clone(), air)); | ||
| *sd = Some((dark_key(sg, dark_patch), d.clone(), air)); | ||
| (d, air) | ||
| } | ||
| }) | ||
|
|
@@ -581,8 +654,9 @@ pub(crate) fn prepare( | |
| pub(crate) fn frame_airlight(src: &Rgb32f, info: &SourceInfo, s: &DevelopSettings, frame: &Frame, proxy_w: usize, proxy_h: usize) -> f32 { | ||
| let mut img = Rgb32f::clone(&frame_proxy(src, frame, proxy_w, proxy_h)); | ||
| white_balance(&mut img, info, s); | ||
| let sigma = (0.02 * frame.px_per_long(proxy_w)).max(1.0) as f32; | ||
| airlight(&gaussian(&img.map(dark_of), sigma)) | ||
| let ppl = frame.px_per_long(proxy_w); | ||
| let sigma = (0.02 * ppl).max(1.0) as f32; | ||
| airlight(&dark_plane(&img, sigma, dehaze_patch_radius(s, ppl))) | ||
| } | ||
|
|
||
| /// How many airlight proxies [`frame_proxy`] keeps (one per photo and frame being zoomed into; each ≤ 1.8 MB). | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Script executed:
Repository: storytold/lightcraft
Length of output: 17290
Script executed:
Repository: storytold/lightcraft
Length of output: 14472
Script executed:
Repository: storytold/lightcraft
Length of output: 6813
Script executed:
Repository: storytold/lightcraft
Length of output: 16873
Include full dark-plane filter context in windowed renders.
When positive V3 Dehaze renders a window, provide enough source context for both
min_hvand the subsequentgaussianbefore cropping the result to the requested pixels. Without this context, pixels near a window edge can use a different dark-plane value from the full render, so zoomed or tiled output can differ at window boundaries.