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
use super::WindowGenerator;
use crate::types::ImageWindow;
use image::{DynamicImage, GenericImageView, SubImage};

/// Struct for generating sliding windows over an image
#[derive(Debug, PartialEq)]
pub struct SlidingWindow {
    /// Width of the window
    pub width: u32,
    /// Height of the window
    pub height: u32,
    /// Step size for moving the window
    pub step_size: u32,
}

// let x_border = if self.width - self.step_size > 0 {
//    self.width
// } else {
//    0
// };

impl WindowGenerator<DynamicImage> for SlidingWindow {
    fn windows<'a, 'b>(
        &'a self,
        image: &'b DynamicImage,
    ) -> Vec<ImageWindow<SubImage<&'b DynamicImage>>> {
        let mut windows = Vec::new();
        let (width, height) = image.dimensions();

        let last_step_y = last_position(height, self.step_size);

        let last_y = if last_step_y + self.height > height {
            height - self.height
        } else {
            last_step_y + self.height
        };

        let last_step_x = last_position(width, self.step_size);

        let last_x = if last_step_x + self.width > width {
            width - self.width
        } else {
            last_step_x + self.width
        };

        for y in (0..last_y).step_by(self.step_size as usize) {
            for x in (0..last_x).step_by(self.step_size as usize) {
                let window = ImageWindow {
                    x,
                    y,
                    view: image.view(x, y, self.width, self.height),
                };
                windows.push(window);
            }
        }
        windows
    }
}

fn last_position(length: u32, step_size: u32) -> u32 {
    if length == 0 || step_size == 0 {
        return 0;
    }
    (length - 1) / step_size * step_size
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_sliding_window() {
        let image = DynamicImage::new_rgb8(10, 10);
        let window_size = 5;
        let step_size = 5;

        let generator = SlidingWindow {
            width: window_size,
            height: window_size,
            step_size: step_size,
        };

        let windows = generator.windows(&image);

        // Check that the correct number of windows was returned
        assert_eq!(windows.len(), 4);

        // Check that the positions and subimages of the returned windows are correct
        assert_eq!(windows[0].x, 0);
        assert_eq!(windows[0].x, 0);
        assert_eq!(windows[1].x, 5);
        assert_eq!(windows[2].y, 5);
        assert_eq!(windows[3].x, 5);
        assert_eq!(windows[3].y, 5);
    }

    #[test]
    fn test_sliding_window_step_size() {
        let image = DynamicImage::new_rgb8(21, 21);
        let window_size = 10;
        let step_size = 5;

        let generator = SlidingWindow {
            width: window_size,
            height: window_size,
            step_size: step_size,
        };

        let windows = generator.windows(&image);

        // Check that the correct number of windows was returned
        assert_eq!(windows.len(), 9);

        // Check that the positions and subimages of the returned windows are correct
        assert_eq!(windows[0].x, 0);
        assert_eq!(windows[1].x, 5);
        assert_eq!(windows[2].y, 0);
        assert_eq!(windows[3].x, 0);
        assert_eq!(windows[3].y, 5);
    }

    #[test]
    fn test_sliding_window_step_size_7() {
        let image = DynamicImage::new_rgb8(10, 10);
        let window_size = 7;
        let step_size = 7;

        let generator = SlidingWindow {
            width: window_size,
            height: window_size,
            step_size: step_size,
        };
        let windows = generator.windows(&image);
        assert_eq!(windows.len(), 1);
        assert_eq!(windows[0].x, 0);
    }

    #[test]
    fn test_sliding_window_size_greater_than_step_size() {
        let image = DynamicImage::new_rgb8(12, 12);
        let window_size = 5;
        let step_size = 4;

        let generator = SlidingWindow {
            width: window_size,
            height: window_size,
            step_size: step_size,
        };
        let windows = generator.windows(&image);
        assert_eq!(windows.len(), 4);
    }
}