Skip to content

Commit

Permalink
fix: convert box sizes to radii
Browse files Browse the repository at this point in the history
write test to compare gaussian and fast blur
  • Loading branch information
torfmaster committed Sep 3, 2024
1 parent df46c46 commit 1592ff5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
2 changes: 1 addition & 1 deletion examples/fast_blur/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ fn main() {
.decode()
.unwrap();

let img2 = img.fast_blur(10.0);
let img2 = img.blur(10.0);

img2.save("examples/fast_blur/mandril_color_blurred.tif")
.unwrap();
Expand Down
4 changes: 2 additions & 2 deletions src/imageops/fast_blur.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ pub fn fast_blur<P: Pixel>(
&samples,
width as usize,
height as usize,
*radius,
(*radius-1)/2,
P::CHANNEL_COUNT as usize,
);
samples = horizontal_fast_blur_half::<P::Subpixel>(
&horizontally_blurred_transposed,
height as usize,
width as usize,
*radius,
(*radius-1)/2,
P::CHANNEL_COUNT as usize,
);
}
Expand Down
23 changes: 23 additions & 0 deletions src/imageops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -542,4 +542,27 @@ mod tests {
let image = GrayImage::new(50, 50);
let _ = fast_blur(&image, 1.0);
}

#[test]
fn fast_blur_approximates_gaussian_blur_well() {
let path = concat!(
env!("CARGO_MANIFEST_DIR"),
"/tests/images/tiff/testsuite/rgb-3c-16b.tiff"
);
let image = crate::open(path).unwrap();
let image1 = image.blur(50.0).to_rgb8();
let binding = image1.as_flat_samples();
let bytes1 = binding.as_slice();
let image2 = image.fast_blur(50.0).to_rgb8();
let binding = image2.as_flat_samples();
let bytes2 = binding.as_slice();

let error = bytes1
.iter()
.zip(bytes2.iter())
.map(|(a, b)| ((*a as f32 - *b as f32) / (*a as f32)))
.sum::<f32>()
/ (bytes1.len() as f32);
assert!(error<0.05);
}
}

0 comments on commit 1592ff5

Please sign in to comment.