diff --git a/TODO.md b/TODO.md index 3ba086c..1792fb3 100644 --- a/TODO.md +++ b/TODO.md @@ -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 diff --git a/src/image.rs b/src/image.rs index a696627..d1a2ef1 100644 --- a/src/image.rs +++ b/src/image.rs @@ -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, } -// impl Image { -// fn from(intermediate: IntermediateImage) -> Image { -// Image { -// name: intermediate.name.to_owned(), -// pixels: intermediate.pixels.split_whitespace().collect::().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 = 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, + 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 { -// 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); + } } diff --git a/src/mock.rs b/src/mock.rs index 39bb9fe..5c4e914 100644 --- a/src/mock.rs +++ b/src/mock.rs @@ -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}; diff --git a/src/test-resources/basic/images/avatar.toml b/src/test-resources/basic/images/avatar.txt similarity index 80% rename from src/test-resources/basic/images/avatar.toml rename to src/test-resources/basic/images/avatar.txt index 7fb0a86..5679c2b 100644 --- a/src/test-resources/basic/images/avatar.toml +++ b/src/test-resources/basic/images/avatar.txt @@ -1,4 +1,3 @@ -pixels = """ 00022000 00022000 00022000 @@ -7,4 +6,3 @@ pixels = """ 20222202 00200200 00200200 -"""