PhotoCraft: An open-source, photo editor written by scratch in pure Rust
fix(compose): paint interior effects over the layer's blend mode (#3996) (#4231)
Alonab828712712f
diff --git a/crates/compose/src/advanced_tests.rs b/crates/compose/src/advanced_tests.rsindex 0ca3bdadb..9a8723c41 100644--- a/crates/compose/src/advanced_tests.rs+++ b/crates/compose/src/advanced_tests.rs@@ -246,6 +246,26 @@ fn blend_interior_effects_as_group_puts_overlays_under_fill_opacity() {}}+#[test]+fn interior_effects_blend_over_the_layer_mode_unless_grouped() {+ // #3996: #888888 under a black Color-mode layer with a red Overlay colour overlay. The+ // layer's mode applies to its own pixels (grey stays grey), then the overlay blends onto+ // that result and tints it; as a group the overlay of black is black, so grey remains.+ let grey = 136.0 / 255.0;+ let lift = 1.0 - 2.0 * (1.0 - grey);+ for depth in DEPTHS {+ let mut d = doc(depth, false);+ d.layers.push(solid(&d, "grey", full(&d), [grey, grey, grey, 1.0]));+ let mut l = solid(&d, "black", full(&d), [0.0, 0.0, 0.0, 1.0]);+ l.blend = BlendMode::Color;+ l.effects.items = vec![Effect::ColorOverlay { common: FxCommon::new(BlendMode::Overlay, 1.0), color: Color::rgb(1.0, 0.0, 0.0) }];+ d.layers.push(l);+ check(&d, 2, 2, [1.0, lift, lift, 1.0], "default: overlay over the Color blend");+ d.layers[1].advanced.blend_interior = true;+ check(&d, 2, 2, [grey, grey, grey, 1.0], "as group: overlay inside the Color blend");+ }+}+#[test]fn transparency_shapes_off_spreads_effects_over_the_whole_layer() {for depth in DEPTHS {diff --git a/crates/compose/src/effects.rs b/crates/compose/src/effects.rsindex 05bd1f10d..33af1a08b 100644--- a/crates/compose/src/effects.rs+++ b/crates/compose/src/effects.rs@@ -4,8 +4,9 @@//! drop shadows, outer glows, outer bevel, then the layer itself (content at//! fill opacity) with pattern/gradient/colour overlays, satin, inner glows,//! inner shadows, strokes and the inner bevel on top. Exterior effects blend-//! straight into the backdrop with their own modes; the layer and its-//! interior effects blend with the layer's mode. Emboss and pillow emboss then+//! straight into the backdrop with their own modes; the layer blends with its+//! mode and the interior effects paint over the result, or blend with it as a+//! group (Blend Interior Effects as Group, [`premerges_layer`]). Emboss and pillow emboss then//! shade the composited result (inside and outside halves). Layer opacity//! applies to the whole stack; fill opacity only to the layer's own pixels.//!@@ -1791,6 +1792,14 @@ fn knock_out_exterior(map: &mut Map, shape: &Map, fill_opacity: f32) {}}+/// Blend Interior Effects as Group off (Photoshop's default): the layer's mode applies to its+/// own pixels only, and the interior effects paint over the blended result. The layer is merged+/// into the backdrop before they paint, so an Overlay colour overlay on a black Color-mode layer+/// tints the grey beneath it (#3996). Normal-like modes give the same result either way.+pub fn premerges_layer(layer: &Layer) -> bool {+ !layer.advanced.blend_interior && !matches!(layer.blend, BlendMode::Normal | BlendMode::PassThrough | BlendMode::Dissolve)+}+/// 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]) {@@ -1889,6 +1898,13 @@ pub(crate) fn composite_with_effects_prepared(}};let mut lay = Buffer { rect: big, px: content.px.iter().zip(&shape.v).enumerate().map(|(i, (p, a))| [p[0], p[1], p[2], lay_alpha(i, p, *a)]).collect() };+ let gamma = crate::text_gamma(layer);+ let premerge = premerges_layer(layer);+ if premerge {+ for (lp, wp) in lay.px.iter_mut().zip(&work.px) {+ *lp = psblend::composite_gamma(layer.blend, *wp, *lp, 1.0, gamma);+ }+ }let rel = |m: Map| -> Map {let v = m.v.iter().zip(&shape.v).map(|(m, a)| if inside(*a) { (m / a).min(1.0) } else { 0.0 }).collect();Map { w: m.w, h: m.h, v }@@ -2051,8 +2067,7 @@ pub(crate) fn composite_with_effects_prepared(}// Layer (with interior effects) onto the exterior result, in the layer's mode.- let mode = if layer.blend == BlendMode::PassThrough { BlendMode::Normal } else { layer.blend };- let gamma = crate::text_gamma(layer);+ let mode = if premerge || layer.blend == BlendMode::PassThrough { BlendMode::Normal } else { layer.blend };for (wp, lp) in work.px.iter_mut().zip(&lay.px) {if lp[3] > 0.0 {*wp = psblend::composite_gamma(mode, *wp, *lp, 1.0, gamma);diff --git a/crates/gpu/src/plan.rs b/crates/gpu/src/plan.rsindex fb135dc13..52c36f14d 100644--- a/crates/gpu/src/plan.rs+++ b/crates/gpu/src/plan.rs@@ -9,7 +9,8 @@//! effect maps (shadow / glow / satin / bevel coverage, stroke bands) are built once per layer//! state by [`crate::fx`] and sampled here; exterior effects paint into a copy of the backdrop,//! the layer at fill opacity takes the interior effects, and the two merge with the layer's mode-//! and opacity. Effect passes are clipped to the layer's effect region and the result is copied+//! (or the layer merges first and the effects paint over it: `effects::premerges_layer`) and+//! opacity. Effect passes are clipped to the layer's effect region and the result is copied//! back into the backdrop in place, so a small text layer costs only its own pixels.use photocraft_color::BlendMode;@@ -1095,6 +1096,19 @@ impl<'a> Planner<'a> {} else {init(self, lay_src, None, INIT_INSIDE, layer.fill_opacity)};+ // Blend Interior Effects as Group off: the layer merges in its mode first and the+ // interior effects paint over the result (`effects::premerges_layer`).+ let premerge = photocraft_compose::effects::premerges_layer(layer);+ if premerge {+ let mut p = Pass::new(Kernel::Blend, 0);+ p.a = Some(self.retain(w));+ p.b = Some(l);+ p.mode = layer.blend;+ p.flags = gamma_flag(layer);+ p.extra[3] = photocraft_compose::text_gamma(layer);+ p.clip = Some(clip);+ l = self.emit(p);+ }for &(_, e) in &rev {if let Effect::PatternOverlay { common, name, id, scale, angle, link, phase } = e&& let Some(pat) = photocraft_doc::pattern::find(self.cx.patterns, id, name).filter(|p| p.is_paintable())@@ -1257,7 +1271,7 @@ impl<'a> Planner<'a> {}}- let mode = if layer.blend == BlendMode::PassThrough { BlendMode::Normal } else { layer.blend };+ let mode = if premerge || layer.blend == BlendMode::PassThrough { BlendMode::Normal } else { layer.blend };let late: Vec<(usize, &'a photocraft_doc::Bevel)> = rev.iter().filter_map(|&(i, e)| if let Effect::BevelEmboss(b) = e { Some((i, b)) } else { None })diff --git a/crates/gpu/tests/parity.rs b/crates/gpu/tests/parity.rsindex 47837a8b6..2bf8bd82b 100644--- a/crates/gpu/tests/parity.rs+++ b/crates/gpu/tests/parity.rs@@ -1461,6 +1461,7 @@ fn layer_effects_opacity_fill_blend_and_off_canvas() {(0.6, 0.35, BlendMode::Multiply),(1.0, 0.5, BlendMode::Screen),(0.8, 1.0, BlendMode::Dissolve),+ (1.0, 1.0, BlendMode::Color),] {let mut d = fx_doc(96, 80, SampleType::U8);let mut l = blob("fx", d.pixel_format(), 46.0, 40.0, 22.0, [0.2, 0.6, 0.9]);