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 ### palettes
* replace toml with .hex format? ### images
* what else is available for palettes?
* ~JASC .pal (yes)~ * ideally support gif/png...
* Photoshop ASE (nah - seems rather proprietary/difficult to parse)
* ~GIMP .gpl (yes!)~
* .png
* .hex
* .txt
### parser ### parser

View File

@ -1,4 +1,6 @@
use std::path::PathBuf;
use serde_derive::{Serialize, Deserialize}; use serde_derive::{Serialize, Deserialize};
use std::fs::read_to_string;
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize)] #[derive(Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct Image { pub struct Image {
@ -7,66 +9,31 @@ pub struct Image {
pub pixels: Vec<u8>, pub pixels: Vec<u8>,
} }
// impl Image { impl Image {
// fn from(intermediate: IntermediateImage) -> Image { pub fn from_file(path: PathBuf) -> Self {
// Image { let name = path.file_stem().unwrap().to_str().unwrap().into();
// name: intermediate.name.to_owned(), let mut pixels: Vec<u8> = Vec::new();
// pixels: intermediate.pixels.split_whitespace().collect::<String>().chars().map(
// |char|char as u8
// ).collect()
// }
// }
// }
/// for toml purposes for line in read_to_string(&path).unwrap().lines() {
#[derive(Debug, Serialize, Deserialize)] for char in line.chars() {
pub(crate) struct IntermediateImages { pixels.push(char.to_string().parse().unwrap());
/// singular so each image is named "image" instead of "images" in toml }
image: Vec<IntermediateImage>, }
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)] #[cfg(test)]
mod test { mod test {
// #[test] use std::path::PathBuf;
// fn test_image_from_toml() { use crate::image::Image;
// let str = include_str!("test-resources/basic/images.toml");
// let output: Image = toml::from_str(str).unwrap(); #[test]
// let expected = crate::mock::image::avatar(); fn image_from_text() {
// assert_eq!(output, expected); 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 { pub(crate) mod image {
// use crate::image::Image; use crate::image::Image;
//
// pub fn bg() -> Image { pub fn bg() -> Image {
// Image { Image {
// name: "bg".to_string(), name: "bg".to_string(),
// pixels: vec![ 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,
// 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 { pub fn block() -> Image {
// Image { Image {
// name: "block".to_string(), name: "block".to_string(),
// pixels: vec![ pixels: vec![
// 1,1,1,1,1,1,1,1, 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,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,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,0,0,0,0,0,0,1, 1,0,0,0,0,0,0,1,
// 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,
// ] ]
// } }
// } }
//
// pub fn avatar() -> Image { pub fn avatar() -> Image {
// Image { Image {
// name: "avatar".to_string(), name: "avatar".to_string(),
// pixels: vec![ 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,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,0,2,2,2,2,0,0,
// 0,2,2,2,2,2,2,0, 0,2,2,2,2,2,2,0,
// 2,0,2,2,2,2,0,2, 2,0,2,2,2,2,0,2,
// 0,0,2,0,0,2,0,0, 0,0,2,0,0,2,0,0,
// 0,0,2,0,0,2,0,0, 0,0,2,0,0,2,0,0,
// ] ]
// } }
// } }
//
// pub fn cat() -> Image { pub fn cat() -> Image {
// Image { Image {
// name: "cat".to_string(), name: "cat".to_string(),
// pixels: vec![ 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,2,0,2,0,0,0,2, 0,2,0,2,0,0,0,2,
// 0,2,2,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,0,0,2,0,
// 0,2,2,2,2,2,0,0, 0,2,2,2,2,2,0,0,
// 0,0,2,2,2,2,0,0, 0,0,2,2,2,2,0,0,
// 0,0,2,0,0,2,0,0, 0,0,2,0,0,2,0,0,
// ] ]
// } }
// } }
// } }
pub(crate) mod palette { pub(crate) mod palette {
use crate::{Palette, Colour}; use crate::{Palette, Colour};

View File

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