Skip to content

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

Chisel bevels: Photoshop's facet slope and ramp ends (#3595)

MrCloudy28b216216717c
diff --git a/crates/compose/src/effects.rs b/crates/compose/src/effects.rs
index cc3272a14..915eb3ddd 100644
--- a/crates/compose/src/effects.rs
+++ b/crates/compose/src/effects.rs
@@ -1455,7 +1455,9 @@ pub fn bevel_geom(b: &Bevel) -> BevelGeom {
BevelGeom {
paint,
width,
- depth: b.depth * if smooth { width } else { size } * sign,
+ // A bevel rises `depth × width / 2` px: Smooth with a slope falling from `depth` at the
+ // edge (the tent), a chisel with the constant slope of its ramp (see `bevel_chisel_h`).
+ depth: b.depth * if smooth { width } else { size / 2.0 } * sign,
pillow: b.style == BevelStyle::PillowEmboss,
chisel_soft: if b.technique == BevelTechnique::ChiselSoft { (size / 4.0).max(1.0) } else { 0.0 },
}
@@ -1476,10 +1478,13 @@ fn bevel_height(shape: &Map, b: &Bevel, g: &BevelGeom, tex: &TextureCtx, pattern
let sharp = sharp_edges(shape);
let (din, dout) = (bevel_dist_inside(shape, &sharp, size), bevel_dist_outside(shape, &sharp, size));
let mut h = Map::new(shape.w, shape.h, 0.0);
+ let soft = g.chisel_soft > 0.0;
for i in 0..h.v.len() {
- h.v[i] = bevel_chisel_h(g.paint, size, din[i], dout[i]);
+ h.v[i] = bevel_chisel_h(g.paint, size, din[i], dout[i], soft);
}
- if g.chisel_soft > 0.0 {
+ // Inner and outer Chisel Soft are not blurred in Photoshop (their facets equal Chisel
+ // Hard's across a straight edge); emboss styles keep the blur until measured.
+ if soft && g.paint == BevelPaint::Both {
tent(&mut h, g.chisel_soft);
}
h
@@ -1521,17 +1526,46 @@ pub struct TextureCtx<'a> {
}
/// Chiselled height from the inside / outside distances (keep in sync with `fs_mbevelh`).
-pub fn bevel_chisel_h(paint: BevelPaint, size: f32, din: f32, dout: f32) -> f32 {
+///
+/// Photoshop 27.11, measured across straight edges (sizes 10 and 20, depth 100 and 300): a chisel
+/// facet has the constant slope `depth × size / (2 (size + 1))`, i.e. one height step of
+/// `1 / (size + 1)` per pixel at the `depth × size / 2` scale of [`bevel_geom`]. Inner Hard's ramp
+/// runs from half a pixel beyond the edge to `size + ½` (the facet is flat right up to the edge
+/// pixel); Inner Soft starts a pixel further out, so its edge pixel is steeper; Outer Hard ends a
+/// pixel further out. All within 1 level of Photoshop on those profiles.
+pub fn bevel_chisel_h(paint: BevelPaint, size: f32, din: f32, dout: f32, soft: bool) -> f32 {
+ let ramp = size + 1.0;
match paint {
- BevelPaint::Outer => 1.0 - (dout / size).clamp(0.0, 1.0),
+ BevelPaint::Outer if !soft => {
+ let top = (size + 2.0) / ramp;
+ if dout > 0.0 { ((size + 1.5 - dout) / ramp).clamp(0.0, top) } else { top }
+ }
+ // Outer Soft: Outer Hard's ramp a pixel shorter, with a steeper edge pixel.
+ BevelPaint::Outer => {
+ if dout > 0.0 {
+ 1.0 - ((dout + 0.5) / ramp).clamp(0.0, 1.0)
+ } else {
+ (size + 2.0) / ramp
+ }
+ }
BevelPaint::Both => {
+ let half = size / 2.0 + 0.5;
+ if din > 0.0 { 0.5 + 0.5 * (din / half).clamp(0.0, 1.0) } else { 0.5 - 0.5 * (dout / half).clamp(0.0, 1.0) }
+ }
+ BevelPaint::Inner if soft => {
+ if din > 0.0 {
+ ((din + 1.5) / ramp).clamp(0.0, (size + 2.0) / ramp)
+ } else {
+ 0.0
+ }
+ }
+ BevelPaint::Inner => {
if din > 0.0 {
- 0.5 + 0.5 * (din / (size / 2.0)).clamp(0.0, 1.0)
+ ((din + 0.5) / ramp).clamp(0.0, 1.0)
} else {
- 0.5 - 0.5 * (dout / (size / 2.0)).clamp(0.0, 1.0)
+ 0.0
}
}
- BevelPaint::Inner => (din / size).clamp(0.0, 1.0),
}
}
@@ -1549,7 +1583,9 @@ fn bevel_maps(shape: &Map, b: &Bevel, light: &GlobalLight, tex: &TextureCtx, pat
// past the blur's reach (a raised 4-neighbour: it still slopes; psd-tools layer_effects'
// emboss shadow runs one row past it), outer bevels don't (Photoshop oracle
// bevel-outer-smooth).
- let reach_past = g.paint == BevelPaint::Both;
+ // Outer chisel bevels too: the pixel where their ramp reaches zero still slopes (Photoshop
+ // 27.11 shades it, half strength, at `size + 1½` px out).
+ let reach_past = g.paint == BevelPaint::Both || (g.paint == BevelPaint::Outer && b.technique != BevelTechnique::Smooth);
let on_bevel = |x: i64, y: i64| {
hmap.get(x, y) > BEVEL_H_EPS || (reach_past && [(1, 0), (-1, 0), (0, 1), (0, -1)].iter().any(|(dx, dy)| hmap.get(x + dx, y + dy) > BEVEL_H_EPS))
};
@@ -2334,6 +2370,40 @@ mod tests {
assert!(at(&maps[0], 20, 15) < 0.05);
}
+ /// Photoshop 27.11, measured across a straight edge (size 10, depth 100 %, light from the top
+ /// at 30°): a chisel facet has the slope `depth × size / (2 (size + 1))`, flat right up to the
+ /// edge pixel for Inner Hard; Inner Soft's edge pixel is half as steep again; Outer Hard
+ /// still shades, partly, the pixel where its ramp reaches zero `size + 1½` px out.
+ #[test]
+ fn chisel_facets_match_photoshop() {
+ let amount = |g: f32| {
+ let (se, ce) = 30f32.to_radians().sin_cos();
+ ((g * ce + se) / (1.0 + g * g).sqrt() - se) / (1.0 - se)
+ };
+ let n = 80;
+ let shape = square(n, 20, 60);
+ let maps = |style, technique| {
+ let mut b = bevel_of(style, technique);
+ b.size = 10.0;
+ bevel_maps(&shape, &b, &GlobalLight::default(), &no_tex(), &PreparedPatterns::new(&[], PREPARED_PATTERN_BYTES)).0
+ };
+ let at = |m: &Map, y: usize| m.v[y * n + 40];
+ let facet = amount(10.0 / 22.0);
+ let hard = maps(BevelStyle::InnerBevel, BevelTechnique::ChiselHard);
+ for y in 20..30 {
+ assert!((at(&hard[0], y) - facet).abs() < 0.01, "inner hard facet at row {y}: {} vs {facet}", at(&hard[0], y));
+ }
+ assert!(at(&hard[0], 30) > 0.05 && at(&hard[0], 30) < facet, "one partial pixel at the end");
+ let soft = maps(BevelStyle::InnerBevel, BevelTechnique::ChiselSoft);
+ assert!((at(&soft[0], 20) - amount(1.5 * 10.0 / 22.0)).abs() < 0.01, "steeper edge pixel: {}", at(&soft[0], 20));
+ assert!((at(&soft[0], 24) - facet).abs() < 0.01, "then Chisel Hard's facet");
+ let outer = maps(BevelStyle::OuterBevel, BevelTechnique::ChiselHard);
+ // Below the shape (rows 60..): shadow facet through 10½ px, partial at 11½.
+ assert!((at(&outer[1], 70) - at(&outer[1], 61)).abs() < 1e-4);
+ assert!(at(&outer[1], 71) > 0.05 && at(&outer[1], 71) < at(&outer[1], 70));
+ assert_eq!(at(&outer[1], 72), 0.0);
+ }
+
#[test]
fn emboss_paints_both_sides_and_pillow_flips_the_outside() {
let shape = square(40, 10, 30);
diff --git a/crates/gpu/src/compose.wgsl b/crates/gpu/src/compose.wgsl
index 6b5321fce..5a3a6fe58 100644
--- a/crates/gpu/src/compose.wgsl
+++ b/crates/gpu/src/compose.wgsl
@@ -1178,7 +1178,7 @@ fn fs_mfinish(in: VOut) -> @location(0) vec4<f32> {
}
// Chiselled bevel height from A = dist_inside, B = dist_outside (`effects::bevel_chisel_h`):
-// p0 = (paint (0 outer, 1 both / emboss, 2 inner), size).
+// p0 = (paint (0 outer, 1 both / emboss, 2 inner), size, Chisel Soft).
@fragment
fn fs_mbevelh(in: VOut) -> @location(0) vec4<f32> {
let p = local(in.pos);
@@ -1186,16 +1186,33 @@ fn fs_mbevelh(in: VOut) -> @location(0) vec4<f32> {
let dout = textureLoad(tex_b, p, 0).r;
let size = op.p0.y;
var h: f32;
+ // The ramp spans size + 1 px, from half a pixel beyond the edge (`effects::bevel_chisel_h`).
+ let ramp = size + 1.0;
+ let soft = op.p0.z > 0.5;
switch i32(op.p0.x) {
- case 0: { h = 1.0 - clamp(dout / size, 0.0, 1.0); }
+ case 0: {
+ if (soft) {
+ h = select((size + 2.0) / ramp, 1.0 - clamp((dout + 0.5) / ramp, 0.0, 1.0), dout > 0.0);
+ } else {
+ let top = (size + 2.0) / ramp;
+ h = select(top, clamp((size + 1.5 - dout) / ramp, 0.0, top), dout > 0.0);
+ }
+ }
case 1: {
+ let half = size / 2.0 + 0.5;
if (din > 0.0) {
- h = 0.5 + 0.5 * clamp(din / (size / 2.0), 0.0, 1.0);
+ h = 0.5 + 0.5 * clamp(din / half, 0.0, 1.0);
+ } else {
+ h = 0.5 - 0.5 * clamp(dout / half, 0.0, 1.0);
+ }
+ }
+ default: {
+ if (soft) {
+ h = select(0.0, clamp((din + 1.5) / ramp, 0.0, (size + 2.0) / ramp), din > 0.0);
} else {
- h = 0.5 - 0.5 * clamp(dout / (size / 2.0), 0.0, 1.0);
+ h = select(0.0, clamp((din + 0.5) / ramp, 0.0, 1.0), din > 0.0);
}
}
- default: { h = clamp(din / size, 0.0, 1.0); }
}
return mout(h);
}
@@ -1209,7 +1226,8 @@ fn fs_mbevelshade(in: VOut) -> @location(0) vec4<f32> {
// effects::bevel_maps' on_bevel: the height above BEVEL_H_EPS (emboss styles: or a
// 4-neighbour's).
var hmax = ra(p);
- if (op.p1.y > 1.5) {
+ // p3.x: an outer chisel bevel, which also reaches one pixel past its ramp.
+ if (op.p1.y > 1.5 || op.p3.x > 0.5) {
hmax = max(max(hmax, max(ra(p + vec2(1, 0)), ra(p - vec2(1, 0)))), max(ra(p + vec2(0, 1)), ra(p - vec2(0, 1))));
}
let outside = select(0.0, 1.0, s < 1.0 - INSIDE_EPS && hmax > 1e-5);
diff --git a/crates/gpu/src/fx.rs b/crates/gpu/src/fx.rs
index e3a6f5863..13cea90af 100644
--- a/crates/gpu/src/fx.rs
+++ b/crates/gpu/src/fx.rs
@@ -376,8 +376,10 @@ pub(crate) fn program_with(e: &Effect, light: &GlobalLight, vector_shape: bool,
} else {
let din = b.field(FieldKind::BevelInside, size);
let dout = b.field(FieldKind::BevelOutside, size);
- let h = b.push(stage(Kernel::MBevelH, Some(din), Some(dout), [paint(g.paint), size, 0.0, 0.0], 0));
- if g.chisel_soft > 0.0 { b.conv(h, photocraft_compose::effects::tent_kernel(g.chisel_soft)) } else { h }
+ let soft = g.chisel_soft > 0.0;
+ let h = b.push(stage(Kernel::MBevelH, Some(din), Some(dout), [paint(g.paint), size, f32::from(u8::from(soft)), 0.0], 0));
+ // As `effects::bevel_height`: only emboss styles blur a Chisel Soft ramp.
+ if soft && g.paint == BevelPaint::Both { b.conv(h, photocraft_compose::effects::tent_kernel(g.chisel_soft)) } else { h }
};
if let Some(c) = &bv.contour {
// Contour element: the height through the contour (`bevel_contour_lut`).
@@ -418,6 +420,7 @@ pub(crate) fn program_with(e: &Effect, light: &GlobalLight, vector_shape: bool,
for which in [0.0, 1.0] {
let mut s = stage(Kernel::MBevelShade, Some(h), None, [ca * ce, -sa * ce, se, se], 1);
s.p1 = [*depth, *region, which, f32::from(u8::from(lut.is_some()))];
+ s.p3 = [f32::from(u8::from(g.paint == BevelPaint::Outer && bv.technique != BevelTechnique::Smooth)), 0.0, 0.0, 0.0];
s.lut = lut.clone();
s.s = Some(In::Shape);
s.out = Some(out);