rework images as text
This commit is contained in:
parent
79b21e38ec
commit
ff6552ce6b
11
TODO.md
11
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
|
||||
|
||||
|
|
77
src/image.rs
77
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<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());
|
||||
}
|
||||
}
|
||||
|
||||
// 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,
|
||||
Self { name, pixels }
|
||||
}
|
||||
}
|
||||
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
|
|
134
src/mock.rs
134
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};
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
pixels = """
|
||||
00022000
|
||||
00022000
|
||||
00022000
|
||||
|
@ -7,4 +6,3 @@ pixels = """
|
|||
20222202
|
||||
00200200
|
||||
00200200
|
||||
"""
|
Loading…
Reference in New Issue