From a20e4df41684792ecfcaae5a24d1c6ba498adb51 Mon Sep 17 00:00:00 2001 From: Terra854 Date: Wed, 2 Dec 2026 18:27:04 +0800 Subject: [PATCH] Limit tiny pressure-sized History Brush restoration --- .../src/cmd/retouch_cmds/clone.rs | 8 +- .../engine-paint/src/cmd/retouch_cmds/heal.rs | 11 +- crates/paint/src/dynamics.rs | 34 +++++- .../src/history_brush_light_pressure_tests.rs | 109 ++++++++++++++++++ crates/paint/src/render.rs | 8 ++ crates/paint/src/retouch.rs | 15 ++- 6 files changed, 178 insertions(+), 7 deletions(-) create mode 100644 crates/paint/src/history_brush_light_pressure_tests.rs diff --git a/crates/engine-paint/src/cmd/retouch_cmds/clone.rs b/crates/engine-paint/src/cmd/retouch_cmds/clone.rs index b47125dfd7d..30dca002436 100644 --- a/crates/engine-paint/src/cmd/retouch_cmds/clone.rs +++ b/crates/engine-paint/src/cmd/retouch_cmds/clone.rs @@ -359,8 +359,12 @@ impl LiveRetouch { let sel = doc.selection.clone(); let (surf, lock) = crate::channel_cmds::target_surface(&mut doc, id, p)?; let pre_surf = surf.clone(); - // As `stroke_coverage`, which the commit uses. - let renderer = photocraft_paint::StrokeRenderer::new(&stroke.brush, None, 1.0); + // Use the same coverage policy as the command's replay path. + let renderer = if cmd == "paint.historyBrush" && !flag(p, "eraseToHistory", false) { + photocraft_paint::StrokeRenderer::for_history_brush(&stroke.brush) + } else { + photocraft_paint::StrokeRenderer::new(&stroke.brush, None, 1.0) + }; let opacity = stroke.brush.opacity; let brush = Box::new(stroke.brush.clone()); let paint = match paint { diff --git a/crates/engine-paint/src/cmd/retouch_cmds/heal.rs b/crates/engine-paint/src/cmd/retouch_cmds/heal.rs index f1f70387b45..3ab993d6e9a 100644 --- a/crates/engine-paint/src/cmd/retouch_cmds/heal.rs +++ b/crates/engine-paint/src/cmd/retouch_cmds/heal.rs @@ -2,8 +2,8 @@ use super::clone::{apply_clone_coverage, blend_param, clamp_samples, clone_mappi use super::periodic; use super::sampling::{SampleLayers, composite_region, sample_all_layers, sample_layers}; use super::{ - BlendMode, CompleteParams, Document, EngineError, LayerId, PixelFormat, Rect, Region, Result, Session, Stroke, Surface, Value, apply_coverage, bad, - coverage, damage_json, flag, inpaint, json, parse_brush, poisson, reject_huge_stroke, restore_coverage, run_stroke, string, + BlendMode, CompleteParams, Document, EngineError, LayerId, MAX_STROKE_PIXELS, PixelFormat, Rect, Region, Result, Session, Stroke, Surface, Value, + apply_coverage, bad, coverage, damage_json, flag, inpaint, json, parse_brush, poisson, reject_huge_stroke, restore_coverage, run_stroke, string, }; /// Grow a stroke's coverage map by `m` pixels of zero coverage (a Dirichlet boundary for the solve). @@ -191,7 +191,12 @@ pub(super) fn history_brush(s: &mut Session, p: &Value) -> Result { } let fmt = surf.format(); let src = if src_surface.format() == fmt { src_surface } else { src_surface.convert(fmt) }; - let (bounds, cov) = coverage(CMD, &stroke)?; + let (bounds, cov) = if erase { + coverage(CMD, &stroke)? + } else { + photocraft_paint::retouch::history_brush_coverage_within(&stroke, MAX_STROKE_PIXELS) + .ok_or_else(|| bad(CMD, format!("stroke too large (at most {MAX_STROKE_PIXELS} px)")))? + }; let paint = Region::read(&src, bounds); Ok(restore_coverage(surf, bounds, &cov, stroke.brush.opacity, sel, lock, &paint, mode)) })?; diff --git a/crates/paint/src/dynamics.rs b/crates/paint/src/dynamics.rs index 9809e215ce3..af81613008d 100644 --- a/crates/paint/src/dynamics.rs +++ b/crates/paint/src/dynamics.rs @@ -635,11 +635,13 @@ pub struct DabBuilder { /// Per-stroke colour (Color Dynamics with per-tip off), computed at the first step. stroke_color: Option<[f32; 4]>, next_index: u64, + /// Only History Brush opts into the measured small pressure-dab restore ceiling. + history_brush: bool, } impl DabBuilder { pub fn new(brush: &BrushSettings) -> Self { - Self { brush: brush.clone(), seed: brush.seed, stroke_color: None, next_index: 0 } + Self { brush: brush.clone(), seed: brush.seed, stroke_color: None, next_index: 0, history_brush: false } } pub fn step_len(&self, p: &StrokePoint) -> f64 { @@ -751,6 +753,9 @@ impl DabBuilder { if b.pressure_opacity && !(tr.enabled && tr.opacity.control != Control::Off) { opacity *= s.point.pressure.clamp(0.0, 1.0); } + if self.history_brush { + opacity *= history_brush_small_dab_ceiling(b, s, diameter); + } // Colour. let color = if b.color_dynamics.enabled { if b.color_dynamics.per_tip { dynamic_color(b, s, seed, i) } else { *self.stroke_color.get_or_insert_with(|| dynamic_color(b, s, seed, 0)) } @@ -849,6 +854,28 @@ impl DualBuilder { } } +/// A tiny hard History Brush dab cannot restore a whole pixel even after overlapping dabs. +/// Keep its unfloored area relative to a 2 px dab as the accumulation ceiling; at 2 px this +/// reaches one, retaining the already-matching wider strokes. Other tools never opt in. +fn history_brush_small_dab_ceiling(b: &BrushSettings, s: &StepInput, diameter: f32) -> f32 { + let sd = &b.shape_dynamics; + let pressure_size = if sd.enabled && sd.size.control != Control::Off { sd.size.control == Control::PenPressure } else { b.pressure_size }; + if pressure_size + && s.point.pressure < 1.0 + && diameter < 2.0 + && b.hardness >= 1.0 + && b.flow >= 1.0 + && !b.aliased + && !b.transfer.enabled + && !b.pressure_opacity + && matches!(&*b.tip, crate::brush::TipShape::Round) + { + (diameter.max(0.0) * 0.5).powi(2) + } else { + 1.0 + } +} + /// Apply Brush Pose to an input point. pub fn apply_pose(b: &BrushSettings, mut p: StrokePoint) -> StrokePoint { let pose = &b.pose; @@ -885,6 +912,11 @@ pub struct DabGenerator { } impl DabGenerator { + pub(crate) fn for_history_brush(mut self) -> Self { + self.builder.history_brush = true; + self + } + /// Does the stroke change with time while the pointer is held still (airbrush Build-up, or /// smoothing still catching up)? The canvas then keeps feeding it the held point. pub fn wants_time(&self) -> bool { diff --git a/crates/paint/src/history_brush_light_pressure_tests.rs b/crates/paint/src/history_brush_light_pressure_tests.rs new file mode 100644 index 00000000000..a6cab54a4dc --- /dev/null +++ b/crates/paint/src/history_brush_light_pressure_tests.rs @@ -0,0 +1,109 @@ +//! #17826: very-light hard History Brush strokes restore a trace instead of piling up to full strength. + +use photocraft_color::{BlendMode, PixelFormat}; +use photocraft_geom::Rect; +use photocraft_raster::Surface; + +use super::{Region, history_brush_coverage_within, restore_coverage, stroke_coverage_within}; +use crate::{BrushSettings, Control, Dynamic, Stroke, StrokePoint, StrokeRenderer, TipShape, tile::GrayTile}; + +fn brush(size: f32) -> BrushSettings { + BrushSettings { size, pressure_size: true, hardness: 1.0, spacing: 0.25, ..BrushSettings::default() } +} + +fn stroke(brush: BrushSettings, pressure: f32) -> Stroke { + Stroke { brush, points: vec![StrokePoint::new(100.0, 100.0, pressure), StrokePoint::new(200.0, 100.0, pressure), StrokePoint::new(300.0, 100.0, pressure)] } +} + +fn restored_rows(stroke: &Stroke, format: PixelFormat) -> Vec { + let (rect, cov) = history_brush_coverage_within(stroke, 1_000_000).unwrap(); + let source = Surface::with_default(format, &[1.0, 1.0, 1.0, 1.0]); + let mut target = Surface::with_default(format, &[0.0, 0.0, 1.0, 1.0]); + restore_coverage(&mut target, rect, &cov, 1.0, None, false, &Region::read(&source, rect), BlendMode::Normal); + (0..200).map(|y| (target.rgba(200, y)[0] * 255.0).round() as u8).filter(|r| *r > 0).collect() +} + +#[test] +fn the_lightest_pressure_restores_at_most_a_trace() { + for format in [PixelFormat::RGBA8, PixelFormat::RGBA16, PixelFormat::RGBA32F] { + let rows = restored_rows(&stroke(brush(41.0), 0.01), format); + eprintln!("41px at pressure 0.01, {format:?}: restored rows {rows:?}"); + assert!(!rows.is_empty() && rows.len() <= 2, "{format:?}: {rows:?}"); + assert!(rows.iter().all(|r| *r <= 30), "{format:?}: {rows:?}"); + } +} + +#[test] +fn a_thirteen_pixel_tip_at_one_tenth_pressure_cannot_restore_fully() { + for format in [PixelFormat::RGBA8, PixelFormat::RGBA16, PixelFormat::RGBA32F] { + let rows = restored_rows(&stroke(brush(13.0), 0.1), format); + eprintln!("13px at pressure 0.1, {format:?}: restored rows {rows:?}"); + assert!((2..=3).contains(&rows.len()), "{format:?}: {rows:?}"); + let centre = *rows.iter().max().unwrap(); + assert!((33..=194).contains(¢re), "{format:?}: {rows:?}"); + assert!(rows.iter().all(|r| *r < 255)); + } +} + +fn assert_legacy(stroke: &Stroke) { + let actual = history_brush_coverage_within(stroke, 1_000_000).unwrap(); + let old = stroke_coverage_within(stroke, 1_000_000).unwrap(); + assert_eq!(actual.0, old.0); + assert_eq!(actual.1.iter().map(|v| v.to_bits()).collect::>(), old.1.iter().map(|v| v.to_bits()).collect::>()); +} + +#[test] +fn wider_strokes_and_mouse_keep_the_previous_coverage_bits() { + for (size, pressures) in [(41.0, &[0.05, 0.25, 0.5, 1.0][..]), (101.0, &[0.02, 0.25, 0.5, 1.0][..])] { + for &pressure in pressures { + assert_legacy(&stroke(brush(size), pressure)); + } + } + let mut mouse = stroke(brush(1.0), 1.0); + mouse.points.iter_mut().for_each(|p| *p = StrokePoint::without_pressure(p.x, p.y)); + assert_legacy(&mouse); +} + +#[test] +fn soft_reduced_flow_sampled_aliased_and_nonpressure_paths_keep_their_coverage() { + let b = brush(41.0); + for b in [ + BrushSettings { hardness: 0.5, ..b.clone() }, + BrushSettings { flow: 0.5, ..b.clone() }, + BrushSettings { pressure_size: false, ..b.clone() }, + BrushSettings { aliased: true, ..b.clone() }, + BrushSettings { aliased: true, square: true, ..b.clone() }, + BrushSettings { tip: TipShape::Sampled(GrayTile::from_f32(1, 1, &[1.0])).into(), ..b }, + ] { + assert_legacy(&stroke(b, 0.01)); + } + let mut b = brush(41.0); + b.shape_dynamics.enabled = true; + b.shape_dynamics.size = Dynamic { control: Control::Fade, ..Dynamic::default() }; + assert_legacy(&stroke(b, 0.01)); +} + +#[test] +fn live_chunks_and_the_press_preview_match_replayed_coverage() { + for (size, pressure) in [(41.0, 0.01), (13.0, 0.1), (41.0, 0.05)] { + let stroke = stroke(brush(size), pressure); + let expected = history_brush_coverage_within(&stroke, 1_000_000).unwrap(); + let mut live = StrokeRenderer::for_history_brush(&stroke.brush); + live.push(&stroke.points[..1]); + let tap = Stroke { brush: stroke.brush.clone(), points: stroke.points[..1].to_vec() }; + assert_eq!(live.tail_preview().unwrap().dense_coverage(), history_brush_coverage_within(&tap, 1_000_000).unwrap()); + for point in &stroke.points[1..] { + live.push(&[*point]); + } + live.finish(); + assert_eq!(live.dense_coverage(), expected); + } +} + +#[test] +fn history_coverage_admits_its_rendered_extent_before_allocating_dense_output() { + let stroke = stroke(brush(41.0), 0.01); + assert!(history_brush_coverage_within(&stroke, 0).is_none()); + let (bounds, _) = history_brush_coverage_within(&stroke, 1_000_000).unwrap(); + assert_ne!(bounds, Rect::EMPTY); +} diff --git a/crates/paint/src/render.rs b/crates/paint/src/render.rs index ca1214b4316..c54fd53a024 100644 --- a/crates/paint/src/render.rs +++ b/crates/paint/src/render.rs @@ -895,6 +895,14 @@ impl Clone for RegionPool { } impl StrokeRenderer { + /// History Brush coverage, including its measured very-light hard pressure-size drop-off. + /// Keep this opt-in separate from Brush, Eraser, and other retouching tools. + pub fn for_history_brush(brush: &BrushSettings) -> Self { + let mut renderer = Self::new(brush, None, 1.0); + renderer.generator = renderer.generator.for_history_brush(); + renderer + } + /// Composite the union of this stroke and a mirrored pass as one stroke. This keeps /// overlapping dabs on a symmetry axis under one opacity ceiling at every bit depth. pub fn composite_union(&self, other: &Self, pre: &Surface, target: &mut Surface, selection: Option<&Surface>, lock_transparency: bool) -> Rect { diff --git a/crates/paint/src/retouch.rs b/crates/paint/src/retouch.rs index e0124426f6a..38ee2777087 100644 --- a/crates/paint/src/retouch.rs +++ b/crates/paint/src/retouch.rs @@ -154,7 +154,16 @@ pub fn stroke_coverage(stroke: &Stroke) -> (Rect, Vec) { /// real extent rather than an estimate from the input points. Only then is the `width × height` /// buffer allocated, which is what callers need bounded. pub fn stroke_coverage_within(stroke: &Stroke, max_pixels: u64) -> Option<(Rect, Vec)> { - let mut r = crate::StrokeRenderer::new(&stroke.brush, None, 1.0); + coverage_with_renderer(stroke, max_pixels, crate::StrokeRenderer::new(&stroke.brush, None, 1.0)) +} + +/// History Brush's coverage with the same extent admission as [`stroke_coverage_within`]. +/// Live strokes use [`crate::StrokeRenderer::for_history_brush`] for the identical dab policy. +pub fn history_brush_coverage_within(stroke: &Stroke, max_pixels: u64) -> Option<(Rect, Vec)> { + coverage_with_renderer(stroke, max_pixels, crate::StrokeRenderer::for_history_brush(&stroke.brush)) +} + +fn coverage_with_renderer(stroke: &Stroke, max_pixels: u64, mut r: crate::StrokeRenderer) -> Option<(Rect, Vec)> { r.push(&stroke.points); r.finish(); let b = r.bounds(); @@ -164,6 +173,10 @@ pub fn stroke_coverage_within(stroke: &Stroke, max_pixels: u64) -> Option<(Rect, Some(r.dense_coverage()) } +#[cfg(test)] +#[path = "history_brush_light_pressure_tests.rs"] +mod history_brush_light_pressure_tests; + /// Index of the alpha channel, if the format has one. #[inline] pub fn alpha_index(fmt: &PixelFormat) -> Option {