diff --git a/src/imageops/fast_blur.rs b/src/imageops/fast_blur.rs index 5d5e019d1d..562a58be50 100644 --- a/src/imageops/fast_blur.rs +++ b/src/imageops/fast_blur.rs @@ -11,25 +11,25 @@ pub fn fast_blur( ) -> ImageBuffer> { let (width, height) = image_buffer.dimensions(); let mut samples = image_buffer.as_flat_samples().samples.to_vec(); + dbg!(samples.len()); let num_passes = 3; let boxes = boxes_for_gauss(sigma, num_passes); - for pass in boxes.iter().take(num_passes) { - let radius = boxes[*pass]; + for radius in boxes.iter().take(num_passes) { let mut horizontally_blurred = my_fast_horizontal_blur::( &samples, width as usize, height as usize, - radius, + *radius, P::CHANNEL_COUNT as usize, ); samples = my_fast_vertical_blur::( &mut horizontally_blurred, width as usize, height as usize, - radius, + *radius, P::CHANNEL_COUNT as usize, ); } @@ -58,8 +58,8 @@ fn boxes_for_gauss(sigma: f32, n: usize) -> Vec { box_sizes } -fn channel_idx(channel: usize, idx: usize) -> usize { - 3 * idx + channel +fn channel_idx(channel: usize, idx: usize, channel_num: usize) -> usize { + channel_num * idx + channel } fn my_fast_horizontal_blur( @@ -71,7 +71,7 @@ fn my_fast_horizontal_blur( ) -> Vec

{ let channel_size = width * height; - let mut out_samples: Vec

= vec![P::from(0).unwrap(); channel_size * 3]; + let mut out_samples: Vec

= vec![P::from(0).unwrap(); channel_size * channel_num]; let mut vals = vec![0.0; channel_num]; let min_value = P::DEFAULT_MIN_VALUE.to_f32().unwrap(); @@ -81,7 +81,7 @@ fn my_fast_horizontal_blur( 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) + extended_f(samples, width, height, x, i as isize, channel, channel_num) .to_f32() .unwrap_or(0.0) }) @@ -100,7 +100,7 @@ fn my_fast_horizontal_blur( }; let val = P::from(val).unwrap(); - out_samples[channel_idx(channel, i * width + j)] = val; + out_samples[channel_idx(channel, i * width + j, channel_num)] = val; vals[channel] = vals[channel] - extended_f( samples, @@ -109,6 +109,7 @@ fn my_fast_horizontal_blur( j as isize - r as isize, i as isize, channel, + channel_num, ) .to_f32() .unwrap_or(0.0) @@ -119,6 +120,7 @@ fn my_fast_horizontal_blur( { j + r + 1 } as isize, i as isize, channel, + channel_num, ) .to_f32() .unwrap_or(0.0) @@ -148,7 +150,7 @@ fn my_fast_vertical_blur( for (channel, value) in vals.iter_mut().enumerate().take(channel_num) { *value = ((-(r as isize))..(r + 1) as isize) .map(|y| { - extended_f(samples, width, height, j as isize, y, channel) + extended_f(samples, width, height, j as isize, y, channel, channel_num) .to_f32() .unwrap() }) @@ -174,7 +176,7 @@ fn my_fast_vertical_blur( } }; - out_samples[channel_idx(channel, i * width + j)] = val; + out_samples[channel_idx(channel, i * width + j, channel_num)] = val; vals[channel] = vals[channel] - extended_f( samples, @@ -183,6 +185,7 @@ fn my_fast_vertical_blur( j as isize, i as isize - r as isize, channel, + channel_num, ) .to_f32() .unwrap() @@ -193,6 +196,7 @@ fn my_fast_vertical_blur( j as isize, i as isize + (r + 1) as isize, channel, + channel_num, ) .to_f32() .unwrap(); @@ -210,8 +214,9 @@ fn extended_f( x: isize, y: isize, channel: usize, + channel_num: usize, ) -> P { let x = min(width as isize - 1, max(0, x)) as usize; let y = min(height as isize - 1, max(0, y)) as usize; - samples[channel_idx(channel, y * width + x)] + samples[channel_idx(channel, y * width + x, channel_num)] } diff --git a/src/imageops/mod.rs b/src/imageops/mod.rs index c9cc9c242a..88fcfa05df 100644 --- a/src/imageops/mod.rs +++ b/src/imageops/mod.rs @@ -486,4 +486,11 @@ mod tests { let image = RgbaImage::new(50, 50); let _ = super::blur(&image, 0.0); } + + #[test] + /// Test blur doesn't panick when passed 0.0 + fn test_fast_blur_zero() { + let image = RgbaImage::new(50, 50); + let _ = super::fast_blur(&image, 0.0); + } }