Skip to content

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

Glows: Photoshop's precise ramp, and centre-source glows inverted after the contour (#3578)

MrCloudy297c6769d6b65
diff --git a/crates/compose/src/effects.rs b/crates/compose/src/effects.rs
index d77941847..cc3272a14 100644
--- a/crates/compose/src/effects.rs
+++ b/crates/compose/src/effects.rs
@@ -1114,27 +1114,24 @@ fn shadow_map(shape: &Map, s: &Shadow, light: &GlobalLight, inner: bool, origin:
}
fn glow_map(shape: &Map, g: &Glow, inner: bool, origin: (i32, i32)) -> Map {
- let src = if inner {
- match g.source {
- GlowSource::Edge => shape.clone().map(|a| 1.0 - a),
- GlowSource::Center => shape.clone(),
- }
- } else {
- shape.clone()
- };
+ // An inner glow is built from the edge inwards; a centre-source glow is its inverse, taken
+ // after the contour and Range (Photoshop 27.11, measured: the centre glow is exactly
+ // 1 − the edge glow, for both techniques).
+ let src = if inner { shape.clone().map(|a| 1.0 - a) } else { shape.clone() };
let mut m = match g.technique {
GlowTechnique::Precise => {
- // 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).
+ // Coverage falls linearly from 0.5 at the edge to 0 at `size + 1` px (centre to
+ // centre), solid within the spread / choke: after the default 50 % Range that is
+ // Photoshop's 1 − D / (size + 1) beyond the solid part (measured: outer and inner,
+ // sizes 10 and 20, spread 0 and 40 %, choke 0 and 30 %: within 1 level; other Range
+ // values are unmeasured). The distances here are 0.5 px short of centre to centre.
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 soft = (g.size + 1.0 - solid).max(1e-3);
let mut m = Map::new(shape.w, shape.h, 0.0);
for (o, d) in m.v.iter_mut().zip(d) {
- *o = if d <= solid { 1.0 } else { (1.0 - (d - solid) / soft).clamp(0.0, 1.0) };
- }
- if inner && g.source == GlowSource::Center {
- m = m.map(|v| 1.0 - v);
+ let d = d + 0.5;
+ *o = if d <= solid { 1.0 } else { 0.5 * (1.0 - (d - solid) / soft).clamp(0.0, 1.0) };
}
m
}
@@ -1142,16 +1139,13 @@ fn glow_map(shape: &Map, g: &Glow, inner: bool, origin: (i32, i32)) -> Map {
let (r, bw) = spread_split(g.size, g.spread);
let mut m = dilate(&src, r);
blur(&mut m, bw);
- if inner && g.source == GlowSource::Center {
- // Brightest in the middle: coverage from the distance to the edge.
- let mut e = dilate(&shape.clone().map(|a| 1.0 - a), r);
- blur(&mut e, bw);
- m = e.map(|v| 1.0 - v);
- }
m
}
};
m = apply_lut(m, glow_lut(g));
+ if inner && g.source == GlowSource::Center {
+ m = m.map(|v| 1.0 - v);
+ }
noise(&mut m, g.noise, origin.0, origin.1);
if inner {
for (v, a) in m.v.iter_mut().zip(&shape.v) {
@@ -2883,6 +2877,51 @@ mod tests {
assert!(glow_lut(&g).is_none());
}
+ /// Photoshop 27.11, measured on a 128 px square (Normal, black, 100 %, Range 50 %): a precise
+ /// glow's coverage across the edge, and a centre-source inner glow as 1 − the edge glow.
+ #[test]
+ fn glows_match_photoshop() {
+ let glow = |technique, size: f32, spread: f32, source| Glow {
+ common: photocraft_doc::FxCommon::new(BlendMode::Normal, 1.0),
+ paint: FxPaint::Color(photocraft_color::Color::BLACK),
+ technique,
+ spread,
+ size,
+ contour: Contour::Linear,
+ anti_alias: false,
+ range: 0.5,
+ jitter: 0.0,
+ noise: 0.0,
+ source,
+ };
+ let n = 60;
+ let mut shape = Map::new(n, n, 0.0);
+ for y in 20..40 {
+ for x in 20..40 {
+ shape.v[y * n + x] = 1.0;
+ }
+ }
+ let at = |m: &Map, x: usize| m.v[30 * n + x];
+ // Outer, precise, size 10: Photoshop's black glow over white reads 47, 93, 140, 186, 232
+ // at 2, 4, 6, 8 and 10 px out from the edge pixel (alpha 1 − D / 11).
+ let m = glow_map(&shape, &glow(GlowTechnique::Precise, 10.0, 0.0, GlowSource::Edge), false, (0, 0));
+ for (d, ps) in [(2, 47.0f32), (4, 93.0), (6, 140.0), (8, 186.0), (10, 232.0)] {
+ let got = 255.0 * (1.0 - at(&m, 20 - d));
+ assert!((got - ps).abs() <= 1.5, "outer precise at {d} px: {got} vs Photoshop {ps}");
+ }
+ // Spread 40 %: solid for the first 4 px.
+ let m = glow_map(&shape, &glow(GlowTechnique::Precise, 10.0, 0.4, GlowSource::Edge), false, (0, 0));
+ assert!((at(&m, 16) - 1.0).abs() < 1e-6 && at(&m, 14) < 1.0);
+ // Centre source = 1 − edge, for both techniques.
+ for t in [GlowTechnique::Precise, GlowTechnique::Softer] {
+ let e = glow_map(&shape, &glow(t, 6.0, 0.0, GlowSource::Edge), true, (0, 0));
+ let c = glow_map(&shape, &glow(t, 6.0, 0.0, GlowSource::Center), true, (0, 0));
+ for x in 20..40 {
+ assert!((at(&c, x) - (1.0 - at(&e, x))).abs() < 1e-5, "{t:?} at {x}");
+ }
+ }
+ }
+
#[test]
fn offsets_follow_light() {
let (dx, dy) = offset(120.0, 10.0);
diff --git a/crates/gpu/src/compose.wgsl b/crates/gpu/src/compose.wgsl
index c6a0edbd5..6b5321fce 100644
--- a/crates/gpu/src/compose.wgsl
+++ b/crates/gpu/src/compose.wgsl
@@ -1152,25 +1152,27 @@ fn fs_mblur(in: VOut) -> @location(0) vec4<f32> {
return mout(acc);
}
-// Precise glow from a distance map: p0 = (solid, soft, invert).
+// Precise glow from a distance map (`effects::glow_map`): p0 = (solid, soft). Coverage is 0.5 at
+// the edge falling to 0 at `size + 1` px centre to centre (the field is 0.5 px short of that),
+// solid within the spread / choke.
@fragment
fn fs_mglow(in: VOut) -> @location(0) vec4<f32> {
- let d = textureLoad(tex_a, local(in.pos), 0).r;
- var m = clamp(1.0 - (d - op.p0.x) / op.p0.y, 0.0, 1.0);
+ let d = textureLoad(tex_a, local(in.pos), 0).r + 0.5;
+ var m = 0.5 * clamp(1.0 - (d - op.p0.x) / op.p0.y, 0.0, 1.0);
if (d <= op.p0.x) { m = 1.0; }
- if (op.p0.z > 0.5) { m = 1.0 - m; }
return mout(m);
}
-// Last step of a coverage map: p0 = (|A - B| (satin), 1 - v, contour (LUT row 0), × shape
-// (`layer_tex`)).
+// Last step of a coverage map: p0 = (|A - B| (satin), 1 - v (1: before the contour, 2: after it,
+// as a centre-source inner glow), contour (LUT row 0), × shape (`layer_tex`)).
@fragment
fn fs_mfinish(in: VOut) -> @location(0) vec4<f32> {
let p = local(in.pos);
var v = textureLoad(tex_a, p, 0).r;
if (op.p0.x > 0.5) { v = abs(v - textureLoad(tex_b, p, 0).r); }
- if (op.p0.y > 0.5) { v = 1.0 - v; }
+ if (op.p0.y > 0.5 && op.p0.y < 1.5) { v = 1.0 - v; }
if (op.p0.z > 0.5) { v = lut(0, v); }
+ if (op.p0.y > 1.5) { v = 1.0 - v; }
if (op.p0.w > 0.5) { v = v * textureLoad(layer_tex, p, 0).r; }
return mout(v);
}
diff --git a/crates/gpu/src/fx.rs b/crates/gpu/src/fx.rs
index 48da34240..e3a6f5863 100644
--- a/crates/gpu/src/fx.rs
+++ b/crates/gpu/src/fx.rs
@@ -236,6 +236,19 @@ impl B {
fn finish(&mut self, a: In, b: Option<In>, invert: bool, contour: &Contour, times_shape: bool, out: usize) {
self.finish_lut(a, b, invert, contour_lut(contour), times_shape, out);
}
+ /// A glow's last step: its contour over the Range, then (centre-source inner glows) the
+ /// inverse, then × the shape when inner.
+ fn finish_glow(&mut self, a: In, lut: Option<Vec<f32>>, inner: bool, center: bool) {
+ let mut s =
+ stage(Kernel::MFinish, Some(a), None, [0.0, if center { 2.0 } else { 0.0 }, f32::from(u8::from(lut.is_some())), f32::from(u8::from(inner))], 0);
+ s.lut = lut.map(Arc::new);
+ if inner {
+ s.s = Some(In::Shape);
+ }
+ s.out = Some(0);
+ self.stages.push(s);
+ }
+
fn finish_lut(&mut self, a: In, b: Option<In>, invert: bool, lut: Option<Vec<f32>>, 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);
@@ -319,9 +332,9 @@ pub(crate) fn program_with(e: &Effect, light: &GlobalLight, vector_shape: bool,
GlowTechnique::Precise => {
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));
- b.finish_lut(m, None, false, photocraft_compose::effects::glow_lut(g), inner, 0);
+ let soft = (g.size + 1.0 - solid).max(1e-3);
+ let m = b.push(stage(Kernel::MGlow, Some(d), None, [solid, soft, 0.0, 0.0], 0));
+ b.finish_glow(m, photocraft_compose::effects::glow_lut(g), inner, center);
}
GlowTechnique::Softer => {
// Inner glows (edge, and centre as 1 - the edge result) spread 1 - alpha.
@@ -333,7 +346,7 @@ pub(crate) fn program_with(e: &Effect, light: &GlobalLight, vector_shape: bool,
m = b.dilate(src, d, r);
}
let m = b.blur(m, bw);
- b.finish_lut(m, None, center, photocraft_compose::effects::glow_lut(g), inner, 0);
+ b.finish_glow(m, photocraft_compose::effects::glow_lut(g), inner, center);
}
}
1