image-pyramid 0.5.1

A small library to compute image pyramids
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
// This file is part of the `image-pyramid` crate: <https://github.com/jnickg/image-pyramid>
// Copyright (C) 2024 jnickg <jnickg83@gmail.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 3.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// Copyright (C) 2024 jnickg <jnickg83@gmail.com>
// SPDX-License-Identifier: GPL-3.0-only

#![doc(html_root_url = "https://docs.rs/image-pyramid/0.5.1")]
#![doc(issue_tracker_base_url = "https://github.com/jnickg/image-pyramid/issues")]

//! # Image Pyramid
//!
//! ![Maintenance](https://img.shields.io/badge/maintenance-actively--developed-brightgreen.svg)
//! [![crates-io](https://img.shields.io/crates/v/image-pyramid.svg)](https://crates.io/crates/image-pyramid)
//! [![api-docs](https://docs.rs/image-pyramid/badge.svg)](https://docs.rs/image-pyramid)
//! [![dependency-status](https://deps.rs/repo/github/jnickg/image-pyramid/status.svg)](https://deps.rs/repo/github/jnickg/image-pyramid)
//!
//! ## Overview
//!
//! This is a small Rust crate that facilitates quickly generating an image
//! pyramid from a user-provided image.
//!
//! - See [OpenCV: Image Pyramids](https://docs.opencv.org/4.x/dc/dff/tutorial_py_pyramids.html)
//!   for an overview of the two most common pyramid types, Lowpass (AKA
//!   Gaussian) and Bandpass (AKA Laplacian).
//! - The Tomasi paper [Lowpass and Bandpass Pyramids](https://courses.cs.duke.edu/cps274/fall14/notes/Pyramids.pdf)
//!   has an authoritative explanation as well.
//! - [Wikipedia](https://en.wikipedia.org/wiki/Pyramid_(image_processing)#Steerable_pyramid)
//!   has a decent explanation of a steerable pyramid
//!
//! ## Usage
//!
//! See the [crates.io page](https://crates.io/crates/image-pyramid) for installation instructions, then check out the [examples directory](./examples/) for example code. Below is a simple illustrative example of computing a default pyramid (Gaussian where each level is half resolution).
//!
//! ```rust
//! use image::DynamicImage;
//! use image_pyramid::*;
//!
//! let image = DynamicImage::new_rgba8(640, 480); // Or load from file
//! let pyramid = match ImagePyramid::create(&image, None) {
//!   Ok(pyramid) => pyramid,
//!   Err(e) => {
//!     eprintln!("Error creating image pyramid: {}", e);
//!     return;
//!   }
//! };
//! ```
//!
//! Or a slightly more complex example, illustrating how to create a bandpass
//! pyramid where each octave is 2/3 the resolution, smoothed using a triangle
//! (linear) filter.
//!
//! ```rust
//! use image::DynamicImage;
//! use image_pyramid::*;
//!
//! let image = DynamicImage::new_rgba8(640, 480); // Or load from file
//! let params = ImagePyramidParams {
//!   scale_factor:   (2.0 / 3.0).into_unit_interval().unwrap(),
//!   pyramid_type:   ImagePyramidType::Bandpass,
//!   smoothing_type: SmoothingType::Triangle,
//! };
//! let pyramid = match ImagePyramid::create(&image, Some(&params)) {
//!   Ok(pyramid) => pyramid,
//!   Err(e) => {
//!     eprintln!("Error creating image pyramid: {}", e);
//!     return;
//!   }
//! };
//! ```
//!
//! [`ImagePyramidParams::scale_factor`] field is a [`UnitIntervalValue`], which
//! must be a floating-point value in the interval (0, 1). Creating a value of
//! this type yields a [`Result`] and will contain an error if the value is not
//! valid.
//!
//! ## Support
//!
//! Open an Issue with questions or bug reports, and feel free to open a PR with
//! proposed changes.
//!
//! ## Contributing
//!
//! Follow standard Rust conventions, and be sure to add tests for any new code
//! added.

#![deny(
  nonstandard_style,
  // unused,
  unsafe_code,
  future_incompatible,
  rust_2018_idioms,
  clippy::all,
  clippy::nursery,
  clippy::pedantic
)]

use std::fmt::Debug;

use image::{DynamicImage, GenericImage, GenericImageView, Pixel};
use num_traits::{clamp, Num, NumCast};
use thiserror::Error;

