PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Selection feather: the radius is the Gaussian's sigma, as in Photoshop (#2529)
MrCloudy281ed59b0e004
diff --git a/crates/algo/src/selection.rs b/crates/algo/src/selection.rsindex 4ecc1aa80..4afaec526 100644--- a/crates/algo/src/selection.rs+++ b/crates/algo/src/selection.rs@@ -741,7 +741,7 @@ pub fn smooth(m: &[f32], w: usize, h: usize, r: f32) -> Vec<f32> {crate::selection_blur::smooth(m, w, h, r)}-/// Feather: Gaussian blur of the mask with sigma = radius / 2 (kernel truncated at 3σ, zero+/// Feather: Gaussian blur of the mask with sigma = radius, as in Photoshop (kernel truncated at 3σ, zero/// beyond the canvas). Parallel, confined to the selection's bounds, and independent of the/// radius (#211).pub fn feather(m: &[f32], w: usize, h: usize, radius: f32) -> Vec<f32> {@@ -1105,7 +1105,8 @@ mod tests {let s = smooth(&m, 20, 20, 1.0);assert_eq!(s[2 * 20 + 2], 0.0);assert_eq!(s[10 * 20 + 10], 1.0);- let f = feather(&square(20, 20), 20, 20, 4.0);+ // Radius 2 (σ 2) keeps the blur inside the 20×20 canvas, so no coverage is lost.+ let f = feather(&square(20, 20), 20, 20, 2.0);let edge = f[10 * 20 + 5];assert!(edge > 0.3 && edge < 0.8, "{edge}");let sum: f32 = f.iter().sum();diff --git a/crates/algo/src/selection_blur.rs b/crates/algo/src/selection_blur.rsindex 1ab57dfea..1d83254cd 100644--- a/crates/algo/src/selection_blur.rs+++ b/crates/algo/src/selection_blur.rs@@ -4,7 +4,7 @@//!//! * **Smooth** is a box average with edge-replicated borders: a running sum per line, `O(1)` per//! pixel whatever the radius.-//! * **Feather** is a Gaussian (`σ = radius / 2`, kernel truncated at `⌈3σ⌉` and normalized, zero+//! * **Feather** is a Gaussian (`σ = radius`, as Photoshop's, kernel truncated at `⌈3σ⌉` and normalized, zero//! outside the canvas). Small kernels are convolved directly. Wide ones are written as a short//! cosine series over the kernel window, `k(j) ≈ a₀ + Σ aₖ cos(ωₖ j)`, and every term is a//! sliding windowed sum `Σ m[x+j]·e^{iωₖj}` updated in `O(1)` per pixel, so the cost doesn't grow@@ -539,12 +539,15 @@ impl CosKernel {}}-/// Select › Modify › Feather: Gaussian blur of the mask with `σ = radius / 2`, the kernel-/// truncated at `⌈3σ⌉` and normalized, zero beyond the canvas.+/// Select › Modify › Feather: Gaussian blur of the mask with `σ = radius`, the kernel truncated+/// at `⌈3σ⌉` and normalized, zero beyond the canvas. Photoshop's feather radius is the Gaussian's+/// σ (measured on Photoshop 27.11; the same mapping the mask feather uses, fitted on the+/// psd-tools corpus in `photocraft_compose::masks::feather_sigma`). It was `radius / 2`: half+/// as wide as Photoshop's.pub(crate) fn feather(m: &[f32], w: usize, h: usize, radius: f32) -> Vec<f32> {let n = w.saturating_mul(h);let sigma = if radius.is_finite() {- radius.max(0.0) / 2.0+ radius.max(0.0)} else if radius > 0.0 {MAX_SIGMA} else {@@ -618,7 +621,7 @@ mod tests {}pub fn feather(m: &[f32], w: usize, h: usize, radius: f32) -> Vec<f32> {- let sigma = radius.max(0.0) / 2.0;+ let sigma = radius.max(0.0);if sigma < 0.1 {return m.to_vec();}@@ -680,6 +683,27 @@ mod tests {vec![("binary", binary), ("soft", soft), ("blob", blob), ("sparse", sparse), ("rect", rect)]}+ /// Photoshop 27.11, measured: a 32×32 rectangle at (16,16) in 64×64, Select › Modify ›+ /// Feather 4 px, filled white. Row y = 32 for x = 8..=24, then (16,16), (18,18), (20,20).+ #[test]+ fn feather_matches_photoshop() {+ const ROW: [u8; 17] = [7, 13, 22, 34, 50, 70, 92, 115, 140, 163, 185, 205, 221, 233, 242, 248, 252];+ const DIAG: [(usize, u8); 3] = [(16, 76), (18, 134), (20, 190)];+ let (w, h) = (64, 64);+ let m: Vec<f32> = (0..w * h).map(|i| if (16..48).contains(&(i % w)) && (16..48).contains(&(i / w)) { 1.0 } else { 0.0 }).collect();+ let f = feather(&m, w, h, 4.0);+ let v = |x: usize, y: usize| (f[y * w + x] * 255.0).round() as i32;+ for (i, want) in ROW.iter().enumerate() {+ let x = 8 + i;+ assert!((v(x, 32) - i32::from(*want)).abs() <= 3, "x {x}: {} vs Photoshop {want}", v(x, 32));+ }+ // Photoshop's corners come out a few levels darker than a true 2-D Gaussian's; the row+ // is the defining measurement.+ for (d, want) in DIAG {+ assert!((v(d, d) - i32::from(want)).abs() <= 4, "({d},{d}): {} vs Photoshop {want}", v(d, d));+ }+ }+#[test]fn feather_matches_direct_gaussian() {for (w, h, seed) in [(97, 61, 1), (40, 150, 2), (13, 7, 3)] {diff --git a/crates/engine/src/selection_cmds.rs b/crates/engine/src/selection_cmds.rsindex d9384ca3d..948ef0763 100644--- a/crates/engine/src/selection_cmds.rs+++ b/crates/engine/src/selection_cmds.rs@@ -386,9 +386,10 @@ fn modify(s: &mut Session, p: &Value, op: &str) -> Result<Value> {let at_bounds = op == "border" || b(p, "applyAtCanvasBounds", false);let (area, m) = current_mask(&s.active().ok_or(EngineError::NoDocument)?.doc);let (w, h) = (area.width() as usize, area.height() as usize);- // Feather reaches 3σ = 1.5 r; the others reach r. A margin past that keeps the canvas edge out of- // reach. A selection clear of the canvas edge needs none: both readings agree there.- let reach = if op == "feather" { r * 1.5 } else { r };+ // Feather reaches 3σ = 3 r (σ = r, as in Photoshop); the others reach r. A margin past that+ // keeps the canvas edge out of reach. A selection clear of the canvas edge needs none: both+ // readings agree there.+ let reach = if op == "feather" { r * 3.0 } else { r };let pad = if touches_edge(&m, w, h) { reach.ceil() as usize + 2 } else { 0 };let grown = |n: usize| pad.checked_mul(2).and_then(|p| n.checked_add(p));let (Some(pw), Some(ph)) = (grown(w), grown(h)) else {diff --git a/crates/engine/src/vector_cmds.rs b/crates/engine/src/vector_cmds.rsindex c4f343374..a91a4cadf 100644--- a/crates/engine/src/vector_cmds.rs+++ b/crates/engine/src/vector_cmds.rs@@ -1001,7 +1001,8 @@ fn path_fill(s: &mut Session, p: &Value) -> Result<Value> {s.edit("Fill Path", |doc, _| {let area = doc.bounds();let r = vector::fill_rasterizer(&path, vector::DEFAULT_TOLERANCE);- let feather_pad = (feather.ceil() as i32).saturating_mul(2);+ // The feather reaches 3σ = 3 × radius (σ = radius, as in Photoshop), plus a pixel.+ let feather_pad = (feather.ceil() as i32).saturating_mul(3).saturating_add(1);let area = r.pixel_bounds().map_or(area, |b| b.inflate(feather_pad).intersect(&area));if area.is_empty() {return Ok(());