rework images as text

This commit is contained in:
Max Bradbury 2021-05-17 16:40:58 +01:00
parent 79b21e38ec
commit ff6552ce6b
4 changed files with 94 additions and 134 deletions

11
TODO.md
View File

@ -20,14 +20,9 @@ idea:
### palettes
* replace toml with .hex format?
* what else is available for palettes?
* ~JASC .pal (yes)~
* Photoshop ASE (nah - seems rather proprietary/difficult to parse)
* ~GIMP .gpl (yes!)~
* .png
* .hex
* .txt
### images
* ideally support gif/png...
### parser

View File

@ -1,4 +1,6 @@
use std::path::PathBuf;
use serde_derive::{Serialize, Deserialize};
use std::fs::read_to_string;
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct Image {
@ -7,66 +9,31 @@ pub struct Image {
pub pixels: Vec<u8>,
}
// impl Image {
// fn from(intermediate: IntermediateImage) -> Image {
// Image {
// name: intermediate.name.to_owned(),
// pixels: intermediate.pixels.split_whitespace().collect::<String>().chars().map(
// |char|char as u8
// ).collect()
// }
// }
// }
impl Image {
pub fn from_file(path: PathBuf) -> Self {
let name = path.file_stem().unwrap().to_str().unwrap().into();
let mut pixels: Vec<u8> = Vec::new();
/// for toml purposes
#[derive(Debug, Serialize, Deserialize)]
pub(crate) struct IntermediateImages {
/// singular so each image is named "image" instead of "images" in toml
image: Vec<IntermediateImage>,
for line in read_to_string(&path).unwrap().lines() {
for char in line.chars() {
pixels.push(char.to_string().parse().unwrap());
}
}
Self { name, pixels }
}
}
// impl IntermediateImages {
// fn to_images(&self) -> Vec<Image> {
// self.image.iter().map(|intermediate|
// Image::from(intermediate.clone())
// ).collect()
// }
// }
#[derive(Clone, Debug, Serialize, Deserialize)]
pub(crate) struct IntermediateImage {
name: String,
pixels: String,
}
// impl IntermediateImage {
// // todo refactor
// fn from(image: Image) -> IntermediateImage {
// let mut string = "\n".to_string();
//
// let sqrt = (image.pixels.len() as f64).sqrt() as usize;
// for line in image.pixels.chunks(sqrt) {
// for pixel in line {
// string.push_str(&format!("{}", *pixel));
// }
// string.push('\n');
// }
//
// IntermediateImage {
// name: image.name.to_owned(),
// /// todo wtf? I guess this crate doesn't handle multiline strings correctly
// pixels: format!("\"\"{}\"\"", string),
// }
// }
// }
#[cfg(test)]
mod test {
// #[test]
// fn test_image_from_toml() {
// let str = include_str!("test-resources/basic/images.toml");
// let output: Image = toml::from_str(str).unwrap();
// let expected = crate::mock::image::avatar();
// assert_eq!(output, expected);
// }
use std::path::PathBuf;
use crate::image::Image;
#[test]
fn image_from_text() {
let path = PathBuf::from("src/test-resources/basic/images/avatar.txt");
let output = Image::from_file(path);
let expected = crate::mock::image::avatar();
assert_eq!(output, expected);
}
}

View File

@ -1,70 +1,70 @@
// pub(crate) mod image {
// use crate::image::Image;
//
// pub fn bg() -> Image {
// Image {
// name: "bg".to_string(),
// pixels: vec![
// 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,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,
// ]
// }
// }
//
// pub fn block() -> Image {
// Image {
// name: "block".to_string(),
// pixels: vec![
// 1,1,1,1,1,1,1,1,
// 1,0,0,0,0,0,0,1,
// 1,0,0,0,0,0,0,1,
// 1,0,0,1,1,0,0,1,
// 1,0,0,1,1,0,0,1,
// 1,0,0,0,0,0,0,1,
// 1,0,0,0,0,0,0,1,
// 1,1,1,1,1,1,1,1,
// ]
// }
// }
//
// pub fn avatar() -> Image {
// Image {
// name: "avatar".to_string(),
// pixels: vec![
// 0,0,0,2,2,0,0,0,
// 0,0,0,2,2,0,0,0,
// 0,0,0,2,2,0,0,0,
// 0,0,2,2,2,2,0,0,
// 0,2,2,2,2,2,2,0,
// 2,0,2,2,2,2,0,2,
// 0,0,2,0,0,2,0,0,
// 0,0,2,0,0,2,0,0,
// ]
// }
// }
//
// pub fn cat() -> Image {
// Image {
// name: "cat".to_string(),
// pixels: vec![
// 0,0,0,0,0,0,0,0,
// 0,0,0,0,0,0,0,0,
// 0,2,0,2,0,0,0,2,
// 0,2,2,2,0,0,0,2,
// 0,2,2,2,0,0,2,0,
// 0,2,2,2,2,2,0,0,
// 0,0,2,2,2,2,0,0,
// 0,0,2,0,0,2,0,0,
// ]
// }
// }
// }
pub(crate) mod image {
use crate::image::Image;
pub fn bg() -> Image {
Image {
name: "bg".to_string(),
pixels: vec![
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,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,
]
}
}
pub fn block() -> Image {
Image {
name: "block".to_string(),
pixels: vec![
1,1,1,1,1,1,1,1,
1,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,1,
1,0,0,1,1,0,0,1,
1,0,0,1,1,0,0,1,
1,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,1,
1,1,1,1,1,1,1,1,
]
}
}
pub fn avatar() -> Image {
Image {
name: "avatar".to_string(),
pixels: vec![
0,0,0,2,2,0,0,0,
0,0,0,2,2,0,0,0,
0,0,0,2,2,0,0,0,
0,0,2,2,2,2,0,0,
0,2,2,2,2,2,2,0,
2,0,2,2,2,2,0,2,
0,0,2,0,0,2,0,0,
0,0,2,0,0,2,0,0,
]
}
}
pub fn cat() -> Image {
Image {
name: "cat".to_string(),
pixels: vec![
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,2,0,2,0,0,0,2,
0,2,2,2,0,0,0,2,
0,2,2,2,0,0,2,0,
0,2,2,2,2,2,0,0,
0,0,2,2,2,2,0,0,
0,0,2,0,0,2,0,0,
]
}
}
}
pub(crate) mod palette {
use crate::{Palette, Colour};

View File

@ -1,4 +1,3 @@
pixels = """
00022000
00022000
00022000
@ -7,4 +6,3 @@ pixels = """
20222202
00200200
00200200
"""