From c3fa1c57de6b35824050d6ca16976dd2c040c0b3 Mon Sep 17 00:00:00 2001 From: Hector Villarreal Date: Sun, 15 Nov 2026 12:58:34 -0600 Subject: [PATCH] Merge to HDR Pro: Remove ghosts catches a moved object where frames are clipped (#14020) Each frame's value bounds the log radiance (one-sided where clipped or black); a pixel whose frame interval misses the base's by more than a factor of two in any channel is a ghost too, so a moved bright patch is taken from the base image even where no frame is well exposed. Unmoved areas and ghost-free sets are unchanged. --- crates/algo/src/hdr.rs | 28 +++++- crates/algo/src/hdr_ghost_clipped_tests.rs | 108 +++++++++++++++++++++ 2 files changed, 135 insertions(+), 1 deletion(-) create mode 100644 crates/algo/src/hdr_ghost_clipped_tests.rs diff --git a/crates/algo/src/hdr.rs b/crates/algo/src/hdr.rs index ddd9a8fc73..08119ad7a2 100644 --- a/crates/algo/src/hdr.rs +++ b/crates/algo/src/hdr.rs @@ -296,8 +296,30 @@ pub fn merge(w: usize, h: usize, imgs: &[&[[f32; 4]]], opts: &MergeOptions) -> M let m = s / sw; ((s2 / sw - m * m).max(0.0)).sqrt() as f32 }); + // A frame whose radiance can't agree with the base's is a ghost too, also where the frames + // are clipped or black and so carry no weight above (#14020): each value bounds the log + // radiance (an interval; one-sided where it is clipped), and a pixel is a ghost when some + // frame's interval misses the base's by more than a factor of two in any channel. + let clash: Vec = crate::photo_util::par_map(w * h, |i| { + let bounds = |j: usize, c: usize| -> (f64, f64) { + let v = imgs[j][i][c].clamp(0.0, 1.0); + let l = g_at(&response[c], v) - log_dt[j]; + match v { + _ if v >= 0.98 => (l, f64::INFINITY), + _ if v <= 0.02 => (f64::NEG_INFINITY, l), + _ => (l, l), + } + }; + (0..3).any(|c| { + let (blo, bhi) = bounds(base, c); + (0..p).filter(|&j| j != base).any(|j| { + let (lo, hi) = bounds(j, c); + lo - bhi > std::f64::consts::LN_2 || blo - hi > std::f64::consts::LN_2 + }) + }) + }); for i in 0..w * h { - ghost[i] = if score[i] > 0.35 { 1.0 } else { 0.0 }; + ghost[i] = if score[i] > 0.35 || clash[i] { 1.0 } else { 0.0 }; } // Grow and feather the mask. let r = (w.max(h) as f32 / 200.0).max(2.0); @@ -539,3 +561,7 @@ mod tests { } } } + +#[cfg(test)] +#[path = "hdr_ghost_clipped_tests.rs"] +mod hdr_ghost_clipped_tests; diff --git a/crates/algo/src/hdr_ghost_clipped_tests.rs b/crates/algo/src/hdr_ghost_clipped_tests.rs new file mode 100644 index 0000000000..0fc67da182 --- /dev/null +++ b/crates/algo/src/hdr_ghost_clipped_tests.rs @@ -0,0 +1,108 @@ +//! #14020: Remove ghosts catches a moved bright object even where its pixels are clipped or black +//! in every frame (so no frame is well exposed there), and leaves unmoved areas alone. + +use super::*; + +const W: usize = 300; +const H: usize = 200; + +/// The issue's scene in linear light: a grey ramp (y 0-99), a mid-grey band (y 100-119) and red, +/// green, blue, white and black 60 x 60 patches at y 120-179 (x 0/60/120/180/240), on mid grey. +/// `ghost`: the white patch moved 30 px right and no black patch. +fn scene(ghost: bool) -> Vec<[f32; 3]> { + let mut px = vec![[0.0f32; 3]; W * H]; + for y in 0..H { + for x in 0..W { + let s = |v: f32| srgb_to_linear(v); + let c = if y < 100 { + let v = s(((x / 10) as f32 * 10.0 / 299.0).min(1.0)); + [v, v, v] + } else if !(120..180).contains(&y) { + [s(0.5); 3] + } else { + let (white, black) = if ghost { (210..270, 300..300) } else { (180..240, 240..300) }; + match x { + _ if white.contains(&x) => [1.0, 1.0, 1.0], + _ if black.contains(&x) => [0.0, 0.0, 0.0], + 0..60 => [s(0.8), s(0.05), s(0.05)], + 60..120 => [s(0.05), s(0.8), s(0.05)], + 120..180 => [s(0.05), s(0.05), s(0.8)], + _ => [s(0.5); 3], + } + }; + px[y * W + x] = c; + } + } + px +} + +/// One frame made with Exposure `ev`: the scene times 2^ev in linear light, clipped, as 8-bit sRGB. +fn frame(scene: &[[f32; 3]], ev: f32) -> Vec<[f32; 4]> { + scene + .iter() + .map(|c| { + let e = |v: f32| (linear_to_srgb((v * 2f32.powf(ev)).clamp(0.0, 1.0)) * 255.0).round() / 255.0; + [e(c[0]), e(c[1]), e(c[2]), 1.0] + }) + .collect() +} + +/// The merge of frames EV -2, 0 and +2 (the +2 one moved when `ghost`), base EV -2. +fn run(ghost: bool, remove_ghosts: bool) -> Merged { + let still = scene(false); + let frames = [frame(&still, -2.0), frame(&still, 0.0), frame(&scene(ghost), 2.0)]; + let refs: Vec<&[[f32; 4]]> = frames.iter().map(|f| f.as_slice()).collect(); + let opts = MergeOptions { exposures: vec![0.25, 1.0, 4.0], remove_ghosts, ghost_base: Some(0), response: None }; + merge(W, H, &refs, &opts) +} + +/// A radiance pixel as 8-bit sRGB levels (the merge is scaled so the base keeps its values). +fn levels(q: [f32; 4]) -> [f32; 3] { + [0, 1, 2].map(|c| linear_to_srgb(q[c].clamp(0.0, 1.0)) * 255.0) +} + +fn diff(a: [f32; 4], b: [f32; 4]) -> f32 { + let (a, b) = (levels(a), levels(b)); + (0..3).map(|c| (a[c] - b[c]).abs()).fold(0.0, f32::max) +} + +fn moved(i: usize) -> bool { + let (x, y) = (i % W, i / W); + (120..180).contains(&y) && x >= 180 +} + +#[test] +fn the_moved_patch_comes_from_the_base_image() { + let on = run(true, true); + let off = run(true, false); + // What the merge gives when nothing moved: the patch where the base image has it. + let still = run(false, false); + let at = |x: usize, y: usize| y * W + x; + // Where the base has the black patch, and where the white patch was left behind. + for (x, y) in [(270, 150), (250, 150), (195, 150)] { + let i = at(x, y); + assert!(diff(on.px[i], still.px[i]) <= 10.0, "({x},{y}): {:?} vs {:?}", levels(on.px[i]), levels(still.px[i])); + assert!(diff(off.px[i], still.px[i]) > 10.0, "({x},{y}) was a ghost without the fix"); + } + // The white patch shows in one place only: almost the whole moved area matches the still merge. + let area: Vec = (0..W * H).filter(|&i| moved(i)).collect(); + let near = area.iter().filter(|&&i| diff(on.px[i], still.px[i]) <= 10.0).count(); + assert!(near * 100 >= area.len() * 95, "{near} of {}", area.len()); +} + +#[test] +fn unmoved_areas_are_left_alone() { + let on = run(true, true); + let off = run(true, false); + let worst = (0..W * H).filter(|&i| !moved(i) && (i % W < 170 || i / W < 110)).map(|i| diff(on.px[i], off.px[i])).fold(0.0, f32::max); + assert!(worst <= 2.0, "an unmoved pixel changed by {worst} levels"); +} + +#[test] +fn without_a_ghost_or_with_remove_ghosts_off_nothing_changes() { + let still_on = run(false, true); + let still_off = run(false, false); + let worst = (0..W * H).map(|i| diff(still_on.px[i], still_off.px[i])).fold(0.0, f32::max); + assert!(worst <= 2.0, "a ghost-free set changed by {worst} levels"); + assert_eq!(still_on.ghost_fraction, 0.0); +}