Skip to content

Commit

Permalink
incorporate review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
torfmaster committed Aug 17, 2024
1 parent cdd617d commit b7a9ae8
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 20 deletions.
3 changes: 2 additions & 1 deletion examples/fast_blur/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
lenna_blurred.png
mandril_color_blurred.tif

Binary file removed examples/fast_blur/lenna.png
Binary file not shown.
5 changes: 3 additions & 2 deletions examples/fast_blur/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use image::ImageReader;

fn main() {
let img = ImageReader::open("examples/fast_blur/lenna.png")
let img = ImageReader::open("examples/fast_blur/mandril_color.tif")
.unwrap()
.decode()
.unwrap();

let img2 = img.fast_blur(10.0);

img2.save("examples/fast_blur/lenna_blurred.png").unwrap();
img2.save("examples/fast_blur/mandril_color_blurred.tif")
.unwrap();
}
Binary file added examples/fast_blur/mandril_color.tif
Binary file not shown.
44 changes: 31 additions & 13 deletions src/imageops/fast_blur.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,24 @@ pub fn fast_blur<P: Pixel>(
sigma: f32,
) -> ImageBuffer<P, Vec<P::Subpixel>> {
let (width, height) = image_buffer.dimensions();

if width == 0 || height == 0 {
return image_buffer.clone();
}
let mut samples = image_buffer.as_flat_samples().samples.to_vec();
let num_passes = 3;

let boxes = boxes_for_gauss(sigma, num_passes);

for radius in boxes.iter().take(num_passes) {
let horizontally_blurred_transposed = my_fast_horizontal_blur::<P::Subpixel>(
let horizontally_blurred_transposed = horizontal_fast_blur_half::<P::Subpixel>(
&samples,
width as usize,
height as usize,
*radius,
P::CHANNEL_COUNT as usize,
);
samples = my_fast_horizontal_blur::<P::Subpixel>(
samples = horizontal_fast_blur_half::<P::Subpixel>(
&horizontally_blurred_transposed,
height as usize,
width as usize,
Expand Down Expand Up @@ -60,7 +64,7 @@ fn channel_idx(channel: usize, idx: usize, channel_num: usize) -> usize {
channel_num * idx + channel
}

fn my_fast_horizontal_blur<P: Primitive>(
fn horizontal_fast_blur_half<P: Primitive>(
samples: &[P],
width: usize,
height: usize,
Expand All @@ -75,18 +79,26 @@ fn my_fast_horizontal_blur<P: Primitive>(
let min_value = P::DEFAULT_MIN_VALUE.to_f32().unwrap();
let max_value = P::DEFAULT_MAX_VALUE.to_f32().unwrap();

for i in 0..height {
for row in 0..height {
for (channel, value) in vals.iter_mut().enumerate().take(channel_num) {
*value = ((-(r as isize))..(r + 1) as isize)
.map(|x| {
extended_f(samples, width, height, x, i as isize, channel, channel_num)
.to_f32()
.unwrap_or(0.0)
extended_f(
samples,
width,
height,
x,
row as isize,
channel,
channel_num,
)
.to_f32()
.unwrap_or(0.0)
})
.sum()
}

for j in 0..width {
for column in 0..width {
for channel in 0..channel_num {
let val = vals[channel] / (2.0 * r as f32 + 1.0);
let val = if val < min_value {
Expand All @@ -98,14 +110,20 @@ fn my_fast_horizontal_blur<P: Primitive>(
};
let val = P::from(val).unwrap();

out_samples[channel_idx(channel, i + j * height, channel_num)] = val;
let destination_row = column;
let destination_column = row;
out_samples[channel_idx(
channel,
destination_column + destination_row * height,
channel_num,
)] = val;
vals[channel] = vals[channel]
- extended_f(
samples,
width,
height,
j as isize - r as isize,
i as isize,
column as isize - r as isize,
row as isize,
channel,
channel_num,
)
Expand All @@ -115,8 +133,8 @@ fn my_fast_horizontal_blur<P: Primitive>(
samples,
width,
height,
{ j + r + 1 } as isize,
i as isize,
{ column + r + 1 } as isize,
row as isize,
channel,
channel_num,
)
Expand Down
26 changes: 22 additions & 4 deletions src/imageops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,23 +481,41 @@ mod tests {
}

#[test]
/// Test blur doesn't panick when passed 0.0
/// Test blur doesn't panic when passed 0.0
fn test_blur_zero() {
let image = RgbaImage::new(50, 50);
let _ = super::blur(&image, 0.0);
}

#[test]
/// Test blur doesn't panick when passed 0.0
/// Test fast blur doesn't panic when passed 0.0
fn test_fast_blur_zero() {
let image = RgbaImage::new(50, 50);
let _ = super::fast_blur(&image, -1.0);
let _ = super::fast_blur(&image, 0.0);
}

#[test]
/// Test blur doesn't panick when passed negative numbers
/// Test fast blur doesn't panic when passed negative numbers
fn test_fast_blur_negative() {
let image = RgbaImage::new(50, 50);
let _ = super::fast_blur(&image, -1.0);
}

#[test]
/// Test fast blur doesn't panic when sigma produces boxes larger than the image
fn test_fast_large_sigma() {
let image = RgbaImage::new(1, 1);
let _ = super::fast_blur(&image, 50.0);
}

#[test]
/// Test blur doesn't panic when passed an empty image (any direction)
fn test_fast_blur_empty() {
let image = RgbaImage::new(0, 0);
let _ = super::fast_blur(&image, 1.0);
let image = RgbaImage::new(20, 0);
let _ = super::fast_blur(&image, 1.0);
let image = RgbaImage::new(0, 20);
let _ = super::fast_blur(&image, 1.0);
}
}

0 comments on commit b7a9ae8

Please sign in to comment.