LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust
Merge PR #699: LR-PROF-CAMERACOLOR: fit the tone curve of low-contrast scenes (range limit 1.5 to 1.2)
aionggcf804ee9f63a
diff --cc crates/engine/src/camera_preview.rsindex e56e80305,3eeba8c8e..8dbc96c96--- a/crates/engine/src/camera_preview.rs+++ b/crates/engine/src/camera_preview.rs@@@ -34,8 -34,8 +34,9 @@@ use lightcraft_raw::/// 7: the sensor proxy follows the file's lens warp when the camera JPEG is lens-corrected./// 8: the bundled NIKON Z 6_2 profile./// 9: the bundled NIKON Z 8 and NIKON D5300 profiles.+/// 10: DNG tone fit for profiles with a look table and for 1024 px (Lightroom) previews.- pub const LOOK_VERSION: u32 = 10;+ /// 12: tone curves of low-contrast scenes (range limit 1.2, with the bright-area check for curves under 1.5, #699).+ pub const LOOK_VERSION: u32 = 12;#[derive(Clone, Debug)]pub(crate) struct CameraLook {@@@ -3982,15 -4128,183 +4129,192 @@@ mod testsassert!(curved.profile.tone_curve.is_some() && !eligible(RawFormat::Dng, curved), "the file's own tone curve is kept");let mut looked = color;looked.profile.look_table = HsvTable::from_tags(&[1, 2, 1], &[0.0, 1.0, 1.0, 0.0, 1.0, 1.0], 0);- assert!(looked.profile.look_table.is_some() && !eligible(RawFormat::Dng, looked), "a look table is the file's own rendering");+ assert!(looked.profile.look_table.is_some() && eligible(RawFormat::Dng, looked), "a look table defines colour, not tone");+ }++ /// The 1024 px preview Lightroom Classic writes into a 7008 px DNG is fitted; a 256 px thumbnail is not.+ #[test]+ fn dng_tone_fit_takes_lightroom_sized_previews() {+ let floor = |raw_long: f64| (MIN_PREVIEW_FRACTION * raw_long).max(384.0);+ assert!(1024.0 >= floor(7008.0));+ assert!(256.0 < floor(7008.0));+ assert!(1024.0 >= floor(9504.0), "a 61 MP raw too");}++ /// Pairs whose scene luminance spans `ratio` from the darkest to the brightest pixel, mapped+ /// to the JPEG through a steep camera curve (a hazy scene the camera stretches).+ fn low_contrast_pairs(ratio: f64) -> Vec<(f64, f64)> {+ (0..4096).map(|i| 0.08 * ratio.powf(i as f64 / 4095.0)).map(|x| (x, (0.2 + (x - 0.08) * 3.0).min(0.95))).collect()+ }++ /// The ratio the tone fits test: scene luminance at the middle of the brightest 1/32 over that+ /// of the darkest 1/32 (their last and first knots).+ fn knot_range(mut scene: Vec<f64>) -> f64 {+ scene.sort_by(f64::total_cmp);+ let n = scene.len();+ scene[31 * n / 32 + n / 64] / scene[n / 64]+ }++ /// Issue #631: a low-contrast scene (its darker and brighter parts under 1.5× apart) still gets+ /// a tone curve; only a nearly uniform one doesn't. Both tone fits use the same limit.+ #[test]+ fn tone_fits_accept_low_contrast_scenes() {+ for fit in [fit_tone, fit_tone_conditional] {+ let pairs = low_contrast_pairs(1.47);+ let range = knot_range(pairs.iter().map(|p| p.0).collect());+ assert!((1.4..1.5).contains(&range), "the scene sits just below the old limit of 1.5: {range}");+ let curve = fit(pairs).expect("a low-contrast scene, as in the burst of issue #631");+ assert!((curve.apply(0.1) - 0.26).abs() < 0.01, "follows the camera curve: {}", curve.apply(0.1));+ assert!(fit(low_contrast_pairs(1.1)).is_none(), "a nearly uniform frame");+ }+ }++ /// The same scene end to end: colour varies, luminance spans under 1.5×, and the camera+ /// stretches it; the look is accepted and lands on the JPEG instead of the neutral fallback.+ #[test]+ fn a_low_contrast_scene_gets_a_camera_look() {+ let known = Mat3([[1.8, -0.4, -0.1], [-0.2, 1.5, -0.1], [-0.05, -0.3, 1.7]]);+ let mut sensor = Rgb32f::new(96, 64);+ let mut reference = sensor.clone();+ for (i, (src, dst)) in sensor.data.iter_mut().zip(&mut reference.data).enumerate() {+ let ev = 0.06 + (i % 31) as f32 * 0.0008;+ *src = [ev * (0.8 + (i % 11) as f32 * 0.025), ev, ev * (0.8 + (i % 17) as f32 * 0.014)];+ let p = known.apply_f32(*src);+ let y = luminance_2020(p);+ *dst = p.map(|v| v * (0.2 + (y - 0.07) * 3.0) / y);+ }+ let look = fit_pairs(&sensor, &reference).expect("a camera look, not the neutral fallback");+ let range = knot_range(sensor.data.iter().map(|x| luma(look.matrix.apply(x.map(f64::from)))).collect());+ assert!(range < 1.5, "the scene sits below the old limit of 1.5: {range}");+ let tone = ToneMap::camera(&look.tone, 0.0, 0.0, 0.0);+ let error: f64 = sensor+ .data+ .iter()+ .zip(&reference.data)+ .map(|(x, y)| {+ let p = displayed(look.matrix.apply(x.map(f64::from)), &tone);+ (0..3).map(|c| (p[c] - f64::from(y[c])).powi(2)).sum::<f64>() / 3.0+ })+ .sum::<f64>()+ / sensor.data.len() as f64;+ assert!(error.sqrt() < 0.02, "{}", error.sqrt());+ }++ /// Codex review of #631: a low-contrast scene's curve is fitted to its midtones only, and the+ /// camera JPEG's bright areas (luminance 0.85 and up) are in neither the fit nor the held-out+ /// gates. With 1.2 alone, this frame's curve passed the gates and rendered its bright sky+ /// (camera JPEG 0.93) at about 0.28. The second review found the same through the retries'+ /// filters: a clipped sky (left out by the refit without clipped pixels), scattered bright+ /// pixels (left out away from edges) and sensor values over 1.5 (out of the pairs' range).+ #[test]+ fn a_narrow_curve_that_darkens_the_bright_areas_is_refused() {+ let known = Mat3([[1.8, -0.4, -0.1], [-0.2, 1.5, -0.1], [-0.05, -0.3, 1.7]]);+ for (case, sky_value, is_sky) in [+ ("bright sky", 0.9, &(|i: usize| i < 5600) as &dyn Fn(usize) -> bool),+ ("clipped sky", 1.0, &|i: usize| i < 5600),+ ("scattered bright pixels", 0.9, &|i: usize| i.is_multiple_of(8)),+ ("sky over the pairs' range", 1.6, &|i: usize| i < 5600),+ ] {+ let mut sensor = Rgb32f::new(96, 64);+ let mut reference = sensor.clone();+ let mut clipped = vec![false; sensor.data.len()];+ for (i, (src, dst)) in sensor.data.iter_mut().zip(&mut reference.data).enumerate() {+ if is_sky(i) {+ // bright, nearly white in the camera JPEG+ *src = [sky_value; 3];+ *dst = [0.93; 3];+ clipped[i] = case == "clipped sky";+ continue;+ }+ let ev = 0.06 + (i % 31) as f32 * 0.0008;+ *src = [ev * (0.8 + (i % 11) as f32 * 0.025), ev, ev * (0.8 + (i % 17) as f32 * 0.014)];+ let p = known.apply_f32(*src);+ let y = luminance_2020(p);+ // nearly flat midtones: the fitted curve's extrapolation can't reach the sky+ *dst = p.map(|v| v * (0.2 + (y - 0.07) * 0.05) / y);+ }+ let Some(look) = fit_look(&sensor, &reference, &clipped, None) else { continue };+ let tone = ToneMap::camera(&look.tone, 0.0, 0.0, 0.0);+ let correction = look.hue_sat.as_ref().and_then(HueSat::new);+ let p = look.matrix.apply([f64::from(sky_value); 3]);+ let p = correction.as_ref().map_or(p, |c| c.apply(p.map(|v| v as f32)).map(f64::from));+ let sky = luma(displayed(p, &tone));+ assert!(sky > 0.8, "{case}: an accepted look must not darken what the camera renders at 0.93: {sky}");+ }+ }++ /// Third Codex review of #631: for a Sony photo shot with DRO, the fitted curve is lowered to+ /// Sony's without DRO after the fit, so it must be checked as lowered. On this frame the+ /// fitted curve keeps the sky (0.81 against the fallback's 0.79), and lowered it rendered the+ /// sky at 0.61 where the camera JPEG has 0.93.+ #[test]+ fn a_narrow_curve_is_checked_as_lowered_for_dro() {+ let known = Mat3([[1.8, -0.4, -0.1], [-0.2, 1.5, -0.1], [-0.05, -0.3, 1.7]]);+ let mut sensor = Rgb32f::new(96, 64);+ let mut reference = sensor.clone();+ for (i, (src, dst)) in sensor.data.iter_mut().zip(&mut reference.data).enumerate() {+ if i < 5600 {+ *src = [0.9; 3];+ *dst = [0.93; 3];+ continue;+ }+ let ev = 0.06 + (i % 31) as f32 * 0.0008;+ *src = [ev * (0.8 + (i % 11) as f32 * 0.025), ev, ev * (0.8 + (i % 17) as f32 * 0.014)];+ let p = known.apply_f32(*src);+ let y = luminance_2020(p);+ *dst = p.map(|v| v * (0.6 + (y - 0.07) * 0.25) / y);+ }+ let neutral = luma(displayed([0.9; 3], &ToneMap::new(0.0, 0.0, 0.0)));+ if let Some(look) = fit_look_with(&sensor, &reference, &vec![false; sensor.data.len()], None, true) {+ let look = with_dro_off_tone(look);+ let tone = ToneMap::camera(&look.tone, 0.0, 0.0, 0.0);+ let correction = look.hue_sat.as_ref().and_then(HueSat::new);+ let p = look.matrix.apply([0.9; 3]);+ let p = correction.as_ref().map_or(p, |c| c.apply(p.map(|v| v as f32)).map(f64::from));+ let sky = luma(displayed(p, &tone));+ assert!(sky > neutral - 0.02, "the DRO-lowered curve must not darken the sky past the fallback ({neutral}): {sky}");+ }+ }++ /// A black-and-white JPEG of a low-contrast scene (issues #577 and #631): grey pixels, scene luminance spanning+ /// under 1.5×, which the old range limit refused before any gate. Its tone curve is fitted.+ #[test]+ fn a_low_contrast_black_and_white_jpeg_gives_its_tone_curve() {+ let mut sensor = Rgb32f::new(96, 64);+ let mut reference = sensor.clone();+ for (i, (src, dst)) in sensor.data.iter_mut().zip(&mut reference.data).enumerate() {+ let ev = 0.06 + (i % 31) as f32 * 0.0008;+ *src = [ev * (0.8 + (i % 11) as f32 * 0.025), ev, ev * (0.8 + (i % 17) as f32 * 0.014)];+ *dst = [0.2 + (luminance_2020(*src) - 0.06) * 3.0; 3];+ }+ let range = knot_range(sensor.data.iter().map(|x| luma(x.map(f64::from))).collect());+ assert!(range < 1.5, "the scene sits below the old limit of 1.5: {range}");+ let look = fit_tone_only(&sensor, &reference, None).expect("the JPEG's tone curve");+ let tone = ToneMap::camera(&look.tone, 0.0, 0.0, 0.0);+ let error = sensor.data.iter().zip(&reference.data).map(|(x, y)| (luma(displayed(x.map(f64::from), &tone)) - f64::from(y[0])).powi(2));+ let rms = (error.sum::<f64>() / sensor.data.len() as f64).sqrt();+ assert!(rms < 0.02, "{rms}");+ }++ /// [`a_narrow_curve_that_darkens_the_bright_areas_is_refused`] for a black-and-white JPEG: its tone curve is+ /// fitted to nearly flat midtones under 1.5×, and the bright sky (camera JPEG 0.93) is left out of the fit.+ #[test]+ fn a_narrow_black_and_white_curve_that_darkens_the_bright_areas_is_refused() {+ let mut sensor = Rgb32f::new(96, 64);+ let mut reference = sensor.clone();+ for (i, (src, dst)) in sensor.data.iter_mut().zip(&mut reference.data).enumerate() {+ if i < 5600 {+ *src = [0.9; 3];+ *dst = [0.93; 3];+ continue;+ }+ let ev = 0.06 + (i % 31) as f32 * 0.0008;+ *src = [ev * (0.8 + (i % 11) as f32 * 0.025), ev, ev * (0.8 + (i % 17) as f32 * 0.014)];+ *dst = [0.2 + (luminance_2020(*src) - 0.06) * 0.05; 3];+ }+ if let Some(look) = fit_tone_only(&sensor, &reference, None) {+ let sky = luma(displayed(look.matrix.apply([0.9; 3]), &ToneMap::camera(&look.tone, 0.0, 0.0, 0.0)));+ assert!(sky > 0.8, "an accepted curve must not darken what the camera renders at 0.93: {sky}");+ }+ }}