image/imageops/
sample.rs

1//! Functions and filters for the sampling of pixels.
2
3// See http://cs.brown.edu/courses/cs123/lectures/08_Image_Processing_IV.pdf
4// for some of the theory behind image scaling and convolution
5
6use num_traits::{NumCast, ToPrimitive, Zero};
7use std::f32;
8use std::ops::Mul;
9
10use crate::imageops::filter_1d::{
11    filter_2d_sep_la, filter_2d_sep_la_f32, filter_2d_sep_la_u16, filter_2d_sep_plane,
12    filter_2d_sep_plane_f32, filter_2d_sep_plane_u16, filter_2d_sep_rgb, filter_2d_sep_rgb_f32,
13    filter_2d_sep_rgb_u16, filter_2d_sep_rgba, filter_2d_sep_rgba_f32, filter_2d_sep_rgba_u16,
14    FilterImageSize,
15};
16use crate::images::buffer::{Gray16Image, GrayAlpha16Image, Rgb16Image, Rgba16Image};
17use crate::traits::{Enlargeable, Pixel, Primitive};
18use crate::utils::clamp;
19use crate::{
20    DynamicImage, GenericImage, GenericImageView, GrayAlphaImage, GrayImage, ImageBuffer,
21    Rgb32FImage, RgbImage, Rgba32FImage, RgbaImage,
22};
23
24/// Available Sampling Filters.
25///
26/// ## Examples
27///
28/// To test the different sampling filters on a real example, you can find two
29/// examples called
30/// [`scaledown`](https://github.com/image-rs/image/tree/main/examples/scaledown)
31/// and
32/// [`scaleup`](https://github.com/image-rs/image/tree/main/examples/scaleup)
33/// in the `examples` directory of the crate source code.
34///
35/// Here is a 3.58 MiB
36/// [test image](https://github.com/image-rs/image/blob/main/examples/scaledown/test.jpg)
37/// that has been scaled down to 300x225 px:
38///
39/// <!-- NOTE: To test new test images locally, replace the GitHub path with `../../../docs/` -->
40/// <div style="display: flex; flex-wrap: wrap; align-items: flex-start;">
41///   <div style="margin: 0 8px 8px 0;">
42///     <img src="https://raw.githubusercontent.com/image-rs/image/main/examples/scaledown/scaledown-test-near.png" title="Nearest"><br>
43///     Nearest Neighbor
44///   </div>
45///   <div style="margin: 0 8px 8px 0;">
46///     <img src="https://raw.githubusercontent.com/image-rs/image/main/examples/scaledown/scaledown-test-tri.png" title="Triangle"><br>
47///     Linear: Triangle
48///   </div>
49///   <div style="margin: 0 8px 8px 0;">
50///     <img src="https://raw.githubusercontent.com/image-rs/image/main/examples/scaledown/scaledown-test-cmr.png" title="CatmullRom"><br>
51///     Cubic: Catmull-Rom
52///   </div>
53///   <div style="margin: 0 8px 8px 0;">
54///     <img src="https://raw.githubusercontent.com/image-rs/image/main/examples/scaledown/scaledown-test-gauss.png" title="Gaussian"><br>
55///     Gaussian
56///   </div>
57///   <div style="margin: 0 8px 8px 0;">
58///     <img src="https://raw.githubusercontent.com/image-rs/image/main/examples/scaledown/scaledown-test-lcz2.png" title="Lanczos3"><br>
59///     Lanczos with window 3
60///   </div>
61/// </div>
62///
63/// ## Speed
64///
65/// Time required to create each of the examples above, tested on an Intel
66/// i7-4770 CPU with Rust 1.37 in release mode:
67///
68/// <table style="width: auto;">
69///   <tr>
70///     <th>Nearest</th>
71///     <td>31 ms</td>
72///   </tr>
73///   <tr>
74///     <th>Triangle</th>
75///     <td>414 ms</td>
76///   </tr>
77///   <tr>
78///     <th>CatmullRom</th>
79///     <td>817 ms</td>
80///   </tr>
81///   <tr>
82///     <th>Gaussian</th>
83///     <td>1180 ms</td>
84///   </tr>
85///   <tr>
86///     <th>Lanczos3</th>
87///     <td>1170 ms</td>
88///   </tr>
89/// </table>
90#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
91#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
92pub enum FilterType {
93    /// Nearest Neighbor
94    Nearest,
95
96    /// Linear Filter
97    Triangle,
98
99    /// Cubic Filter
100    CatmullRom,
101
102    /// Gaussian Filter
103    Gaussian,
104
105    /// Lanczos with window 3
106    Lanczos3,
107}
108
109/// A Representation of a separable filter.
110pub(crate) struct Filter<'a> {
111    /// The filter's filter function.
112    pub(crate) kernel: Box<dyn Fn(f32) -> f32 + 'a>,
113
114    /// The window on which this filter operates.
115    pub(crate) support: f32,
116}
117
118struct FloatNearest(f32);
119
120// to_i64, to_u64, and to_f64 implicitly affect all other lower conversions.
121// Note that to_f64 by default calls to_i64 and thus needs to be overridden.
122impl ToPrimitive for FloatNearest {
123    // to_{i,u}64 is required, to_{i,u}{8,16} are useful.
124    // If a usecase for full 32 bits is found its trivial to add
125    fn to_i8(&self) -> Option<i8> {
126        self.0.round().to_i8()
127    }
128    fn to_i16(&self) -> Option<i16> {
129        self.0.round().to_i16()
130    }
131    fn to_i64(&self) -> Option<i64> {
132        self.0.round().to_i64()
133    }
134    fn to_u8(&self) -> Option<u8> {
135        self.0.round().to_u8()
136    }
137    fn to_u16(&self) -> Option<u16> {
138        self.0.round().to_u16()
139    }
140    fn to_u64(&self) -> Option<u64> {
141        self.0.round().to_u64()
142    }
143    fn to_f64(&self) -> Option<f64> {
144        self.0.to_f64()
145    }
146}
147
148// sinc function: the ideal sampling filter.
149fn sinc(t: f32) -> f32 {
150    let a = t * f32::consts::PI;
151
152    if t == 0.0 {
153        1.0
154    } else {
155        a.sin() / a
156    }
157}
158
159// lanczos kernel function. A windowed sinc function.
160fn lanczos(x: f32, t: f32) -> f32 {
161    if x.abs() < t {
162        sinc(x) * sinc(x / t)
163    } else {
164        0.0
165    }
166}
167
168// Calculate a splice based on the b and c parameters.
169// from authors Mitchell and Netravali.
170fn bc_cubic_spline(x: f32, b: f32, c: f32) -> f32 {
171    let a = x.abs();
172
173    let k = if a < 1.0 {
174        (12.0 - 9.0 * b - 6.0 * c) * a.powi(3)
175            + (-18.0 + 12.0 * b + 6.0 * c) * a.powi(2)
176            + (6.0 - 2.0 * b)
177    } else if a < 2.0 {
178        (-b - 6.0 * c) * a.powi(3)
179            + (6.0 * b + 30.0 * c) * a.powi(2)
180            + (-12.0 * b - 48.0 * c) * a
181            + (8.0 * b + 24.0 * c)
182    } else {
183        0.0
184    };
185
186    k / 6.0
187}
188
189/// The Gaussian Function.
190/// ```r``` is the standard deviation.
191pub(crate) fn gaussian(x: f32, r: f32) -> f32 {
192    ((2.0 * f32::consts::PI).sqrt() * r).recip() * (-x.powi(2) / (2.0 * r.powi(2))).exp()
193}
194
195/// Calculate the lanczos kernel with a window of 3
196pub(crate) fn lanczos3_kernel(x: f32) -> f32 {
197    lanczos(x, 3.0)
198}
199
200/// Calculate the gaussian function with a
201/// standard deviation of 0.5
202pub(crate) fn gaussian_kernel(x: f32) -> f32 {
203    gaussian(x, 0.5)
204}
205
206/// Calculate the Catmull-Rom cubic spline.
207/// Also known as a form of `BiCubic` sampling in two dimensions.
208pub(crate) fn catmullrom_kernel(x: f32) -> f32 {
209    bc_cubic_spline(x, 0.0, 0.5)
210}
211
212/// Calculate the triangle function.
213/// Also known as `BiLinear` sampling in two dimensions.
214pub(crate) fn triangle_kernel(x: f32) -> f32 {
215    if x.abs() < 1.0 {
216        1.0 - x.abs()
217    } else {
218        0.0
219    }
220}
221
222/// Calculate the box kernel.
223/// Only pixels inside the box should be considered, and those
224/// contribute equally.  So this method simply returns 1.
225pub(crate) fn box_kernel(_x: f32) -> f32 {
226    1.0
227}
228
229// Sample the rows of the supplied image using the provided filter.
230// The height of the image remains unchanged.
231// ```new_width``` is the desired width of the new image
232// ```filter``` is the filter to use for sampling.
233// ```image``` is not necessarily Rgba and the order of channels is passed through.
234//
235// Note: if an empty image is passed in, panics unless the image is truly empty.
236fn horizontal_sample<P, S>(
237    image: &Rgba32FImage,
238    new_width: u32,
239    filter: &mut Filter,
240) -> ImageBuffer<P, Vec<S>>
241where
242    P: Pixel<Subpixel = S> + 'static,
243    S: Primitive + 'static,
244{
245    let (width, height) = image.dimensions();
246    // This is protection against a memory usage similar to #2340. See `vertical_sample`.
247    assert!(
248        // Checks the implication: (width == 0) -> (height == 0)
249        width != 0 || height == 0,
250        "Unexpected prior allocation size. This case should have been handled by the caller"
251    );
252
253    let mut out = ImageBuffer::new(new_width, height);
254    out.copy_color_space_from(image);
255    let mut ws = Vec::new();
256
257    let max: f32 = NumCast::from(S::DEFAULT_MAX_VALUE).unwrap();
258    let min: f32 = NumCast::from(S::DEFAULT_MIN_VALUE).unwrap();
259    let ratio = width as f32 / new_width as f32;
260    let sratio = if ratio < 1.0 { 1.0 } else { ratio };
261    let src_support = filter.support * sratio;
262
263    for outx in 0..new_width {
264        // Find the point in the input image corresponding to the centre
265        // of the current pixel in the output image.
266        let inputx = (outx as f32 + 0.5) * ratio;
267
268        // Left and right are slice bounds for the input pixels relevant
269        // to the output pixel we are calculating.  Pixel x is relevant
270        // if and only if (x >= left) && (x < right).
271
272        // Invariant: 0 <= left < right <= width
273
274        let left = (inputx - src_support).floor() as i64;
275        let left = clamp(left, 0, <i64 as From<_>>::from(width) - 1) as u32;
276
277        let right = (inputx + src_support).ceil() as i64;
278        let right = clamp(
279            right,
280            <i64 as From<_>>::from(left) + 1,
281            <i64 as From<_>>::from(width),
282        ) as u32;
283
284        // Go back to left boundary of pixel, to properly compare with i
285        // below, as the kernel treats the centre of a pixel as 0.
286        let inputx = inputx - 0.5;
287
288        ws.clear();
289        let mut sum = 0.0;
290        for i in left..right {
291            let w = (filter.kernel)((i as f32 - inputx) / sratio);
292            ws.push(w);
293            sum += w;
294        }
295        for w in ws.iter_mut() {
296            *w /= sum;
297        }
298
299        for y in 0..height {
300            let mut t = (0.0, 0.0, 0.0, 0.0);
301
302            for (i, w) in ws.iter().enumerate() {
303                let p = image.get_pixel(left + i as u32, y);
304
305                #[allow(deprecated)]
306                let vec = p.channels4();
307
308                t.0 += vec.0 * w;
309                t.1 += vec.1 * w;
310                t.2 += vec.2 * w;
311                t.3 += vec.3 * w;
312            }
313
314            #[allow(deprecated)]
315            let t = Pixel::from_channels(
316                NumCast::from(FloatNearest(clamp(t.0, min, max))).unwrap(),
317                NumCast::from(FloatNearest(clamp(t.1, min, max))).unwrap(),
318                NumCast::from(FloatNearest(clamp(t.2, min, max))).unwrap(),
319                NumCast::from(FloatNearest(clamp(t.3, min, max))).unwrap(),
320            );
321
322            out.put_pixel(outx, y, t);
323        }
324    }
325
326    out
327}
328
329/// Linearly sample from an image using coordinates in [0, 1].
330pub fn sample_bilinear<P: Pixel>(
331    img: &impl GenericImageView<Pixel = P>,
332    u: f32,
333    v: f32,
334) -> Option<P> {
335    if ![u, v].iter().all(|c| (0.0..=1.0).contains(c)) {
336        return None;
337    }
338
339    let (w, h) = img.dimensions();
340    if w == 0 || h == 0 {
341        return None;
342    }
343
344    let ui = w as f32 * u - 0.5;
345    let vi = h as f32 * v - 0.5;
346    interpolate_bilinear(
347        img,
348        ui.max(0.).min((w - 1) as f32),
349        vi.max(0.).min((h - 1) as f32),
350    )
351}
352
353/// Sample from an image using coordinates in [0, 1], taking the nearest coordinate.
354pub fn sample_nearest<P: Pixel>(
355    img: &impl GenericImageView<Pixel = P>,
356    u: f32,
357    v: f32,
358) -> Option<P> {
359    if ![u, v].iter().all(|c| (0.0..=1.0).contains(c)) {
360        return None;
361    }
362
363    let (w, h) = img.dimensions();
364    let ui = w as f32 * u - 0.5;
365    let ui = ui.max(0.).min((w.saturating_sub(1)) as f32);
366
367    let vi = h as f32 * v - 0.5;
368    let vi = vi.max(0.).min((h.saturating_sub(1)) as f32);
369    interpolate_nearest(img, ui, vi)
370}
371
372/// Sample from an image using coordinates in [0, w-1] and [0, h-1], taking the
373/// nearest pixel.
374///
375/// Coordinates outside the image bounds will return `None`, however the
376/// behavior for points within half a pixel of the image bounds may change in
377/// the future.
378pub fn interpolate_nearest<P: Pixel>(
379    img: &impl GenericImageView<Pixel = P>,
380    x: f32,
381    y: f32,
382) -> Option<P> {
383    let (w, h) = img.dimensions();
384    if w == 0 || h == 0 {
385        return None;
386    }
387    if !(0.0..=((w - 1) as f32)).contains(&x) {
388        return None;
389    }
390    if !(0.0..=((h - 1) as f32)).contains(&y) {
391        return None;
392    }
393
394    Some(img.get_pixel(x.round() as u32, y.round() as u32))
395}
396
397/// Linearly sample from an image using coordinates in [0, w-1] and [0, h-1].
398pub fn interpolate_bilinear<P: Pixel>(
399    img: &impl GenericImageView<Pixel = P>,
400    x: f32,
401    y: f32,
402) -> Option<P> {
403    // assumption needed for correctness of pixel creation
404    assert!(P::CHANNEL_COUNT <= 4);
405
406    let (w, h) = img.dimensions();
407    if w == 0 || h == 0 {
408        return None;
409    }
410    if !(0.0..=((w - 1) as f32)).contains(&x) {
411        return None;
412    }
413    if !(0.0..=((h - 1) as f32)).contains(&y) {
414        return None;
415    }
416
417    // keep these as integers, for fewer FLOPs
418    let uf = x.floor() as u32;
419    let vf = y.floor() as u32;
420    let uc = (uf + 1).min(w - 1);
421    let vc = (vf + 1).min(h - 1);
422
423    // clamp coords to the range of the image
424    let mut sxx = [[0.; 4]; 4];
425
426    // do not use Array::map, as it can be slow with high stack usage,
427    // for [[f32; 4]; 4].
428
429    // convert samples to f32
430    // currently rgba is the largest one,
431    // so just store as many items as necessary,
432    // because there's not a simple way to be generic over all of them.
433    let mut compute = |u: u32, v: u32, i| {
434        let s = img.get_pixel(u, v);
435        for (j, c) in s.channels().iter().enumerate() {
436            sxx[j][i] = c.to_f32().unwrap();
437        }
438        s
439    };
440
441    // hacky reuse since cannot construct a generic Pixel
442    let mut out: P = compute(uf, vf, 0);
443    compute(uf, vc, 1);
444    compute(uc, vf, 2);
445    compute(uc, vc, 3);
446
447    // weights, the later two are independent from the first 2 for better vectorization.
448    let ufw = x - uf as f32;
449    let vfw = y - vf as f32;
450    let ucw = (uf + 1) as f32 - x;
451    let vcw = (vf + 1) as f32 - y;
452
453    // https://en.wikipedia.org/wiki/Bilinear_interpolation#Weighted_mean
454    // the distance between pixels is 1 so there is no denominator
455    let wff = ucw * vcw;
456    let wfc = ucw * vfw;
457    let wcf = ufw * vcw;
458    let wcc = ufw * vfw;
459    // was originally assert, but is actually not a cheap computation
460    debug_assert!(f32::abs((wff + wfc + wcf + wcc) - 1.) < 1e-3);
461
462    // hack to see if primitive is an integer or a float
463    let is_float = P::Subpixel::DEFAULT_MAX_VALUE.to_f32().unwrap() == 1.0;
464
465    for (i, c) in out.channels_mut().iter_mut().enumerate() {
466        let v = wff * sxx[i][0] + wfc * sxx[i][1] + wcf * sxx[i][2] + wcc * sxx[i][3];
467        // this rounding may introduce quantization errors,
468        // Specifically what is meant is that many samples may deviate
469        // from the mean value of the originals, but it's not possible to fix that.
470        *c = <P::Subpixel as NumCast>::from(if is_float { v } else { v.round() }).unwrap_or({
471            if v < 0.0 {
472                P::Subpixel::DEFAULT_MIN_VALUE
473            } else {
474                P::Subpixel::DEFAULT_MAX_VALUE
475            }
476        });
477    }
478
479    Some(out)
480}
481
482// Sample the columns of the supplied image using the provided filter.
483// The width of the image remains unchanged.
484// ```new_height``` is the desired height of the new image
485// ```filter``` is the filter to use for sampling.
486// The return value is not necessarily Rgba, the underlying order of channels in ```image``` is
487// preserved.
488//
489// Note: if an empty image is passed in, panics unless the image is truly empty.
490fn vertical_sample<I, P, S>(image: &I, new_height: u32, filter: &mut Filter) -> Rgba32FImage
491where
492    I: GenericImageView<Pixel = P>,
493    P: Pixel<Subpixel = S> + 'static,
494    S: Primitive + 'static,
495{
496    let (width, height) = image.dimensions();
497
498    // This is protection against a regression in memory usage such as #2340. Since the strategy to
499    // deal with it depends on the caller it is a precondition of this function.
500    assert!(
501        // Checks the implication: (height == 0) -> (width == 0)
502        height != 0 || width == 0,
503        "Unexpected prior allocation size. This case should have been handled by the caller"
504    );
505
506    let mut out = ImageBuffer::new(width, new_height);
507    out.copy_color_space_from(&image.buffer_with_dimensions(0, 0));
508    let mut ws = Vec::new();
509
510    let ratio = height as f32 / new_height as f32;
511    let sratio = if ratio < 1.0 { 1.0 } else { ratio };
512    let src_support = filter.support * sratio;
513
514    for outy in 0..new_height {
515        // For an explanation of this algorithm, see the comments
516        // in horizontal_sample.
517        let inputy = (outy as f32 + 0.5) * ratio;
518
519        let left = (inputy - src_support).floor() as i64;
520        let left = clamp(left, 0, <i64 as From<_>>::from(height) - 1) as u32;
521
522        let right = (inputy + src_support).ceil() as i64;
523        let right = clamp(
524            right,
525            <i64 as From<_>>::from(left) + 1,
526            <i64 as From<_>>::from(height),
527        ) as u32;
528
529        let inputy = inputy - 0.5;
530
531        ws.clear();
532        let mut sum = 0.0;
533        for i in left..right {
534            let w = (filter.kernel)((i as f32 - inputy) / sratio);
535            ws.push(w);
536            sum += w;
537        }
538        for w in ws.iter_mut() {
539            *w /= sum;
540        }
541
542        for x in 0..width {
543            let mut t = (0.0, 0.0, 0.0, 0.0);
544
545            for (i, w) in ws.iter().enumerate() {
546                let p = image.get_pixel(x, left + i as u32);
547
548                #[allow(deprecated)]
549                let (k1, k2, k3, k4) = p.channels4();
550                let vec: (f32, f32, f32, f32) = (
551                    NumCast::from(k1).unwrap(),
552                    NumCast::from(k2).unwrap(),
553                    NumCast::from(k3).unwrap(),
554                    NumCast::from(k4).unwrap(),
555                );
556
557                t.0 += vec.0 * w;
558                t.1 += vec.1 * w;
559                t.2 += vec.2 * w;
560                t.3 += vec.3 * w;
561            }
562
563            #[allow(deprecated)]
564            // This is not necessarily Rgba.
565            let t = Pixel::from_channels(t.0, t.1, t.2, t.3);
566
567            out.put_pixel(x, outy, t);
568        }
569    }
570
571    out
572}
573
574/// Local struct for keeping track of pixel sums for fast thumbnail averaging
575struct ThumbnailSum<S: Primitive + Enlargeable>(S::Larger, S::Larger, S::Larger, S::Larger);
576
577impl<S: Primitive + Enlargeable> ThumbnailSum<S> {
578    fn zeroed() -> Self {
579        ThumbnailSum(
580            S::Larger::zero(),
581            S::Larger::zero(),
582            S::Larger::zero(),
583            S::Larger::zero(),
584        )
585    }
586
587    fn sample_val(val: S) -> S::Larger {
588        <S::Larger as NumCast>::from(val).unwrap()
589    }
590
591    fn add_pixel<P: Pixel<Subpixel = S>>(&mut self, pixel: P) {
592        #[allow(deprecated)]
593        let pixel = pixel.channels4();
594        self.0 += Self::sample_val(pixel.0);
595        self.1 += Self::sample_val(pixel.1);
596        self.2 += Self::sample_val(pixel.2);
597        self.3 += Self::sample_val(pixel.3);
598    }
599}
600
601/// Resize the supplied image to the specific dimensions.
602///
603/// For downscaling, this method uses a fast integer algorithm where each source pixel contributes
604/// to exactly one target pixel.  May give aliasing artifacts if new size is close to old size.
605///
606/// In case the current width is smaller than the new width or similar for the height, another
607/// strategy is used instead.  For each pixel in the output, a rectangular region of the input is
608/// determined, just as previously.  But when no input pixel is part of this region, the nearest
609/// pixels are interpolated instead.
610///
611/// For speed reasons, all interpolation is performed linearly over the colour values.  It will not
612/// take the pixel colour spaces into account.
613pub fn thumbnail<I, P, S>(image: &I, new_width: u32, new_height: u32) -> ImageBuffer<P, Vec<S>>
614where
615    I: GenericImageView<Pixel = P>,
616    P: Pixel<Subpixel = S> + 'static,
617    S: Primitive + Enlargeable + 'static,
618{
619    let (width, height) = image.dimensions();
620    let mut out = image.buffer_with_dimensions(new_width, new_height);
621
622    if height == 0 || width == 0 {
623        return out;
624    }
625
626    let x_ratio = width as f32 / new_width as f32;
627    let y_ratio = height as f32 / new_height as f32;
628
629    for outy in 0..new_height {
630        let bottomf = outy as f32 * y_ratio;
631        let topf = bottomf + y_ratio;
632
633        let bottom = clamp(bottomf.ceil() as u32, 0, height - 1);
634        let top = clamp(topf.ceil() as u32, bottom, height);
635
636        for outx in 0..new_width {
637            let leftf = outx as f32 * x_ratio;
638            let rightf = leftf + x_ratio;
639
640            let left = clamp(leftf.ceil() as u32, 0, width - 1);
641            let right = clamp(rightf.ceil() as u32, left, width);
642
643            let avg = if bottom != top && left != right {
644                thumbnail_sample_block(image, left, right, bottom, top)
645            } else if bottom != top {
646                // && left == right
647                // In the first column we have left == 0 and right > ceil(y_scale) > 0 so this
648                // assertion can never trigger.
649                debug_assert!(
650                    left > 0 && right > 0,
651                    "First output column must have corresponding pixels"
652                );
653
654                let fraction_horizontal = (leftf.fract() + rightf.fract()) / 2.;
655                thumbnail_sample_fraction_horizontal(
656                    image,
657                    right - 1,
658                    fraction_horizontal,
659                    bottom,
660                    top,
661                )
662            } else if left != right {
663                // && bottom == top
664                // In the first line we have bottom == 0 and top > ceil(x_scale) > 0 so this
665                // assertion can never trigger.
666                debug_assert!(
667                    bottom > 0 && top > 0,
668                    "First output row must have corresponding pixels"
669                );
670
671                let fraction_vertical = (topf.fract() + bottomf.fract()) / 2.;
672                thumbnail_sample_fraction_vertical(image, left, right, top - 1, fraction_vertical)
673            } else {
674                // bottom == top && left == right
675                let fraction_horizontal = (topf.fract() + bottomf.fract()) / 2.;
676                let fraction_vertical = (leftf.fract() + rightf.fract()) / 2.;
677
678                thumbnail_sample_fraction_both(
679                    image,
680                    right - 1,
681                    fraction_horizontal,
682                    top - 1,
683                    fraction_vertical,
684                )
685            };
686
687            #[allow(deprecated)]
688            let pixel = Pixel::from_channels(avg.0, avg.1, avg.2, avg.3);
689            out.put_pixel(outx, outy, pixel);
690        }
691    }
692
693    out
694}
695
696/// Get a pixel for a thumbnail where the input window encloses at least a full pixel.
697fn thumbnail_sample_block<I, P, S>(
698    image: &I,
699    left: u32,
700    right: u32,
701    bottom: u32,
702    top: u32,
703) -> (S, S, S, S)
704where
705    I: GenericImageView<Pixel = P>,
706    P: Pixel<Subpixel = S>,
707    S: Primitive + Enlargeable,
708{
709    let mut sum = ThumbnailSum::zeroed();
710
711    for y in bottom..top {
712        for x in left..right {
713            let k = image.get_pixel(x, y);
714            sum.add_pixel(k);
715        }
716    }
717
718    let n = <S::Larger as NumCast>::from((right - left) * (top - bottom)).unwrap();
719    let round = <S::Larger as NumCast>::from(n / NumCast::from(2).unwrap()).unwrap();
720    (
721        S::clamp_from((sum.0 + round) / n),
722        S::clamp_from((sum.1 + round) / n),
723        S::clamp_from((sum.2 + round) / n),
724        S::clamp_from((sum.3 + round) / n),
725    )
726}
727
728/// Get a thumbnail pixel where the input window encloses at least a vertical pixel.
729fn thumbnail_sample_fraction_horizontal<I, P, S>(
730    image: &I,
731    left: u32,
732    fraction_horizontal: f32,
733    bottom: u32,
734    top: u32,
735) -> (S, S, S, S)
736where
737    I: GenericImageView<Pixel = P>,
738    P: Pixel<Subpixel = S>,
739    S: Primitive + Enlargeable,
740{
741    let fract = fraction_horizontal;
742
743    let mut sum_left = ThumbnailSum::zeroed();
744    let mut sum_right = ThumbnailSum::zeroed();
745    for x in bottom..top {
746        let k_left = image.get_pixel(left, x);
747        sum_left.add_pixel(k_left);
748
749        let k_right = image.get_pixel(left + 1, x);
750        sum_right.add_pixel(k_right);
751    }
752
753    // Now we approximate: left/n*(1-fract) + right/n*fract
754    let fact_right = fract / ((top - bottom) as f32);
755    let fact_left = (1. - fract) / ((top - bottom) as f32);
756
757    let mix_left_and_right = |leftv: S::Larger, rightv: S::Larger| {
758        <S as NumCast>::from(
759            fact_left * leftv.to_f32().unwrap() + fact_right * rightv.to_f32().unwrap(),
760        )
761        .expect("Average sample value should fit into sample type")
762    };
763
764    (
765        mix_left_and_right(sum_left.0, sum_right.0),
766        mix_left_and_right(sum_left.1, sum_right.1),
767        mix_left_and_right(sum_left.2, sum_right.2),
768        mix_left_and_right(sum_left.3, sum_right.3),
769    )
770}
771
772/// Get a thumbnail pixel where the input window encloses at least a horizontal pixel.
773fn thumbnail_sample_fraction_vertical<I, P, S>(
774    image: &I,
775    left: u32,
776    right: u32,
777    bottom: u32,
778    fraction_vertical: f32,
779) -> (S, S, S, S)
780where
781    I: GenericImageView<Pixel = P>,
782    P: Pixel<Subpixel = S>,
783    S: Primitive + Enlargeable,
784{
785    let fract = fraction_vertical;
786
787    let mut sum_bot = ThumbnailSum::zeroed();
788    let mut sum_top = ThumbnailSum::zeroed();
789    for x in left..right {
790        let k_bot = image.get_pixel(x, bottom);
791        sum_bot.add_pixel(k_bot);
792
793        let k_top = image.get_pixel(x, bottom + 1);
794        sum_top.add_pixel(k_top);
795    }
796
797    // Now we approximate: bot/n*fract + top/n*(1-fract)
798    let fact_top = fract / ((right - left) as f32);
799    let fact_bot = (1. - fract) / ((right - left) as f32);
800
801    let mix_bot_and_top = |botv: S::Larger, topv: S::Larger| {
802        <S as NumCast>::from(fact_bot * botv.to_f32().unwrap() + fact_top * topv.to_f32().unwrap())
803            .expect("Average sample value should fit into sample type")
804    };
805
806    (
807        mix_bot_and_top(sum_bot.0, sum_top.0),
808        mix_bot_and_top(sum_bot.1, sum_top.1),
809        mix_bot_and_top(sum_bot.2, sum_top.2),
810        mix_bot_and_top(sum_bot.3, sum_top.3),
811    )
812}
813
814/// Get a single pixel for a thumbnail where the input window does not enclose any full pixel.
815fn thumbnail_sample_fraction_both<I, P, S>(
816    image: &I,
817    left: u32,
818    fraction_vertical: f32,
819    bottom: u32,
820    fraction_horizontal: f32,
821) -> (S, S, S, S)
822where
823    I: GenericImageView<Pixel = P>,
824    P: Pixel<Subpixel = S>,
825    S: Primitive + Enlargeable,
826{
827    #[allow(deprecated)]
828    let k_bl = image.get_pixel(left, bottom).channels4();
829    #[allow(deprecated)]
830    let k_tl = image.get_pixel(left, bottom + 1).channels4();
831    #[allow(deprecated)]
832    let k_br = image.get_pixel(left + 1, bottom).channels4();
833    #[allow(deprecated)]
834    let k_tr = image.get_pixel(left + 1, bottom + 1).channels4();
835
836    let frac_v = fraction_vertical;
837    let frac_h = fraction_horizontal;
838
839    let fact_tr = frac_v * frac_h;
840    let fact_tl = frac_v * (1. - frac_h);
841    let fact_br = (1. - frac_v) * frac_h;
842    let fact_bl = (1. - frac_v) * (1. - frac_h);
843
844    let mix = |br: S, tr: S, bl: S, tl: S| {
845        <S as NumCast>::from(
846            fact_br * br.to_f32().unwrap()
847                + fact_tr * tr.to_f32().unwrap()
848                + fact_bl * bl.to_f32().unwrap()
849                + fact_tl * tl.to_f32().unwrap(),
850        )
851        .expect("Average sample value should fit into sample type")
852    };
853
854    (
855        mix(k_br.0, k_tr.0, k_bl.0, k_tl.0),
856        mix(k_br.1, k_tr.1, k_bl.1, k_tl.1),
857        mix(k_br.2, k_tr.2, k_bl.2, k_tl.2),
858        mix(k_br.3, k_tr.3, k_bl.3, k_tl.3),
859    )
860}
861
862/// Perform a 3x3 box filter on the supplied image.
863///
864/// # Arguments:
865///
866/// * `image` - source image.
867/// * `kernel` - is an array of the filter weights of length 9.
868///
869/// This method typically assumes that the input is scene-linear light.
870/// If it is not, color distortion may occur.
871pub fn filter3x3<I, P, S>(image: &I, kernel: &[f32]) -> ImageBuffer<P, Vec<S>>
872where
873    I: GenericImageView<Pixel = P>,
874    P: Pixel<Subpixel = S> + 'static,
875    S: Primitive + 'static,
876{
877    // The kernel's input positions relative to the current pixel.
878    let taps: &[(isize, isize)] = &[
879        (-1, -1),
880        (0, -1),
881        (1, -1),
882        (-1, 0),
883        (0, 0),
884        (1, 0),
885        (-1, 1),
886        (0, 1),
887        (1, 1),
888    ];
889
890    let (width, height) = image.dimensions();
891    let mut out = image.buffer_like();
892
893    let max = S::DEFAULT_MAX_VALUE;
894    let max: f32 = NumCast::from(max).unwrap();
895
896    #[allow(clippy::redundant_guards)]
897    let sum = match kernel.iter().fold(0.0, |s, &item| s + item) {
898        x if x == 0.0 => 1.0,
899        sum => sum,
900    };
901    let sum = (sum, sum, sum, sum);
902
903    for y in 1..height - 1 {
904        for x in 1..width - 1 {
905            let mut t = (0.0, 0.0, 0.0, 0.0);
906
907            // TODO: There is no need to recalculate the kernel for each pixel.
908            // Only a subtract and addition is needed for pixels after the first
909            // in each row.
910            for (&k, &(a, b)) in kernel.iter().zip(taps.iter()) {
911                let k = (k, k, k, k);
912                let x0 = x as isize + a;
913                let y0 = y as isize + b;
914
915                let p = image.get_pixel(x0 as u32, y0 as u32);
916
917                #[allow(deprecated)]
918                let (k1, k2, k3, k4) = p.channels4();
919
920                let vec: (f32, f32, f32, f32) = (
921                    NumCast::from(k1).unwrap(),
922                    NumCast::from(k2).unwrap(),
923                    NumCast::from(k3).unwrap(),
924                    NumCast::from(k4).unwrap(),
925                );
926
927                t.0 += vec.0 * k.0;
928                t.1 += vec.1 * k.1;
929                t.2 += vec.2 * k.2;
930                t.3 += vec.3 * k.3;
931            }
932
933            let (t1, t2, t3, t4) = (t.0 / sum.0, t.1 / sum.1, t.2 / sum.2, t.3 / sum.3);
934
935            #[allow(deprecated)]
936            let t = Pixel::from_channels(
937                NumCast::from(clamp(t1, 0.0, max)).unwrap(),
938                NumCast::from(clamp(t2, 0.0, max)).unwrap(),
939                NumCast::from(clamp(t3, 0.0, max)).unwrap(),
940                NumCast::from(clamp(t4, 0.0, max)).unwrap(),
941            );
942
943            out.put_pixel(x, y, t);
944        }
945    }
946
947    out
948}
949
950/// Resize the supplied image to the specified dimensions.
951///
952/// # Arguments:
953///
954/// * `nwidth` - new image width.
955/// * `nheight` - new image height.
956/// * `filter` -  is the sampling filter to use, see [FilterType] for mor information.
957///
958/// This method assumes alpha pre-multiplication for images that contain non-constant alpha.
959///
960/// This method typically assumes that the input is scene-linear light.
961/// If it is not, color distortion may occur.
962pub fn resize<I: GenericImageView>(
963    image: &I,
964    nwidth: u32,
965    nheight: u32,
966    filter: FilterType,
967) -> ImageBuffer<I::Pixel, Vec<<I::Pixel as Pixel>::Subpixel>>
968where
969    I::Pixel: 'static,
970    <I::Pixel as Pixel>::Subpixel: 'static,
971{
972    // Check if there is nothing to sample from.
973    let is_empty = {
974        let (width, height) = image.dimensions();
975        width == 0 || height == 0
976    };
977
978    if is_empty {
979        return image.buffer_with_dimensions(nwidth, nheight);
980    }
981
982    // check if the new dimensions are the same as the old. if they are, make a copy instead of resampling
983    if (nwidth, nheight) == image.dimensions() {
984        let mut tmp = image.buffer_like();
985        tmp.copy_from(image, 0, 0).unwrap();
986        return tmp;
987    }
988
989    let mut method = match filter {
990        FilterType::Nearest => Filter {
991            kernel: Box::new(box_kernel),
992            support: 0.0,
993        },
994        FilterType::Triangle => Filter {
995            kernel: Box::new(triangle_kernel),
996            support: 1.0,
997        },
998        FilterType::CatmullRom => Filter {
999            kernel: Box::new(catmullrom_kernel),
1000            support: 2.0,
1001        },
1002        FilterType::Gaussian => Filter {
1003            kernel: Box::new(gaussian_kernel),
1004            support: 3.0,
1005        },
1006        FilterType::Lanczos3 => Filter {
1007            kernel: Box::new(lanczos3_kernel),
1008            support: 3.0,
1009        },
1010    };
1011
1012    // Note: tmp is not necessarily actually Rgba
1013    let tmp: Rgba32FImage = vertical_sample(image, nheight, &mut method);
1014    horizontal_sample(&tmp, nwidth, &mut method)
1015}
1016
1017/// Performs a Gaussian blur on the supplied image.
1018///
1019/// # Arguments
1020///
1021///  - `sigma` - gaussian bell flattening level.
1022///
1023/// Use [`crate::imageops::fast_blur()`] for a faster but less
1024/// accurate version.
1025/// This method assumes alpha pre-multiplication for images that contain non-constant alpha.
1026/// This method typically assumes that the input is scene-linear light.
1027/// If it is not, color distortion may occur.
1028pub fn blur<I: GenericImageView>(
1029    image: &I,
1030    sigma: f32,
1031) -> ImageBuffer<I::Pixel, Vec<<I::Pixel as Pixel>::Subpixel>>
1032where
1033    I::Pixel: 'static,
1034{
1035    gaussian_blur_indirect(
1036        image,
1037        GaussianBlurParameters::new_from_sigma(if sigma == 0.0 { 0.8 } else { sigma }),
1038    )
1039}
1040
1041/// Performs a Gaussian blur on the supplied image.
1042///
1043/// # Arguments
1044///
1045///  - `parameters` - see [GaussianBlurParameters] for more info.
1046///
1047/// This method assumes alpha pre-multiplication for images that contain non-constant alpha.
1048/// This method typically assumes that the input is scene-linear light.
1049/// If it is not, color distortion may occur.
1050pub fn blur_advanced<I: GenericImageView>(
1051    image: &I,
1052    parameters: GaussianBlurParameters,
1053) -> ImageBuffer<I::Pixel, Vec<<I::Pixel as Pixel>::Subpixel>>
1054where
1055    I::Pixel: 'static,
1056{
1057    gaussian_blur_indirect(image, parameters)
1058}
1059
1060fn get_gaussian_kernel_1d(width: usize, sigma: f32) -> Vec<f32> {
1061    let mut sum_norm: f32 = 0f32;
1062    let mut kernel = vec![0f32; width];
1063    let scale = 1f32 / (f32::sqrt(2f32 * f32::consts::PI) * sigma);
1064    let mean = (width / 2) as f32;
1065
1066    for (x, weight) in kernel.iter_mut().enumerate() {
1067        let new_weight = f32::exp(-0.5f32 * f32::powf((x as f32 - mean) / sigma, 2.0f32)) * scale;
1068        *weight = new_weight;
1069        sum_norm += new_weight;
1070    }
1071
1072    if sum_norm != 0f32 {
1073        let sum_scale = 1f32 / sum_norm;
1074        for weight in &mut kernel {
1075            *weight = weight.mul(sum_scale);
1076        }
1077    }
1078
1079    kernel
1080}
1081
1082/// Holds analytical gaussian blur representation
1083#[derive(Copy, Clone, PartialOrd, PartialEq)]
1084pub struct GaussianBlurParameters {
1085    /// X-axis kernel, must be odd
1086    x_axis_kernel_size: u32,
1087    /// X-axis sigma, must > 0, not subnormal, and not NaN
1088    x_axis_sigma: f32,
1089    /// Y-axis kernel, must be odd
1090    y_axis_kernel_size: u32,
1091    /// Y-axis sigma, must > 0, not subnormal, and not NaN
1092    y_axis_sigma: f32,
1093}
1094
1095impl GaussianBlurParameters {
1096    /// Built-in smoothing kernel with size 3.
1097    pub const SMOOTHING_3: GaussianBlurParameters = GaussianBlurParameters {
1098        x_axis_kernel_size: 3,
1099        x_axis_sigma: 0.8,
1100        y_axis_kernel_size: 3,
1101        y_axis_sigma: 0.8,
1102    };
1103
1104    /// Built-in smoothing kernel with size 5.
1105    pub const SMOOTHING_5: GaussianBlurParameters = GaussianBlurParameters {
1106        x_axis_kernel_size: 5,
1107        x_axis_sigma: 1.1,
1108        y_axis_kernel_size: 5,
1109        y_axis_sigma: 1.1,
1110    };
1111
1112    /// Built-in smoothing kernel with size 7.
1113    pub const SMOOTHING_7: GaussianBlurParameters = GaussianBlurParameters {
1114        x_axis_kernel_size: 7,
1115        x_axis_sigma: 1.4,
1116        y_axis_kernel_size: 7,
1117        y_axis_sigma: 1.4,
1118    };
1119
1120    /// Creates a new parameters set from radius only.
1121    pub fn new_from_radius(radius: f32) -> GaussianBlurParameters {
1122        // Previous implementation was allowing passing 0 so we'll allow here also.
1123        assert!(radius >= 0.0);
1124        if radius != 0. {
1125            assert!(
1126                radius.is_normal(),
1127                "Radius do not allow infinities, NaNs or subnormals"
1128            );
1129        }
1130        GaussianBlurParameters::new_from_kernel_size(radius * 2. + 1.)
1131    }
1132
1133    /// Creates a new parameters set from kernel size only.
1134    ///
1135    /// Kernel size will be rounded to nearest odd, and used with fraction
1136    /// to compute accurate required sigma.
1137    pub fn new_from_kernel_size(kernel_size: f32) -> GaussianBlurParameters {
1138        assert!(
1139            kernel_size > 0.,
1140            "Kernel size do not allow infinities, zeros, NaNs or subnormals or negatives"
1141        );
1142        assert!(
1143            kernel_size.is_normal(),
1144            "Kernel size do not allow infinities, zeros, NaNs or subnormals or negatives"
1145        );
1146        let i_kernel_size = GaussianBlurParameters::round_to_nearest_odd(kernel_size);
1147        assert_ne!(i_kernel_size % 2, 0, "Kernel size must be odd");
1148        let v_sigma = GaussianBlurParameters::sigma_size(kernel_size);
1149        GaussianBlurParameters {
1150            x_axis_kernel_size: i_kernel_size,
1151            x_axis_sigma: v_sigma,
1152            y_axis_kernel_size: i_kernel_size,
1153            y_axis_sigma: v_sigma,
1154        }
1155    }
1156
1157    /// Creates a new anisotropic parameter set from kernel sizes
1158    ///
1159    /// Kernel size will be rounded to nearest odd, and used with fraction
1160    /// to compute accurate required sigma.
1161    pub fn new_anisotropic_kernel_size(
1162        x_axis_kernel_size: f32,
1163        y_axis_kernel_size: f32,
1164    ) -> GaussianBlurParameters {
1165        assert!(
1166            x_axis_kernel_size > 0.,
1167            "Kernel size do not allow infinities, zeros, NaNs or subnormals or negatives"
1168        );
1169        assert!(
1170            y_axis_kernel_size.is_normal(),
1171            "Kernel size do not allow infinities, zeros, NaNs or subnormals or negatives"
1172        );
1173        assert!(
1174            y_axis_kernel_size > 0.,
1175            "Kernel size do not allow infinities, zeros, NaNs or subnormals or negatives"
1176        );
1177        assert!(
1178            y_axis_kernel_size.is_normal(),
1179            "Kernel size do not allow infinities, zeros, NaNs or subnormals or negatives"
1180        );
1181        let x_kernel_size = GaussianBlurParameters::round_to_nearest_odd(x_axis_kernel_size);
1182        assert_ne!(x_kernel_size % 2, 0, "Kernel size must be odd");
1183        let y_kernel_size = GaussianBlurParameters::round_to_nearest_odd(y_axis_kernel_size);
1184        assert_ne!(y_kernel_size % 2, 0, "Kernel size must be odd");
1185        let x_sigma = GaussianBlurParameters::sigma_size(x_axis_kernel_size);
1186        let y_sigma = GaussianBlurParameters::sigma_size(y_axis_kernel_size);
1187        GaussianBlurParameters {
1188            x_axis_kernel_size: x_kernel_size,
1189            x_axis_sigma: x_sigma,
1190            y_axis_kernel_size: y_kernel_size,
1191            y_axis_sigma: y_sigma,
1192        }
1193    }
1194
1195    /// Creates a new parameters set from sigma only
1196    pub fn new_from_sigma(sigma: f32) -> GaussianBlurParameters {
1197        assert!(
1198            sigma.is_normal(),
1199            "Sigma cannot be NaN, Infinities, subnormal or zero"
1200        );
1201        assert!(sigma > 0.0, "Sigma must be positive");
1202        let kernel_size = GaussianBlurParameters::kernel_size_from_sigma(sigma);
1203        GaussianBlurParameters {
1204            x_axis_kernel_size: kernel_size,
1205            x_axis_sigma: sigma,
1206            y_axis_kernel_size: kernel_size,
1207            y_axis_sigma: sigma,
1208        }
1209    }
1210
1211    #[inline]
1212    fn round_to_nearest_odd(x: f32) -> u32 {
1213        let n = x.round() as u32;
1214        if n % 2 != 0 {
1215            n
1216        } else {
1217            let lower = n - 1;
1218            let upper = n + 1;
1219
1220            let dist_lower = (x - lower as f32).abs();
1221            let dist_upper = (x - upper as f32).abs();
1222
1223            if dist_lower <= dist_upper {
1224                lower
1225            } else {
1226                upper
1227            }
1228        }
1229    }
1230
1231    fn sigma_size(kernel_size: f32) -> f32 {
1232        let safe_kernel_size = if kernel_size <= 1. { 0.8 } else { kernel_size };
1233        0.3 * ((safe_kernel_size - 1.) * 0.5 - 1.) + 0.8
1234    }
1235
1236    fn kernel_size_from_sigma(sigma: f32) -> u32 {
1237        let possible_size = (((((sigma - 0.8) / 0.3) + 1.) * 2.) + 1.).max(3.) as u32;
1238        if possible_size % 2 == 0 {
1239            return possible_size + 1;
1240        }
1241        possible_size
1242    }
1243}
1244
1245pub(crate) fn gaussian_blur_dyn_image(
1246    image: &DynamicImage,
1247    parameters: GaussianBlurParameters,
1248) -> DynamicImage {
1249    let x_axis_kernel = get_gaussian_kernel_1d(
1250        parameters.x_axis_kernel_size as usize,
1251        parameters.x_axis_sigma,
1252    );
1253
1254    let y_axis_kernel = get_gaussian_kernel_1d(
1255        parameters.y_axis_kernel_size as usize,
1256        parameters.y_axis_sigma,
1257    );
1258
1259    let filter_image_size = FilterImageSize {
1260        width: image.width() as usize,
1261        height: image.height() as usize,
1262    };
1263
1264    let mut target = match image {
1265        DynamicImage::ImageLuma8(img) => {
1266            let mut dest_image = vec![0u8; img.len()];
1267            filter_2d_sep_plane(
1268                img.as_raw(),
1269                &mut dest_image,
1270                filter_image_size,
1271                &x_axis_kernel,
1272                &y_axis_kernel,
1273            )
1274            .unwrap();
1275            DynamicImage::ImageLuma8(
1276                GrayImage::from_raw(img.width(), img.height(), dest_image).unwrap(),
1277            )
1278        }
1279        DynamicImage::ImageLumaA8(img) => {
1280            let mut dest_image = vec![0u8; img.len()];
1281            filter_2d_sep_la(
1282                img.as_raw(),
1283                &mut dest_image,
1284                filter_image_size,
1285                &x_axis_kernel,
1286                &y_axis_kernel,
1287            )
1288            .unwrap();
1289            DynamicImage::ImageLumaA8(
1290                GrayAlphaImage::from_raw(img.width(), img.height(), dest_image).unwrap(),
1291            )
1292        }
1293        DynamicImage::ImageRgb8(img) => {
1294            let mut dest_image = vec![0u8; img.len()];
1295            filter_2d_sep_rgb(
1296                img.as_raw(),
1297                &mut dest_image,
1298                filter_image_size,
1299                &x_axis_kernel,
1300                &y_axis_kernel,
1301            )
1302            .unwrap();
1303            DynamicImage::ImageRgb8(
1304                RgbImage::from_raw(img.width(), img.height(), dest_image).unwrap(),
1305            )
1306        }
1307        DynamicImage::ImageRgba8(img) => {
1308            let mut dest_image = vec![0u8; img.len()];
1309            filter_2d_sep_rgba(
1310                img.as_raw(),
1311                &mut dest_image,
1312                filter_image_size,
1313                &x_axis_kernel,
1314                &y_axis_kernel,
1315            )
1316            .unwrap();
1317            DynamicImage::ImageRgba8(
1318                RgbaImage::from_raw(img.width(), img.height(), dest_image).unwrap(),
1319            )
1320        }
1321        DynamicImage::ImageLuma16(img) => {
1322            let mut dest_image = vec![0u16; img.len()];
1323            filter_2d_sep_plane_u16(
1324                img.as_raw(),
1325                &mut dest_image,
1326                filter_image_size,
1327                &x_axis_kernel,
1328                &y_axis_kernel,
1329            )
1330            .unwrap();
1331            DynamicImage::ImageLuma16(
1332                Gray16Image::from_raw(img.width(), img.height(), dest_image).unwrap(),
1333            )
1334        }
1335        DynamicImage::ImageLumaA16(img) => {
1336            let mut dest_image = vec![0u16; img.len()];
1337            filter_2d_sep_la_u16(
1338                img.as_raw(),
1339                &mut dest_image,
1340                filter_image_size,
1341                &x_axis_kernel,
1342                &y_axis_kernel,
1343            )
1344            .unwrap();
1345            DynamicImage::ImageLumaA16(
1346                GrayAlpha16Image::from_raw(img.width(), img.height(), dest_image).unwrap(),
1347            )
1348        }
1349        DynamicImage::ImageRgb16(img) => {
1350            let mut dest_image = vec![0u16; img.len()];
1351            filter_2d_sep_rgb_u16(
1352                img.as_raw(),
1353                &mut dest_image,
1354                filter_image_size,
1355                &x_axis_kernel,
1356                &y_axis_kernel,
1357            )
1358            .unwrap();
1359            DynamicImage::ImageRgb16(
1360                Rgb16Image::from_raw(img.width(), img.height(), dest_image).unwrap(),
1361            )
1362        }
1363        DynamicImage::ImageRgba16(img) => {
1364            let mut dest_image = vec![0u16; img.len()];
1365            filter_2d_sep_rgba_u16(
1366                img.as_raw(),
1367                &mut dest_image,
1368                filter_image_size,
1369                &x_axis_kernel,
1370                &y_axis_kernel,
1371            )
1372            .unwrap();
1373            DynamicImage::ImageRgba16(
1374                Rgba16Image::from_raw(img.width(), img.height(), dest_image).unwrap(),
1375            )
1376        }
1377        DynamicImage::ImageRgb32F(img) => {
1378            let mut dest_image = vec![0f32; img.len()];
1379            filter_2d_sep_rgb_f32(
1380                img.as_raw(),
1381                &mut dest_image,
1382                filter_image_size,
1383                &x_axis_kernel,
1384                &y_axis_kernel,
1385            )
1386            .unwrap();
1387            DynamicImage::ImageRgb32F(
1388                Rgb32FImage::from_raw(img.width(), img.height(), dest_image).unwrap(),
1389            )
1390        }
1391        DynamicImage::ImageRgba32F(img) => {
1392            let mut dest_image = vec![0f32; img.len()];
1393            filter_2d_sep_rgba_f32(
1394                img.as_raw(),
1395                &mut dest_image,
1396                filter_image_size,
1397                &x_axis_kernel,
1398                &y_axis_kernel,
1399            )
1400            .unwrap();
1401            DynamicImage::ImageRgba32F(
1402                Rgba32FImage::from_raw(img.width(), img.height(), dest_image).unwrap(),
1403            )
1404        }
1405    };
1406
1407    // Must succeed.
1408    let _ = target.set_color_space(image.color_space());
1409    target
1410}
1411
1412fn gaussian_blur_indirect<I: GenericImageView>(
1413    image: &I,
1414    parameters: GaussianBlurParameters,
1415) -> ImageBuffer<I::Pixel, Vec<<I::Pixel as Pixel>::Subpixel>>
1416where
1417    I::Pixel: 'static,
1418{
1419    match I::Pixel::CHANNEL_COUNT {
1420        1 => gaussian_blur_indirect_impl::<I, 1>(image, parameters),
1421        2 => gaussian_blur_indirect_impl::<I, 2>(image, parameters),
1422        3 => gaussian_blur_indirect_impl::<I, 3>(image, parameters),
1423        4 => gaussian_blur_indirect_impl::<I, 4>(image, parameters),
1424        _ => unimplemented!(),
1425    }
1426}
1427
1428fn gaussian_blur_indirect_impl<I: GenericImageView, const CN: usize>(
1429    image: &I,
1430    parameters: GaussianBlurParameters,
1431) -> ImageBuffer<I::Pixel, Vec<<I::Pixel as Pixel>::Subpixel>>
1432where
1433    I::Pixel: 'static,
1434{
1435    let mut transient = vec![0f32; image.width() as usize * image.height() as usize * CN];
1436    for (pixel, dst) in image.pixels().zip(transient.chunks_exact_mut(CN)) {
1437        let px = pixel.2.channels();
1438        match CN {
1439            1 => {
1440                dst[0] = NumCast::from(px[0]).unwrap();
1441            }
1442            2 => {
1443                dst[0] = NumCast::from(px[0]).unwrap();
1444                dst[1] = NumCast::from(px[1]).unwrap();
1445            }
1446            3 => {
1447                dst[0] = NumCast::from(px[0]).unwrap();
1448                dst[1] = NumCast::from(px[1]).unwrap();
1449                dst[2] = NumCast::from(px[2]).unwrap();
1450            }
1451            4 => {
1452                dst[0] = NumCast::from(px[0]).unwrap();
1453                dst[1] = NumCast::from(px[1]).unwrap();
1454                dst[2] = NumCast::from(px[2]).unwrap();
1455                dst[3] = NumCast::from(px[3]).unwrap();
1456            }
1457            _ => unreachable!(),
1458        }
1459    }
1460
1461    let mut transient_dst = vec![0.; image.width() as usize * image.height() as usize * CN];
1462
1463    let x_axis_kernel = get_gaussian_kernel_1d(
1464        parameters.x_axis_kernel_size as usize,
1465        parameters.x_axis_sigma,
1466    );
1467    let y_axis_kernel = get_gaussian_kernel_1d(
1468        parameters.y_axis_kernel_size as usize,
1469        parameters.y_axis_sigma,
1470    );
1471
1472    let filter_image_size = FilterImageSize {
1473        width: image.width() as usize,
1474        height: image.height() as usize,
1475    };
1476
1477    match CN {
1478        1 => {
1479            filter_2d_sep_plane_f32(
1480                &transient,
1481                &mut transient_dst,
1482                filter_image_size,
1483                &x_axis_kernel,
1484                &y_axis_kernel,
1485            )
1486            .unwrap();
1487        }
1488        2 => {
1489            filter_2d_sep_la_f32(
1490                &transient,
1491                &mut transient_dst,
1492                filter_image_size,
1493                &x_axis_kernel,
1494                &y_axis_kernel,
1495            )
1496            .unwrap();
1497        }
1498        3 => {
1499            filter_2d_sep_rgb_f32(
1500                &transient,
1501                &mut transient_dst,
1502                filter_image_size,
1503                &x_axis_kernel,
1504                &y_axis_kernel,
1505            )
1506            .unwrap();
1507        }
1508        4 => {
1509            filter_2d_sep_rgba_f32(
1510                &transient,
1511                &mut transient_dst,
1512                filter_image_size,
1513                &x_axis_kernel,
1514                &y_axis_kernel,
1515            )
1516            .unwrap();
1517        }
1518        _ => unreachable!(),
1519    }
1520
1521    let mut out = image.buffer_like();
1522    for (dst, src) in out.pixels_mut().zip(transient_dst.chunks_exact_mut(CN)) {
1523        match CN {
1524            1 => {
1525                let v0 = NumCast::from(FloatNearest(src[0])).unwrap();
1526                #[allow(deprecated)]
1527                let t = Pixel::from_channels(v0, v0, v0, v0);
1528                *dst = t;
1529            }
1530            2 => {
1531                let v0 = NumCast::from(FloatNearest(src[0])).unwrap();
1532                let v1 = NumCast::from(FloatNearest(src[1])).unwrap();
1533                #[allow(deprecated)]
1534                let t = Pixel::from_channels(v0, v1, v0, v0);
1535                *dst = t;
1536            }
1537            3 => {
1538                let v0 = NumCast::from(FloatNearest(src[0])).unwrap();
1539                let v1 = NumCast::from(FloatNearest(src[1])).unwrap();
1540                let v2 = NumCast::from(FloatNearest(src[2])).unwrap();
1541                #[allow(deprecated)]
1542                let t = Pixel::from_channels(v0, v1, v2, v0);
1543                *dst = t;
1544            }
1545            4 => {
1546                let v0 = NumCast::from(FloatNearest(src[0])).unwrap();
1547                let v1 = NumCast::from(FloatNearest(src[1])).unwrap();
1548                let v2 = NumCast::from(FloatNearest(src[2])).unwrap();
1549                let v3 = NumCast::from(FloatNearest(src[3])).unwrap();
1550                #[allow(deprecated)]
1551                let t = Pixel::from_channels(v0, v1, v2, v3);
1552                *dst = t;
1553            }
1554            _ => unreachable!(),
1555        }
1556    }
1557
1558    out
1559}
1560
1561/// Performs an unsharpen mask on the supplied image.
1562///
1563/// # Arguments:
1564///
1565/// * `sigma` - is the amount to blur the image by.
1566/// * `threshold` - is the threshold for minimal brightness change that will be sharpened.
1567///
1568/// This method typically assumes that the input is scene-linear light.
1569/// If it is not, color distortion may occur.
1570///
1571/// See [Digital unsharp masking](https://en.wikipedia.org/wiki/Unsharp_masking#Digital_unsharp_masking) for more information.
1572pub fn unsharpen<I, P, S>(image: &I, sigma: f32, threshold: i32) -> ImageBuffer<P, Vec<S>>
1573where
1574    I: GenericImageView<Pixel = P>,
1575    P: Pixel<Subpixel = S> + 'static,
1576    S: Primitive + 'static,
1577{
1578    let mut tmp = blur_advanced(image, GaussianBlurParameters::new_from_sigma(sigma));
1579
1580    let max = S::DEFAULT_MAX_VALUE;
1581    let max: i32 = NumCast::from(max).unwrap();
1582    let (width, height) = image.dimensions();
1583
1584    for y in 0..height {
1585        for x in 0..width {
1586            let a = image.get_pixel(x, y);
1587            let b = tmp.get_pixel_mut(x, y);
1588
1589            let p = a.map2(b, |c, d| {
1590                let ic: i32 = NumCast::from(c).unwrap();
1591                let id: i32 = NumCast::from(d).unwrap();
1592
1593                let diff = ic - id;
1594
1595                if diff.abs() > threshold {
1596                    let e = clamp(ic + diff, 0, max); // FIXME what does this do for f32? clamp 0-1 integers??
1597
1598                    NumCast::from(e).unwrap()
1599                } else {
1600                    c
1601                }
1602            });
1603
1604            *b = p;
1605        }
1606    }
1607
1608    tmp
1609}
1610
1611#[cfg(test)]
1612mod tests {
1613    use super::{resize, sample_bilinear, sample_nearest, FilterType};
1614    use crate::{GenericImageView, ImageBuffer, RgbImage};
1615    #[cfg(feature = "benchmarks")]
1616    use test;
1617
1618    #[bench]
1619    #[cfg(all(feature = "benchmarks", feature = "png"))]
1620    fn bench_resize(b: &mut test::Bencher) {
1621        use std::path::Path;
1622        let img = crate::open(Path::new("./examples/fractal.png")).unwrap();
1623        b.iter(|| {
1624            test::black_box(resize(&img, 200, 200, FilterType::Nearest));
1625        });
1626        b.bytes = 800 * 800 * 3 + 200 * 200 * 3;
1627    }
1628
1629    #[test]
1630    #[cfg(feature = "png")]
1631    fn test_resize_same_size() {
1632        use std::path::Path;
1633        let img = crate::open(Path::new("./examples/fractal.png")).unwrap();
1634        let resize = img.resize(img.width(), img.height(), FilterType::Triangle);
1635        assert!(img.pixels().eq(resize.pixels()));
1636    }
1637
1638    #[test]
1639    #[cfg(feature = "png")]
1640    fn test_sample_bilinear() {
1641        use std::path::Path;
1642        let img = crate::open(Path::new("./examples/fractal.png")).unwrap();
1643        assert!(sample_bilinear(&img, 0., 0.).is_some());
1644        assert!(sample_bilinear(&img, 1., 0.).is_some());
1645        assert!(sample_bilinear(&img, 0., 1.).is_some());
1646        assert!(sample_bilinear(&img, 1., 1.).is_some());
1647        assert!(sample_bilinear(&img, 0.5, 0.5).is_some());
1648
1649        assert!(sample_bilinear(&img, 1.2, 0.5).is_none());
1650        assert!(sample_bilinear(&img, 0.5, 1.2).is_none());
1651        assert!(sample_bilinear(&img, 1.2, 1.2).is_none());
1652
1653        assert!(sample_bilinear(&img, -0.1, 0.2).is_none());
1654        assert!(sample_bilinear(&img, 0.2, -0.1).is_none());
1655        assert!(sample_bilinear(&img, -0.1, -0.1).is_none());
1656    }
1657    #[test]
1658    #[cfg(feature = "png")]
1659    fn test_sample_nearest() {
1660        use std::path::Path;
1661        let img = crate::open(Path::new("./examples/fractal.png")).unwrap();
1662        assert!(sample_nearest(&img, 0., 0.).is_some());
1663        assert!(sample_nearest(&img, 1., 0.).is_some());
1664        assert!(sample_nearest(&img, 0., 1.).is_some());
1665        assert!(sample_nearest(&img, 1., 1.).is_some());
1666        assert!(sample_nearest(&img, 0.5, 0.5).is_some());
1667
1668        assert!(sample_nearest(&img, 1.2, 0.5).is_none());
1669        assert!(sample_nearest(&img, 0.5, 1.2).is_none());
1670        assert!(sample_nearest(&img, 1.2, 1.2).is_none());
1671
1672        assert!(sample_nearest(&img, -0.1, 0.2).is_none());
1673        assert!(sample_nearest(&img, 0.2, -0.1).is_none());
1674        assert!(sample_nearest(&img, -0.1, -0.1).is_none());
1675    }
1676    #[test]
1677    fn test_sample_bilinear_correctness() {
1678        use crate::Rgba;
1679        let img = ImageBuffer::from_fn(2, 2, |x, y| match (x, y) {
1680            (0, 0) => Rgba([255, 0, 0, 0]),
1681            (0, 1) => Rgba([0, 255, 0, 0]),
1682            (1, 0) => Rgba([0, 0, 255, 0]),
1683            (1, 1) => Rgba([0, 0, 0, 255]),
1684            _ => panic!(),
1685        });
1686        assert_eq!(sample_bilinear(&img, 0.5, 0.5), Some(Rgba([64; 4])));
1687        assert_eq!(sample_bilinear(&img, 0.0, 0.0), Some(Rgba([255, 0, 0, 0])));
1688        assert_eq!(sample_bilinear(&img, 0.0, 1.0), Some(Rgba([0, 255, 0, 0])));
1689        assert_eq!(sample_bilinear(&img, 1.0, 0.0), Some(Rgba([0, 0, 255, 0])));
1690        assert_eq!(sample_bilinear(&img, 1.0, 1.0), Some(Rgba([0, 0, 0, 255])));
1691
1692        assert_eq!(
1693            sample_bilinear(&img, 0.5, 0.0),
1694            Some(Rgba([128, 0, 128, 0]))
1695        );
1696        assert_eq!(
1697            sample_bilinear(&img, 0.0, 0.5),
1698            Some(Rgba([128, 128, 0, 0]))
1699        );
1700        assert_eq!(
1701            sample_bilinear(&img, 0.5, 1.0),
1702            Some(Rgba([0, 128, 0, 128]))
1703        );
1704        assert_eq!(
1705            sample_bilinear(&img, 1.0, 0.5),
1706            Some(Rgba([0, 0, 128, 128]))
1707        );
1708    }
1709    #[bench]
1710    #[cfg(feature = "benchmarks")]
1711    fn bench_sample_bilinear(b: &mut test::Bencher) {
1712        use crate::Rgba;
1713        let img = ImageBuffer::from_fn(2, 2, |x, y| match (x, y) {
1714            (0, 0) => Rgba([255, 0, 0, 0]),
1715            (0, 1) => Rgba([0, 255, 0, 0]),
1716            (1, 0) => Rgba([0, 0, 255, 0]),
1717            (1, 1) => Rgba([0, 0, 0, 255]),
1718            _ => panic!(),
1719        });
1720        b.iter(|| {
1721            sample_bilinear(&img, test::black_box(0.5), test::black_box(0.5));
1722        });
1723    }
1724    #[test]
1725    fn test_sample_nearest_correctness() {
1726        use crate::Rgba;
1727        let img = ImageBuffer::from_fn(2, 2, |x, y| match (x, y) {
1728            (0, 0) => Rgba([255, 0, 0, 0]),
1729            (0, 1) => Rgba([0, 255, 0, 0]),
1730            (1, 0) => Rgba([0, 0, 255, 0]),
1731            (1, 1) => Rgba([0, 0, 0, 255]),
1732            _ => panic!(),
1733        });
1734
1735        assert_eq!(sample_nearest(&img, 0.0, 0.0), Some(Rgba([255, 0, 0, 0])));
1736        assert_eq!(sample_nearest(&img, 0.0, 1.0), Some(Rgba([0, 255, 0, 0])));
1737        assert_eq!(sample_nearest(&img, 1.0, 0.0), Some(Rgba([0, 0, 255, 0])));
1738        assert_eq!(sample_nearest(&img, 1.0, 1.0), Some(Rgba([0, 0, 0, 255])));
1739
1740        assert_eq!(sample_nearest(&img, 0.5, 0.5), Some(Rgba([0, 0, 0, 255])));
1741        assert_eq!(sample_nearest(&img, 0.5, 0.0), Some(Rgba([0, 0, 255, 0])));
1742        assert_eq!(sample_nearest(&img, 0.0, 0.5), Some(Rgba([0, 255, 0, 0])));
1743        assert_eq!(sample_nearest(&img, 0.5, 1.0), Some(Rgba([0, 0, 0, 255])));
1744        assert_eq!(sample_nearest(&img, 1.0, 0.5), Some(Rgba([0, 0, 0, 255])));
1745    }
1746
1747    #[bench]
1748    #[cfg(all(feature = "benchmarks", feature = "tiff"))]
1749    fn bench_resize_same_size(b: &mut test::Bencher) {
1750        let path = concat!(
1751            env!("CARGO_MANIFEST_DIR"),
1752            "/tests/images/tiff/testsuite/mandrill.tiff"
1753        );
1754        let image = crate::open(path).unwrap();
1755        b.iter(|| {
1756            test::black_box(image.resize(image.width(), image.height(), FilterType::CatmullRom));
1757        });
1758        b.bytes = u64::from(image.width() * image.height() * 3);
1759    }
1760
1761    #[test]
1762    fn test_issue_186() {
1763        let img: RgbImage = ImageBuffer::new(100, 100);
1764        let _ = resize(&img, 50, 50, FilterType::Lanczos3);
1765    }
1766
1767    #[bench]
1768    #[cfg(all(feature = "benchmarks", feature = "tiff"))]
1769    fn bench_thumbnail(b: &mut test::Bencher) {
1770        let path = concat!(
1771            env!("CARGO_MANIFEST_DIR"),
1772            "/tests/images/tiff/testsuite/mandrill.tiff"
1773        );
1774        let image = crate::open(path).unwrap();
1775        b.iter(|| {
1776            test::black_box(image.thumbnail(256, 256));
1777        });
1778        b.bytes = 512 * 512 * 4 + 256 * 256 * 4;
1779    }
1780
1781    #[bench]
1782    #[cfg(all(feature = "benchmarks", feature = "tiff"))]
1783    fn bench_thumbnail_upsize(b: &mut test::Bencher) {
1784        let path = concat!(
1785            env!("CARGO_MANIFEST_DIR"),
1786            "/tests/images/tiff/testsuite/mandrill.tiff"
1787        );
1788        let image = crate::open(path).unwrap().thumbnail(256, 256);
1789        b.iter(|| {
1790            test::black_box(image.thumbnail(512, 512));
1791        });
1792        b.bytes = 512 * 512 * 4 + 256 * 256 * 4;
1793    }
1794
1795    #[bench]
1796    #[cfg(all(feature = "benchmarks", feature = "tiff"))]
1797    fn bench_thumbnail_upsize_irregular(b: &mut test::Bencher) {
1798        let path = concat!(
1799            env!("CARGO_MANIFEST_DIR"),
1800            "/tests/images/tiff/testsuite/mandrill.tiff"
1801        );
1802        let image = crate::open(path).unwrap().thumbnail(193, 193);
1803        b.iter(|| {
1804            test::black_box(image.thumbnail(256, 256));
1805        });
1806        b.bytes = 193 * 193 * 4 + 256 * 256 * 4;
1807    }
1808
1809    #[test]
1810    #[cfg(feature = "png")]
1811    fn resize_transparent_image() {
1812        use super::FilterType::{CatmullRom, Gaussian, Lanczos3, Nearest, Triangle};
1813        use crate::imageops::crop_imm;
1814        use crate::RgbaImage;
1815
1816        fn assert_resize(image: &RgbaImage, filter: FilterType) {
1817            let resized = resize(image, 16, 16, filter);
1818            let cropped = crop_imm(&resized, 5, 5, 6, 6).to_image();
1819            for pixel in cropped.pixels() {
1820                let alpha = pixel.0[3];
1821                assert!(
1822                    alpha != 254 && alpha != 253,
1823                    "alpha value: {alpha}, {filter:?}"
1824                );
1825            }
1826        }
1827
1828        let path = concat!(
1829            env!("CARGO_MANIFEST_DIR"),
1830            "/tests/images/png/transparency/tp1n3p08.png"
1831        );
1832        let img = crate::open(path).unwrap();
1833        let rgba8 = img.as_rgba8().unwrap();
1834        let filters = &[Nearest, Triangle, CatmullRom, Gaussian, Lanczos3];
1835        for filter in filters {
1836            assert_resize(rgba8, *filter);
1837        }
1838    }
1839
1840    #[test]
1841    fn bug_1600() {
1842        let image = crate::RgbaImage::from_raw(629, 627, vec![255; 629 * 627 * 4]).unwrap();
1843        let result = resize(&image, 22, 22, FilterType::Lanczos3);
1844        assert!(result.into_raw().into_iter().any(|c| c != 0));
1845    }
1846
1847    #[test]
1848    fn issue_2340() {
1849        let empty = crate::GrayImage::from_raw(1 << 31, 0, vec![]).unwrap();
1850        // Really we're checking that no overflow / outsized allocation happens here.
1851        let result = resize(&empty, 1, 1, FilterType::Lanczos3);
1852        assert!(result.into_raw().into_iter().all(|c| c == 0));
1853        // With the previous strategy before the regression this would allocate 1TB of memory for a
1854        // temporary during the sampling evaluation.
1855        let result = resize(&empty, 256, 256, FilterType::Lanczos3);
1856        assert!(result.into_raw().into_iter().all(|c| c == 0));
1857    }
1858
1859    #[test]
1860    fn issue_2340_refl() {
1861        // Tests the swapped coordinate version of `issue_2340`.
1862        let empty = crate::GrayImage::from_raw(0, 1 << 31, vec![]).unwrap();
1863        let result = resize(&empty, 1, 1, FilterType::Lanczos3);
1864        assert!(result.into_raw().into_iter().all(|c| c == 0));
1865        let result = resize(&empty, 256, 256, FilterType::Lanczos3);
1866        assert!(result.into_raw().into_iter().all(|c| c == 0));
1867    }
1868}