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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
use std::fmt;
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Image {
pub pixels: Vec<u8>,
}
impl Image {
pub fn invert(&mut self) {
self.pixels = self.pixels.iter().map(|pixel| 1 - pixel).collect();
}
pub fn flip(&mut self) {
let mut pixels = Vec::with_capacity(64);
for row in self.pixels.chunks(8).rev() {
for pixel in row {
pixels.push(*pixel);
}
}
self.pixels = pixels;
}
pub fn mirror(&mut self) {
let mut pixels = Vec::with_capacity(64);
for row in self.pixels.chunks(8) {
for i in (0..8).rev() {
pixels.push(row[i]);
}
}
self.pixels = pixels;
}
pub fn rotate(&mut self) {
let mut pixels = Vec::with_capacity(64);
for x in 0..8 {
for y in (0..8).rev() {
pixels.push(self.pixels[(y * 8) + x]);
}
}
self.pixels = pixels;
}
fn from_str(str: &str) -> Result<(Image, Vec<crate::Error>), crate::Error> {
let mut warnings = Vec::new();
if str.contains("NaN") {
warnings.push(crate::Error::Image);
}
let string = str.trim().replace("NaN", "0");
let lines: Vec<&str> = string.lines().collect();
let dimension = lines.len();
let mut pixels: Vec<u8> = Vec::new();
for line in lines {
let line = &line[..dimension];
for char in line.chars().into_iter() {
pixels.push(match char {'1' => 1, _ => 0});
}
}
if [64, 256].contains(&pixels.len()) {
Ok((Image { pixels }, warnings))
} else {
Err(crate::Error::Image)
}
}
}
impl fmt::Display for Image {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut string = String::new();
let sqrt = (self.pixels.len() as f64).sqrt() as usize;
for line in self.pixels.chunks(sqrt) {
for pixel in line {
string.push_str(&format!("{}", *pixel));
}
string.push('\n');
}
string.pop();
write!(f, "{}", string)
}
}
pub fn animation_frames_from_str(str: &str) -> Vec<Image> {
str
.split('>')
.collect::<Vec<&str>>()
.iter()
.map(|&frame| Image::from_str(frame).unwrap().0)
.collect()
}
#[cfg(test)]
mod test {
use crate::image::{Image, animation_frames_from_str};
use crate::mock;
#[test]
fn image_from_string() {
let (output, _) = Image::from_str(include_str!("test-resources/image")).unwrap();
let expected = Image {
pixels: vec![
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 0, 0, 1, 1, 1, 1,
1, 0, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
],
};
assert_eq!(output, expected);
}
#[test]
fn image_to_string() {
let output = mock::image::chequers_1().to_string();
let expected = include_str!("test-resources/image-chequers-1").to_string();
assert_eq!(output, expected);
}
#[test]
fn test_animation_frames_from_string() {
let output = animation_frames_from_str(
include_str!("test-resources/animation_frames")
);
let expected = mock::image::animation_frames();
assert_eq!(output, expected);
}
#[test]
fn image_out_of_bounds() {
let (output, _) = Image::from_str(include_str!("test-resources/image-oob")).unwrap();
let expected = Image {
pixels: vec![
1,1,1,1,1,1,1,1,
1,1,0,0,1,1,1,1,
1,0,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,
0,0,0,0,0,0,0,0,
]
};
assert_eq!(output, expected);
}
#[test]
fn invert() {
let mut output = crate::mock::image::chequers_1();
output.invert();
let expected = crate::mock::image::chequers_2();
assert_eq!(output, expected);
}
#[test]
fn flip() {
let mut image = crate::mock::image::asymmetrical();
image.flip();
let flipped = Image { pixels: vec![
0,0,0,1,0,0,0,0,
0,0,1,0,0,0,0,0,
0,1,0,0,0,0,0,0,
1,0,0,0,0,0,0,0,
1,0,0,0,0,0,0,0,
0,1,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
]};
assert_eq!(image, flipped);
}
#[test]
fn mirror() {
let mut image = crate::mock::image::asymmetrical();
image.mirror();
let mirrored = Image { pixels: vec![
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,1,0,
0,0,0,0,0,0,0,1,
0,0,0,0,0,0,0,1,
0,0,0,0,0,0,1,0,
0,0,0,0,0,1,0,0,
0,0,0,0,1,0,0,0,
]};
assert_eq!(image, mirrored);
}
#[test]
fn rotate() {
let mut image = crate::mock::image::asymmetrical();
image.rotate();
let rotated = Image { pixels: vec![
0,0,0,1,1,0,0,0,
0,0,1,0,0,1,0,0,
0,1,0,0,0,0,0,0,
1,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
]};
assert_eq!(image, rotated);
}
}