bitsy-parser/src/image.rs

230 lines
5.9 KiB
Rust
Raw Normal View History

use std::fmt;
2020-05-31 15:12:23 +00:00
#[derive(Clone, Debug, Eq, PartialEq)]
2020-04-12 11:48:07 +00:00
pub struct Image {
2020-04-29 07:23:51 +00:00
pub pixels: Vec<u8>, // 64 for SD, 256 for HD
2020-04-12 11:48:07 +00:00
}
2020-07-20 13:42:33 +00:00
impl Image {
2020-07-20 20:05:25 +00:00
pub fn invert(&mut self) {
self.pixels = self.pixels.iter().map(|pixel| 1 - pixel).collect();
}
2020-07-20 13:42:33 +00:00
/// flip image vertically
2020-07-20 18:52:31 +00:00
pub fn flip(&mut self) {
2020-07-20 13:42:33 +00:00
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
2020-07-20 18:52:31 +00:00
pub fn mirror(&mut self) {
2020-07-20 13:42:33 +00:00
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;
}
2020-07-20 14:07:26 +00:00
/// rotate image 90° clockwise
2020-07-20 18:52:31 +00:00
pub fn rotate(&mut self) {
2020-07-20 14:07:26 +00:00
let mut pixels = Vec::with_capacity(64);
// start from bottom-left corner, work upward in that column,
// then work on the next column to the right
2020-07-20 14:10:03 +00:00
// for counter-clockwise, just reverse x instead of y
2020-07-20 14:07:26 +00:00
for x in 0..8 {
for y in (0..8).rev() {
pixels.push(self.pixels[(y * 8) + x]);
}
}
self.pixels = pixels;
}
2020-07-20 13:42:33 +00:00
fn from_str(str: &str) -> Result<Image, crate::Error> {
let string = str.trim().replace("NaN", "0");
2020-04-29 07:23:51 +00:00
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});
}
}
2020-04-12 11:48:07 +00:00
Ok(Image { pixels })
2020-04-12 11:48:07 +00:00
}
}
impl fmt::Display for Image {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2020-04-12 11:48:07 +00:00
let mut string = String::new();
let sqrt = (self.pixels.len() as f64).sqrt() as usize; // 8 for SD, 16 for HD
for line in self.pixels.chunks(sqrt) {
for pixel in line {
string.push_str(&format!("{}", *pixel));
}
string.push('\n');
}
string.pop(); // remove trailing newline
write!(f, "{}", string)
2020-04-12 11:48:07 +00:00
}
}
/// todo return Result<(Vec<Image>, Vec<crate::Error>), crate::Error>
pub fn animation_frames_from_str(str: &str) -> Vec<Image> {
str
.split('>')
.collect::<Vec<&str>>()
.iter()
.map(|&frame| Image::from_str(frame).unwrap())
.collect()
2020-04-29 07:27:35 +00:00
}
2020-04-19 07:13:55 +00:00
#[cfg(test)]
mod test {
use crate::image::{Image, animation_frames_from_str};
2020-04-29 07:27:35 +00:00
use crate::mock;
2020-04-19 07:13:55 +00:00
#[test]
2020-07-26 11:37:41 +00:00
fn image_from_string() {
let output = Image::from_str(include_str!("test-resources/image")).unwrap();
2020-04-19 07:13:55 +00:00
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,
],
};
2020-04-12 11:48:07 +00:00
2020-04-19 07:13:55 +00:00
assert_eq!(output, expected);
}
#[test]
2020-07-26 11:37:41 +00:00
fn image_to_string() {
2020-04-29 07:27:35 +00:00
let output = mock::image::chequers_1().to_string();
2020-04-19 07:13:55 +00:00
let expected = include_str!("test-resources/image-chequers-1").to_string();
assert_eq!(output, expected);
}
2020-04-29 07:27:35 +00:00
#[test]
fn test_animation_frames_from_string() {
let output = animation_frames_from_str(
include_str!("test-resources/animation_frames")
2020-04-29 07:27:35 +00:00
);
let expected = mock::image::animation_frames();
assert_eq!(output, expected);
}
2020-04-29 08:28:20 +00:00
/// lots of Bitsy games have editor errors where pixels can be placed out of bounds
/// check that these extraneous pixels are stripped out
#[test]
2020-07-26 11:37:41 +00:00
fn image_out_of_bounds() {
let output = Image::from_str(include_str!("test-resources/image-oob")).unwrap();
2020-10-16 10:55:15 +00:00
2020-04-29 08:28:20 +00:00
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,
]
};
2020-10-16 10:55:15 +00:00
2020-04-29 08:28:20 +00:00
assert_eq!(output, expected);
}
2020-07-20 13:42:33 +00:00
2020-07-20 20:05:25 +00:00
#[test]
fn invert() {
let mut output = crate::mock::image::chequers_1();
output.invert();
let expected = crate::mock::image::chequers_2();
assert_eq!(output, expected);
}
2020-07-20 13:42:33 +00:00
#[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]
2020-07-20 14:07:26 +00:00
fn mirror() {
2020-07-20 13:42:33 +00:00
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);
}
2020-07-20 14:07:26 +00:00
#[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);
}
2020-04-12 11:48:07 +00:00
}