/// An enumeration of the errors that may be emitted from the `image_pyramid`
/// crate
#[derive(Error, Debug)]
#[non_exhaustive]
pub enum ImagePyramidError {
  /// Raised when the user provides an invalid scale value
  #[error("Invalid scale_factor value {0} (expected: 0.0 < scale_factor < 1.0)")]
  BadScaleFactor(f32),

  /// Raised when the requested functionality is not yet supported.
  #[error("Functionality \"{0}\" is not yet implemented.")]
  NotImplemented(String),

  /// Raised when something unexpected went wrong in the library.
  #[error("Internal error: {0}")]
  Internal(String),
}

/// A container for a value falling on the range (0.0, 1.0) (exclusive, meaning
/// the values 0.0 and 1.0 are not valid)
///
/// This is useful for safely defining decreasing scale factors.
///
/// Because this type is currently used only for computing the resized
/// dimensions of each level of the pyramid, the choice was made to support only
/// [`f32`]. In the future support may be added for other floating-point types,
/// such as [`f64`], rational values, or fixed-point, if there arises a need for
/// such precision and/or performance.
#[derive(Debug, Copy, Clone)]
pub struct UnitIntervalValue(f32);

/// A trait describing some floating-point type that can be converted to a
/// unit-interval value (0.0 to 1.0, exclusive)
pub trait IntoUnitInterval {
  /// Attempts to convert this value into a guaranteed unit-interval value.
  ///
  /// Returns an error string if the value is not valid.
  ///
  /// # Errors
  /// - The value is not within the unit range
  fn into_unit_interval(self) -> Result<UnitIntervalValue, ImagePyramidError>;
}

impl IntoUnitInterval for f32 {
  fn into_unit_interval(self) -> Result<UnitIntervalValue, ImagePyramidError> {
    match self {
      v if v <= 0.0 || v >= 1.0 => Err(ImagePyramidError::BadScaleFactor(v)),
      _ => Ok(UnitIntervalValue(self)),
    }
  }
}

impl UnitIntervalValue {
  /// Attempts to create a new instance from the provided value
  ///
  /// # Errors
  /// - The value is not within the unit range
  pub fn new<T: IntoUnitInterval>(val: T) -> Result<Self, ImagePyramidError> {
    val.into_unit_interval()
  }

  /// Retrieves the stored value which is guaranteed to fall between 0.0 and 1.0
  /// (exclusive)
  #[must_use]
  pub const fn get(self) -> f32 { self.0 }
}

fn accumulate<P, K>(acc: &mut [K], pixel: &P, weight: K)
where
  P: Pixel,
  <P as Pixel>::Subpixel: Into<K>,
  K: Num + Copy + Debug,
{
  acc
    .iter_mut()
    .zip(pixel.channels().iter())
    .for_each(|(a, c)| {
      let new_val = <<P as Pixel>::Subpixel as Into<K>>::into(*c) * weight;
      *a = *a + new_val;
    });
}

struct Kernel<K> {
  data:   Vec<K>,
  width:  u32,
  height: u32,
}

impl<K: Num + Copy + Debug> Kernel<K> {
  /// Construct a kernel from a slice and its dimensions. The input slice is
  /// in row-major form. For example, a 3x3 matrix with data
  /// `[0,1,0,1,2,1,0,1,0`] describes the following matrix:
  ///
  /// ```text
  /// ┌       ┐
  /// | 0 1 0 |  
  /// | 1 2 1 |  
  /// | 0 1 0 |
  /// └       ┘
  /// ```
  ///
  /// # Errors
  ///
  /// - If `width == 0 || height == 0`, [`ImagePyramidError::Internal`] is
  ///   raised
  /// - If the provided data does not match the size corresponding to the given
  ///   dimensions
  ///
  /// # Panics
  ///
  /// In debug builds, this factory panics under the conditions that [`Err`] is
  /// returned for release builds.
  pub fn new(data: &[K], width: u32, height: u32) -> Result<Self, ImagePyramidError> {
    debug_assert!(width > 0 && height > 0, "width and height must be non-zero");
    debug_assert!(
      (width * height) as usize == data.len(),
      "Invalid kernel len: expecting {}, found {}",
      width * height,
      data.len()
    );
    // Take the above asserts and return Internal error when appropriate
    if width == 0 || height == 0 {
      return Err(ImagePyramidError::Internal(
        "width and height must be non-zero".to_string(),
      ));
    }
    if (width * height) as usize != data.len() {
      return Err(ImagePyramidError::Internal(format!(
        "Invalid kernel len: expecting {}, found {}",
        width * height,
        data.len()
      )));
    }

    Ok(Self {
      data: data.to_vec(),
      width,
      height,
    })
  }

