PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Spot Healing: Proximity Match samples the original pixels (#1968)
Nigel Ihza Firdausyc02ffa975551
diff --git a/crates/engine/src/retouch_cmds.rs b/crates/engine/src/retouch_cmds.rsindex 242bba76b..472bea882 100644--- a/crates/engine/src/retouch_cmds.rs+++ b/crates/engine/src/retouch_cmds.rs@@ -552,6 +552,30 @@ fn dilate(w: usize, h: usize, m: &[bool], r: usize) -> Vec<bool> {out}+/// Copy a translated Proximity Match patch from the *pre-healing* pixels. Reading+/// `out` here would make overlapping offsets repeatedly copy the pixels just written,+/// leaving streaks instead of the selected source texture.+fn proximity_source_patch(img: &[f32], w: usize, h: usize, ch: usize, mask: &[bool], dx: i32, dy: i32) -> Option<Vec<f32>> {+ if w == 0 || h == 0 || ch == 0 || mask.len() != w.checked_mul(h)? || img.len() != mask.len().checked_mul(ch)? {+ return None;+ }+ let mut out = img.to_vec();+ for (p, masked) in mask.iter().enumerate() {+ if !masked {+ continue;+ }+ let x = i32::try_from(p % w).ok()?.checked_add(dx)?;+ let y = i32::try_from(p / w).ok()?.checked_add(dy)?;+ if x < 0 || y < 0 || x >= i32::try_from(w).ok()? || y >= i32::try_from(h).ok()? {+ return None;+ }+ let src = (usize::try_from(y).ok()?.checked_mul(w)? + usize::try_from(x).ok()?).checked_mul(ch)?;+ let dst = p.checked_mul(ch)?;+ out.get_mut(dst..dst + ch)?.copy_from_slice(img.get(src..src + ch)?);+ }+ Some(out)+}+#[derive(Clone, Copy, Debug, PartialEq, Eq)]enum SpotType {ContentAware,@@ -597,18 +621,7 @@ fn spot_heal_surface(surf: &mut Surface, pre: &Document, stroke: &Stroke, kind:SpotType::ProximityMatch => {let ring = (size / 8.0).clamp(3.0, 16.0) as usize;match inpaint::best_offset(w, h, ch, &img.data, &domain, ring, margin) {- Some((dx, dy)) => {- let mut out = img.data.clone();- for y in 0..h {- for x in 0..w {- if domain[y * w + x] {- let (i, j) = ((y * w + x) * ch, (((y as i32 + dy) as usize) * w + (x as i32 + dx) as usize) * ch);- out.copy_within(j..j + ch, i);- }- }- }- out- }+ Some((dx, dy)) => proximity_source_patch(&img.data, w, h, ch, &domain, dx, dy).unwrap_or_else(|| content_aware(&img.data)),None => content_aware(&img.data),}}diff --git a/crates/engine/src/retouch_cmds/tests.rs b/crates/engine/src/retouch_cmds/tests.rsindex bb13463e2..ed7eb271b 100644--- a/crates/engine/src/retouch_cmds/tests.rs+++ b/crates/engine/src/retouch_cmds/tests.rs@@ -3,6 +3,37 @@ use photocraft_color::ColorMode;const DEPTHS: [u64; 3] = [8, 16, 32];+#[test]+fn proximity_match_reads_original_pixels_for_overlapping_offsets() {+ // Shifting source one pixel to the left overlaps the destination in a+ // left-to-right traversal: reading the output would repeat the first value.+ let img = vec![10.0, 20.0, 30.0, 40.0, 50.0, 60.0];+ let mask = [false, true, true, true, false, false];+ let result = proximity_source_patch(&img, 6, 1, 1, &mask, -1, 0).unwrap();+ assert_eq!(result, vec![10.0, 10.0, 20.0, 30.0, 50.0, 60.0]);+ assert_eq!(img, vec![10.0, 20.0, 30.0, 40.0, 50.0, 60.0], "source remains untouched");+}++#[test]+fn proximity_match_preserves_channels_and_unmasked_pixels() {+ // A two-channel image shifted from the row above.+ let img = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0];+ let mask = [false, false, true, false];+ let result = proximity_source_patch(&img, 2, 2, 2, &mask, 0, -1).unwrap();+ assert_eq!(result, vec![1.0, 2.0, 3.0, 4.0, 1.0, 2.0, 7.0, 8.0]);+}++#[test]+fn proximity_match_rejects_invalid_translations_without_panicking() {+ let img = vec![1.0, 2.0, 3.0, 4.0];+ let mask = [true, false, false, false];+ assert!(proximity_source_patch(&img, 2, 2, 1, &mask, -1, 0).is_none());+ assert!(proximity_source_patch(&img, 2, 2, 1, &mask, 0, -1).is_none());+ assert!(proximity_source_patch(&img, 2, 2, 1, &mask, i32::MAX, 0).is_none());+ assert!(proximity_source_patch(&img, 2, 2, 1, &mask[..3], 0, 0).is_none());+ assert!(proximity_source_patch(&img, usize::MAX, 2, 1, &mask, 0, 0).is_none());+}+#[test]fn clone_preview_matches_source_without_mutating_session() {use crate::presets::clone_source::Mapping;