diff --git a/crates/raster/src/blur.rs b/crates/raster/src/blur.rs index cc6fe35df..489b32755 100644 --- a/crates/raster/src/blur.rs +++ b/crates/raster/src/blur.rs @@ -297,7 +297,15 @@ fn boxes_unbounded(line: &[Px], r: &[i64], shift: i64, out: &mut [Px]) { }) .collect(); let norm: f64 = r.iter().map(|&ri| 2.0 * ri as f64 + 1.0).product(); + // The boxes read the line within `reach` of `x − shift`. Outside that the result is 0, and + // the polynomials far past the line would only cancel to their rounding error. + let reach = r.iter().fold(0i64, |s, &ri| s.saturating_add(ri)); for (x, o) in (0i64..).zip(out.iter_mut()) { + let c = x.saturating_sub(shift); + if c.saturating_add(reach) < 0 || c.saturating_sub(reach) >= n_i { + *o = [0.0; 4]; + continue; + } let mut v = [0.0f64; 4]; for &(offset, sign) in &terms { let s = at(x.saturating_add(offset)); @@ -678,6 +686,19 @@ mod tests { } } + /// #3464: a window the blur doesn't reach stays exactly transparent, however far the move + /// (far past the line, the cancelling polynomials left a rounding residue of about 5). + #[test] + fn unbounded_gaussian_at_a_far_shift_is_exactly_transparent() { + let img = Image::filled(1, 1, [1.0, 1.0, 1.0, 1.0]); + for shift in [[-1_000_000_030i64, 0i64], [0, 1_000_000_030], [i64::MIN, i64::MAX]] { + let out = gaussian_blur_unbounded_at(&img, 1.5, 1.5, shift, 4, 3); + assert!(out.data.iter().flatten().all(|v| *v == 0.0), "{shift:?}"); + } + // Next to the window the tail still reaches in. + assert!(gaussian_blur_unbounded_at(&img, 1.5, 1.5, [-1, 0], 4, 3).get(0, 0)[3] > 0.0); + } + /// #3464: a sigma past any buffer, or infinite, blurs as the widest one does: finite, no /// overflow in the radii. A NaN sigma leaves the axis alone. #[test] diff --git a/crates/raster/src/lib.rs b/crates/raster/src/lib.rs index 900416808..1708f40f6 100644 --- a/crates/raster/src/lib.rs +++ b/crates/raster/src/lib.rs @@ -17,7 +17,9 @@ pub mod yuv; use effectcraft_color::{BlendMode, blend_pixel}; use rayon::prelude::*; -pub use blur::{box_blur, directional_blur, gaussian_blur, gaussian_blur_exact, gaussian_blur_unbounded, gaussian_blur_unbounded_at, gaussian_is_exact, radial_blur}; +pub use blur::{ + box_blur, directional_blur, gaussian_blur, gaussian_blur_exact, gaussian_blur_unbounded, gaussian_blur_unbounded_at, gaussian_is_exact, radial_blur, +}; pub use channels3d::AuxChannels; pub use warp::{Sampling, WarpOpts, accumulate_warp, composite_warp, composite_warp_changed, composite_warp_reference, resample};