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
//! Contiguous iterator.

use crate::{
    geometry::{Point, Size},
    pixelcolor::PixelColor,
    primitives::{rectangle, PointsIter, Rectangle},
    Pixel,
};
use core::iter::Zip;

/// Converts a contiguous iterator into a pixel iterator.
#[derive(Debug)]
pub struct IntoPixels<I>
where
    I: Iterator,
    I::Item: PixelColor,
{
    iter: Zip<rectangle::Points, I>,
}

impl<I> IntoPixels<I>
where
    I: Iterator,
    I::Item: PixelColor,
{
    pub(super) fn new(iter: I, bounding_box: Rectangle) -> Self {
        Self {
            iter: bounding_box.points().zip(iter),
        }
    }
}

impl<I> Iterator for IntoPixels<I>
where
    I: Iterator,
    I::Item: PixelColor,
{
    type Item = Pixel<I::Item>;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        self.iter.next().map(|(p, c)| Pixel(p, c))
    }
}

/// Crops a part of the underlying iterator.
#[derive(Debug)]
pub(crate) struct Cropped<I>
where
    I: Iterator,
    I::Item: PixelColor,
{
    iter: I,

    x: u32,
    y: u32,
    size: Size,
    row_skip: usize,
}

impl<I> Cropped<I>
where
    I: Iterator,
    I::Item: PixelColor,
{
    pub(crate) fn new(mut iter: I, size: Size, crop_area: &Rectangle) -> Self {
        let crop_area = Rectangle::new(Point::zero(), size).intersection(crop_area);

        let initial_skip =
            crop_area.top_left.y as usize * size.width as usize + crop_area.top_left.x as usize;

        if initial_skip > 0 {
            iter.nth(initial_skip - 1);
        }

        Self {
            iter,
            x: 0,
            y: 0,
            size: crop_area.size,
            row_skip: (size.width - crop_area.size.width) as usize,
        }
    }
}

impl<I> Iterator for Cropped<I>
where
    I: Iterator,
    I::Item: PixelColor,
{
    type Item = I::Item;

    fn next(&mut self) -> Option<Self::Item> {
        if self.y >= self.size.height || self.size.width == 0 {
            return None;
        }

        if self.x < self.size.width {
            self.x += 1;

            self.iter.next()
        } else {
            self.x = 1;
            self.y += 1;

            if self.y < self.size.height {
                self.iter.nth(self.row_skip)
            } else {
                None
            }
        }
    }
}

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

    #[test]
    fn cropped() {
        let parent = (0..=255).map(Gray8::new);
        let parent_size = Size::new(16, 16);

        let crop_area = Rectangle::new(Point::new(2, 3), Size::new(4, 3));

        let mut cropped_iter = Cropped::new(parent, parent_size, &crop_area);

        let expected = &[
            50, 51, 52, 53, //
            66, 67, 68, 69, //
            82, 83, 84, 85, //
        ];

        for value in expected {
            assert_eq!(cropped_iter.next(), Some(Gray8::new(*value)));
        }
        assert_eq!(cropped_iter.next(), None);
    }

    #[test]
    fn cropped_empty() {
        let parent = (0..=255).map(Gray8::new);
        let parent_size = Size::new(16, 16);

        let crop_area = Rectangle::zero();

        let mut cropped_iter = Cropped::new(parent, parent_size, &crop_area);

        assert_eq!(cropped_iter.next(), None);
    }

    #[test]
    fn cropped_overlapping() {
        let parent = (0..=255).map(Gray8::new);
        let parent_size = Size::new(16, 16);

        let crop_area = Rectangle::new(Point::new(14, 10), Size::new(4, 4));

        let mut cropped_iter = Cropped::new(parent, parent_size, &crop_area);

        let expected = &[
            174, 175, //
            190, 191, //
            206, 207, //
            222, 223, //
        ];

        for value in expected {
            assert_eq!(cropped_iter.next(), Some(Gray8::new(*value)));
        }
        assert_eq!(cropped_iter.next(), None);
    }
}