From b84eb0fc744c88e8546aa353b95294118af539ad Mon Sep 17 00:00:00 2001 From: NaufalDzz Date: Mon, 16 Nov 2026 08:08:38 +0000 Subject: [PATCH] Layer Style: Satin's Invert flips the result after the contour (#14981) satin_map inverted the coverage before the contour LUT, so a curved contour with Invert on was not the opposite of the same Satin without Invert. Invert now applies after the contour on the CPU, and the GPU satin stage uses fs_mfinish's existing after-contour mode. Co-authored-by: OpenBot <335604065+openbotgit[bot]@users.noreply.github.com> --- crates/compose/src/effects.rs | 2 + crates/compose/src/effects/bevel.rs | 5 +- .../src/effects/satin_invert_contour_tests.rs | 80 +++++++++++++++++++ crates/gpu/src/fx.rs | 3 +- 4 files changed, 87 insertions(+), 3 deletions(-) create mode 100644 crates/compose/src/effects/satin_invert_contour_tests.rs diff --git a/crates/compose/src/effects.rs b/crates/compose/src/effects.rs index 077fae98b2..f4e3590c39 100644 --- a/crates/compose/src/effects.rs +++ b/crates/compose/src/effects.rs @@ -249,5 +249,7 @@ impl FxMaps { #[cfg(test)] mod tests; +#[cfg(test)] +mod satin_invert_contour_tests; #[cfg(test)] mod satin_size_one_tests; diff --git a/crates/compose/src/effects/bevel.rs b/crates/compose/src/effects/bevel.rs index 266c3f7613..42bd98de01 100644 --- a/crates/compose/src/effects/bevel.rs +++ b/crates/compose/src/effects/bevel.rs @@ -277,11 +277,12 @@ pub(super) fn satin_map(shape: &Map, s: &photocraft_doc::effects::Satin) -> Map tent(&mut a, s.size); tent(&mut b, s.size); } - let mut m = Map { w: shape.w, h: shape.h, v: a.v.iter().zip(&b.v).map(|(x, y)| (x - y).abs()).collect() }; + let m = Map { w: shape.w, h: shape.h, v: a.v.iter().zip(&b.v).map(|(x, y)| (x - y).abs()).collect() }; + let mut m = apply_lut(m, satin_lut(&s.contour)); if s.invert { + // #14981: Photoshop inverts after the contour, so Invert is the exact opposite of the same Satin without it. m = m.map(|v| 1.0 - v); } - let mut m = apply_lut(m, satin_lut(&s.contour)); for (v, a) in m.v.iter_mut().zip(&shape.v) { *v *= a; } diff --git a/crates/compose/src/effects/satin_invert_contour_tests.rs b/crates/compose/src/effects/satin_invert_contour_tests.rs new file mode 100644 index 0000000000..82094f17b8 --- /dev/null +++ b/crates/compose/src/effects/satin_invert_contour_tests.rs @@ -0,0 +1,80 @@ +use super::{Contour, Map, satin_map}; +use photocraft_color::{BlendMode, Color}; +use photocraft_doc::adjust::CurvePoint; +use photocraft_doc::{FxCommon, Satin}; + +/// The built-in Gaussian contour (input, output, smooth). +const GAUSSIAN: [(f32, f32, bool); 11] = [ + (0.0, 0.0, false), + (0.117, 0.023, true), + (0.172, 0.055, true), + (0.234, 0.117, true), + (0.305, 0.234, true), + (0.383, 0.414, true), + (0.523, 0.688, true), + (0.617, 0.812, true), + (0.703, 0.891, true), + (0.805, 0.953, true), + (1.0, 1.0, false), +]; + +fn gaussian() -> Contour { + Contour::Custom { + name: "Gaussian".into(), + points: GAUSSIAN.iter().map(|&(input, output, _)| CurvePoint { input, output }).collect(), + corners: GAUSSIAN.iter().map(|p| p.2).collect(), + } +} + +/// #14981: black Satin (Angle 90, Distance 20, Size 10) over an orange rectangle, G channel down +/// its centre column x 200 from y 75 (128 = no satin, 0 = full black). +fn green_column(contour: Contour, invert: bool) -> Vec { + let mut shape = Map::new(400, 300, 0.0); + for row in shape.v.as_chunks_mut::<400>().0.iter_mut().skip(75).take(150) { + row[100..300].fill(1.0); + } + let satin = Satin { + common: FxCommon::new(BlendMode::Normal, 1.0), + color: Color::BLACK, + angle: 90.0, + distance: 20.0, + size: 10.0, + contour, + anti_alias: false, + invert, + }; + let coverage = satin_map(&shape, &satin); + (75..225).map(|y| (128.0 * (1.0 - coverage.get(200, y))).round() as u8).collect() +} + +/// `edge` from y 75 inwards, the middle value up to the mirrored edge. +fn mirrored(edge: &[u8], middle: u8) -> Vec { + let mut column = edge.to_vec(); + column.resize(150 - edge.len(), middle); + column.extend(edge.iter().rev()); + column +} + +fn assert_close(actual: &[u8], expected: &[u8], tolerance: u8, case: &str) { + for (i, (a, e)) in actual.iter().zip(expected).enumerate() { + assert!(a.abs_diff(*e) <= tolerance, "{case}, y {}: expected G {e}, got {a}", i + 75); + } +} + +#[test] +fn inverted_gaussian_satin_is_the_opposite_of_the_plain_one() { + let mut edge = vec![128; 13]; + edge.extend([127, 126, 123, 116, 103, 82, 58, 35, 21, 13, 8, 5, 3, 2, 1, 1]); + let inverted = green_column(gaussian(), true); + assert_close(&inverted, &mirrored(&edge, 0), 3, "Gaussian, Invert on"); + let plain = green_column(gaussian(), false); + let opposite: Vec = plain.iter().map(|g| 128 - g).collect(); + assert_close(&inverted, &opposite, 1, "Gaussian, Invert on vs 128 - Invert off"); +} + +#[test] +fn inverted_linear_satin_keeps_its_profile() { + let mut edge = vec![128; 11]; + edge.extend([127, 124, 120, 115, 109, 101, 92, 82, 71, 58, 46, 36, 27, 20, 13, 8, 4, 2]); + assert_close(&green_column(Contour::Linear, true), &mirrored(&edge, 0), 2, "Linear, Invert on"); +} diff --git a/crates/gpu/src/fx.rs b/crates/gpu/src/fx.rs index 4f265a3202..2e949a42e1 100644 --- a/crates/gpu/src/fx.rs +++ b/crates/gpu/src/fx.rs @@ -255,9 +255,10 @@ impl B { self.stages.push(s); } + /// `invert` flips the coverage after the contour (`fs_mfinish` mode 2), as the CPU's `satin_map` (#14981). fn finish_lut(&mut self, a: In, b: Option, invert: bool, lut: Option>, times_shape: bool, out: usize) { let flag = |v: bool| f32::from(u8::from(v)); - let mut s = stage(Kernel::MFinish, Some(a), b, [flag(b.is_some()), flag(invert), flag(lut.is_some()), flag(times_shape)], 0); + let mut s = stage(Kernel::MFinish, Some(a), b, [flag(b.is_some()), 2.0 * flag(invert), flag(lut.is_some()), flag(times_shape)], 0); s.lut = lut.map(Arc::new); if times_shape { s.s = Some(In::Shape);