flipping/mirroring images
This commit is contained in:
66
src/image.rs
66
src/image.rs
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
13
src/mock.rs
13
src/mock.rs
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user