  /// Construct a kernel from a slice and its dimensions, normalizing the data
  /// to sum to 1.0. The input slice is in row-major form. For example, a 3x3
  /// matrix with data `[0,1,0,1,2,1,0,1,0`] describes the following matrix:
  /// ```text
  /// ┌       ┐
  /// | 0 1 0 |
  /// | 1 2 1 | / 6
  /// | 0 1 0 |
  /// └       ┘
  /// ```
  /// ...where `6` is computed dynamically by summing the elements of the
  /// kernel. In other words, all the weights in a normalized kernel sum to
  /// 1.0. This is useful, as many filters have this property
  ///
  /// # Errors
  ///
  /// - If `width == 0 || height == 0`, [`ImagePyramidError::Internal`] is
  ///   raised
  /// - If the provided data does not match the size corresponding to the given
  ///   dimensions
  ///
  /// # Panics
  ///
  /// In debug builds, this factory panics under the conditions that [`Err`] is
  /// returned for release builds.
  pub fn new_normalized(data: &[K], width: u32, height: u32) -> Result<Kernel<f32>, ImagePyramidError>
    where K: Into<f32>
  {
    let mut sum = K::zero();
    for i in data {
      sum = sum + *i;
    }
    let data_norm: Vec<f32> = data.iter().map(|x| <K as Into<f32>>::into(*x) / <K as Into<f32>>::into(sum)).collect();
    Kernel::<f32>::new(&data_norm, width, height)
  }

  /// Returns 2d correlation of an image. Intermediate calculations are
  /// performed at type K, and the results converted to pixel Q via f. Pads by
  /// continuity.
  #[allow(unsafe_code)]
  #[allow(unused)]
  pub fn filter_in_place<I, F>(&self, image: &mut I, mut f: F)
  where
    I: GenericImage + Clone,
    <<I as GenericImageView>::Pixel as Pixel>::Subpixel: Into<K>,
    F: FnMut(&mut <<I as GenericImageView>::Pixel as Pixel>::Subpixel, K),
  {
    use core::cmp::{max, min};
    let (width, height) = image.dimensions();
    let num_channels = <<I as GenericImageView>::Pixel as Pixel>::CHANNEL_COUNT as usize;
    let zero = K::zero();
    let mut acc = vec![zero; num_channels];
    #[allow(clippy::cast_lossless)]
    let (k_width, k_height) = (self.width as i64, self.height as i64);
    #[allow(clippy::cast_lossless)]
    let (width, height) = (width as i64, height as i64);

    for y in 0..height {
      for x in 0..width {
        #[allow(clippy::cast_possible_truncation)]
        #[allow(clippy::cast_sign_loss)]
        let x_u32 = x as u32;
        #[allow(clippy::cast_possible_truncation)]
        #[allow(clippy::cast_sign_loss)]
        let y_u32 = y as u32;
        for k_y in 0..k_height {
          #[allow(clippy::cast_possible_truncation)]
          #[allow(clippy::cast_sign_loss)]
          let y_p = clamp(y + k_y - k_height / 2, 0, height - 1) as u32;
          for k_x in 0..k_width {
            #[allow(clippy::cast_possible_truncation)]
            #[allow(clippy::cast_sign_loss)]
            let x_p = clamp(x + k_x - k_width / 2, 0, width - 1) as u32;
            #[allow(clippy::cast_possible_truncation)]
            #[allow(clippy::cast_sign_loss)]
            let k_idx = (k_y * k_width + k_x) as usize;

            accumulate(
              &mut acc,
              unsafe { &image.unsafe_get_pixel(x_p, y_p) },
              unsafe { *self.data.get_unchecked(k_idx) },
            );
          }
        }
        let mut out_pel = image.get_pixel(x_u32, y_u32);
        let out_channels = out_pel.channels_mut();
        for (a, c) in acc.iter_mut().zip(out_channels.iter_mut()) {
          f(c, *a);
          *a = zero;
        }
        image.put_pixel(x_u32, y_u32, out_pel);
      }
    }
  }
}

/// A simple wrapper extending the functionality of the given image with
/// image-pyramid support
pub struct ImageToProcess<'a>(pub &'a DynamicImage);

