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