From d355ed8310fe43b2875637d6f17e12b88bf8ce2f Mon Sep 17 00:00:00 2001 From: qvest-ssels <214787280+qvest-ssels@users.noreply.github.com> Date: Mon, 23 Nov 2026 10:43:38 +0100 Subject: [PATCH] Dodge: keep today's curve at unmeasured 32-bit exposures Issue #12687 measured the reference's 32-bit Dodge at Exposure 50 % only and asks that unmeasured settings keep today's behaviour, except that values above 1.0 are not clipped. The linear Midtones curve now runs only at 50 %; other exposures, ranges, Protect Tones off and Grayscale keep the gamma curve, and in 32-bit documents Protect Tones scales a colour with light above white to its new luma without gamut compression, so it is no longer pulled back to 1.0. --- crates/engine/src/cmd/retouch_cmds/dab.rs | 31 ++++++++-- .../retouch_cmds/dodge_32bit_linear_tests.rs | 56 +++++++++++++++++-- 2 files changed, 78 insertions(+), 9 deletions(-) diff --git a/crates/engine/src/cmd/retouch_cmds/dab.rs b/crates/engine/src/cmd/retouch_cmds/dab.rs index dbe460b14fd..096ab81aa7e 100644 --- a/crates/engine/src/cmd/retouch_cmds/dab.rs +++ b/crates/engine/src/cmd/retouch_cmds/dab.rs @@ -4,7 +4,7 @@ use super::{ Region, Result, Session, Smudge, Stroke, StrokePoint, Surface, ToneRange, Value, alpha_index, apply_dab_stroke, bad, damage_json, dodge_burn, flag, from_rgba_into, has_pixel_layer, json, local_blur, local_sharpen, num, parse_blend, parse_brush, run_stroke, sponge, string, to_rgba, }; -use photocraft_algo::retouch::sponge_rgb; +use photocraft_algo::retouch::{luma, sponge_rgb, tone_curve}; use photocraft_color::{ColorMode, SampleType}; // --------------------------------------------------------------------------------------------- @@ -203,6 +203,23 @@ fn linear_midtones(v: f32, e: f32, burn: bool) -> f32 { if burn { v - d } else { v + d } } +/// The 32-bit reference was measured at Exposure 50 % only (#12687); other exposures keep the +/// gamma curve. +const MEASURED_LINEAR_EXPOSURE: f32 = 0.5; + +/// Dodge or Burn of a 32-bit colour with the curve the other bit depths use, except that light above +/// white isn't clipped: Protect Tones scales such a colour to its new luma without gamut +/// compression, which would pull it back to 1.0 (#12687). Other colours get today's result. +#[inline] +fn dodge_burn_float(c: [f32; 3], e: f32, range: ToneRange, burn: bool, protect: bool) -> [f32; 3] { + let l = luma(c); + if protect && c.iter().any(|&v| v > 1.0) && l > 1e-6 { + let nl = tone_curve(l, e, range, burn); + return c.map(|v| v * nl / l); + } + dodge_burn(c, e, range, burn, protect) +} + fn tone_range(p: &Value, cmd: &str) -> Result { match string(p, "range", "midtones") { "shadows" => Ok(ToneRange::Shadows), @@ -324,11 +341,17 @@ impl DabTool { let sp = spacing; match self.clone() { // A 32-bit RGB document holds linear values, which the reference's default Dodge tones - // with its own curve. Grayscale, the other ranges and Protect Tones off weren't measured - // there and keep the gamma curve. - Self::Tone { burn: false, range: ToneRange::Midtones, exposure, protect: true } if fmt.sample == SampleType::F32 && fmt.mode == ColorMode::Rgb => { + // with its own curve. Grayscale, the other ranges, Protect Tones off and the other + // exposures weren't measured there and keep the gamma curve, without clipping light + // above white. + Self::Tone { burn: false, range: ToneRange::Midtones, exposure, protect: true } + if fmt.sample == SampleType::F32 && fmt.mode == ColorMode::Rgb && (exposure - MEASURED_LINEAR_EXPOSURE).abs() < 1e-4 => + { tone_stroke(fmt, move |c| c.map(|v| linear_midtones(v, exposure, false))) } + Self::Tone { burn, range, exposure, protect } if fmt.sample == SampleType::F32 => { + tone_stroke(fmt, move |c| dodge_burn_float(c, exposure, range, burn, protect)) + } Self::Tone { burn, range, exposure, protect } => tone_stroke(fmt, move |c| dodge_burn(c, exposure, range, burn, protect)), // Flow is already in the dab's coverage. The reference RGB response applies to integer // RGB surfaces, including 16-bit. Float RGB and the other models use the legacy curve. diff --git a/crates/engine/src/cmd/retouch_cmds/dodge_32bit_linear_tests.rs b/crates/engine/src/cmd/retouch_cmds/dodge_32bit_linear_tests.rs index 72a1b2698b5..59b5ab3d9ed 100644 --- a/crates/engine/src/cmd/retouch_cmds/dodge_32bit_linear_tests.rs +++ b/crates/engine/src/cmd/retouch_cmds/dodge_32bit_linear_tests.rs @@ -2,6 +2,7 @@ //! (#12687); 8- and 16-bit documents keep their results. use crate::Session; +use photocraft_algo::retouch::{ToneRange, dodge_burn}; use photocraft_color::convert::linear_to_srgb; use serde_json::{Value, json}; @@ -83,9 +84,8 @@ fn dodge_keeps_8_and_16_bit_results() { assert_eq!(got, [[11825, 11825, 11825], [41392, 41392, 41392], [58654, 58654, 58654], [65535, 16658, 16658], [62104, 47989, 39521], [49139, 54599, 62789]]); } -#[test] -fn dodge_keeps_light_above_white_in_32_bit() { - let mut s = patches(32); +/// Fill the first patch with light above white (4.0, 1.5, 0.2, linear). +fn above_white(s: &mut Session) { s.edit("bright", |doc, active| { let surf = doc.layer_mut(active.unwrap()).unwrap().surface_mut().unwrap(); let r = photocraft_geom::Rect::new(0, 0, SIDE, SIDE); @@ -94,13 +94,59 @@ fn dodge_keeps_light_above_white_in_32_bit() { Ok(()) }) .unwrap(); +} + +#[test] +fn dodge_keeps_light_above_white_in_32_bit() { + let mut s = patches(32); + above_white(&mut s); let c = dodge_each(&mut s, json!({}))[0]; assert_eq!([c[0], c[1]], [4.0, 1.5], "light above white must stay as it was: {c:?}"); assert!(c[2] > 0.2, "the in-range channel is still dodged: {c:?}"); } #[test] -fn dodge_keeps_tones_in_order_in_32_bit_at_full_exposure() { +fn dodge_keeps_todays_curve_at_unmeasured_exposures_in_32_bit() { + // Only 50 % was measured in 32-bit; other exposures keep the gamma curve with Protect Tones' + // luma scaling, run on the stored values. + for exposure in [25, 100] { + let mut s = patches(32); + let before: Vec<_> = (0..PATCHES.len() as i32).map(|i| rgb(&s, i * SIDE + SIDE / 2, SIDE / 2)).collect(); + let got = dodge_each(&mut s, json!({"exposure": exposure})); + for (g, o) in got.iter().zip(before) { + let e = dodge_burn(o, exposure as f32 / 100.0, ToneRange::Midtones, false, true); + assert!(g.iter().zip(e).all(|(g, e)| (g - e).abs() < 1e-5), "{exposure} % Dodge of {o:?}: got {g:?}, today's curve gives {e:?}"); + } + } +} + +#[test] +fn dodge_keeps_light_above_white_with_unmeasured_settings_in_32_bit() { + // The settings that keep today's curve must not clip light above white either. + for extra in [json!({"exposure": 100}), json!({"range": "shadows"}), json!({"range": "highlights"}), json!({"protectTones": false})] { + let mut s = patches(32); + above_white(&mut s); + let c = dodge_each(&mut s, extra.clone())[0]; + assert!(c[0] >= 4.0 && c[1] >= 1.5, "Dodge with {extra} clipped light above white: {c:?}"); + } + let mut s = Session::new(); + s.execute("file.new", json!({"width": SIDE, "height": SIDE, "depth": 32, "mode": "gray"})).unwrap(); + s.edit("bright", |doc, active| { + let surf = doc.layer_mut(active.unwrap()).unwrap().surface_mut().unwrap(); + let px = photocraft_raster::from_rgba(&surf.format(), [2.0, 2.0, 2.0, 1.0]); + surf.write_region(photocraft_geom::Rect::new(0, 0, SIDE, SIDE), &px.repeat((SIDE * SIDE) as usize)); + Ok(()) + }) + .unwrap(); + let before = rgb(&s, SIDE / 2, SIDE / 2)[0]; + assert!(before > 1.0, "{before}"); + s.execute("paint.dodge", json!({"points": [[SIDE / 2, SIDE / 2]], "size": 40, "hardness": 100})).unwrap(); + let after = rgb(&s, SIDE / 2, SIDE / 2)[0]; + assert!(after >= before, "32-bit Grayscale Dodge clipped {before} to {after}"); +} + +#[test] +fn dodge_keeps_tones_in_order_in_32_bit() { let mut s = Session::new(); s.execute("file.new", json!({"width": 256, "height": 8, "depth": 32, "mode": "rgb"})).unwrap(); s.edit("ramp", |doc, active| { @@ -111,7 +157,7 @@ fn dodge_keeps_tones_in_order_in_32_bit_at_full_exposure() { Ok(()) }) .unwrap(); - s.execute("paint.dodge", json!({"points": [[-20, 4], [276, 4]], "size": 40, "hardness": 100, "exposure": 100})).unwrap(); + s.execute("paint.dodge", json!({"points": [[-20, 4], [276, 4]], "size": 40, "hardness": 100})).unwrap(); let row: Vec = (0..256).map(|x| rgb(&s, x, 4)[0]).collect(); assert!(row.windows(2).all(|w| w[1] >= w[0]), "{row:?}"); assert!(row.iter().all(|v| (0.0..=1.0).contains(v)) && row[0] == 0.0 && row[255] == 1.0, "{row:?}");