/// How to smooth an image when downsampling
///
/// For now, these all use a 3x3 kernel for smoothing. As a consequence, the
/// Gaussian and Triangle smoothing types produce identical results
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum SmoothingType {
  /// Use a Gaussian filter
  /// `[[1,2,1],[2,4,2],[1,2,1]] * 1/16`
  Gaussian,
  /// Use a linear box filter
  /// `[[1,1,1],[1,1,1],[1,1,1]] * 1/9`
  Box,
  /// Use a linear triangle filter:
  /// `[[1,2,1],[2,4,2],[1,2,1]] * 1/16`
  Triangle,
}

/// What type of pyramid to compute. Each has different properties,
/// applications, and computation cost.
#[derive(Debug, Clone)]
pub enum ImagePyramidType {
  /// Use smoothing & subsampling to compute pyramid. This is used to generate
  /// mipmaps, thumbnails, display low-resolution previews of expensive image
  /// processing operations, texture synthesis, and more.
  Lowpass,

  /// AKA Laplacian pyramid, where adjacent levels of the lowpass pyramid are
  /// upscaled and their pixel differences are computed. This used in image
  /// processing routines such as blending.
  Bandpass,

  /// Uses a bank of multi-orientation bandpass filters. Used for used for
  /// applications including image compression, texture synthesis, and object
  /// recognition.
  Steerable,
}

/// The set of parameters required for computing an image pyramid. For most
/// applications, the default set of parameters is correct.
#[derive(Debug, Clone)]
pub struct ImagePyramidParams {
  /// The scale factor to use on image dimensions when downsampling. This is
  /// most commonly 0.5
  pub scale_factor: UnitIntervalValue,

  /// What type of pyramid to compute. See [`ImagePyramidType`] for more
  /// information.
  pub pyramid_type: ImagePyramidType,

  /// What type of smoothing to use when computing pyramid levels. See
  /// [`SmoothingType`] for more information.
  pub smoothing_type: SmoothingType,
}

/// Generates a useful default set of parameters.
///
/// Defaults to a traditional image pyramid: Gaussian lowpass image pyramid with
/// scale factor of 0.5.
impl Default for ImagePyramidParams {
  fn default() -> Self {
    Self {
      scale_factor:   UnitIntervalValue::new(0.5).unwrap(),
      pyramid_type:   ImagePyramidType::Lowpass,
      smoothing_type: SmoothingType::Gaussian,
    }
  }
}

/// A computed image pyramid and its associated metadata.
///
/// Image pyramids consist of multiple, successively smaller-scale versions of
/// an original image. These are called the _levels_ (sometimes called
/// _octaves_) of the image pyramid.
///
/// Closely related to a traditional Gaussian image pyramid is a mipmap, which
/// is a specific application of the more general image pyramid concept. A
/// mipmap is essentially a way of storing a `scale=0.5` lowpass image pyramid
/// such that an appropriate octave can be sampled by a graphics renderer, for
/// the purpose of avoiding anti-aliasing.
pub struct ImagePyramid {
  /// The ordered levels of the pyramid. Index N refers to pyramid level N.
  /// Depending on the scale factor S in `params`, and image dimensions `(W,
  /// H)`, there will be `ceil(log_{1/S}(min(W, H)))` levels.
  ///
  /// For example, a `(800, 600)` image with scale factor `S=0.5` will have
  /// `ceil(log_2(600))` levels, which comes out to `10`. Similarly, a `(640,
  /// 480)` image would have `(ceil(log_2(480))` (`9`) levels.
  pub levels: Vec<DynamicImage>,

  /// A copy of the parameters used to compute the levels in this pyramid.
  pub params: ImagePyramidParams,
}

impl ImagePyramid {
  /// Create a new image pyramid for the given image, using the optionally
  /// provided parameters.
  ///
  /// If no parameters are passed, the default parameters will be used.
  ///
  /// # Errors
  /// See [`CanComputePyramid::compute_image_pyramid`] for errors that may be
  /// raised
  pub fn create(
    image: &DynamicImage,
    params: Option<&ImagePyramidParams>,
  ) -> Result<Self, ImagePyramidError> {
    let image_to_process = ImageToProcess(image);
    let pyramid = image_to_process.compute_image_pyramid(params)?;

    Ok(pyramid)
  }
}

