Skip to content

EffectCraft: An open-source motion graphics and compositing tool written by scratch in pure Rust

fix: move Buf offsets by the pad Image::padded actually applies (#2092 review follow-up)

Christopher Linnett34242e8c1b00
diff --git a/crates/effects/src/lib.rs b/crates/effects/src/lib.rs
index 40ed79a7d..ba9314ee6 100644
--- a/crates/effects/src/lib.rs
+++ b/crates/effects/src/lib.rs
@@ -286,14 +286,15 @@ impl Buf {
self.offset[0] += origin[0] * self.scale;
self.offset[1] += origin[1] * self.scale;
}
- /// Grow by `pad` transparent pixels on every side.
+ /// Grow by `pad` transparent pixels on every side (as clamped by [`Image::effective_pad`]).
pub fn pad(&mut self, pad: u32) {
- if pad == 0 {
+ let eff = self.img.effective_pad(pad);
+ if eff == 0 {
return;
}
- self.img = self.img.padded(pad);
- self.offset[0] += pad as f64;
- self.offset[1] += pad as f64;
+ self.img = self.img.padded(eff);
+ self.offset[0] += eff as f64;
+ self.offset[1] += eff as f64;
}
}
@@ -1267,6 +1268,20 @@ mod tests {
assert_eq!(out.img.data, img.data);
}
+ #[test]
+ fn pad_offset_follows_the_clamped_pad() {
+ // Image::padded clamps a hostile pad to keep each side within MAX_PADDED_SIDE; the offset
+ // must move by the pad actually applied, or the content drifts from its layer position.
+ let mut img = Image::new(29_990, 2);
+ img.set(0, 0, [1.0, 0.0, 0.0, 1.0]);
+ let mut buf = Buf { img, offset: [3.0, 4.0], scale: 1.0 };
+ buf.pad(u32::MAX);
+ // Width binds: (MAX_PADDED_SIDE - 29 990) / 2 = 5.
+ assert_eq!(buf.offset, [8.0, 9.0]);
+ let (x, y) = buf.to_px([-3.0, -4.0]);
+ assert_eq!(buf.img.get(x as i64, y as i64), [1.0, 0.0, 0.0, 1.0]);
+ }
+
#[test]
#[ignore]
fn dump_registry() {
diff --git a/crates/raster/src/lib.rs b/crates/raster/src/lib.rs
index 3d31da299..8a57cafc7 100644
--- a/crates/raster/src/lib.rs
+++ b/crates/raster/src/lib.rs
@@ -143,16 +143,23 @@ impl Image {
}
o
}
- /// Copy with `pad` transparent pixels on every side.
+ /// The pad [`Image::padded`] applies for a requested `pad`: clamped so neither output side
+ /// exceeds [`MAX_PADDED_SIDE`]. At most `(MAX_PADDED_SIDE - side) / 2`, so
+ /// `side + 2 · pad ≤ MAX_PADDED_SIDE` and overflow is unreachable by construction (a side
+ /// already past the cap gets pad 0). Callers that track where the pixels went (a buffer
+ /// offset) must move by this, not by the requested pad.
+ pub fn effective_pad(&self, pad: u32) -> u32 {
+ pad.min(MAX_PADDED_SIDE.saturating_sub(self.width) / 2).min(MAX_PADDED_SIDE.saturating_sub(self.height) / 2)
+ }
+ /// Copy with [`Image::effective_pad`] transparent pixels on every side.
///
- /// `pad` is clamped so neither output side exceeds [`MAX_PADDED_SIDE`]: the never-crash bound
- /// lives here rather than in each caller. The effective pad is at most
- /// `(MAX_PADDED_SIDE - side) / 2`, so `side + 2 · pad ≤ MAX_PADDED_SIDE` and overflow is
- /// unreachable by construction (a side already past the cap gets pad 0 — still no panic).
- /// Callers that must *reject* rather than clamp an oversized pad pre-check it (see
- /// `padded_size` in render's `three_d/compose.rs`); legitimate bounded pads are byte-identical.
+ /// The clamp keeps the output within the largest image the engine supports (a max-size comp
+ /// frame), so an unbounded pad cannot overflow or size a buffer past that. It is not a memory
+ /// budget: callers that pad by a derived, possibly large amount bound the area themselves
+ /// (`DOF_PAD_BUDGET` in render's `three_d/compose.rs`, `fits()` on the GPU path) or reject an
+ /// oversized pad first (`padded_size`); legitimate bounded pads are byte-identical.
pub fn padded(&self, pad: u32) -> Image {
- let eff = pad.min(MAX_PADDED_SIDE.saturating_sub(self.width) / 2).min(MAX_PADDED_SIDE.saturating_sub(self.height) / 2);
+ let eff = self.effective_pad(pad);
let mut out = Image::new(self.width + 2 * eff, self.height + 2 * eff);
let ow = out.width as usize;
for y in 0..self.height as usize {