Skip to content

Commit

Permalink
add basic test
Browse files Browse the repository at this point in the history
  • Loading branch information
torfmaster committed Aug 4, 2024
1 parent 2339f81 commit 7d1683e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
29 changes: 17 additions & 12 deletions src/imageops/fast_blur.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,25 @@ pub fn fast_blur<P: Pixel>(
) -> ImageBuffer<P, Vec<P::Subpixel>> {
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::<P::Subpixel>(
&samples,
width as usize,
height as usize,
radius,
*radius,
P::CHANNEL_COUNT as usize,
);
samples = my_fast_vertical_blur::<P::Subpixel>(
&mut horizontally_blurred,
width as usize,
height as usize,
radius,
*radius,
P::CHANNEL_COUNT as usize,
);
}
Expand Down Expand Up @@ -58,8 +58,8 @@ fn boxes_for_gauss(sigma: f32, n: usize) -> Vec<usize> {
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<P: Primitive>(
Expand All @@ -71,7 +71,7 @@ fn my_fast_horizontal_blur<P: Primitive>(
) -> Vec<P> {
let channel_size = width * height;

let mut out_samples: Vec<P> = vec![P::from(0).unwrap(); channel_size * 3];
let mut out_samples: Vec<P> = 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();
Expand All @@ -81,7 +81,7 @@ fn my_fast_horizontal_blur<P: Primitive>(
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)
})
Expand All @@ -100,7 +100,7 @@ fn my_fast_horizontal_blur<P: Primitive>(
};
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,
Expand All @@ -109,6 +109,7 @@ fn my_fast_horizontal_blur<P: Primitive>(
j as isize - r as isize,
i as isize,
channel,
channel_num,
)
.to_f32()
.unwrap_or(0.0)
Expand All @@ -119,6 +120,7 @@ fn my_fast_horizontal_blur<P: Primitive>(
{ j + r + 1 } as isize,
i as isize,
channel,
channel_num,
)
.to_f32()
.unwrap_or(0.0)
Expand Down Expand Up @@ -148,7 +150,7 @@ fn my_fast_vertical_blur<P: Primitive>(
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()
})
Expand All @@ -174,7 +176,7 @@ fn my_fast_vertical_blur<P: Primitive>(
}
};

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,
Expand All @@ -183,6 +185,7 @@ fn my_fast_vertical_blur<P: Primitive>(
j as isize,
i as isize - r as isize,
channel,
channel_num,
)
.to_f32()
.unwrap()
Expand All @@ -193,6 +196,7 @@ fn my_fast_vertical_blur<P: Primitive>(
j as isize,
i as isize + (r + 1) as isize,
channel,
channel_num,
)
.to_f32()
.unwrap();
Expand All @@ -210,8 +214,9 @@ fn extended_f<P: Primitive>(
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)]
}
7 changes: 7 additions & 0 deletions src/imageops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 7d1683e

Please sign in to comment.