Skip to content

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

Fix #1543: oversized Satin and Bevel & Emboss sizes render instead of panicking (#1563)

BFlatf82a55f05601
diff --git a/crates/compose/src/effects.rs b/crates/compose/src/effects.rs
index 6fcfe9ecf..dd589aef6 100644
--- a/crates/compose/src/effects.rs
+++ b/crates/compose/src/effects.rs
@@ -706,10 +706,17 @@ fn paint_glow(dst: &mut Buffer, m: &Map, g: &Glow, shape_bounds: Rect, anchor: (
paint(dst, &strength, |i| prepared.sample(1.0 - m.v.get(i).copied().unwrap_or(0.0).clamp(0.0, 1.0)), g.common.blend, g.common.opacity);
}
+/// A box width as the blurs use it: at least 1, at most [`MAX_REACH`] (an effect never reaches
+/// further, see [`margin`]), and 1 for NaN. A size or softness from a command or a file can be
+/// anything, and an unbounded width sizes the kernel and its buffers (#1543).
+fn box_width(w: f32) -> f32 {
+ if w.is_nan() { 1.0 } else { w.clamp(1.0, MAX_REACH) }
+}
+
/// Normalised weights of a centred box of (fractional) width `w`: tap `i` gets the overlap of
/// `[i - 0.5, i + 0.5]` with `[-w/2, w/2]`.
fn box_weights(w: f32) -> Vec<f32> {
- let w = w.max(1.0);
+ let w = box_width(w);
let half = w / 2.0;
let r = (half - 0.5).ceil().max(0.0) as i64;
let v: Vec<f32> = (-r..=r).map(|i| ((i as f32 + 0.5).min(half) - (i as f32 - 0.5).max(-half)).max(0.0)).collect();
@@ -735,10 +742,11 @@ pub fn tent_kernel(w: f32) -> (i32, Vec<f32>) {
/// Box geometry for a (fractional) width: (`r`, end-tap weight `f`, 1 / width) — the
/// [`box_weights`] taps are `r - 1` full ones each side of the centre plus the two end taps at `f`.
fn box_geom(bw: f32) -> (i64, f64, f64) {
- let half = bw.max(1.0) / 2.0;
+ let bw = box_width(bw);
+ let half = bw / 2.0;
let r = (half - 0.5).ceil().max(0.0) as i64;
let f = f64::from((half - (r as f32 - 0.5)).clamp(0.0, 1.0));
- (r, f, 1.0 / f64::from(bw.max(1.0)))
+ (r, f, 1.0 / f64::from(bw))
}
/// One box pass over `src` (zero outside it) evaluated at `x0 .. x0 + dst.len()`, as a running
@@ -1691,6 +1699,11 @@ mod tests {
assert_eq!(r, 4);
assert!(k[0] > 0.0 && k[0] < 1.0 / 25.0);
assert_eq!(tent_kernel(1.0), (0, vec![1.0]));
+ // #1543: a huge, infinite or NaN width is capped, not turned into a kernel of 2^62 taps.
+ for w in [1e30, f32::INFINITY, f32::MAX] {
+ assert_eq!(tent_kernel(w), tent_kernel(MAX_REACH), "{w}");
+ }
+ assert_eq!(tent_kernel(f32::NAN), (0, vec![1.0]));
}
fn no_tex() -> TextureCtx<'static> {
diff --git a/crates/engine/src/layer_style.rs b/crates/engine/src/layer_style.rs
index 07e944c0b..15d4a88d6 100644
--- a/crates/engine/src/layer_style.rs
+++ b/crates/engine/src/layer_style.rs
@@ -753,6 +753,27 @@ mod tests {
}
}
+ /// #1543: a Satin or Bevel & Emboss size or softness far past Photoshop's range rendered a
+ /// blur kernel of 2^62 taps ("capacity overflow"), or ran for minutes at 1e6. The blur width
+ /// is capped at the effect reach, so the document still renders, quickly.
+ #[test]
+ fn oversized_satin_and_bevel_sizes_render() {
+ for (cmd, p) in [
+ ("satin", json!({"size": 1e308})),
+ ("satin", json!({"size": 1_000_000})),
+ ("bevelEmboss", json!({"size": 1e308})),
+ ("bevelEmboss", json!({"soften": 1e308})),
+ ("bevelEmboss", json!({"size": 1_000_000, "soften": 1_000_000})),
+ ] {
+ let mut s = session();
+ s.execute("edit.fill", json!({"color": "#ff0000"})).unwrap();
+ s.execute(&format!("layer.layerStyle.{cmd}"), p.clone()).unwrap();
+ let start = std::time::Instant::now();
+ s.execute("document.pixel", json!({"x": 0, "y": 0})).unwrap_or_else(|e| panic!("{cmd} {p}: {e}"));
+ assert!(start.elapsed().as_secs() < 20, "{cmd} {p}: {:?}", start.elapsed());
+ }
+ }
+
#[test]
fn params_are_applied_and_replace_or_add() {
let mut s = session();