Skip to content

Commit cd7b022

Browse files
authored
Merge pull request #2816 from image-rs/backport-codegen-filter-1d
Backport codegen filter 1d
2 parents 913b706 + 6bbc505 commit cd7b022

2 files changed

Lines changed: 21 additions & 27 deletions

File tree

‎src/imageops/fast_blur.rs‎

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -190,25 +190,16 @@ fn box_blur_vertical_pass_strategy<T: Pixel, P: Primitive>(
190190
height: u32,
191191
radius: usize,
192192
) {
193-
if T::CHANNEL_COUNT == 1 {
194-
box_blur_vertical_pass_impl::<P, 1>(
195-
src, src_stride, dst, dst_stride, width, height, radius,
196-
);
197-
} else if T::CHANNEL_COUNT == 2 {
198-
box_blur_vertical_pass_impl::<P, 2>(
199-
src, src_stride, dst, dst_stride, width, height, radius,
200-
);
201-
} else if T::CHANNEL_COUNT == 3 {
202-
box_blur_vertical_pass_impl::<P, 3>(
203-
src, src_stride, dst, dst_stride, width, height, radius,
204-
);
205-
} else if T::CHANNEL_COUNT == 4 {
206-
box_blur_vertical_pass_impl::<P, 4>(
207-
src, src_stride, dst, dst_stride, width, height, radius,
208-
);
209-
} else {
210-
unimplemented!("More than 4 channels is not yet implemented");
211-
}
193+
box_blur_vertical_pass_impl::<P>(
194+
src,
195+
src_stride,
196+
dst,
197+
dst_stride,
198+
width,
199+
height,
200+
radius,
201+
T::CHANNEL_COUNT as usize,
202+
);
212203
}
213204

214205
fn box_blur_horizontal_pass_impl<T, const CN: usize>(
@@ -415,14 +406,16 @@ fn box_blur_horizontal_pass_impl<T, const CN: usize>(
415406
}
416407
}
417408

418-
fn box_blur_vertical_pass_impl<T: Primitive, const CN: usize>(
409+
#[allow(clippy::too_many_arguments)]
410+
fn box_blur_vertical_pass_impl<T: Primitive>(
419411
src: &[T],
420412
src_stride: usize,
421413
dst: &mut [T],
422414
dst_stride: usize,
423415
width: u32,
424416
height: u32,
425417
radius: usize,
418+
n: usize,
426419
) {
427420
assert!(width > 0, "Width must be sanitized before this method");
428421
assert!(height > 0, "Height must be sanitized before this method");
@@ -435,7 +428,7 @@ fn box_blur_vertical_pass_impl<T: Primitive, const CN: usize>(
435428

436429
let weight = 1f32 / (radius * 2 + 1) as f32;
437430

438-
let buf_size = width as usize * CN;
431+
let buf_size = width as usize * n;
439432

440433
let buf_cap = buf_size;
441434

@@ -462,8 +455,8 @@ fn box_blur_vertical_pass_impl<T: Primitive, const CN: usize>(
462455
let next = (y + half_kernel + 1).min(height_bound) * src_stride;
463456
let previous = (y as i64 - half_kernel as i64).max(0) as usize * src_stride;
464457

465-
let next_row = &src[next..next + width as usize * CN];
466-
let previous_row = &src[previous..previous + width as usize * CN];
458+
let next_row = &src[next..next + width as usize * n];
459+
let previous_row = &src[previous..previous + width as usize * n];
467460

468461
for (((src_next, src_previous), buffer), dst) in next_row
469462
.iter()

‎src/imageops/filter_1d.rs‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,11 +210,12 @@ impl ToStorage<f32> for f32 {
210210
/// Common convolution formula O(x,y)=∑K(k)⋅I(x,y+k); where sums goes from 0...R
211211
/// when filter is symmetric that we can half kernel reads by using formula
212212
/// O(x,y)=(∑K(k)⋅(I(x,y+k) + I(x,y+(R-k)))) + K(R/2)⋅I(x,y+R/2); where sums goes from 0...R/2
213-
fn filter_symmetric_column<T, F, const N: usize>(
213+
fn filter_symmetric_column<T, F>(
214214
arena_src: &[&[T]],
215215
dst_row: &mut [T],
216216
image_size: FilterImageSize,
217217
kernel: &[F],
218+
n: usize,
218219
) where
219220
T: Copy + AsPrimitive<F>,
220221
F: ToStorage<T>
@@ -225,7 +226,7 @@ fn filter_symmetric_column<T, F, const N: usize>(
225226
+ Copy
226227
+ 'static,
227228
{
228-
let dst_stride = image_size.width * N;
229+
let dst_stride = image_size.width * n;
229230

230231
let length = kernel.len();
231232
let half_len = length / 2;
@@ -634,7 +635,7 @@ where
634635

635636
let dst = &mut destination[dy * full_width..(dy + 1) * full_width];
636637

637-
filter_symmetric_column::<T, I, N>(&brows, dst, image_size, column_kernel);
638+
filter_symmetric_column::<T, I>(&brows, dst, image_size, column_kernel, N);
638639
}
639640

640641
start_ky += 1;
@@ -801,7 +802,7 @@ where
801802

802803
let brows_slice = brows.as_slice();
803804

804-
filter_symmetric_column::<T, I, N>(brows_slice, dst, image_size, &scanned_column_kernel);
805+
filter_symmetric_column::<T, I>(brows_slice, dst, image_size, &scanned_column_kernel, N);
805806
}
806807

807808
Ok(())

0 commit comments

Comments
 (0)