ndarray_ndimage/filters/
mod.rs

1use crate::PadMode;
2
3pub mod con_corr;
4pub mod gaussian;
5pub mod median;
6pub mod min_max;
7pub mod symmetry;
8
9// TODO We might want to offer all NumPy mode (use PadMode instead)
10/// Method that will be used to determines how the input array is extended beyond its boundaries.
11#[derive(Copy, Clone, Debug, PartialEq)]
12pub enum BorderMode<T> {
13    /// The input is extended by filling all values beyond the edge with the same constant value,
14    ///
15    /// `[1, 2, 3] -> [T, T, 1, 2, 3, T, T]`
16    Constant(T),
17
18    /// The input is extended by reflecting about the center of the last pixel.
19    ///
20    /// `[1, 2, 3] -> [3, 2, 1, 2, 3, 2, 1]`
21    Mirror,
22
23    /// The input is extended by replicating the last pixel.
24    ///
25    /// `[1, 2, 3] -> [1, 1, 1, 2, 3, 3, 3]`
26    Nearest,
27
28    /// The input is extended by reflecting about the edge of the last pixel.
29    ///
30    /// `[1, 2, 3] -> [2, 1, 1, 2, 3, 3, 2]`
31    Reflect,
32
33    /// The input is extended by wrapping around to the opposite edge.
34    ///
35    /// `[1, 2, 3] -> [2, 3, 1, 2, 3, 1, 2]`
36    Wrap,
37}
38
39impl<T: Copy> BorderMode<T> {
40    fn to_pad_mode(&self) -> PadMode<T> {
41        match *self {
42            BorderMode::Constant(t) => PadMode::Constant(t),
43            BorderMode::Nearest => PadMode::Edge,
44            BorderMode::Mirror => PadMode::Reflect,
45            BorderMode::Reflect => PadMode::Symmetric,
46            BorderMode::Wrap => PadMode::Wrap,
47        }
48    }
49}
50
51fn origin_check(len: usize, origin: isize, left: usize, right: usize) -> [usize; 2] {
52    let len = len as isize;
53    assert!(
54        origin >= -len / 2 && origin <= (len - 1) / 2,
55        "origin must satisfy: -(len(weights) / 2) <= origin <= (len(weights) - 1) / 2"
56    );
57    [(left as isize + origin) as usize, (right as isize - origin) as usize]
58}