flipping/mirroring images

This commit is contained in:
Max Bradbury 2020-07-20 14:42:33 +01:00
parent c01a36cfb4
commit 24e11362a0
2 changed files with 79 additions and 0 deletions

View File

@ -3,6 +3,34 @@ pub struct Image {
pub pixels: Vec<u8>, // 64 for SD, 256 for HD
}
impl Image {
/// flip image vertically
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;
}
/// mirror image horizontally
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;
}
}
impl From<String> for Image {
fn from(string: String) -> Image {
let string = string.replace("NaN", "0");
@ -114,4 +142,42 @@ mod test {
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 test_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);
}
}

View File

@ -24,6 +24,19 @@ pub mod image {
}
}
pub fn asymmetrical() -> Image {
Image { pixels: vec![
0,0,0,0,0,0,0,0,
0,0,0,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,1,0,0,0,0,0,
0,0,0,1,0,0,0,0,
]}
}
pub fn animation_frames() -> Vec<Image> {
vec![
Image {