Skip to content

Commit

Permalink
refactor iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
cospectrum committed Apr 21, 2024
1 parent 18704cd commit d7e05bd
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/integral_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,17 @@ where
let out_width = in_width + 1;
let out_height = in_height + 1;

let mut out = Image::<ChannelMap<P, T>>::new(out_width, out_height);
let mut out = Image::new(out_width, out_height);

if in_width == 0 || in_height == 0 {
return out;
}

let mut sum = vec![T::zero(); P::CHANNEL_COUNT as usize];
let zero = T::zero();
let mut sum = vec![zero; P::CHANNEL_COUNT as usize];
for y in 0..in_height {
sum.iter_mut().for_each(|x| {
*x = T::zero();
*x = zero;
});
for x in 0..in_width {
// JUSTIFICATION
Expand All @@ -125,8 +126,8 @@ where
// Correctness
// x and y are within bounds by definition of in_width and in_height
let input = unsafe { image.unsafe_get_pixel(x, y) };
for (s, c) in sum.iter_mut().zip(input.channels()) {
let pix: T = (*c).into();
for (s, &c) in sum.iter_mut().zip(input.channels()) {
let pix: T = c.into();
*s += if square { pix * pix } else { pix };
}

Expand All @@ -141,8 +142,8 @@ where
// pixel here we need to use the method with bounds checking
let current = out.get_pixel_mut(x + 1, y + 1);
// Using zip here makes this slower.
for c in 0..P::CHANNEL_COUNT {
current.channels_mut()[c as usize] = above.channels()[c as usize] + sum[c as usize];
for c in 0..P::CHANNEL_COUNT as usize {
current.channels_mut()[c] = above.channels()[c] + sum[c];
}
}
}
Expand Down

0 comments on commit d7e05bd

Please sign in to comment.