Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

fix(retouch): Spot Healing honours Sample All Layers (#769)

reese1353917f1501
diff --git a/crates/engine/src/retouch_cmds.rs b/crates/engine/src/retouch_cmds.rs
index 8dffa256f..1a18398e7 100644
--- a/crates/engine/src/retouch_cmds.rs
+++ b/crates/engine/src/retouch_cmds.rs
@@ -379,17 +379,18 @@ enum SpotType {
ProximityMatch,
}
-/// Fill the stroke area with no source, then gradient-domain blend it into its surroundings.
-fn spot_heal_surface(surf: &mut Surface, area: Rect, stroke: &Stroke, kind: SpotType, sel: Option<&Surface>, lock: bool) -> Rect {
+/// Fill the stroke area with no source, then gradient-domain blend it into its surroundings. With
+/// `all` (Sample All Layers) it heals what is visible, so it works on an empty layer above the image.
+fn spot_heal_surface(surf: &mut Surface, pre: &Document, stroke: &Stroke, kind: SpotType, all: bool, sel: Option<&Surface>, lock: bool) -> Rect {
let (bounds, cov) = stroke_coverage(stroke);
let size = stroke.brush.size;
let margin = (size.max(8.0) * 1.0).ceil() as i32 + 8;
- let region_rect = bounds.inflate(margin).intersect(&area);
+ let region_rect = bounds.inflate(margin).intersect(&pre.bounds());
if region_rect.is_empty() {
return Rect::EMPTY;
}
let fmt = surf.format();
- let img = Region::read(surf, region_rect);
+ let img = if all { composite_region(pre, None, SampleLayers::All, region_rect, fmt) } else { Region::read(surf, region_rect) };
let (w, h, ch) = (img.width(), img.height(), img.ch);
let bw = bounds.width() as usize;
let mut stroke_cov = vec![0.0f32; w * h];
@@ -448,7 +449,8 @@ fn spot_healing(s: &mut Session, p: &Value) -> Result<Value> {
"proximityMatch" => SpotType::ProximityMatch,
o => return Err(bad(CMD, format!("unknown type `{o}` (contentAware|createTexture|proximityMatch)"))),
};
- let dmg = run_stroke(s, "Spot Healing Brush", id, p, |pre, surf, sel, lock| Ok(spot_heal_surface(surf, pre.bounds(), &stroke, kind, sel, lock)))?;
+ let all = sample_all_layers(p);
+ let dmg = run_stroke(s, "Spot Healing Brush", id, p, |pre, surf, sel, lock| Ok(spot_heal_surface(surf, pre, &stroke, kind, all, sel, lock)))?;
Ok(json!({ "damage": damage_json(dmg) }))
}
@@ -531,8 +533,8 @@ fn sponge_cmd(s: &mut Session, p: &Value) -> Result<Value> {
Ok(json!({ "damage": damage_json(dmg) }))
}
-/// "Sample All Layers" for Blur, Sharpen and Smudge (layer pixels only: a mask or channel has
-/// nothing to sample from other layers).
+/// "Sample All Layers" for Spot Healing, Blur, Sharpen and Smudge (layer pixels only: a mask or
+/// channel has nothing to sample from other layers).
fn sample_all_layers(p: &Value) -> bool {
flag(p, "sampleAllLayers", false) && targets_pixels(p)
}
@@ -681,7 +683,7 @@ pub fn specs() -> Vec<CommandSpec> {
label: "Spot Healing Brush",
menu: &[],
shortcut: None,
- params: brush_params!(r#","type":"contentAware|createTexture|proximityMatch"="contentAware""#),
+ params: brush_params!(r#","type":"contentAware|createTexture|proximityMatch"="contentAware","sampleAllLayers":bool=false"#),
enabled: has_pixel_layer,
run: spot_healing,
journal: true,
diff --git a/crates/engine/src/retouch_cmds/tests.rs b/crates/engine/src/retouch_cmds/tests.rs
index 11334b33f..662918793 100644
--- a/crates/engine/src/retouch_cmds/tests.rs
+++ b/crates/engine/src/retouch_cmds/tests.rs
@@ -520,6 +520,25 @@ fn sample_all_layers_smudge_drags_colour_from_below() {
s.execute("paint.smudge", json!({"points": [[20, 25], [70, 25]], "size": 6, "sampleAllLayers": true, "target": "mask"})).unwrap();
}
+#[test]
+fn sample_all_layers_spot_healing_heals_onto_an_empty_layer() {
+ // #731: the blemish below is healed onto the empty layer.
+ for depth in DEPTHS {
+ let heal = |all: bool| {
+ let mut s = session(100, 30, depth, "rgb");
+ paint_layer(&mut s, blemished(46));
+ s.execute("layer.new.layer", json!({})).unwrap();
+ s.execute("paint.spotHealing", json!({"points": [[50, 24]], "size": 12, "hardness": 100, "sampleAllLayers": all})).unwrap();
+ s
+ };
+ assert_eq!(rgba(&heal(false), 50, 24)[3], 0.0, "depth {depth}: without Sample All Layers the layer stays empty");
+ let s = heal(true);
+ let (p, t) = (rgba(&s, 50, 24), texture(50, 24));
+ assert!(p[3] > 0.99 && (p[0] - t[0]).abs() < 0.1 && (p[1] - t[1]).abs() < 0.1, "depth {depth}: healed, not red: {p:?} want {t:?}");
+ assert_eq!(rgba(&s, 80, 15)[3], 0.0, "depth {depth}: outside the stroke untouched");
+ }
+}
+
#[test]
fn smudge_across_a_transparent_edge_has_no_dark_fringe() {
for depth in DEPTHS {
diff --git a/crates/ui-egui/src/retouch_ui.rs b/crates/ui-egui/src/retouch_ui.rs
index 4fb2dceed..bf4082c5e 100644
--- a/crates/ui-egui/src/retouch_ui.rs
+++ b/crates/ui-egui/src/retouch_ui.rs
@@ -15,7 +15,7 @@ pub fn finish_stroke(app: &mut PhotocraftApp, tool: Tool, points: &[[f64; 3]], m
let o = app.ui.tool_options.clone();
let pts = json!(points);
let (cmd, mut p): (&str, Value) = match tool {
- Tool::SpotHealing => ("paint.spotHealing", json!({"type": o.spot_type})),
+ Tool::SpotHealing => ("paint.spotHealing", json!({"type": o.spot_type, "sampleAllLayers": o.sample_all_layers})),
Tool::MixerBrush => ("paint.mixerBrush", json!({})),
Tool::Healing | Tool::CloneStamp => {
let mut p = json!({"aligned": o.clone_aligned, "sampleLayer": o.clone_sample});
@@ -335,9 +335,9 @@ mod tests {
}
#[test]
- fn sample_all_layers_reaches_blur_sharpen_and_smudge() {
- // #207: the options-bar checkbox reaches the command.
- for tool in [Tool::Blur, Tool::Sharpen, Tool::Smudge] {
+ fn sample_all_layers_reaches_the_command() {
+ // #207, #731: the options-bar checkbox reaches the command.
+ for tool in [Tool::SpotHealing, Tool::Blur, Tool::Sharpen, Tool::Smudge] {
for all in [false, true] {
let mut app = app();
stripes(&mut app, 4, "pixels");