diff --git a/crates/engine/src/cmd/retouch_cmds/dab.rs b/crates/engine/src/cmd/retouch_cmds/dab.rs index 765057062a..061ed82545 100644 --- a/crates/engine/src/cmd/retouch_cmds/dab.rs +++ b/crates/engine/src/cmd/retouch_cmds/dab.rs @@ -16,6 +16,20 @@ fn per_dab(e: f32, spacing: f32) -> f32 { 1.0 - (1.0 - e.clamp(0.0, 1.0)).powf(spacing.clamp(0.01, 1.0)) } +/// The dab spacing, as a share of the tip's diameter, at which the reference's Sharpen drags +/// with Protect Detail were measured: each point of a drag got 4 dabs (#12938). +const PROTECT_DETAIL_SPACING: f32 = 0.25; + +/// The share of a full Protect Detail dab that a dab of `radius` at `moved` px from the previous +/// one applies: the first dab of a stroke (a click) is a full dab, and a drag adds up to the +/// reference's 4 full dabs over each point whatever the spacing, so many overlapping dabs don't +/// compound into black and white bands. It scales the strength itself rather than `1 − k` like +/// [`per_dab`], which stays 1 for a hard tip at 100 %. +#[inline] +fn protect_detail_share(moved: Option, radius: f32) -> f32 { + moved.map_or(1.0, |m| (m / (2.0 * radius.max(0.5) * PROTECT_DETAIL_SPACING)).clamp(0.0, 1.0)) +} + /// Apply an RGB colour transform `f(rgb, strength)` to each covered pixel of a dab (through straight /// RGBA, so it works for Gray/CMYK/Lab too; untouched pixels are never round-tripped). fn color_dab(fmt: &PixelFormat, work: &mut Region, fp: &Footprint, strength: f32, spacing: f32, f: impl Fn([f32; 3], f32) -> [f32; 3]) { @@ -226,9 +240,14 @@ impl DabTool { Self::Sponge { saturate, vibrance } => Box::new(move |work, fp| color_dab(&fmt, work, fp, 0.5, sp, |c, k| sponge(c, k, saturate, vibrance))), Self::Focus { sharpen, strength, protect, sigma } => { let a = alpha_index(&fmt); + // Protect Detail: where the previous dab was, for `protect_detail_share`. + let mut last: Option<(f64, f64)> = None; Box::new(move |work, fp| { + let c = (fp.dab.center.x, fp.dab.center.y); + let moved = last.replace(c).map(|(x, y)| (c.0 - x).hypot(c.1 - y) as f32); + let share = protect_detail_share(moved, fp.dab.radius); let r = fp.rect.intersect(&work.rect); - if r.is_empty() { + if r.is_empty() || (sharpen && protect && share <= 0.0) { return; } let (w, h, n) = (work.width(), work.height(), work.ch); @@ -243,10 +262,11 @@ impl DabTool { return; } for x in r.x0..r.x1 { - // Protect Detail applies Strength once per dab, so one click at 50 % - // does half of what 100 % does (#12938). Spread over the overlapping - // dabs (`per_dab`), it did 7 % of it at the default 10 % spacing. - let k = if sharpen && protect { fp.at(x, y) * strength } else { per_dab(fp.at(x, y) * strength, sp) }; + // Protect Detail: one click at 50 % does half of what 100 % does, and + // a drag over a point adds up to the reference's 4 dabs (#12938). + // Spread over `1/spacing` dabs (`per_dab`), a click did 7 % of it at + // the default 10 % spacing. + let k = if sharpen && protect { fp.at(x, y) * strength * share } else { per_dab(fp.at(x, y) * strength, sp) }; if k <= 0.0 { continue; } diff --git a/crates/engine/src/cmd/retouch_cmds/tests.rs b/crates/engine/src/cmd/retouch_cmds/tests.rs index b07de9d765..9ef20c7fd2 100644 --- a/crates/engine/src/cmd/retouch_cmds/tests.rs +++ b/crates/engine/src/cmd/retouch_cmds/tests.rs @@ -498,6 +498,44 @@ fn protect_detail_click_on_a_step_follows_strength() { } } +#[test] +fn protect_detail_drags_at_the_default_spacing_widen_the_halo_without_banding() { + // #12938: three 40 px hard drags down a grey 96 | 160 step with the brush's own spacing. The + // reference gives a smooth halo that deepens and widens with every pass, with no 0/255 + // pattern. Its drags put 4 dabs on each point (25 % spacing); at the default 10 % the drag + // must add up to the same, not to 10 full dabs. + let level = |s: &Session, x| (rgba(s, x, 80)[0] * 255.0).round() as i32; + let drag = |s: &mut Session, strength, spacing: Option| { + let mut p = json!({"points": [[40, 10], [40, 150]], "size": 40, "hardness": 100, "strength": strength}); + if let Some(sp) = spacing { + p["spacing"] = json!(sp); + } + s.execute("paint.sharpen", p).unwrap(); + }; + let step = |x: i32, _| if x < 40 { [96.0 / 255.0, 96.0 / 255.0, 96.0 / 255.0, 1.0] } else { [160.0 / 255.0, 160.0 / 255.0, 160.0 / 255.0, 1.0] }; + for strength in [50, 100] { + let (mut s, mut at25) = (session(80, 160, 8, "rgb"), session(80, 160, 8, "rgb")); + assert!((s.tools.brush.spacing - 0.1).abs() < 1e-6, "the default retouch spacing"); + paint_layer(&mut s, step); + paint_layer(&mut at25, step); + let (mut depth, mut width) = (0, 0); + for pass in 1..=3 { + drag(&mut s, strength, None); + drag(&mut at25, strength, Some(25)); + let row: Vec = (0..80).map(|x| level(&s, x)).collect(); + assert!(row.iter().all(|v| (1..=254).contains(v)), "{strength} % pass {pass}: black and white bands {row:?}"); + for x in [39, 40] { + let (got, want) = (level(&s, x), level(&at25, x)); + assert!((got - want).abs() <= 4, "{strength} % pass {pass}: x {x} = {got} at the default spacing, {want} at 25 %"); + } + let (d, w) = (96 - row[39], (20..40).filter(|&x| row[x] < 94).count()); + assert!(d > depth && w >= width, "{strength} % pass {pass}: halo depth {d} (was {depth}), width {w} (was {width}): {row:?}"); + (depth, width) = (d, w); + } + assert!(width >= 4, "{strength} %: the halo widens to {width} px"); + } +} + #[test] fn smudge_moves_colour_along_the_stroke() { for depth in DEPTHS {