From de014e1fd09dc12a99bc733faedb8019547e62da Mon Sep 17 00:00:00 2001 From: Hector Villarreal Date: Sat, 31 Oct 2026 02:01:43 -0600 Subject: [PATCH 1/2] Small mask feathers spread as Photoshop's do (#9476) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A 1 px Properties › Feather was up to 8 levels narrower than Photoshop's on a binary mask. Photoshop's small feathers spread wider than sigma = feather: the feather sigma is now sqrt(r² + min(r, 1) / 6), which keeps 1–10 px within 2–3 levels of measured Photoshop edges, and tiny feathers still barely blur. Co-Authored-By: Claude Opus 5.5 --- crates/vector/src/feather.rs | 50 +++++++++++++++++++++++++++++++----- 1 file changed, 43 insertions(+), 7 deletions(-) diff --git a/crates/vector/src/feather.rs b/crates/vector/src/feather.rs index 2e3cd10d08..1139884974 100644 --- a/crates/vector/src/feather.rs +++ b/crates/vector/src/feather.rs @@ -5,17 +5,27 @@ use photocraft_doc::Path; use photocraft_geom::Rect; -/// Gaussian sigma of a mask feather of `px` pixels (Properties › Feather): the feather itself. -/// Blurred with exactly that variance, psd-tools mask-parameters-no-real-channel.psd (6 px vector -/// and 3 px pixel feather) matches Photoshop within 1.5 levels and layer_mask_data.psd's -/// feathered edges are 27 % → 4 % off (the rest are its shapes' antialiased edges). +/// Gaussian sigma of a mask feather of `px` pixels (Properties › Feather): the feather itself, +/// widened by [`SMALL_FEATHER_VARIANCE`] for the small ones. Blurred with that variance, +/// psd-tools mask-parameters-no-real-channel.psd (6 px vector and 3 px pixel feather) matches +/// Photoshop within 1.5 levels and layer_mask_data.psd's feathered edges are 27 % → 4 % off (the +/// rest are its shapes' antialiased edges). pub fn feather_sigma(px: f32) -> f32 { - if px.is_finite() { (px * FEATHER_SIGMA).clamp(0.0, MAX_FEATHER_SIGMA) } else { 0.0 } + if px.is_finite() && px > 0.0 { + let px = px.min(MAX_FEATHER_SIGMA); + (px * px + SMALL_FEATHER_VARIANCE * px.min(1.0)).sqrt().min(MAX_FEATHER_SIGMA) + } else { + 0.0 + } } -const FEATHER_SIGMA: f32 = 1.0; +/// Photoshop's small feathers spread wider than sigma = feather: measured on a binary edge, a +/// 1 px feather has the variance 1.18, not 1, and from 1 to 10 px sigma² = feather² + 1/6 keeps +/// its edge within 2 levels (sigma = feather was 8 off at 1 px, #9476). Below 1 px the extra +/// variance shrinks with the feather, so a tiny feather still barely blurs. +const SMALL_FEATHER_VARIANCE: f32 = 1.0 / 6.0; /// Photoshop's feather tops out at 1000 px. -const MAX_FEATHER_SIGMA: f32 = 1000.0 * FEATHER_SIGMA; +const MAX_FEATHER_SIGMA: f32 = 1000.0; /// `r` grown by a feather of Gaussian `sigma`: how far the blur carries a mask's edge (three /// sigmas and two pixels, beyond which it is constant to well under a level). @@ -318,6 +328,32 @@ mod tests { } } + /// #9476: Photoshop's Properties › Feather on a synthetic 256×128 mask, white on x 64..192, + /// y 32..96: row 64 from x 60 and the pixel (64, 33), as 8-bit levels. Sigma = feather was up + /// to 8 levels off at 1 px. + #[test] + fn small_feathers_spread_as_photoshops_do() { + let cases: [(f32, [u8; 8], u8); 3] = [ + (1.0, [0, 2, 20, 80, 175, 235, 253, 255], 161), + (1.5, [3, 14, 43, 95, 160, 212, 241, 252], 133), + (2.0, [12, 29, 60, 104, 151, 195, 226, 243], 116), + ]; + let (w, h) = (256usize, 128usize); + for (feather, row, corner) in cases { + let mut v: Vec = (0..w * h).map(|i| if (64..192).contains(&(i % w)) && (32..96).contains(&(i / w)) { 1.0 } else { 0.0 }).collect(); + gaussian(&mut v, w, h, feather_sigma(feather)); + let level = |x: usize, y: usize| (v[y * w + x] * 255.0).round() as i32; + for (x, &want) in (60..).zip(&row) { + assert!((level(x, 64) - i32::from(want)).abs() <= 2, "{feather} px at x {x}: {} vs Photoshop {want}", level(x, 64)); + } + assert!((level(64, 33) - i32::from(corner)).abs() <= 2, "{feather} px at (64, 33): {} vs Photoshop {corner}", level(64, 33)); + } + assert_eq!(feather_sigma(0.0), 0.0); + assert_eq!(feather_sigma(f32::NAN), 0.0); + assert!(feather_sigma(0.01) < 0.1, "a tiny feather barely blurs"); + assert!((feather_sigma(1000.0) - 1000.0).abs() < 1e-3); + } + #[test] fn parallel_gaussian_matches_the_sequential_reference() { let (w, h, sigma): (usize, usize, f32) = (320, 256, 13.0); From 774cc4a6b4ea1f2eb65c22b78071dc6085a2b2f0 Mon Sep 17 00:00:00 2001 From: Hector Villarreal Date: Sat, 31 Oct 2026 21:54:25 -0600 Subject: [PATCH 2/2] Tests: feather_sigma at 0.5, 3 and 6 px (FredApps review) Co-Authored-By: Claude Opus 5.5 --- crates/vector/src/feather.rs | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/crates/vector/src/feather.rs b/crates/vector/src/feather.rs index 1139884974..20a8ae8e44 100644 --- a/crates/vector/src/feather.rs +++ b/crates/vector/src/feather.rs @@ -306,9 +306,10 @@ mod tests { assert!(BoxPass::for_sigma(f32::MAX).is_some_and(|p| p.r < 2000 && (0.0..1.0).contains(&p.end))); } - /// A feathered edge follows the Gaussian Photoshop draws with sigma = feather: psd-tools - /// mask-parameters-no-real-channel.psd (a 6 px vector and 3 px pixel feather) matches it - /// within 1.5 levels, where whole-pixel boxes were 4 off. + /// A feathered edge follows the Gaussian Photoshop draws with sigma = [`feather_sigma`] (for + /// 6 px, within 0.02 px of the feather): psd-tools mask-parameters-no-real-channel.psd (a 6 px + /// vector and 3 px pixel feather) matches it within 1.5 levels, where whole-pixel boxes were + /// 4 off. #[test] fn a_feathered_edge_is_a_gaussian_step() { // erf, Abramowitz & Stegun 7.1.26 (1.5e-7). @@ -317,7 +318,7 @@ mod tests { let y = 1.0 - (((((1.061_405_429 * t - 1.453_152_027) * t) + 1.421_413_741) * t - 0.284_496_736) * t + 0.254_829_592) * t * (-x * x).exp(); y.copysign(x) }; - let (w, edge, sigma) = (240usize, 120usize, 6.0f32); + let (w, edge, sigma) = (240usize, 120usize, feather_sigma(6.0)); let mut v: Vec = (0..w).map(|i| if i >= edge { 1.0 } else { 0.0 }).collect(); gaussian(&mut v, w, 1, sigma); for (i, &got) in v.iter().enumerate().skip(edge - 24).take(48) { @@ -354,6 +355,19 @@ mod tests { assert!((feather_sigma(1000.0) - 1000.0).abs() < 1e-3); } + /// A half-pixel feather blurs between none and a 1 px one, and the 3 px and 6 px feathers + /// (psd-tools mask-parameters-no-real-channel.psd's) keep sigma = feather within 0.05 px, so + /// the small-feather widening leaves the large feathers' calibration as it was. + #[test] + fn feather_sigma_widens_only_small_feathers() { + let half = feather_sigma(0.5); + assert!(half.is_finite() && feather_sigma(0.0) < half && half < feather_sigma(1.0), "0.5 px: {half}"); + for feather in [3.0f32, 6.0] { + let sigma = feather_sigma(feather); + assert!((sigma - feather).abs() < 0.05, "{feather} px: sigma {sigma}"); + } + } + #[test] fn parallel_gaussian_matches_the_sequential_reference() { let (w, h, sigma): (usize, usize, f32) = (320, 256, 13.0);