diff --git a/src/blur.rs b/src/blur.rs index e28000a..8813618 100644 --- a/src/blur.rs +++ b/src/blur.rs @@ -30,18 +30,17 @@ impl Blur { /// Approximate 1D Gaussian filter of standard deviation sigma with N box filter passes. Each element in the output array contains the radius of the box filter for the corresponding pass. fn boxes_for_gaussian(sigma: f32) -> [usize; N] { - let w_ideal = (12.0 * sigma * sigma / N as f32 + 1.0).sqrt(); + let sigma_sq = sigma.powi(2); + let w_ideal = (12.0 * sigma_sq / N as f32 + 1.0).sqrt(); let mut w = w_ideal as usize; w -= 1 - (w & 1); - let mut m = 0.25 * (N * (w + 3)) as f32; - m -= 3.0 * sigma * sigma / (w + 1) as f32; - let m = m.round() as usize; + let m = (0.25 * (N * (w + 3)) as f32 - 3.0 * sigma_sq / (w + 1) as f32).round() as usize; - let mut result = [0; N]; - for (i, value) in result.iter_mut().enumerate() { - *value = (if i < m { w - 1 } else { w + 1 }) / 2; - } - result + (0..N) + .map(|i| (w + 1 - 2 * (i < m) as usize) / 2) + .collect::>() + .try_into() + .unwrap() } /// Perform one pass of the 2D box filter of the given radius. The result will be written to the src slice, while the buf slice is used as a scratch space.