-
Notifications
You must be signed in to change notification settings - Fork 0
/
pixels.rs
175 lines (144 loc) · 4.51 KB
/
pixels.rs
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
use crate::{Color, Stamp};
use core::iter::FusedIterator;
/// An iterator that yields all pixels of a [`Stamp`].
///
/// This type is created by the [`pixels`](Stamp::pixels) method on [`Stamp`]. See
/// its documentation for more details.
#[derive(Debug, Clone)]
pub struct Pixels<'a> {
cursor: Cursor<'a>,
cursor_back: CursorBack<'a>,
remaining: usize,
}
impl<'a> Pixels<'a> {
pub(crate) fn new(stamp: &'a Stamp) -> Self {
Self {
cursor: Cursor::new(stamp),
cursor_back: CursorBack::new(stamp),
remaining: stamp.pixel_count(),
}
}
}
/// An iterator that cycles throygh all pixels of a [`Stamp`] from front to back.
#[derive(Debug, Clone)]
struct Cursor<'a> {
x: usize,
y: usize,
stamp: &'a Stamp,
}
impl<'a> Cursor<'a> {
fn new(stamp: &'a Stamp) -> Self {
Self { x: 0, y: 0, stamp }
}
}
impl Iterator for Cursor<'_> {
type Item = (usize, usize, Color);
fn next(&mut self) -> Option<(usize, usize, Color)> {
let color = self.stamp.get_color_checked(self.x, self.y)?;
let res = (self.x, self.y, color);
self.x += 1;
if self.x == self.stamp.width() {
self.x = 0;
self.y += 1;
if self.y == self.stamp.height() {
self.y = 0;
}
}
Some(res)
}
}
/// An iterator that cycles throygh all pixels of a [`Stamp`] from back to front.
#[derive(Debug, Clone)]
struct CursorBack<'a> {
x: usize,
y: usize,
stamp: &'a Stamp,
}
impl<'a> CursorBack<'a> {
fn new(stamp: &'a Stamp) -> Self {
Self {
x: stamp.width().saturating_sub(1),
y: stamp.height().saturating_sub(1),
stamp,
}
}
}
impl Iterator for CursorBack<'_> {
type Item = (usize, usize, Color);
fn next(&mut self) -> Option<(usize, usize, Color)> {
let color = self.stamp.get_color_checked(self.x, self.y)?;
let res = (self.x, self.y, color);
match self.x.checked_sub(1) {
Some(x) => self.x = x,
None => {
self.x = self.stamp.width().saturating_sub(1);
match self.y.checked_sub(1) {
Some(y) => self.y = y,
None => self.y = self.stamp.height().saturating_sub(1),
}
}
}
Some(res)
}
}
impl Iterator for Pixels<'_> {
type Item = (usize, usize, Color);
fn next(&mut self) -> Option<Self::Item> {
self.remaining = self.remaining.checked_sub(1)?;
self.cursor.next()
}
fn size_hint(&self) -> (usize, Option<usize>) {
(self.remaining, Some(self.remaining))
}
}
impl DoubleEndedIterator for Pixels<'_> {
fn next_back(&mut self) -> Option<Self::Item> {
self.remaining = self.remaining.checked_sub(1)?;
self.cursor_back.next()
}
}
impl ExactSizeIterator for Pixels<'_> {}
impl FusedIterator for Pixels<'_> {}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_zero_size_stamp() {
let stamp = unsafe { Stamp::from_raw(0, 0, [].as_ptr()) };
let mut pixels = stamp.pixels();
assert_eq!(pixels.next(), None);
}
#[test]
fn test_zero_width_stamp() {
let stamp = unsafe { Stamp::from_raw(0, 3, [].as_ptr()) };
let mut pixels = stamp.pixels();
assert_eq!(pixels.next(), None);
}
#[test]
fn test_zero_height_stamp() {
let stamp = unsafe { Stamp::from_raw(3, 0, [].as_ptr()) };
let mut pixels = stamp.pixels();
assert_eq!(pixels.next(), None);
}
#[test]
fn test_double_ended() {
let stamp = unsafe { Stamp::from_raw(2, 2, [0b1010_0000].as_ptr()) };
let mut pixels = stamp.pixels();
assert_eq!(pixels.next(), Some((0, 0, Color::White)));
assert_eq!(pixels.next_back(), Some((1, 1, Color::Black)));
assert_eq!(pixels.next_back(), Some((0, 1, Color::White)));
assert_eq!(pixels.next(), Some((1, 0, Color::Black)));
assert_eq!(pixels.next(), None);
assert_eq!(pixels.next_back(), None);
}
#[test]
fn test_rev() {
let stamp = unsafe { Stamp::from_raw(2, 2, [0b1010_0000].as_ptr()) };
let mut pixels = stamp.pixels().rev();
assert_eq!(pixels.next(), Some((1, 1, Color::Black)));
assert_eq!(pixels.next(), Some((0, 1, Color::White)));
assert_eq!(pixels.next(), Some((1, 0, Color::Black)));
assert_eq!(pixels.next(), Some((0, 0, Color::White)));
assert_eq!(pixels.next(), None);
}
}