diff --git a/crates/algo/src/retouch.rs b/crates/algo/src/retouch.rs index 69ed15194c..ba496d0e48 100644 --- a/crates/algo/src/retouch.rs +++ b/crates/algo/src/retouch.rs @@ -83,6 +83,20 @@ pub fn dodge_burn(c: [f32; 3], e: f32, range: ToneRange, burn: bool, protect_ton } } +/// The default Midtones Dodge (`burn = false`) or Burn of one grey value `v` (0..1) in a Grayscale +/// document or on a layer mask, at Exposure 50 % with Protect Tones on: `v + 0.094·v^0.2·(1 − v)` +/// for Dodge and `v − 0.236·v·(1 − v)^1.15` for Burn. Both are fitted to the reference's 8-bit +/// single-channel results (#12689, #12675), which differ from what an RGB grey gets; each lands +/// within 1 level of the six measured greys. Black and white stay put, tones stay in order, and +/// values outside 0..1 are returned as they are. +#[inline] +pub fn single_channel_midtones(v: f32, burn: bool) -> f32 { + if !(v > 0.0 && v < 1.0) { + return v; + } + if burn { v - 0.236 * v * (1.0 - v).powf(1.15) } else { v + 0.094 * v.powf(0.2) * (1.0 - v) } +} + /// HSV-style saturation (chroma / max) of an RGB triple. #[inline] pub fn saturation(c: [f32; 3]) -> f32 { diff --git a/crates/engine-paint/src/cmd/retouch_cmds/burn_grayscale_tests.rs b/crates/engine-paint/src/cmd/retouch_cmds/burn_grayscale_tests.rs new file mode 100644 index 0000000000..71b7ec79b5 --- /dev/null +++ b/crates/engine-paint/src/cmd/retouch_cmds/burn_grayscale_tests.rs @@ -0,0 +1,224 @@ +//! Burn in an 8-bit Grayscale document and on a layer mask uses the reference's single-channel +//! curve (#12675); RGB documents and the cases the reference wasn't measured in keep the RGB +//! curve. The helpers are shared with `dodge_grayscale_tests`. + +use super::LiveDab; +use crate::Session; +use photocraft_algo::retouch::{ToneRange, dodge_burn, single_channel_midtones}; +use photocraft_raster::Surface; +use serde_json::{Value, json}; + +/// The greys the reference was measured on. +pub(super) const GREYS: [u8; 6] = [32, 105, 128, 180, 197, 224]; +const SIDE: i32 = 60; + +/// Fill `surf` with the `GREYS` bands, side by side. +fn bands(surf: &mut Surface, b: photocraft_geom::Rect) { + let fmt = surf.format(); + let mut data = Vec::new(); + for _ in b.y0..b.y1 { + for x in b.x0..b.x1 { + let v = f32::from(GREYS[(x / SIDE) as usize]) / 255.0; + data.extend(photocraft_raster::from_rgba(&fmt, [v, v, v, 1.0])); + } + } + surf.write_region(b, &data); +} + +/// A `mode` (`"gray"` or `"rgb"`) document of `depth` bits whose layer holds the `GREYS` bands. +pub(super) fn doc(mode: &str, depth: u32) -> Session { + let mut s = crate::session(); + s.execute("file.new", json!({"width": SIDE * GREYS.len() as i32, "height": SIDE, "depth": depth, "mode": mode, "background": "white"})).unwrap(); + s.edit("bands", |doc, active| { + let b = doc.bounds(); + bands(doc.layer_mut(active.unwrap()).unwrap().surface_mut().unwrap(), b); + Ok(()) + }) + .unwrap(); + s +} + +/// The reference's mask setup in a `depth`-bit RGB document: a white pixel layer over a black one, +/// with a layer mask holding the `GREYS` bands. +pub(super) fn masked(depth: u32) -> Session { + let mut s = crate::session(); + s.execute( + "file.new", + json!({"width": SIDE * GREYS.len() as i32, "height": SIDE, "depth": depth, "background": "backgroundColor", "backgroundColor": [0.0, 0.0, 0.0]}), + ) + .unwrap(); + s.execute("layer.new.layer", json!({})).unwrap(); + s.execute("layer.layerMask.revealAll", json!({})).unwrap(); + s.edit("bands", |doc, active| { + let b = doc.bounds(); + let layer = doc.layer_mut(active.unwrap()).unwrap(); + let surf = layer.surface_mut().unwrap(); + let white = photocraft_raster::from_rgba(&surf.format(), [1.0; 4]); + surf.write_region(b, &white.repeat((b.width() * b.height()) as usize)); + bands(&mut layer.mask.as_mut().unwrap().surface, b); + Ok(()) + }) + .unwrap(); + s +} + +/// The stored value (0..255) the target of `extra` holds at `(x, y)`. +fn read(s: &Session, extra: &Value, x: i32, y: i32) -> f32 { + let d = s.active().unwrap(); + let surf = match extra.get("target") { + Some(Value::String(t)) if t == "mask" => &d.doc.layer(d.active_layer.unwrap()).unwrap().mask.as_ref().unwrap().surface, + Some(Value::String(t)) if t == "quickMask" => &d.doc.quick_mask.as_ref().unwrap().surface, + Some(Value::Object(_)) => &d.doc.channels[0].surface, + _ => d.doc.layer(d.active_layer.unwrap()).unwrap().surface().unwrap(), + }; + surf.rgba(x, y)[0] * 255.0 +} + +/// One `cmd` click (40 px, hardness 100, plus `extra`) on each band's centre; the values there. +pub(super) fn click_each(s: &mut Session, cmd: &str, extra: &Value) -> Vec { + (0..GREYS.len() as i32) + .map(|i| { + let (x, y) = (i * SIDE + SIDE / 2, SIDE / 2); + let mut p = json!({"points": [[x, y]], "size": 40, "hardness": 100}); + p.as_object_mut().unwrap().extend(extra.as_object().unwrap().clone()); + s.execute(cmd, p).unwrap(); + read(s, extra, x, y) + }) + .collect() +} + +pub(super) fn assert_near(got: &[f32], want: &[f32], tol: f32, what: &str) { + assert!(got.iter().zip(want).all(|(g, w)| (g - w).abs() <= tol), "{what}: got {got:?}, expected {want:?} (±{tol})"); +} + +/// Today's RGB curve for the `GREYS`, as 0..255. +pub(super) fn rgb_curve(e: f32, range: ToneRange, burn: bool, protect: bool) -> Vec { + GREYS.iter().map(|&g| dodge_burn([f32::from(g) / 255.0; 3], e, range, burn, protect)[0] * 255.0).collect() +} + +/// The cases the reference wasn't measured in keep the RGB curve: 16- and 32-bit Grayscale, a +/// mask in a 16- or 32-bit document, the other ranges and exposures, Protect Tones off, an alpha +/// channel and the Quick Mask. +pub(super) fn keeps_rgb_curve_where_not_measured(cmd: &str, burn: bool) { + let mid = |e, protect| rgb_curve(e, ToneRange::Midtones, burn, protect); + for depth in [16, 32] { + let tol = if depth == 16 { 0.05 } else { 0.01 }; + assert_near(&click_each(&mut doc("gray", depth), cmd, &json!({})), &mid(0.5, true), tol, &format!("{cmd} gray {depth}-bit")); + assert_near(&click_each(&mut masked(depth), cmd, &json!({"target": "mask"})), &mid(0.5, true), tol, &format!("{cmd} mask {depth}-bit")); + } + let cases = [ + (json!({"range": "shadows"}), rgb_curve(0.5, ToneRange::Shadows, burn, true)), + (json!({"range": "highlights"}), rgb_curve(0.5, ToneRange::Highlights, burn, true)), + (json!({"exposure": 100}), mid(1.0, true)), + (json!({"exposure": 49}), mid(0.49, true)), + (json!({"protectTones": false}), mid(0.5, false)), + ]; + for (extra, want) in cases { + // A rounding step: the 8-bit document stores whole levels. + let want: Vec = want.iter().map(|v| v.round()).collect(); + assert_near(&click_each(&mut doc("gray", 8), cmd, &extra), &want, 0.51, &format!("{cmd} gray 8-bit {extra}")); + } + let want: Vec = mid(0.5, true).iter().map(|v| v.round()).collect(); + let mut s = doc("gray", 8); + s.execute("channel.new", json!({})).unwrap(); + s.edit("bands", |doc, _| { + let b = doc.bounds(); + bands(&mut doc.channels[0].surface, b); + Ok(()) + }) + .unwrap(); + assert_near(&click_each(&mut s, cmd, &json!({"target": {"channel": 0}})), &want, 0.51, &format!("{cmd} alpha channel")); + let mut s = doc("gray", 8); + s.execute("select.editInQuickMaskMode", json!({"on": true})).unwrap(); + s.edit("bands", |doc, _| { + let b = doc.bounds(); + bands(&mut doc.quick_mask.as_mut().unwrap().surface, b); + Ok(()) + }) + .unwrap(); + assert_near(&click_each(&mut s, cmd, &json!({"target": "quickMask"})), &want, 0.51, &format!("{cmd} Quick Mask")); +} + +/// The live stroke shows the same single-channel result its commit lays down, on a short drag +/// across the grey 128 band (the stroke's last dab shows on release, so the start is compared). +pub(super) fn live_matches_commit(cmd: &str) { + let mut s = doc("gray", 8); + s.tools.brush.smoothing.enabled = false; + let (x, y) = (2 * SIDE + 15, SIDE / 2); + let all = [[x, y], [x + 10, y], [x + 20, y]]; + let opts = |p: &[[i32; 2]]| json!({"points": p, "size": 20, "hardness": 100}); + let mut live = LiveDab::begin(&s, cmd, &opts(&all[..1])).unwrap(); + live.push(&all[1..].iter().map(|q| photocraft_paint::StrokePoint::new(f64::from(q[0]), f64::from(q[1]), 1.0)).collect::>()).unwrap(); + let id = s.active().unwrap().active_layer.unwrap(); + let shown = live.doc.layer(id).unwrap().surface().unwrap().rgba(x, y)[0] * 255.0; + s.execute(cmd, opts(&all)).unwrap(); + let committed = read(&s, &json!({}), x, y); + assert_ne!(committed, 128.0, "{cmd}: the stroke toned the band"); + assert_eq!(shown, committed, "{cmd}: live against commit"); +} + +/// The single-channel curve keeps black and white, keeps tones in order and passes values outside +/// 0..1 through. +pub(super) fn curve_is_monotone_and_bounded(burn: bool) { + let mut last = 0.0f32; + for i in 0..=10_000 { + let v = i as f32 / 10_000.0; + let o = single_channel_midtones(v, burn); + assert!(o.is_finite() && o >= last - 1e-6 && (0.0..=1.0).contains(&o), "burn={burn}: {v} → {o} after {last}"); + assert!(if burn { o <= v } else { o >= v }, "burn={burn}: {v} → {o}"); + last = o; + } + assert_eq!(single_channel_midtones(0.0, burn), 0.0); + assert_eq!(single_channel_midtones(1.0, burn), 1.0); + for v in [-0.5, 1.5, 4.0, f32::NAN] { + let o = single_channel_midtones(v, burn); + assert!(o == v || (v.is_nan() && o.is_nan()), "burn={burn}: {v} → {o}"); + } +} + +/// Measured in the reference: one default Burn click in an 8-bit Grayscale document. +const REFERENCE: [f32; 6] = [25.0, 92.0, 115.0, 170.0, 188.0, 219.0]; + +#[test] +fn burn_in_8_bit_grayscale_matches_the_reference() { + let got = click_each(&mut doc("gray", 8), "paint.burn", &json!({})); + assert_near(&got, &REFERENCE, 2.0, "Burn, 8-bit Grayscale"); +} + +#[test] +fn burn_on_a_layer_mask_matches_grayscale() { + let mut s = masked(8); + let before = s.active().unwrap().history.past_len(); + let got = click_each(&mut s, "paint.burn", &json!({"target": "mask"})); + assert_near(&got, &REFERENCE, 2.0, "Burn, layer mask"); + let h = &s.active().unwrap().history; + assert_eq!((h.past_len(), h.undo_label()), (before + GREYS.len(), Some("Burn Tool"))); +} + +#[test] +fn burn_keeps_rgb_results() { + // Pinned from main d5582062a0: neutral greys and a colour in an 8-bit RGB document. + let got = click_each(&mut doc("rgb", 8), "paint.burn", &json!({})); + assert_near(&got, &[23.0, 69.0, 91.0, 156.0, 180.0, 218.0], 0.0, "Burn, RGB greys"); + let mut s = crate::session(); + s.execute("file.new", json!({"width": 60, "height": 60, "background": "backgroundColor", "backgroundColor": [0.7843137, 0.15686275, 0.15686275]})).unwrap(); + s.execute("paint.burn", json!({"points": [[30, 30]], "size": 40, "hardness": 100})).unwrap(); + let d = s.active().unwrap(); + let px = d.doc.layer(d.active_layer.unwrap()).unwrap().surface().unwrap().rgba(30, 30); + assert_eq!([px[0], px[1], px[2]].map(|v| (v * 255.0).round()), [125.0, 25.0, 25.0]); +} + +#[test] +fn burn_keeps_the_rgb_curve_where_the_reference_was_not_measured() { + keeps_rgb_curve_where_not_measured("paint.burn", true); +} + +#[test] +fn live_burn_in_grayscale_matches_the_commit() { + live_matches_commit("paint.burn"); +} + +#[test] +fn single_channel_burn_curve_is_monotone_and_bounded() { + curve_is_monotone_and_bounded(true); +} diff --git a/crates/engine-paint/src/cmd/retouch_cmds/dab.rs b/crates/engine-paint/src/cmd/retouch_cmds/dab.rs index e77ba563fd..4ee3ca8f1d 100644 --- a/crates/engine-paint/src/cmd/retouch_cmds/dab.rs +++ b/crates/engine-paint/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::{luma, sponge_rgb, tone_curve}; +use photocraft_algo::retouch::{luma, single_channel_midtones, sponge_rgb, tone_curve}; use photocraft_color::{ColorMode, SampleType}; // --------------------------------------------------------------------------------------------- @@ -203,6 +203,10 @@ fn linear_midtones(v: f32, e: f32, burn: bool) -> f32 { if burn { v - d } else { v + d } } +/// The Grayscale and layer-mask reference was measured at Exposure 50 % only (#12675, #12689); +/// other exposures keep the RGB curve. +const MEASURED_SINGLE_CHANNEL_EXPOSURE: f32 = 0.5; + /// The 32-bit reference was measured at Exposure 50 % only (#12687, #12674); other exposures keep the /// gamma curve. const MEASURED_LINEAR_EXPOSURE: f32 = 0.5; @@ -239,7 +243,7 @@ fn tone_range(p: &Value, cmd: &str) -> Result { /// turned into its per-dab effect, for the commit and the live preview alike. #[derive(Clone, Debug, PartialEq)] pub(super) enum DabTool { - Tone { burn: bool, range: ToneRange, exposure: f32, protect: bool }, + Tone { burn: bool, range: ToneRange, exposure: f32, protect: bool, single: bool }, Sponge { saturate: bool, vibrance: bool }, Focus { sharpen: bool, strength: f32, protect: bool, sigma: f32 }, Smudge { strength: f32, finger: Option<[f32; 4]>, max_size: f32, mode: BlendMode }, @@ -301,6 +305,10 @@ impl DabTool { range: tone_range(p, cmd)?, exposure: num(p, "exposure", 50.0).clamp(1.0, 100.0) / 100.0, protect: flag(p, "protectTones", true), + // The layer's pixels or its layer mask: there a Grayscale surface takes the + // reference's single-channel curve. An alpha channel, the Quick Mask and a + // filter mask weren't measured. + single: matches!(crate::channel_cmds::target_of(p), crate::channel_cmds::Target::Pixels | crate::channel_cmds::Target::Mask), }; (tool, if burn { "Burn Tool" } else { "Dodge Tool" }) } @@ -346,19 +354,27 @@ impl DabTool { pub(super) fn effect(&self, fmt: PixelFormat, spacing: f32) -> DabEffect { let sp = spacing; match self.clone() { + // An 8-bit Grayscale document or layer mask has its own default curve in the reference + // (#12675, #12689). Other depths, ranges, exposures, Protect Tones off and the other + // single-channel targets weren't measured and keep the RGB curve. + Self::Tone { burn, range: ToneRange::Midtones, exposure, protect: true, single: true } + if fmt.mode == ColorMode::Grayscale && fmt.sample == SampleType::U8 && (exposure - MEASURED_SINGLE_CHANNEL_EXPOSURE).abs() < 1e-4 => + { + tone_stroke(fmt, move |c| c.map(|v| single_channel_midtones(v, burn))) + } // A 32-bit RGB document holds linear values, which the reference's default Dodge and Burn // tone with their 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, range: ToneRange::Midtones, exposure, protect: true } + Self::Tone { burn, 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, burn))) } - Self::Tone { burn, range, exposure, protect } if fmt.sample == SampleType::F32 => { + 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)), + 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. Self::Sponge { saturate, vibrance } if fmt.mode == ColorMode::Rgb && fmt.sample != SampleType::F32 => { @@ -882,6 +898,14 @@ mod smudge_refuses_indexed_tests; #[path = "smudge_mode_tests.rs"] mod smudge_mode_tests; +#[cfg(test)] +#[path = "burn_grayscale_tests.rs"] +mod burn_grayscale_tests; + +#[cfg(test)] +#[path = "dodge_grayscale_tests.rs"] +mod dodge_grayscale_tests; + #[cfg(test)] #[path = "dodge_32bit_linear_tests.rs"] mod dodge_32bit_linear_tests; diff --git a/crates/engine-paint/src/cmd/retouch_cmds/dodge_grayscale_tests.rs b/crates/engine-paint/src/cmd/retouch_cmds/dodge_grayscale_tests.rs new file mode 100644 index 0000000000..d545e26b48 --- /dev/null +++ b/crates/engine-paint/src/cmd/retouch_cmds/dodge_grayscale_tests.rs @@ -0,0 +1,61 @@ +//! Dodge in an 8-bit Grayscale document and on a layer mask uses the reference's single-channel +//! curve (#12689); RGB documents and the cases the reference wasn't measured in keep the RGB +//! curve. The helpers live in `burn_grayscale_tests`. + +use super::burn_grayscale_tests::{ + GREYS, assert_near, click_each, curve_is_monotone_and_bounded, doc, keeps_rgb_curve_where_not_measured, live_matches_commit, masked, +}; +use serde_json::json; + +/// Measured in the reference: one default Dodge click in an 8-bit Grayscale document. +const REFERENCE: [f32; 6] = [46.0, 117.0, 138.0, 187.0, 202.0, 227.0]; + +#[test] +fn dodge_in_8_bit_grayscale_matches_the_reference() { + let got = click_each(&mut doc("gray", 8), "paint.dodge", &json!({})); + assert_near(&got, &REFERENCE, 2.0, "Dodge, 8-bit Grayscale"); +} + +#[test] +fn dodge_on_a_layer_mask_matches_grayscale() { + let mut s = masked(8); + let before = s.active().unwrap().history.past_len(); + let got = click_each(&mut s, "paint.dodge", &json!({"target": "mask"})); + assert_near(&got, &REFERENCE, 2.0, "Dodge, layer mask"); + let h = &s.active().unwrap().history; + assert_eq!((h.past_len(), h.undo_label()), (before + GREYS.len(), Some("Dodge Tool"))); +} + +#[test] +fn dodge_on_an_adjustment_layers_white_mask_keeps_it_white() { + let mut s = doc("gray", 8); + s.execute("layer.newAdjustmentLayer.invert", json!({})).unwrap(); + let before = s.active().unwrap().history.past_len(); + s.execute("paint.dodge", json!({"points": [[30, 30]], "size": 40, "hardness": 100, "target": "mask"})).unwrap(); + let d = s.active().unwrap(); + let mask = &d.doc.layer(d.active_layer.unwrap()).unwrap().mask.as_ref().unwrap().surface; + assert_eq!(mask.rgba(30, 30)[0], 1.0); + assert_eq!((d.history.past_len(), d.history.undo_label()), (before + 1, Some("Dodge Tool"))); +} + +#[test] +fn dodge_keeps_rgb_results() { + // Pinned from main d5582062a0: neutral greys and a colour in an 8-bit RGB document. + let got = click_each(&mut doc("rgb", 8), "paint.dodge", &json!({})); + assert_near(&got, &[46.0, 140.0, 161.0, 198.0, 209.0, 228.0], 0.0, "Dodge, RGB greys"); +} + +#[test] +fn dodge_keeps_the_rgb_curve_where_the_reference_was_not_measured() { + keeps_rgb_curve_where_not_measured("paint.dodge", false); +} + +#[test] +fn live_dodge_in_grayscale_matches_the_commit() { + live_matches_commit("paint.dodge"); +} + +#[test] +fn single_channel_dodge_curve_is_monotone_and_bounded() { + curve_is_monotone_and_bounded(false); +}