Coverage Report

Created: 2025-12-11 07:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/image/src/imageops/fast_blur.rs
Line
Count
Source
1
use num_traits::Bounded;
2
3
use crate::imageops::filter_1d::{SafeAdd, SafeMul};
4
use crate::{ImageBuffer, Pixel, Primitive};
5
6
/// Approximation of Gaussian blur.
7
///
8
/// # Arguments
9
///
10
/// * `image_buffer` - source image.
11
/// * `sigma` - value controls image flattening level.
12
///
13
/// This method assumes alpha pre-multiplication for images that contain non-constant alpha.
14
///
15
/// This method typically assumes that the input is scene-linear light.
16
/// If it is not, color distortion may occur.
17
///
18
/// Source: Kovesi, P.:  Fast Almost-Gaussian Filtering The Australian Pattern
19
/// Recognition Society Conference: DICTA 2010. December 2010. Sydney.
20
#[must_use]
21
0
pub fn fast_blur<P: Pixel>(
22
0
    input_buffer: &ImageBuffer<P, Vec<P::Subpixel>>,
23
0
    sigma: f32,
24
0
) -> ImageBuffer<P, Vec<P::Subpixel>> {
25
0
    let (width, height) = input_buffer.dimensions();
26
27
0
    if width == 0 || height == 0 {
28
0
        return input_buffer.clone();
29
0
    }
30
31
0
    let num_passes = 3;
32
33
0
    let boxes = boxes_for_gauss(sigma, num_passes);
34
0
    if boxes.is_empty() {
35
0
        return input_buffer.clone();
36
0
    }
37
38
0
    let samples = input_buffer.as_flat_samples().samples;
39
40
0
    let destination_size = match (width as usize)
41
0
        .safe_mul(height as usize)
42
0
        .and_then(|x| x.safe_mul(P::CHANNEL_COUNT as usize))
Unexecuted instantiation: image::imageops::fast_blur::fast_blur::<image::color::Rgb<f32>>::{closure#0}
Unexecuted instantiation: image::imageops::fast_blur::fast_blur::<image::color::Rgb<u8>>::{closure#0}
Unexecuted instantiation: image::imageops::fast_blur::fast_blur::<image::color::Rgb<u16>>::{closure#0}
Unexecuted instantiation: image::imageops::fast_blur::fast_blur::<image::color::Luma<u8>>::{closure#0}
Unexecuted instantiation: image::imageops::fast_blur::fast_blur::<image::color::Luma<u16>>::{closure#0}
Unexecuted instantiation: image::imageops::fast_blur::fast_blur::<image::color::Rgba<f32>>::{closure#0}
Unexecuted instantiation: image::imageops::fast_blur::fast_blur::<image::color::Rgba<u8>>::{closure#0}
Unexecuted instantiation: image::imageops::fast_blur::fast_blur::<image::color::Rgba<u16>>::{closure#0}
Unexecuted instantiation: image::imageops::fast_blur::fast_blur::<image::color::LumaA<u8>>::{closure#0}
Unexecuted instantiation: image::imageops::fast_blur::fast_blur::<image::color::LumaA<u16>>::{closure#0}
43
    {
44
0
        Ok(s) => s,
45
0
        Err(_) => panic!("Width and height and channels count exceeded pointer size"),
46
    };
47
48
0
    let first_box = boxes[0];
49
50
0
    let mut transient = vec![P::Subpixel::min_value(); destination_size];
51
0
    let mut dst = vec![P::Subpixel::min_value(); destination_size];
52
53
    // If destination_size isn't failed this one must not fail either
54
0
    let stride = width as usize * P::CHANNEL_COUNT as usize;
55
56
    // bound + radius + 1 must fit in a pointer size
57
0
    test_radius_size(width as usize, first_box);
58
0
    test_radius_size(height as usize, first_box);
59
60
0
    box_blur_horizontal_pass_strategy::<P, P::Subpixel>(
61
0
        samples,
62
0
        stride,
63
0
        &mut transient,
64
0
        stride,
65
0
        width,
66
0
        first_box,
67
    );
68
69
0
    box_blur_vertical_pass_strategy::<P, P::Subpixel>(
70
0
        &transient, stride, &mut dst, stride, width, height, first_box,
71
    );
72
73
0
    for &box_container in boxes.iter().skip(1) {
74
0
        // bound + radius + 1 must fit in a pointer size
75
0
        test_radius_size(width as usize, box_container);
76
0
        test_radius_size(height as usize, box_container);
77
0
78
0
        box_blur_horizontal_pass_strategy::<P, P::Subpixel>(
79
0
            &dst,
80
0
            stride,
81
0
            &mut transient,
82
0
            stride,
83
0
            width,
84
0
            box_container,
85
0
        );
86
0
87
0
        box_blur_vertical_pass_strategy::<P, P::Subpixel>(
88
0
            &transient,
89
0
            stride,
90
0
            &mut dst,
91
0
            stride,
92
0
            width,
93
0
            height,
94
0
            box_container,
95
0
        );
96
0
    }
97
98
0
    let mut buffer = ImageBuffer::from_raw(width, height, dst).unwrap();
99
0
    buffer.copy_color_space_from(input_buffer);
100
0
    buffer
101
0
}
Unexecuted instantiation: image::imageops::fast_blur::fast_blur::<image::color::Rgb<f32>>
Unexecuted instantiation: image::imageops::fast_blur::fast_blur::<image::color::Rgb<u8>>
Unexecuted instantiation: image::imageops::fast_blur::fast_blur::<image::color::Rgb<u16>>
Unexecuted instantiation: image::imageops::fast_blur::fast_blur::<image::color::Luma<u8>>
Unexecuted instantiation: image::imageops::fast_blur::fast_blur::<image::color::Luma<u16>>
Unexecuted instantiation: image::imageops::fast_blur::fast_blur::<image::color::Rgba<f32>>
Unexecuted instantiation: image::imageops::fast_blur::fast_blur::<image::color::Rgba<u8>>
Unexecuted instantiation: image::imageops::fast_blur::fast_blur::<image::color::Rgba<u16>>
Unexecuted instantiation: image::imageops::fast_blur::fast_blur::<image::color::LumaA<u8>>
Unexecuted instantiation: image::imageops::fast_blur::fast_blur::<image::color::LumaA<u16>>
102
103
#[inline]
104
0
fn test_radius_size(bound: usize, radius: usize) {
105
0
    match bound.safe_add(radius) {
106
0
        Ok(_) => {}
107
0
        Err(_) => panic!("Radius overflowed maximum possible size"),
108
    }
109
0
}
110
111
0
fn boxes_for_gauss(sigma: f32, n: usize) -> Vec<usize> {
112
0
    let w_ideal = f32::sqrt((12.0 * sigma.powi(2) / (n as f32)) + 1.0);
113
0
    let mut w_l = w_ideal.floor();
114
0
    if w_l % 2.0 == 0.0 {
115
0
        w_l -= 1.0;
116
0
    }
117
0
    let w_u = w_l + 2.0;
118
119
0
    let m_ideal = 0.25 * (n as f32) * (w_l + 3.0) - 3.0 * sigma.powi(2) * (w_l + 1.0).recip();
120
121
0
    let m = f32::round(m_ideal) as usize;
122
123
0
    (0..n)
124
0
        .map(|i| if i < m { w_l as usize } else { w_u as usize })
125
0
        .map(|i| ceil_to_odd(i.saturating_sub(1) / 2))
126
0
        .collect::<Vec<_>>()
127
0
}
128
129
#[inline]
130
0
fn ceil_to_odd(x: usize) -> usize {
131
0
    if x % 2 == 0 {
132
0
        x + 1
133
    } else {
134
0
        x
135
    }
136
0
}
137
138
#[inline]
139
#[allow(clippy::manual_clamp)]
140
0
fn rounding_saturating_mul<T: Primitive>(v: f32, w: f32) -> T {
141
    // T::DEFAULT_MAX_VALUE is equal to 1.0 only in cases where storage type if `f32/f64`,
142
    // that means it should be safe to round here.
143
0
    if T::DEFAULT_MAX_VALUE.to_f32().unwrap() != 1.0 {
144
0
        T::from(
145
0
            (v * w)
146
0
                .round()
147
0
                .min(T::DEFAULT_MAX_VALUE.to_f32().unwrap())
148
0
                .max(T::DEFAULT_MIN_VALUE.to_f32().unwrap()),
149
        )
150
0
        .unwrap()
151
    } else {
152
0
        T::from(
153
0
            (v * w)
154
0
                .min(T::DEFAULT_MAX_VALUE.to_f32().unwrap())
155
0
                .max(T::DEFAULT_MIN_VALUE.to_f32().unwrap()),
156
        )
157
0
        .unwrap()
158
    }
159
0
}
Unexecuted instantiation: image::imageops::fast_blur::rounding_saturating_mul::<f32>
Unexecuted instantiation: image::imageops::fast_blur::rounding_saturating_mul::<u8>
Unexecuted instantiation: image::imageops::fast_blur::rounding_saturating_mul::<u16>
160
161
0
fn box_blur_horizontal_pass_strategy<T, P: Primitive>(
162
0
    src: &[P],
163
0
    src_stride: usize,
164
0
    dst: &mut [P],
165
0
    dst_stride: usize,
166
0
    width: u32,
167
0
    radius: usize,
168
0
) where
169
0
    T: Pixel,
170
{
171
0
    if T::CHANNEL_COUNT == 1 {
172
0
        box_blur_horizontal_pass_impl::<P, 1>(src, src_stride, dst, dst_stride, width, radius);
173
0
    } else if T::CHANNEL_COUNT == 2 {
174
0
        box_blur_horizontal_pass_impl::<P, 2>(src, src_stride, dst, dst_stride, width, radius);
175
0
    } else if T::CHANNEL_COUNT == 3 {
176
0
        box_blur_horizontal_pass_impl::<P, 3>(src, src_stride, dst, dst_stride, width, radius);
177
0
    } else if T::CHANNEL_COUNT == 4 {
178
0
        box_blur_horizontal_pass_impl::<P, 4>(src, src_stride, dst, dst_stride, width, radius);
179
0
    } else {
180
0
        unimplemented!("More than 4 channels is not yet implemented");
181
    }
182
0
}
Unexecuted instantiation: image::imageops::fast_blur::box_blur_horizontal_pass_strategy::<image::color::Rgb<f32>, f32>
Unexecuted instantiation: image::imageops::fast_blur::box_blur_horizontal_pass_strategy::<image::color::Rgb<u8>, u8>
Unexecuted instantiation: image::imageops::fast_blur::box_blur_horizontal_pass_strategy::<image::color::Rgb<u16>, u16>
Unexecuted instantiation: image::imageops::fast_blur::box_blur_horizontal_pass_strategy::<image::color::Luma<u8>, u8>
Unexecuted instantiation: image::imageops::fast_blur::box_blur_horizontal_pass_strategy::<image::color::Luma<u16>, u16>
Unexecuted instantiation: image::imageops::fast_blur::box_blur_horizontal_pass_strategy::<image::color::Rgba<f32>, f32>
Unexecuted instantiation: image::imageops::fast_blur::box_blur_horizontal_pass_strategy::<image::color::Rgba<u8>, u8>
Unexecuted instantiation: image::imageops::fast_blur::box_blur_horizontal_pass_strategy::<image::color::Rgba<u16>, u16>
Unexecuted instantiation: image::imageops::fast_blur::box_blur_horizontal_pass_strategy::<image::color::LumaA<u8>, u8>
Unexecuted instantiation: image::imageops::fast_blur::box_blur_horizontal_pass_strategy::<image::color::LumaA<u16>, u16>
183
184
0
fn box_blur_vertical_pass_strategy<T: Pixel, P: Primitive>(
185
0
    src: &[P],
186
0
    src_stride: usize,
187
0
    dst: &mut [P],
188
0
    dst_stride: usize,
189
0
    width: u32,
190
0
    height: u32,
191
0
    radius: usize,
192
0
) {
193
0
    if T::CHANNEL_COUNT == 1 {
194
0
        box_blur_vertical_pass_impl::<P, 1>(
195
0
            src, src_stride, dst, dst_stride, width, height, radius,
196
0
        );
197
0
    } else if T::CHANNEL_COUNT == 2 {
198
0
        box_blur_vertical_pass_impl::<P, 2>(
199
0
            src, src_stride, dst, dst_stride, width, height, radius,
200
0
        );
201
0
    } else if T::CHANNEL_COUNT == 3 {
202
0
        box_blur_vertical_pass_impl::<P, 3>(
203
0
            src, src_stride, dst, dst_stride, width, height, radius,
204
0
        );
205
0
    } else if T::CHANNEL_COUNT == 4 {
206
0
        box_blur_vertical_pass_impl::<P, 4>(
207
0
            src, src_stride, dst, dst_stride, width, height, radius,
208
0
        );
209
0
    } else {
210
0
        unimplemented!("More than 4 channels is not yet implemented");
211
    }
212
0
}
Unexecuted instantiation: image::imageops::fast_blur::box_blur_vertical_pass_strategy::<image::color::Rgb<f32>, f32>
Unexecuted instantiation: image::imageops::fast_blur::box_blur_vertical_pass_strategy::<image::color::Rgb<u8>, u8>
Unexecuted instantiation: image::imageops::fast_blur::box_blur_vertical_pass_strategy::<image::color::Rgb<u16>, u16>
Unexecuted instantiation: image::imageops::fast_blur::box_blur_vertical_pass_strategy::<image::color::Luma<u8>, u8>
Unexecuted instantiation: image::imageops::fast_blur::box_blur_vertical_pass_strategy::<image::color::Luma<u16>, u16>
Unexecuted instantiation: image::imageops::fast_blur::box_blur_vertical_pass_strategy::<image::color::Rgba<f32>, f32>
Unexecuted instantiation: image::imageops::fast_blur::box_blur_vertical_pass_strategy::<image::color::Rgba<u8>, u8>
Unexecuted instantiation: image::imageops::fast_blur::box_blur_vertical_pass_strategy::<image::color::Rgba<u16>, u16>
Unexecuted instantiation: image::imageops::fast_blur::box_blur_vertical_pass_strategy::<image::color::LumaA<u8>, u8>
Unexecuted instantiation: image::imageops::fast_blur::box_blur_vertical_pass_strategy::<image::color::LumaA<u16>, u16>
213
214
0
fn box_blur_horizontal_pass_impl<T, const CN: usize>(
215
0
    src: &[T],
216
0
    src_stride: usize,
217
0
    dst: &mut [T],
218
0
    dst_stride: usize,
219
0
    width: u32,
220
0
    radius: usize,
221
0
) where
222
0
    T: Primitive,
223
{
224
0
    assert!(width > 0, "Width must be sanitized before this method");
225
0
    test_radius_size(width as usize, radius);
226
227
0
    let kernel_size = radius * 2 + 1;
228
0
    let edge_count = ((kernel_size / 2) + 1) as f32;
229
0
    let half_kernel = kernel_size / 2;
230
231
0
    let weight = 1f32 / (radius * 2 + 1) as f32;
232
233
0
    let width_bound = width as usize - 1;
234
235
    // Horizontal blurring consists from 4 phases
236
    // 1 - Fill initial sliding window
237
    // 2 - Blur dangerous leading zone where clamping is required
238
    // 3 - Blur *normal* zone where clamping is not required
239
    // 4 - Blur dangerous trailing zone where clamping is required
240
241
0
    for (dst, src) in dst
242
0
        .chunks_exact_mut(dst_stride)
243
0
        .zip(src.chunks_exact(src_stride))
244
    {
245
0
        let mut weight1: f32 = 0.;
246
0
        let mut weight2: f32 = 0.;
247
0
        let mut weight3: f32 = 0.;
248
249
0
        let chunk0 = &src[..CN];
250
251
        // replicate edge
252
0
        let mut weight0 = chunk0[0].to_f32().unwrap() * edge_count;
253
0
        if CN > 1 {
254
0
            weight1 = chunk0[1].to_f32().unwrap() * edge_count;
255
0
        }
256
0
        if CN > 2 {
257
0
            weight2 = chunk0[2].to_f32().unwrap() * edge_count;
258
0
        }
259
0
        if CN == 4 {
260
0
            weight3 = chunk0[3].to_f32().unwrap() * edge_count;
261
0
        }
262
263
0
        for x in 1..=half_kernel {
264
0
            let px = x.min(width_bound) * CN;
265
0
            let chunk0 = &src[px..px + CN];
266
0
            weight0 += chunk0[0].to_f32().unwrap();
267
0
            if CN > 1 {
268
0
                weight1 += chunk0[1].to_f32().unwrap();
269
0
            }
270
0
            if CN > 2 {
271
0
                weight2 += chunk0[2].to_f32().unwrap();
272
0
            }
273
0
            if CN == 4 {
274
0
                weight3 += chunk0[3].to_f32().unwrap();
275
0
            }
276
        }
277
278
0
        for x in 0..half_kernel.min(width as usize) {
279
0
            let next = (x + half_kernel + 1).min(width_bound) * CN;
280
0
            let previous = (x as i64 - half_kernel as i64).max(0) as usize * CN;
281
282
0
            let dst_chunk = &mut dst[x * CN..x * CN + CN];
283
0
            dst_chunk[0] = rounding_saturating_mul(weight0, weight);
284
0
            if CN > 1 {
285
0
                dst_chunk[1] = rounding_saturating_mul(weight1, weight);
286
0
            }
287
0
            if CN > 2 {
288
0
                dst_chunk[2] = rounding_saturating_mul(weight2, weight);
289
0
            }
290
0
            if CN == 4 {
291
0
                dst_chunk[3] = rounding_saturating_mul(weight3, weight);
292
0
            }
293
294
0
            let next_chunk = &src[next..next + CN];
295
0
            let previous_chunk = &src[previous..previous + CN];
296
297
0
            weight0 += next_chunk[0].to_f32().unwrap();
298
0
            if CN > 1 {
299
0
                weight1 += next_chunk[1].to_f32().unwrap();
300
0
            }
301
0
            if CN > 2 {
302
0
                weight2 += next_chunk[2].to_f32().unwrap();
303
0
            }
304
0
            if CN == 4 {
305
0
                weight3 += next_chunk[3].to_f32().unwrap();
306
0
            }
307
308
0
            weight0 -= previous_chunk[0].to_f32().unwrap();
309
0
            if CN > 1 {
310
0
                weight1 -= previous_chunk[1].to_f32().unwrap();
311
0
            }
312
0
            if CN > 2 {
313
0
                weight2 -= previous_chunk[2].to_f32().unwrap();
314
0
            }
315
0
            if CN == 4 {
316
0
                weight3 -= previous_chunk[3].to_f32().unwrap();
317
0
            }
318
        }
319
320
0
        let max_x_before_clamping = width_bound.saturating_sub(half_kernel + 1);
321
0
        let row_length = src.len();
322
323
0
        let mut last_processed_item = half_kernel;
324
325
0
        if ((half_kernel * 2 + 1) * CN < row_length) && ((max_x_before_clamping * CN) < row_length)
326
        {
327
0
            let data_section = src;
328
0
            let advanced_kernel_part = &data_section[(half_kernel * 2 + 1) * CN..];
329
0
            let section_length = max_x_before_clamping - half_kernel;
330
0
            let dst = &mut dst[half_kernel * CN..(half_kernel * CN + section_length * CN)];
331
332
0
            for ((dst, src_previous), src_next) in dst
333
0
                .chunks_exact_mut(CN)
334
0
                .zip(data_section.chunks_exact(CN))
335
0
                .zip(advanced_kernel_part.chunks_exact(CN))
336
            {
337
0
                let dst_chunk = &mut dst[..CN];
338
0
                dst_chunk[0] = rounding_saturating_mul(weight0, weight);
339
0
                if CN > 1 {
340
0
                    dst_chunk[1] = rounding_saturating_mul(weight1, weight);
341
0
                }
342
0
                if CN > 2 {
343
0
                    dst_chunk[2] = rounding_saturating_mul(weight2, weight);
344
0
                }
345
0
                if CN == 4 {
346
0
                    dst_chunk[3] = rounding_saturating_mul(weight3, weight);
347
0
                }
348
349
0
                weight0 += src_next[0].to_f32().unwrap();
350
0
                if CN > 1 {
351
0
                    weight1 += src_next[1].to_f32().unwrap();
352
0
                }
353
0
                if CN > 2 {
354
0
                    weight2 += src_next[2].to_f32().unwrap();
355
0
                }
356
0
                if CN == 4 {
357
0
                    weight3 += src_next[3].to_f32().unwrap();
358
0
                }
359
360
0
                weight0 -= src_previous[0].to_f32().unwrap();
361
0
                if CN > 1 {
362
0
                    weight1 -= src_previous[1].to_f32().unwrap();
363
0
                }
364
0
                if CN > 2 {
365
0
                    weight2 -= src_previous[2].to_f32().unwrap();
366
0
                }
367
0
                if CN == 4 {
368
0
                    weight3 -= src_previous[3].to_f32().unwrap();
369
0
                }
370
            }
371
372
0
            last_processed_item = max_x_before_clamping;
373
0
        }
374
375
0
        for x in last_processed_item..width as usize {
376
0
            let next = (x + half_kernel + 1).min(width_bound) * CN;
377
0
            let previous = (x as i64 - half_kernel as i64).max(0) as usize * CN;
378
0
            let dst_chunk = &mut dst[x * CN..x * CN + CN];
379
0
            dst_chunk[0] = rounding_saturating_mul(weight0, weight);
380
0
            if CN > 1 {
381
0
                dst_chunk[1] = rounding_saturating_mul(weight1, weight);
382
0
            }
383
0
            if CN > 2 {
384
0
                dst_chunk[2] = rounding_saturating_mul(weight2, weight);
385
0
            }
386
0
            if CN == 4 {
387
0
                dst_chunk[3] = rounding_saturating_mul(weight3, weight);
388
0
            }
389
390
0
            let next_chunk = &src[next..next + CN];
391
0
            let previous_chunk = &src[previous..previous + CN];
392
393
0
            weight0 += next_chunk[0].to_f32().unwrap();
394
0
            if CN > 1 {
395
0
                weight1 += next_chunk[1].to_f32().unwrap();
396
0
            }
397
0
            if CN > 2 {
398
0
                weight2 += next_chunk[2].to_f32().unwrap();
399
0
            }
400
0
            if CN == 4 {
401
0
                weight3 += next_chunk[3].to_f32().unwrap();
402
0
            }
403
404
0
            weight0 -= previous_chunk[0].to_f32().unwrap();
405
0
            if CN > 1 {
406
0
                weight1 -= previous_chunk[1].to_f32().unwrap();
407
0
            }
408
0
            if CN > 2 {
409
0
                weight2 -= previous_chunk[2].to_f32().unwrap();
410
0
            }
411
0
            if CN == 4 {
412
0
                weight3 -= previous_chunk[3].to_f32().unwrap();
413
0
            }
414
        }
415
    }
416
0
}
Unexecuted instantiation: image::imageops::fast_blur::box_blur_horizontal_pass_impl::<f32, 1>
Unexecuted instantiation: image::imageops::fast_blur::box_blur_horizontal_pass_impl::<f32, 2>
Unexecuted instantiation: image::imageops::fast_blur::box_blur_horizontal_pass_impl::<f32, 3>
Unexecuted instantiation: image::imageops::fast_blur::box_blur_horizontal_pass_impl::<f32, 4>
Unexecuted instantiation: image::imageops::fast_blur::box_blur_horizontal_pass_impl::<u8, 1>
Unexecuted instantiation: image::imageops::fast_blur::box_blur_horizontal_pass_impl::<u8, 2>
Unexecuted instantiation: image::imageops::fast_blur::box_blur_horizontal_pass_impl::<u8, 3>
Unexecuted instantiation: image::imageops::fast_blur::box_blur_horizontal_pass_impl::<u8, 4>
Unexecuted instantiation: image::imageops::fast_blur::box_blur_horizontal_pass_impl::<u16, 1>
Unexecuted instantiation: image::imageops::fast_blur::box_blur_horizontal_pass_impl::<u16, 2>
Unexecuted instantiation: image::imageops::fast_blur::box_blur_horizontal_pass_impl::<u16, 3>
Unexecuted instantiation: image::imageops::fast_blur::box_blur_horizontal_pass_impl::<u16, 4>
417
418
0
fn box_blur_vertical_pass_impl<T: Primitive, const CN: usize>(
419
0
    src: &[T],
420
0
    src_stride: usize,
421
0
    dst: &mut [T],
422
0
    dst_stride: usize,
423
0
    width: u32,
424
0
    height: u32,
425
0
    radius: usize,
426
0
) {
427
0
    assert!(width > 0, "Width must be sanitized before this method");
428
0
    assert!(height > 0, "Height must be sanitized before this method");
429
0
    test_radius_size(width as usize, radius);
430
431
0
    let kernel_size = radius * 2 + 1;
432
433
0
    let edge_count = ((kernel_size / 2) + 1) as f32;
434
0
    let half_kernel = kernel_size / 2;
435
436
0
    let weight = 1f32 / (radius * 2 + 1) as f32;
437
438
0
    let buf_size = width as usize * CN;
439
440
0
    let buf_cap = buf_size;
441
442
0
    let height_bound = height as usize - 1;
443
444
    // Instead of summing each column separately we use here transient buffer that
445
    // averages columns in row manner.
446
    // So, we make the initial buffer at the top edge
447
    // and then doing blur by averaging the whole row ( which is in buffer )
448
    // and subtracting and adding next and previous rows in horizontal manner.
449
450
0
    let mut buffer = vec![0f32; buf_cap];
451
452
0
    for (x, (v, bf)) in src.iter().zip(buffer.iter_mut()).enumerate() {
453
0
        let mut w = v.to_f32().unwrap() * edge_count;
454
0
        for y in 1..=half_kernel {
455
0
            let y_src_shift = y.min(height_bound) * src_stride;
456
0
            w += src[y_src_shift + x].to_f32().unwrap();
457
0
        }
458
0
        *bf = w;
459
    }
460
461
0
    for (dst, y) in dst.chunks_exact_mut(dst_stride).zip(0..height as usize) {
462
0
        let next = (y + half_kernel + 1).min(height_bound) * src_stride;
463
0
        let previous = (y as i64 - half_kernel as i64).max(0) as usize * src_stride;
464
465
0
        let next_row = &src[next..next + width as usize * CN];
466
0
        let previous_row = &src[previous..previous + width as usize * CN];
467
468
0
        for (((src_next, src_previous), buffer), dst) in next_row
469
0
            .iter()
470
0
            .zip(previous_row.iter())
471
0
            .zip(buffer.iter_mut())
472
0
            .zip(dst.iter_mut())
473
0
        {
474
0
            let mut weight0 = *buffer;
475
0
476
0
            *dst = rounding_saturating_mul(weight0, weight);
477
0
478
0
            weight0 += src_next.to_f32().unwrap();
479
0
            weight0 -= src_previous.to_f32().unwrap();
480
0
481
0
            *buffer = weight0;
482
0
        }
483
    }
484
0
}
Unexecuted instantiation: image::imageops::fast_blur::box_blur_vertical_pass_impl::<f32, 1>
Unexecuted instantiation: image::imageops::fast_blur::box_blur_vertical_pass_impl::<f32, 2>
Unexecuted instantiation: image::imageops::fast_blur::box_blur_vertical_pass_impl::<f32, 3>
Unexecuted instantiation: image::imageops::fast_blur::box_blur_vertical_pass_impl::<f32, 4>
Unexecuted instantiation: image::imageops::fast_blur::box_blur_vertical_pass_impl::<u8, 1>
Unexecuted instantiation: image::imageops::fast_blur::box_blur_vertical_pass_impl::<u8, 2>
Unexecuted instantiation: image::imageops::fast_blur::box_blur_vertical_pass_impl::<u8, 3>
Unexecuted instantiation: image::imageops::fast_blur::box_blur_vertical_pass_impl::<u8, 4>
Unexecuted instantiation: image::imageops::fast_blur::box_blur_vertical_pass_impl::<u16, 1>
Unexecuted instantiation: image::imageops::fast_blur::box_blur_vertical_pass_impl::<u16, 2>
Unexecuted instantiation: image::imageops::fast_blur::box_blur_vertical_pass_impl::<u16, 3>
Unexecuted instantiation: image::imageops::fast_blur::box_blur_vertical_pass_impl::<u16, 4>
485
486
#[cfg(test)]
487
mod tests {
488
    use crate::{DynamicImage, GrayAlphaImage, GrayImage, RgbImage, RgbaImage};
489
    use std::time::{SystemTime, UNIX_EPOCH};
490
491
    struct Rng {
492
        state: u64,
493
    }
494
495
    impl Rng {
496
        fn new(seed: u64) -> Self {
497
            Self { state: seed }
498
        }
499
        fn next_u32(&mut self) -> u32 {
500
            self.state = self.state.wrapping_mul(6364136223846793005).wrapping_add(1);
501
            (self.state >> 32) as u32
502
        }
503
504
        fn next_u8(&mut self) -> u8 {
505
            (self.next_u32() % 256) as u8
506
        }
507
508
        fn next_f32_in_range(&mut self, a: f32, b: f32) -> f32 {
509
            let u = self.next_u32();
510
            let unit = (u as f32) / (u32::MAX as f32 + 1.0);
511
            a + (b - a) * unit
512
        }
513
    }
514
515
    #[test]
516
    fn test_box_blur() {
517
        let now = SystemTime::now().duration_since(UNIX_EPOCH).unwrap();
518
        let mut rng = Rng::new((now.as_millis() & 0xffff_ffff_ffff_ffff) as u64);
519
        for _ in 0..35 {
520
            let width = rng.next_u8();
521
            let height = rng.next_u8();
522
            let sigma = rng.next_f32_in_range(0., 100.);
523
            let px = rng.next_u8();
524
            let cn = rng.next_u8();
525
            if width == 0 || height == 0 || sigma <= 0. {
526
                continue;
527
            }
528
            match cn % 4 {
529
                0 => {
530
                    let vc = vec![px; width as usize * height as usize];
531
                    let image = DynamicImage::from(
532
                        GrayImage::from_vec(u32::from(width), u32::from(height), vc).unwrap(),
533
                    );
534
                    let res = image.fast_blur(sigma);
535
                    for clr in res.as_bytes() {
536
                        assert_eq!(*clr, px);
537
                    }
538
                }
539
                1 => {
540
                    let vc = vec![px; width as usize * height as usize * 2];
541
                    let image = DynamicImage::from(
542
                        GrayAlphaImage::from_vec(u32::from(width), u32::from(height), vc).unwrap(),
543
                    );
544
                    let res = image.fast_blur(sigma);
545
                    for clr in res.as_bytes() {
546
                        assert_eq!(*clr, px);
547
                    }
548
                }
549
                2 => {
550
                    let vc = vec![px; width as usize * height as usize * 3];
551
                    let image = DynamicImage::from(
552
                        RgbImage::from_vec(u32::from(width), u32::from(height), vc).unwrap(),
553
                    );
554
                    let res = image.fast_blur(sigma);
555
                    for clr in res.as_bytes() {
556
                        assert_eq!(*clr, px);
557
                    }
558
                }
559
                3 => {
560
                    let vc = vec![px; width as usize * height as usize * 4];
561
                    let image = DynamicImage::from(
562
                        RgbaImage::from_vec(u32::from(width), u32::from(height), vc).unwrap(),
563
                    );
564
                    let res = image.fast_blur(sigma);
565
                    for clr in res.as_bytes() {
566
                        assert_eq!(*clr, px);
567
                    }
568
                }
569
                _ => {}
570
            }
571
        }
572
    }
573
}