Spot Healing: the drag trail shows the dabs the heal covers (#2364) (#2642)

The Spot Healing Brush's drag preview stamped a hard round footprint at the
base brush size, so a soft brush and pressure/shape dynamics did not show,
while the heal itself uses the brush engine's dab coverage. The trail now
renders the same coverage (`stroke_coverage`'s StrokeRenderer, brush built by
the new shared `retouch_cmds::stroke_brush`), including the press's lone
first dab via `tail_preview`.

Co-authored-by: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Valentin KleinandClaude Opus 5.5 authored and GitHub committed 2026-10-10 13:09:05 -04:00
1 parent 0426b1bb09
commit 5bb768b8f1
3 files changed
+130 -17

No files matched your search

+18 -12
View File
@@ -66,6 +66,23 @@ fn point(p: &Value, k: &str) -> Option<(f64, f64)> {
Some((a.first()?.as_f64()?, a.get(1)?.as_f64()?))
}
/// The brush a retouching stroke paints with: the tool's brush (`base`, its tip, hardness and
/// shape dynamics) with the shared params of `p` applied. UI previews of a stroke (the Spot Healing
/// Brush's trail) render their coverage with it, so they match what the command changes.
pub fn stroke_brush(base: &BrushSettings, p: &Value) -> BrushSettings {
let pct = |k: &str, d: f32, lo: f32, hi: f32| num(p, k, d).clamp(lo, hi) / 100.0;
BrushSettings {
size: num(p, "size", base.size).max(1.0),
hardness: pct("hardness", base.hardness * 100.0, 0.0, 100.0),
opacity: pct("opacity", 100.0, 1.0, 100.0),
flow: pct("flow", 100.0, 1.0, 100.0),
// Brush Settings › Brush Tip Shape › Spacing, as the Brush uses it (#2140).
spacing: pct("spacing", base.spacing * 100.0, 1.0, 1000.0),
erase: false,
..base.clone()
}
}
/// Parse the shared brush params into a stroke on a layer (`None` when an alpha channel or the
/// Quick Mask is targeted).
fn parse_brush(s: &Session, p: &Value, cmd: &str) -> Result<(Stroke, Option<LayerId>)> {
@@ -83,18 +100,7 @@ fn parse_brush(s: &Session, p: &Value, cmd: &str) -> Result<(Stroke, Option<Laye
return Err(bad(cmd, "`points` is empty"));
}
crate::brush_cmds::check_coords(&pts, cmd)?;
let base = s.tools.brush.clone();
let pct = |k: &str, d: f32, lo: f32, hi: f32| num(p, k, d).clamp(lo, hi) / 100.0;
let brush = BrushSettings {
size: num(p, "size", base.size).max(1.0),
hardness: pct("hardness", base.hardness * 100.0, 0.0, 100.0),
opacity: pct("opacity", 100.0, 1.0, 100.0),
flow: pct("flow", 100.0, 1.0, 100.0),
// Brush Settings › Brush Tip Shape › Spacing, as the Brush uses it (#2140).
spacing: pct("spacing", base.spacing * 100.0, 1.0, 1000.0),
erase: false,
..base
};
let brush = stroke_brush(&s.tools.brush, p);
crate::brush_cmds::validate_brush(&brush, cmd)?;
if crate::channel_cmds::is_channel_target(p) {
return Ok((Stroke { brush, points: pts }, None));
+41 -3
View File
@@ -3503,7 +3503,12 @@ fn draw_drag_preview(app: &mut PhotocraftApp, painter: &egui::Painter, xf: &View
let size = [st.doc.size.width, st.doc.size.height];
let doc_rect = xf.doc_rect(st.doc.bounds());
let trail = app.trail.get_or_insert_with(|| crate::stroke_trail::Trail::new(size));
trail.feed(&d.points, app.session.tools.brush.size);
if t == Tool::SpotHealing {
// What the heal covers, dab by dab: soft edges, pressure and shape dynamics (#2364).
trail.feed_dabs(&d.points, || photocraft_engine::retouch_cmds::stroke_brush(&app.session.tools.brush, &json!({})));
} else {
trail.feed(&d.points, app.session.tools.brush.size);
}
trail.draw(painter, doc_rect, xf.flip, col);
}
t if crate::vector_ui::is_shape_tool(t) => crate::vector_ui::draw_shape_preview(app, painter, xf, t, d.start, last, d.live),
@@ -5484,8 +5489,9 @@ mod tests {
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default());
app.run("file.new", json!({"width": 120, "height": 60})).unwrap();
app.run("tools.setBrush", json!({"brush": {"size": 30}})).unwrap();
// Spot Healing: the dab tools draw their stroke live instead of a trail.
app.ui.tool = Tool::SpotHealing;
// Remove: the dab tools draw their stroke live instead of a trail, and Spot Healing's
// trail is its dabs' coverage.
app.ui.tool = Tool::Remove;
tool_event(&mut app, ToolEvent::Down { x: 20.0, y: 30.0, pressure: 1.0 }, egui::Modifiers::NONE);
let mut pts = vec![[20.0, 30.0]];
for i in 1..80 {
@@ -5517,6 +5523,38 @@ mod tests {
assert!(app.trail.is_none(), "the trail ends with the drag");
}
#[test]
fn spot_healing_trail_is_the_coverage_the_heal_uses() {
// #2364: a soft brush with pen-pressure size showed as a hard footprint at full size.
let ctx = egui::Context::default();
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default());
app.run("file.new", json!({"width": 120, "height": 60})).unwrap();
let brush = json!({"size": 30, "hardness": 0.0, "shapeDynamics": {"enabled": true, "size": {"control": "penPressure"}}});
app.run("tools.setBrush", json!({ "brush": brush })).unwrap();
app.ui.tool = Tool::SpotHealing;
tool_event(&mut app, ToolEvent::Down { x: 20.0, y: 30.0, pressure: 0.5 }, egui::Modifiers::NONE);
for x in [40.0, 60.0, 80.0] {
tool_event(&mut app, ToolEvent::Move { x, y: 30.0, pressure: 0.5 }, egui::Modifiers::NONE);
drag_preview_shapes(&mut app, &ctx, 12.0);
}
// The stroke the heal gets on release, and its coverage.
let points = app.drag.as_ref().unwrap().points.iter().map(|p| photocraft_paint::StrokePoint::new(p[0], p[1], p[2] as f32)).collect();
let brush = photocraft_engine::retouch_cmds::stroke_brush(&app.session.tools.brush, &json!({}));
let (b, cov) = photocraft_paint::retouch::stroke_coverage(&photocraft_paint::Stroke { brush, points });
let trail = app.trail.as_ref().unwrap();
let mut soft = 0;
for y in 0..60 {
for x in 0..120 {
let want = if b.contains(x, y) { cov[(y - b.y0) as usize * b.width() as usize + (x - b.x0) as usize] } else { 0.0 };
let got = trail.coverage(x as usize, y as usize);
assert!((f32::from(got) - want * 255.0).abs() <= 1.0, "({x}, {y}): trail {got}, heal {want}");
soft += usize::from(got > 0 && got < 200);
}
}
assert!(soft > 50, "a soft brush shows a soft edge");
assert_eq!(trail.coverage(50, 30 + 10), 0, "half pressure halves the 30 px tip");
}
#[test]
fn layer_style_dialog_previews_live_and_cancel_restores() {
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default());
+71 -2
View File
@@ -2,11 +2,14 @@
//! Selection show while dragging: the union of the brush's round footprint along the drag.
//!
//! It is a coverage mask in document space, stamped segment by segment as the pointer moves and
//! drawn as one texture over the document. It used to be an egui polyline as wide as the brush:
//! drawn as one texture over the document. The Spot Healing Brush's trail is instead the coverage
//! of the brush's own dabs ([`Trail::feed_dabs`]): hardness, pressure and shape dynamics show as
//! they heal (#2364). It used to be an egui polyline as wide as the brush:
//! egui tessellates a polyline whose width is much larger than its steps (any zoomed-in drag)
//! into hard pie wedges fanning out across the canvas (#189), and translucent overlaps doubled up.
use egui::{Color32, ColorImage, Rect, TextureHandle, TextureOptions, pos2};
use photocraft_paint::{BrushSettings, StrokePoint, StrokeRenderer};
/// Longest side of the mask, in mask pixels (big documents are stamped at a reduced scale).
const MAX_SIDE: u32 = 1024;
@@ -26,13 +29,22 @@ pub struct Trail {
/// Mask pixels changed since the last upload (x0, y0, x1, y1), if any.
dirty: Option<[usize; 4]>,
tex: Option<TextureHandle>,
/// The stroke's dabs, when the trail shows their coverage ([`Self::feed_dabs`]).
dabs: Option<Dabs>,
}
/// The brush stroke behind a [`Trail::feed_dabs`] trail.
struct Dabs {
renderer: StrokeRenderer,
/// Document pixels the last "finish now" tail (a lone first dab, smoothing catch-up) covered.
tail: photocraft_geom::Rect,
}
impl Trail {
pub fn new(doc: [u32; 2]) -> Self {
let k = doc[0].max(doc[1]).div_ceil(MAX_SIDE).max(1);
let (w, h) = (doc[0].div_ceil(k).max(1) as usize, doc[1].div_ceil(k).max(1) as usize);
Self { doc, k: k as f32, w, h, cov: vec![0; w * h], fed: 0, dirty: None, tex: None }
Self { doc, k: k as f32, w, h, cov: vec![0; w * h], fed: 0, dirty: None, tex: None, dabs: None }
}
/// Coverage at a mask pixel (tests).
@@ -62,6 +74,63 @@ impl Trail {
self.fed = points.len();
}
/// Show the coverage the brush's own dabs leave along the drag, as the stroke commit renders
/// it (`photocraft_paint::retouch::stroke_coverage`): soft edges for a soft brush, the size and
/// shape each dab takes from pen pressure and dynamics. `brush` is asked for once, on the first
/// call. What finishing the stroke now would add (the press's lone first dab) shows too.
pub fn feed_dabs(&mut self, points: &[[f64; 3]], brush: impl FnOnce() -> BrushSettings) {
if self.dabs.is_some() && self.fed >= points.len() {
return;
}
let mut dabs = self.dabs.take().unwrap_or_else(|| Dabs { renderer: StrokeRenderer::new(&brush(), None, 1.0), tail: photocraft_geom::Rect::EMPTY });
let new: Vec<StrokePoint> = points
.get(self.fed..)
.unwrap_or(&[])
.iter()
.filter(|p| p.iter().all(|v| v.is_finite()) && p[0].abs() < 1e7 && p[1].abs() < 1e7)
.map(|p| StrokePoint::new(p[0], p[1], p[2] as f32))
.collect();
self.fed = points.len();
dabs.renderer.push(&new);
let mut changed = dabs.renderer.take_dirty_rect().union(&dabs.tail);
let tail = dabs.renderer.tail_preview().map(|mut t| (t.take_dirty_rect(), t));
dabs.tail = tail.as_ref().map_or(photocraft_geom::Rect::EMPTY, |(r, _)| *r);
changed = changed.union(&dabs.tail);
let tail = tail.map(|(_, t)| t);
let renderer = &dabs.renderer;
self.restamp(changed, |x, y| renderer.coverage_at(x, y).max(tail.as_ref().map_or(0.0, |t| t.coverage_at(x, y))));
self.dabs = Some(dabs);
}
/// Recompute the mask pixels over the document rectangle `r` from `cov` (document pixel →
/// coverage 0..=1), sampled at each mask pixel's centre.
fn restamp(&mut self, r: photocraft_geom::Rect, cov: impl Fn(i32, i32) -> f32) {
if r.is_empty() {
return;
}
let k = f64::from(self.k);
let x0 = (f64::from(r.x0) / k).floor().max(0.0) as usize;
let y0 = (f64::from(r.y0) / k).floor().max(0.0) as usize;
let x1 = (f64::from(r.x1) / k).ceil().clamp(0.0, self.w as f64) as usize;
let y1 = (f64::from(r.y1) / k).ceil().clamp(0.0, self.h as f64) as usize;
if x1 <= x0 || y1 <= y0 {
return;
}
for y in y0..y1 {
let dy = ((y as f64 + 0.5) * k).floor() as i32;
for x in x0..x1 {
let dx = ((x as f64 + 0.5) * k).floor() as i32;
let c = cov(dx, dy);
let c = if c.is_finite() { (c.clamp(0.0, 1.0) * 255.0).round() as u8 } else { 0 };
if let Some(v) = self.cov.get_mut(y * self.w + x) {
*v = c;
}
}
}
let d = self.dirty.get_or_insert([x0, y0, x1, y1]);
*d = [d[0].min(x0), d[1].min(y0), d[2].max(x1), d[3].max(y1)];
}
fn capsule(&mut self, a: [f64; 2], b: [f64; 2], r: f64) {
if !(a.iter().chain(&b).all(|v| v.is_finite()) && r.is_finite()) {
return;