From a4fc3a714ee99724d132910492aa5d5828b4e073 Mon Sep 17 00:00:00 2001 From: SBaksa Date: Fri, 23 Oct 2026 09:58:18 +1100 Subject: [PATCH] Raw: scale floating-point samples by the highest white level, not the first WhiteLevel holds one value per sample plane, but the float scale came from the first only. A plane whose white level was higher than the first's was clipped before the DNG reader applied its own level. The scale now comes from the highest valid white level (default 1.0). --- crates/raw/src/sensor.rs | 9 ++++++--- crates/raw/tests/dng.rs | 13 +++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/crates/raw/src/sensor.rs b/crates/raw/src/sensor.rs index 9c2aaf55cc..4894e232ce 100644 --- a/crates/raw/src/sensor.rs +++ b/crates/raw/src/sensor.rs @@ -250,9 +250,12 @@ pub(crate) fn read_plane(t: &Tiff, ifd: &Ifd, limits: &Limits, layout: JpegLayou return Err(RawError::unsupported(format!("{bits}-bit floating-point raw samples"))); } let bits = if float { 16 } else { bits }; - // The floats' white level (default 1.0, DNG 1.7 WhiteLevel) becomes 65535. - let float_scale = - float.then(|| 65535.0 / t.tag_floats(ifd, tag::WHITE_LEVEL).first().map(|v| *v as f32).filter(|v| v.is_finite() && *v > 0.0).unwrap_or(1.0)); + // The floats' highest white level (default 1.0; DNG 1.7 WhiteLevel has one per sample plane) + // becomes 65535, so no plane is clipped below its own white level. + let float_scale = float.then(|| { + let white = t.tag_floats(ifd, tag::WHITE_LEVEL).iter().map(|v| *v as f32).filter(|v| v.is_finite() && *v > 0.0).fold(0.0f32, f32::max); + 65535.0 / if white > 0.0 { white } else { 1.0 } + }); if !(1..=16).contains(&bits) { return Err(RawError::unsupported(format!("{bits}-bit raw samples"))); } diff --git a/crates/raw/tests/dng.rs b/crates/raw/tests/dng.rs index c336c97484..f3d40dd9c9 100644 --- a/crates/raw/tests/dng.rs +++ b/crates/raw/tests/dng.rs @@ -736,6 +736,19 @@ fn floating_point_raw_data_that_isnt_jpeg_xl_is_unsupported() { } } +/// WhiteLevel has one value per sample plane: the floats are scaled by the highest, so a plane +/// with a higher white level than the first keeps its highlights. +#[test] +fn floating_point_white_levels_per_plane_scale_from_the_highest() { + let (w, h) = (24, 16); + let data = noise(w * h * 3, 16); + let mut spec = float_jxl_dng(&data, w, h, None); + spec.raw_extra.push((50717, Val::Double(vec![0.5, 1.0, 0.5]))); + let s = sensor(&spec.build()); + assert_eq!(s.data, data, "scaled by 1.0, the highest: nothing clipped"); + assert_eq!(s.white, [32767.5, 65535.0, 32767.5]); +} + /// Floating-point samples are read into four bytes each before they become 16-bit: the limits /// are charged for that buffer, not for the two-byte result. #[test]