Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

fix(compose): a Precise inner glow from the centre is no longer blank (#966) (#1708)

MarekSpodBiedry66ab5012bef5
diff --git a/crates/compose/src/effects.rs b/crates/compose/src/effects.rs
index dd589aef6..dbab3ca6d 100644
--- a/crates/compose/src/effects.rs
+++ b/crates/compose/src/effects.rs
@@ -586,7 +586,9 @@ fn glow_map(shape: &Map, g: &Glow, inner: bool, origin: (i32, i32)) -> Map {
};
let mut m = match g.technique {
GlowTechnique::Precise => {
- let d = if inner && g.source == GlowSource::Edge { dist_inside(shape) } else { dist_outside(&src) };
+ // An inner glow, from either source, measures from the edge inwards; a centre glow is
+ // the inverse below (the distance outside is ≤ 0 everywhere inside, #966).
+ let d = if inner { dist_inside(shape) } else { dist_outside(&src) };
let solid = g.size * g.spread;
let soft = (g.size - solid).max(1e-3);
let mut m = Map::new(shape.w, shape.h, 0.0);
@@ -1549,9 +1551,9 @@ fn mix_premul(a: [f32; 4], b: [f32; 4], k: f32) -> [f32; 4] {
/// `glow_map`, `bevel_maps` and `build_maps`).
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum FieldKind {
- /// `dist_outside` of the shape: precise outer and centre glows, bevels.
+ /// `dist_outside` of the shape: precise outer glows, bevels.
Outside,
- /// `dist_inside` of the shape: precise edge inner glows, bevels.
+ /// `dist_inside` of the shape: precise inner glows, bevels.
Inside,
/// Chamfer distance outside the shape: outside strokes, and the spread of drop shadows and
/// softer outer glows ([`dilate`]).
diff --git a/crates/compose/src/tests.rs b/crates/compose/src/tests.rs
index faff16595..259805aec 100644
--- a/crates/compose/src/tests.rs
+++ b/crates/compose/src/tests.rs
@@ -570,6 +570,17 @@ fn outer_and_inner_glow_regions() {
assert!(close4(px(&d, 5, 20), [1.0; 4]));
}
+// A Precise inner glow from the centre is brightest in the middle and fades towards the edge,
+// like the Softer one; it used to paint nothing at all (#966).
+#[test]
+fn precise_inner_glow_from_center_paints_the_middle() {
+ let d = fx_doc(vec![Effect::InnerGlow(glow(GlowTechnique::Precise, GlowSource::Center))]);
+ let (center, edge) = (px(&d, 20, 20), px(&d, 10, 20));
+ assert!(center[1] > 0.9 && center[0] < 0.1, "green in the middle: {center:?}");
+ assert!(edge[0] > center[0] && edge[1] < center[1], "fades towards the edge: {edge:?} vs {center:?}");
+ assert!(close4(px(&d, 5, 20), [1.0; 4]));
+}
+
// A glow gradient runs along the glow, not across the canvas: the first stop hugs the shape on
// every side and later stops lie further out, opaque until the glow fades (#443).
#[test]
diff --git a/crates/gpu/src/fx.rs b/crates/gpu/src/fx.rs
index ca50711f0..76761e327 100644
--- a/crates/gpu/src/fx.rs
+++ b/crates/gpu/src/fx.rs
@@ -305,11 +305,10 @@ pub(crate) fn program_with(e: &Effect, light: &GlobalLight, vector_shape: bool,
Effect::OuterGlow(g) | Effect::InnerGlow(g) => {
// glow_map
let inner = matches!(e, Effect::InnerGlow(_));
- let edge = inner && g.source == GlowSource::Edge;
let center = inner && g.source == GlowSource::Center;
match g.technique {
GlowTechnique::Precise => {
- let d = b.field(if edge { FieldKind::Inside } else { FieldKind::Outside }, g.size);
+ let d = b.field(if inner { FieldKind::Inside } else { FieldKind::Outside }, g.size);
let solid = g.size * g.spread;
let soft = (g.size - solid).max(1e-3);
let m = b.push(stage(Kernel::MGlow, Some(d), None, [solid, soft, f32::from(u8::from(center)), 0.0], 0));