PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Spot Healing: Create Texture keeps the grain, joining blocks along min-error cuts (#2104) (#2762)
Valentin Klein1320dac5bf90
diff --git a/crates/algo/src/inpaint.rs b/crates/algo/src/inpaint.rsindex 4f485ad46..215c337a5 100644--- a/crates/algo/src/inpaint.rs+++ b/crates/algo/src/inpaint.rs@@ -27,7 +27,8 @@//! ring around the hole (sum of squared differences), i.e. an automatic Healing Brush source.//! * [`synthesize`], create texture: patch-based texture synthesis in the style of A. Efros,//! W. Freeman, *Image Quilting for Texture Synthesis and Transfer*, SIGGRAPH 2001 (blocks picked from-//! random candidates by overlap error, feathered overlap instead of a min-cut seam).+//! random candidates by overlap error, joined along minimum-error boundary cuts, so the texture is+//! copied, never averaged).//!//! All functions take interleaved `w × h × ch` normalised floats (any colour model/depth) and a//! `w × h` hole mask, and return a full buffer equal to the input outside the hole. They are@@ -971,20 +972,34 @@ pub fn synthesize(w: usize, h: usize, ch: usize, img: &[f32], hole: &[bool], blo}}let c = best.0;- for y in (by - ov)..(by + b + ov) {- for x in (bx - ov)..(bx + b + ov) {- if x < 0 || y < 0 || x >= w as i32 || y >= h as i32 {- continue;- }+ let (x0, y0) = ((bx - ov).max(0), (by - ov).max(0));+ let (x1, y1) = ((bx + b + ov).min(w as i32), (by + b + ov).min(h as i32));+ let src = |x: i32, y: i32| ((y - by + c.1) as usize * w + (x - bx + c.0) as usize) * ch;+ // Squared difference between what is there and the block, where both exist.+ let err = |x: i32, y: i32| {+ let i = y as usize * w + x as usize;+ if !(hole[i] && filled[i]) {+ return 0.0;+ }+ let (a, s) = (i * ch, src(x, y));+ (0..ch).map(|q| (out[a + q] - img[s + q]).powi(2)).sum::<f32>()+ };+ // Minimum-error boundary cuts through the overlaps with the blocks to the left and+ // above: the block is copied right of and below them, untouched pixels are kept+ // on the other side. Copying (rather than averaging) keeps the texture's grain.+ let left = min_cut((x0..(bx + ov).min(x1)).len(), (y0..y1).len(), |u, v| err(x0 + u as i32, y0 + v as i32));+ let top = min_cut((y0..(by + ov).min(y1)).len(), (x0..x1).len(), |u, v| err(x0 + v as i32, y0 + u as i32));+ for y in y0..y1 {+ for x in x0..x1 {let i = y as usize * w + x as usize;if !hole[i] {continue;}- let (a, s) = (i * ch, ((y - by + c.1) as usize * w + (x - bx + c.0) as usize) * ch);- // Feather into already-synthesised overlap.- let k = if filled[i] { 0.5 } else { 1.0 };- for q in 0..ch {- out[a + q] += (img[s + q] - out[a + q]) * k;+ let (u, v) = ((x - x0) as usize, (y - y0) as usize);+ let keep = filled[i] && (left.get(v).is_some_and(|s| u < *s) || top.get(u).is_some_and(|s| v < *s));+ if !keep {+ let (a, s) = (i * ch, src(x, y));+ out[a..a + ch].copy_from_slice(&img[s..s + ch]);}filled[i] = true;}@@ -997,6 +1012,39 @@ pub fn synthesize(w: usize, h: usize, ch: usize, img: &[f32], hole: &[bool], bloout}+/// The minimum-error cut through a band `across` wide and `along` long (Efros & Freeman's+/// boundary cut, by dynamic programming): for each step along the band, the offset across it+/// where the cut runs (`err(across, along)` is the cost of a pixel). Pixels before the cut keep+/// what is there. Empty when there is no cost anywhere (nothing to cut through).+fn min_cut(across: usize, along: usize, err: impl Fn(usize, usize) -> f32) -> Vec<usize> {+ if across == 0 || along == 0 {+ return Vec::new();+ }+ // Cumulative cost, step by step along the band; the cut may move by one pixel per step.+ let mut acc: Vec<f32> = (0..along).flat_map(|v| (0..across).map(move |u| (u, v))).map(|(u, v)| err(u, v)).collect();+ if acc.iter().all(|x| *x == 0.0) {+ return Vec::new();+ }+ for v in 1..along {+ for u in 0..across {+ let lo = u.saturating_sub(1);+ let hi = (u + 1).min(across - 1);+ let m = (lo..=hi).map(|k| acc[(v - 1) * across + k]).fold(f32::INFINITY, f32::min);+ acc[v * across + u] += m;+ }+ }+ let argmin = |v: usize, lo: usize, hi: usize| (lo..=hi).min_by(|a, b| acc[v * across + a].total_cmp(&acc[v * across + b])).unwrap_or(lo);+ let mut cut = vec![0; along];+ let mut u = argmin(along - 1, 0, across - 1);+ for v in (0..along).rev() {+ if v + 1 < along {+ u = argmin(v, u.saturating_sub(1), (u + 1).min(across - 1));+ }+ cut[v] = u;+ }+ cut+}+#[cfg(test)]mod tests {use super::*;diff --git a/crates/engine/src/retouch_cmds/tests.rs b/crates/engine/src/retouch_cmds/tests.rsindex b13981ea5..6ceb14c10 100644--- a/crates/engine/src/retouch_cmds/tests.rs+++ b/crates/engine/src/retouch_cmds/tests.rs@@ -1474,3 +1474,38 @@ fn remove_bad_params_are_errors() {let err = s.execute("paint.remove", json!({"points": [[48, 32]], "size": 3000})).unwrap_err().to_string();assert!(err.contains("nothing is left"), "{err}");}++/// Flat (150, 120, 100) with Gaussian film grain of σ 12 levels (the #2104 test image).+fn film_grain(x: i32, y: i32) -> [f32; 4] {+ let u = |k: u32| {+ let mut z = (x as u32).wrapping_mul(0x9E37_79B9) ^ (y as u32).wrapping_mul(0x85EB_CA6B) ^ k.wrapping_mul(0xC2B2_AE35);+ z = (z ^ (z >> 16)).wrapping_mul(0x7FEB_352D);+ z = (z ^ (z >> 15)).wrapping_mul(0x846C_A68B);+ ((z ^ (z >> 16)) as f32 + 1.0) / (u32::MAX as f32 + 2.0)+ };+ let n = (-2.0 * u(1).ln()).sqrt() * (std::f32::consts::TAU * u(2)).cos() * 12.0;+ [(150.0 + n) / 255.0, (120.0 + n) / 255.0, (100.0 + n) / 255.0, 1.0]+}++#[test]+fn spot_healing_keeps_film_grain_through_overlapping_strokes() {+ // #2104: on flat colour with grain the heal must carry the grain, not leave a smooth patch,+ // and must not lose more with each overlapping stroke (each samples the ones before).+ let red = |s: &Session, x0: i32, x1: i32, y0: i32, y1: i32| {+ let v: Vec<f32> = (y0..y1).flat_map(|y| (x0..x1).map(move |x| (x, y))).map(|(x, y)| rgba(s, x, y)[0] * 255.0).collect();+ let m = v.iter().sum::<f32>() / v.len() as f32;+ (m, (v.iter().map(|a| (a - m).powi(2)).sum::<f32>() / v.len() as f32).sqrt())+ };+ for (depth, kind) in [(8u64, "contentAware"), (8, "proximityMatch"), (8, "createTexture"), (16, "createTexture")] {+ let mut s = session(400, 300, depth, "rgb");+ paint_layer(&mut s, film_grain);+ let (m0, sd0) = red(&s, 0, 100, 0, 100);+ for i in 0..8 {+ let y = 100 + 10 * i;+ s.execute("paint.spotHealing", json!({"points": [[140, y], [260, y]], "size": 30, "hardness": 100, "type": kind})).unwrap();+ let (m, sd) = red(&s, 150, 250, y - 4, y + 5);+ assert!(sd > 0.8 * sd0 && sd < 1.25 * sd0, "{kind} depth {depth} stroke {}: grain {sd} vs {sd0} around", i + 1);+ assert!((m - m0).abs() < 3.0, "{kind} depth {depth} stroke {}: mean {m} vs {m0}", i + 1);+ }+ }+}