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
|
|
|
}
|
|
|
|
|
|
|
|
impl From<String> for Image {
|
|
|
|
#[inline]
|
|
|
|
fn from(string: String) -> Image {
|
2020-04-28 17:12:56 +00:00
|
|
|
let string = string.replace("NaN", "0");
|
2020-04-29 07:23:51 +00:00
|
|
|
let string = string.trim();
|
|
|
|
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
|
|
|
|
|
|
|
Image { pixels }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToString for Image {
|
|
|
|
#[inline]
|
|
|
|
fn to_string(&self) -> String {
|
|
|
|
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
|
|
|
|
|
|
|
|
string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-29 07:27:35 +00:00
|
|
|
#[inline]
|
|
|
|
pub(crate) fn animation_frames_from_string(string: String) -> Vec<Image> {
|
|
|
|
let frames: Vec<&str> = string.split(">").collect();
|
|
|
|
|
|
|
|
frames.iter().map(|&frame| Image::from(frame.to_string())).collect()
|
|
|
|
}
|
|
|
|
|
2020-04-19 07:13:55 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2020-04-29 07:27:35 +00:00
|
|
|
use crate::image::{Image, animation_frames_from_string};
|
|
|
|
use crate::mock;
|
2020-04-19 07:13:55 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_image_from_string() {
|
2020-04-29 07:27:35 +00:00
|
|
|
let output = Image::from(
|
|
|
|
include_str!("test-resources/image").to_string()
|
|
|
|
);
|
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]
|
|
|
|
fn test_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_string(
|
|
|
|
include_str!("test-resources/animation_frames").to_string()
|
|
|
|
);
|
|
|
|
|
|
|
|
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]
|
|
|
|
fn test_image_out_of_bounds() {
|
|
|
|
let output = Image::from(
|
|
|
|
include_str!("test-resources/image-oob").to_string()
|
|
|
|
);
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2020-04-12 11:48:07 +00:00
|
|
|
}
|