From 5e584d4bc6453c606dd0623bab7ea3690e9323cf Mon Sep 17 00:00:00 2001 From: robmdunn <1900604+robmdunn@users.noreply.github.com> Date: Sun, 1 Nov 2026 14:44:52 -0500 Subject: [PATCH] LR-PROF-CAMERACOLOR: Upright analyses the source with negative channels clipped Raw decoding now keeps negative channels. Upright's analysis image resampled them as they are, which lowered the luminance its segments are found in, so it found a different correction than for the same photo before (the GX85 night frames with PerspectiveUpright=3 that radialmonster measured on #2278). analysis_plane clips a copy of the source at zero when it has negative channels, so the analysis is the one it was; sources without them take the same path as before. --- crates/pipeline/src/tests_geometry.rs | 22 ++++++++++++++++++++++ crates/pipeline/src/upright.rs | 10 ++++++++++ 2 files changed, 32 insertions(+) diff --git a/crates/pipeline/src/tests_geometry.rs b/crates/pipeline/src/tests_geometry.rs index d88b6cf5dd..b43f79004f 100644 --- a/crates/pipeline/src/tests_geometry.rs +++ b/crates/pipeline/src/tests_geometry.rs @@ -432,6 +432,28 @@ fn upright_analysis_is_resolution_independent() { assert_eq!(lazy.data, stored.data); } +/// Raw decoding keeps negative channels; Upright analyses a dark noisy frame as it did when decoding clipped them at +/// zero (resampled as they are, they lowered the luminance and changed the correction found). +#[test] +fn upright_analysis_clips_negative_channels_first() { + let shot = lightcraft_develop::Geometry { vertical: 30.0, rotate: 1.5, ..Default::default() }; + let grid = keystoned_grid(1200, 800, crate::transform::manual(&shot), 80.0); + // ±0.04 of noise per channel, a hash of the position + let noise = |i: usize, c: usize| { + let h = ((i * 7919 + c * 1_299_709) as u32).wrapping_mul(2_654_435_761); + (h >> 8) as f32 / (1u32 << 24) as f32 * 0.08 - 0.04 + }; + let mut signed = grid.clone(); + for (i, p) in signed.data.iter_mut().enumerate() { + *p = std::array::from_fn(|c| p[c] * 0.02 + noise(i, c)); + } + let clipped = signed.map(|p| p.map(|v| v.max(0.0))); + assert!(signed.data.iter().flatten().filter(|v| **v < 0.0).count() > signed.data.len() / 4, "the frame has few negatives"); + let (info, s) = (SourceInfo::default(), DevelopSettings::default()); + let (a, b) = (crate::upright::analysis_plane(&signed, &info, &s), crate::upright::analysis_plane(&clipped, &info, &s)); + assert!(a.data == b.data, "negative channels change Upright's analysis image"); +} + /// Regression: Upright Auto must not invent perspective in natural scenes (conical trees, ridgelines, a level /// water line) — the demo library has no buildings, so Auto should stay close to identity everywhere. #[test] diff --git a/crates/pipeline/src/upright.rs b/crates/pipeline/src/upright.rs index a77369eb4b..69e023dc84 100644 --- a/crates/pipeline/src/upright.rs +++ b/crates/pipeline/src/upright.rs @@ -579,6 +579,16 @@ pub(crate) fn analysis_key(src: &Rgb32f, info: &SourceInfo, s: &DevelopSettings) /// The image an analysis reads: the lens-corrected source (optics applied, no perspective or crop) /// at [`ANALYSIS_LONG`] px, as perceptual lightness normalized to its bright end. pub(crate) fn analysis_plane(src: &Rgb32f, info: &SourceInfo, s: &DevelopSettings) -> Plane { + // Raw decoding keeps negative channels (noise near black through the camera matrix). Resampled as they are, + // they lower the luminance the segments are found in, so Upright would find a different correction than for + // the same photo decoded with its channels clipped at zero; it analyses that clipped source, as it always has. + let clipped; + let src = if src.data.iter().flatten().any(|v| *v < 0.0) { + clipped = src.map(|p| p.map(|v| v.max(0.0))); + &clipped + } else { + src + }; let mut base = s.clone(); base.geometry = Geometry::default(); base.crop = Crop::default();