PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Outer Glow stays behind a see-through fill (#1964)
shmuelbiller1f1413ea12ea9
diff --git a/crates/compose/src/effects.rs b/crates/compose/src/effects.rsindex dbab3ca6d..2adc04341 100644--- a/crates/compose/src/effects.rs+++ b/crates/compose/src/effects.rs@@ -1194,6 +1194,16 @@ pub(crate) fn build_maps_prepared(/// Far outside any shape (distance fill for cropped distance fields).const FAR: f32 = 1.0e9;+/// Keep an exterior effect behind the layer's shape when Fill is partly or wholly+/// transparent. The visible part beneath a fully opaque fill is already covered by the+/// layer, so avoid attenuating anti-aliased edges twice in that case.+fn knock_out_exterior(map: &mut Map, shape: &Map, fill_opacity: f32) {+ let see_through = 1.0 - fill_opacity.clamp(0.0, 1.0);+ for (coverage, alpha) in map.v.iter_mut().zip(&shape.v) {+ *coverage *= 1.0 - alpha * see_through;+ }+}+/// Composites `content` (the layer's own pixels over `big`, alpha already/// masked, clipped layers applied) plus its effects into `backdrop`.pub fn composite_with_effects(layer: &Layer, content: &Buffer, backdrop: &mut Buffer, maps: &FxMaps, layer_bounds: Rect, patterns: &[Pattern]) {@@ -1255,17 +1265,18 @@ pub(crate) fn composite_with_effects_prepared(// The layer hides the shadow beneath it only where its fill is see-through: at// 100 % fill the layer covers it anyway (and anti-aliased edges are not// attenuated twice), at 0 % the shape shows the bare backdrop.- let see_through = 1.0 - layer.fill_opacity.clamp(0.0, 1.0);- for (v, a) in m.v.iter_mut().zip(&shape.v) {- *v *= 1.0 - a * see_through;- }+ knock_out_exterior(&mut m, &shape, layer.fill_opacity);}paint_color(&mut work, &m, rgb(&s.color), s.common.blend, s.common.opacity);}}for (i, e) in rev() {if let Effect::OuterGlow(g) = e {- paint_glow(&mut work, &fx(i, 0), g, sb, anchor, big, patterns);+ let mut m = fx(i, 0);+ // The glow is behind the shape, not painted across its interior when Fill is 0 %.+ // Preserve the existing anti-aliased edge when the fill is fully opaque.+ knock_out_exterior(&mut m, &shape, layer.fill_opacity);+ paint_glow(&mut work, &m, g, sb, anchor, big, patterns);}}diff --git a/crates/compose/src/tests.rs b/crates/compose/src/tests.rsindex 3474ef205..9ba45d768 100644--- a/crates/compose/src/tests.rs+++ b/crates/compose/src/tests.rs@@ -1758,3 +1758,43 @@ fn clipped_brightness_and_desaturation_whiten_a_lighter_color_logo() {assert!(p[..3].iter().all(|v| *v * 255.0 >= 252.0), "logo whitened: {p:?}");assert!(close4(px(&d, 1, 0), yellow), "the yellow beside it is untouched");}++#[test]+fn outer_glow_does_not_paint_through_a_zero_fill_layer() {+ use photocraft_doc::{Contour, Effect, FxCommon, FxPaint, Glow, GlowSource, GlowTechnique};++ let background = [0.18, 0.42, 0.85, 1.0];+ let rect = Rect::new(24, 24, 72, 72);+ for technique in [GlowTechnique::Softer, GlowTechnique::Precise] {+ let mut d = doc_white(96, 96);+ let bounds = d.bounds();+ d.layers[0].surface_mut().unwrap().fill_rect(bounds, &background);+ let mut layer = solid_layer("glow", rect, [1.0, 0.92, 0.25, 1.0]);+ layer.fill_opacity = 0.0;+ layer.effects.items.push(Effect::OuterGlow(Glow {+ common: FxCommon::new(BlendMode::Normal, 1.0),+ paint: FxPaint::Color(Color::rgb(1.0, 0.94, 0.28)),+ technique,+ spread: 0.5,+ size: 12.0,+ contour: Contour::Linear,+ anti_alias: false,+ range: 0.5,+ jitter: 0.0,+ noise: 0.0,+ source: GlowSource::Edge,+ }));+ d.layers.push(layer);++ let center = px(&d, 48, 48);+ assert!(close4(center, background), "{technique:?}: glow leaked into fully transparent fill: {center:?}");+ let distant = px(&d, 5, 5);+ assert!(close4(distant, background), "{technique:?}: changed distant background: {distant:?}");+ let halo = px(&d, 22, 48);+ assert!(halo[0] > background[0] + 0.1, "{technique:?}: exterior halo disappeared: {halo:?}");++ d.layers[1].fill_opacity = 1.0;+ let opaque = px(&d, 48, 48);+ assert!(opaque[0] > 0.9 && opaque[1] > 0.8, "{technique:?}: opaque fill disappeared: {opaque:?}");+ }+}diff --git a/crates/gpu/src/plan.rs b/crates/gpu/src/plan.rsindex 30dc06a76..ffc17d27d 100644--- a/crates/gpu/src/plan.rs+++ b/crates/gpu/src/plan.rs@@ -1019,7 +1019,10 @@ impl<'a> Planner<'a> {for &(i, e) in &rev {if let Effect::OuterGlow(g) = e {let paint = self.glow_paint(g, anchor);- w = self.paint(w, content, Cov::Map(map(i, 0), 0.0), &paint, g.common.blend, g.common.opacity, 0, clip, sb);+ // Match CPU exterior knockout: a see-through fill must not reveal the+ // glow through its own shape. The shader applies m × (1 − alpha × k).+ let see_through = 1.0 - layer.fill_opacity.clamp(0.0, 1.0);+ w = self.paint_k(w, content, Cov::Map(map(i, 0), 0.0), &paint, g.common.blend, g.common.opacity, F_KNOCKOUT, clip, sb, see_through);}}@@ -1635,6 +1638,41 @@ mod tests {use photocraft_color::{Color, ColorMode, SampleType};use photocraft_geom::Size;+ #[test]+ fn outer_glow_plan_knocks_out_see_through_fill() {+ use photocraft_doc::{Contour, FxCommon, GlowSource, GlowTechnique};++ for fill in [0.0, 0.4, 1.0] {+ let mut d = Document::with_background("glow", Size::new(64, 64), ColorMode::Rgb, SampleType::U8, Color::WHITE);+ let mut layer = Layer::raster("frame", d.pixel_format());+ layer.surface_mut().unwrap().fill_rect(Rect::new(16, 16, 48, 48), &[1.0, 0.0, 0.0, 1.0]);+ layer.fill_opacity = fill;+ layer.effects.items.push(Effect::OuterGlow(Glow {+ common: FxCommon::new(BlendMode::Normal, 1.0),+ paint: FxPaint::Color(Color::rgb(1.0, 1.0, 0.0)),+ technique: GlowTechnique::Softer,+ spread: 0.3,+ size: 8.0,+ contour: Contour::Linear,+ anti_alias: false,+ range: 0.5,+ jitter: 0.0,+ noise: 0.0,+ source: GlowSource::Edge,+ }));+ d.layers.push(layer);+ let planned = plan(&d).unwrap();+ let glow = planned+ .passes+ .iter()+ .find(|pass| {+ pass.kernel == Kernel::FxPaint && pass.flags & F_KNOCKOUT != 0 && pass.color[0] > 0.9 && pass.color[1] > 0.9 && pass.color[2] < 0.1+ })+ .expect("outer glow paint pass");+ assert!((glow.extra[3] - (1.0 - fill)).abs() < 1e-6, "fill {fill}");+ }+ }+#[test]fn color_lookup_with_an_overflowing_stored_size_uploads_no_table() {for size in [1 << 22, u32::MAX] {