This commit is contained in:
Simon Gardling
2021-03-31 10:13:06 -04:00
parent f76c9f7401
commit bec6340607
6 changed files with 36 additions and 45 deletions

View File

@@ -21,8 +21,7 @@ impl Blur {
}
}
/// Blur an image with 2 box filter passes. The result will be written to the src slice, while
/// the buf slice is used as a scratch space.
// Blur an image with 2 box filter passes. The result will be written to the src slice, while the buf slice is used as a scratch space.
pub fn run(
&mut self,
src: &mut [f32],
@@ -37,8 +36,7 @@ impl Blur {
self.box_blur(src, buf, width, height, boxes[1], decay);
}
/// 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.
// 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<const N: usize>(sigma: f32) -> ([usize; N]) {
let w_ideal = (12.0 * sigma * sigma / N as f32 + 1.0).sqrt();
let mut w = w_ideal as usize;
@@ -54,8 +52,7 @@ impl Blur {
result
}
/// 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.
// 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.
fn box_blur(
&mut self,
src: &mut [f32],
@@ -69,15 +66,14 @@ impl Blur {
self.box_blur_v(buf, src, width, height, radius, decay);
}
/// Perform one pass of the 1D box filter of the given radius along x axis.
// Perform one pass of the 1D box filter of the given radius along x axis.
fn box_blur_h(&mut self, src: &[f32], dst: &mut [f32], width: usize, radius: usize) {
let weight = 1.0 / (2 * radius + 1) as f32;
src.par_chunks_exact(width)
.zip(dst.par_chunks_exact_mut(width))
.for_each(|(src_row, dst_row)| {
// First we build a value for the beginning of each row. We assume periodic boundary
// conditions, so we need to push the left index to the opposite side of the row.
// First we build a value for the beginning of each row. We assume periodic boundary conditions, so we need to push the left index to the opposite side of the row.
let width_sub_radius = width - radius;
let mut value = src_row[width - radius - 1];
for j in 0..radius {
@@ -93,8 +89,7 @@ impl Blur {
})
}
/// Perform one pass of the 1D box filter of the given radius along y axis. Applies the decay
/// factor to the destination buffer.
// Perform one pass of the 1D box filter of the given radius along y axis. Applies the decay factor to the destination buffer.
fn box_blur_v(
&mut self,
src: &[f32],
@@ -106,9 +101,7 @@ impl Blur {
) {
let weight = decay / (2 * radius + 1) as f32;
// We don't replicate the horizontal filter logic because of the cache-unfriendly memory
// access patterns of sequential iteration over individual columns. Instead, we iterate over
// rows via loop interchange.
// We don't replicate the horizontal filter logic because of the cache-unfriendly memory access patterns of sequential iteration over individual columns. Instead, we iterate over rows via loop interchange.
let height_sub_radius = height - radius;
let offset = (height_sub_radius - 1) * width;
self.row_buffer