/// Describes types that can compute their own image pyramid
pub trait CanComputePyramid {
  /// Compute an image pyramid for this instance's data, using the optionally
  /// provided parameters.
  ///
  /// If no parameters are passed, the default parameters will be used.
  ///
  /// # Errors
  /// Errors of type [`ImagePyramidError::NotImplemented`] are raised for the
  /// following parameter values, which are not yet implemented:
  ///
  /// - [`SmoothingType::Box`] - This smoothing type is not yet supported in the
  ///   `image` crate and is also not yet implemented manually
  /// - [`ImagePyramidType::Steerable`] - Not yet implemented
  fn compute_image_pyramid(
    &self,
    params: Option<&ImagePyramidParams>,
  ) -> Result<ImagePyramid, ImagePyramidError>;
}

impl<'a> CanComputePyramid for ImageToProcess<'a> {
  fn compute_image_pyramid(
    &self,
    params: Option<&ImagePyramidParams>,
  ) -> Result<ImagePyramid, ImagePyramidError> {
    /// Compute a lowpass pyramid with the given params. Ignores
    /// `params.pyramid_type`.
    fn compute_lowpass_pyramid(
      image: &DynamicImage,
      params: &ImagePyramidParams,
    ) -> Result<Vec<DynamicImage>, ImagePyramidError> {
      let mut levels = vec![image.clone()];
      let kernel = match params.smoothing_type {
        SmoothingType::Gaussian => Kernel::new_normalized(&[1u8, 2, 3, 2, 4, 2, 1, 2, 1], 3, 3)?,
        SmoothingType::Box => Kernel::new_normalized(&[1u8, 1, 1, 1, 1, 1, 1, 1, 1], 3, 3)?,
        SmoothingType::Triangle => Kernel::new_normalized(&[1u8, 2, 1, 2, 4, 2, 1, 2, 1], 3, 3)?,
      };
      let mut current_level = image.clone();
      #[allow(clippy::cast_possible_truncation)]
      #[allow(clippy::cast_precision_loss)]
      #[allow(clippy::cast_sign_loss)]
      while current_level.width() > 1 && current_level.height() > 1 {
        kernel.filter_in_place(&mut current_level, |c, a| *c = a as u8);
        current_level = current_level.resize_exact(
          (current_level.width() as f32 * params.scale_factor.get()) as u32,
          (current_level.height() as f32 * params.scale_factor.get()) as u32,
          image::imageops::FilterType::Gaussian,
        );
        levels.push(current_level.clone());
      }
      Ok(levels)
    }

    /// Takes the diference in pixel values between `image` and `other`, adds
    /// that value to the center of the Subpixel container type's range, and
    /// applies the result to `image`.
    fn bandpass_in_place<I>(image: &mut I, other: &I)
    where I: GenericImage {
      use image::Primitive;
      type Subpixel<I> = <<I as GenericImageView>::Pixel as Pixel>::Subpixel;
      // The center value for the given container type. We leverage the `image`
      // crate's definition of these values to be 1.0 and 0.0 respectively, for
      // floating-point types (where we want `mid_val` to be 0.5). For unsigned
      // integer types, we should get half the primitive container's capacity
      // (e.g. 127 for a u8)
      let mid_val = ((Subpixel::<I>::DEFAULT_MAX_VALUE - Subpixel::<I>::DEFAULT_MIN_VALUE)
        / NumCast::from(2).unwrap())
        + Subpixel::<I>::DEFAULT_MIN_VALUE;
      debug_assert_eq!(image.dimensions(), other.dimensions());
      // Iterate through pixels and compute difference. Add difference to
      // mid_val and apply that to i1
      let (width, height) = image.dimensions();
      for y in 0..height {
        for x in 0..width {
          let other_p = other.get_pixel(x, y);
          let mut p = image.get_pixel(x, y);
          p.apply2(&other_p, |b1, b2| {
            let diff = <f32 as NumCast>::from(b1).unwrap() - <f32 as NumCast>::from(b2).unwrap();
            let new_val = <f32 as NumCast>::from(mid_val).unwrap() + diff;
            NumCast::from(new_val).unwrap_or(mid_val)
          });
          image.put_pixel(x, y, p);
        }
      }
    }

    // If unspecified, use default parameters.
    let params = params.map_or_else(ImagePyramidParams::default, std::clone::Clone::clone);

    match params.pyramid_type {
      ImagePyramidType::Lowpass =>
        Ok(ImagePyramid {
          levels: compute_lowpass_pyramid(self.0, &params)?,
          params: params.clone(),
        }),
      ImagePyramidType::Bandpass => {
        // First, we need a lowpass pyramid to work with.
        let mut levels = compute_lowpass_pyramid(self.0, &params)?;

        // For each index N, upscale the resolution N+1 to match N's resolution.
        // Then we compute the pixel-wise difference between them, and
        // store the result in the current level
        for i in 0..levels.len() - 1 {
          let next_level = levels[i + 1].resize_exact(
            levels[i].width(),
            levels[i].height(),
            image::imageops::FilterType::Nearest,
          );
          bandpass_in_place(&mut levels[i], &next_level);
        }

        Ok(ImagePyramid {
          levels,
          params,
        })
      }
      ImagePyramidType::Steerable =>
        Err(ImagePyramidError::NotImplemented(
          "ImagePyramidType::Steerable".to_string(),
        )),
    }
  }
}

#[cfg(test)]
mod tests {
  use test_case::test_matrix;

  use super::*;

  #[test]
  fn kernel_filter_in_place() {
    let mut image = DynamicImage::new_rgb8(3, 3);
    let mut other = DynamicImage::new_rgb8(3, 3);
    let mut i = 0;
    for y in 0..3 {
      for x in 0..3 {
        let mut pel = image.get_pixel(x, y);
        pel.apply_without_alpha(|_| i);
        image.put_pixel(x, y, pel);

        let mut pel = other.get_pixel(x, y);
        pel.apply_without_alpha(|_| i + 1);
        other.put_pixel(x, y, pel);
        i += 1;
      }
    }
    let kernel = Kernel::new_normalized(&[1u8, 2, 1, 2, 4, 2, 1, 2, 1], 3, 3).unwrap();
    kernel.filter_in_place(&mut image, |c, a| *c = a as u8);
    assert_eq!(image.get_pixel(1, 1), image::Rgba::<u8>([4, 4, 4, 255]));
  }

  #[test]
  fn compute_image_pyramid_imagepyramidtype_steerable_unimplemented() {
    let image = DynamicImage::new_rgb8(640, 480);
    let ipr = ImageToProcess(&image);

    let params = ImagePyramidParams {
      pyramid_type: ImagePyramidType::Steerable,
      ..Default::default()
    };

    let pyramid = ipr.compute_image_pyramid(Some(&params));
    assert!(pyramid.is_err());
  }

  #[test_matrix(
    [ImagePyramidType::Lowpass, ImagePyramidType::Bandpass],
    [SmoothingType::Gaussian, SmoothingType::Triangle, SmoothingType::Box]
  )]
  #[allow(clippy::needless_pass_by_value)]
  fn compute_image_pyramid_every_type(
    pyramid_type: ImagePyramidType,
    smoothing_type: SmoothingType,
  ) {
    // test_case crate won't let these be parameterized so we loop through them
    // here.
    let functors = vec![
      DynamicImage::new_luma16,
      DynamicImage::new_luma8,
      DynamicImage::new_luma_a16,
      DynamicImage::new_luma_a8,
      DynamicImage::new_rgb16,
      DynamicImage::new_rgb8,
      DynamicImage::new_rgb32f,
      DynamicImage::new_rgba16,
      DynamicImage::new_rgba8,
      DynamicImage::new_rgba32f,
    ];
    for functor in functors {
      let image = functor(128, 128);
      let ipr = ImageToProcess(&image);

      let params = ImagePyramidParams {
        pyramid_type: pyramid_type.clone(),
        smoothing_type: smoothing_type.clone(),
        ..Default::default()
      };

      let pyramid = ipr.compute_image_pyramid(Some(&params));
      assert!(pyramid.is_ok());
      let pyramid = pyramid.unwrap();
      assert_eq!(pyramid.levels.len(), 8);
    }
  }

  #[test]
  fn into_unit_interval_f32() {
    let i = 0.5f32.into_unit_interval();
    assert!(i.is_ok());
    assert_eq!(0.5f32, i.unwrap().get());
  }

  #[test]
  fn into_unit_interval_err_when_0_0f32() {
    let i = 0.0f32.into_unit_interval();
    assert!(i.is_err());
  }

  #[test]
  fn into_unit_interval_err_when_1_0f32() {
    let i = 1.0f32.into_unit_interval();
    assert!(i.is_err());
  }
}