From e5e9d2797aa921963902e93d0bd872d04306e906 Mon Sep 17 00:00:00 2001 From: Kenzi Connor Date: Sat, 10 Aug 2024 14:54:49 -0700 Subject: [PATCH] wip - gotta figure out some issues with EGA multi-width display --- src/file_data.rs | 6 +++--- src/lib.rs | 19 +++++++++++++++---- src/parser.rs | 4 ++++ src/terminal/main.rs | 2 ++ src/webc/main.rs | 16 ++++++++++------ 5 files changed, 34 insertions(+), 13 deletions(-) diff --git a/src/file_data.rs b/src/file_data.rs index 2abd080..b5cc33f 100644 --- a/src/file_data.rs +++ b/src/file_data.rs @@ -32,12 +32,12 @@ impl Raw { Image(parser.process_input(&self.0, width)) } - pub fn previews(&self) -> Vec { + pub fn previews(&self, parser: ParserType) -> Vec { // if let Some(width) = width { // }else { - self.widths(ImageType::CGA) + self.widths(parser.image_type()) .iter() - .map(|w| Image(ParserType::CGA.process_input(&self.0, *w as usize))) + .map(|w| Image(parser.process_input(&self.0, *w as usize))) .collect() // } } diff --git a/src/lib.rs b/src/lib.rs index eb790f3..48b509d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,6 +2,8 @@ use factor::factor::factor; +use crate::color::palette::palette_from_abbr; + pub mod color; pub mod file_data; pub mod image; @@ -34,6 +36,13 @@ pub enum ImageType { } impl ImageType { + pub fn default_color_palette(&self) -> ColorPalette { + match self { + Self::CGA => palette_from_abbr("cga0"), + Self::EGA => palette_from_abbr("ega"), + } + } + pub fn palette_length(&self) -> usize { match self { Self::CGA => 4, @@ -62,17 +71,19 @@ impl ImageType { } pub fn widths(&self, byte_count: usize) -> Vec { - Self::factors(self.pixel_count(byte_count), 80) + let widths = Self::factors(self.pixel_count(byte_count), 8, 80); + println!("{:?}", widths); + widths } pub fn heights(&self, byte_count: usize, width: usize) -> Vec { - Self::factors(self.pixel_count(byte_count) / width, 50) + Self::factors(self.pixel_count(byte_count) / width, 4, 50) } - pub fn factors(num: usize, upper: usize) -> Vec { + pub fn factors(num: usize, lower: usize, upper: usize) -> Vec { factor(num.try_into().unwrap()) .into_iter() - .filter(|&x| x > 4 && x <= upper.try_into().unwrap()) + .filter(|&x| x >= lower.try_into().unwrap() && x <= upper.try_into().unwrap()) .collect() } } diff --git a/src/parser.rs b/src/parser.rs index 4b7ccb3..29368df 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -125,6 +125,10 @@ impl ProcessBinary for EGARowPlanar { impl EGARowPlanar { fn words_to_bytes_row(&self, buffer: &[u8]) -> Vec { let width = buffer.len() * 2; + if width < 8 { + //TODO don't know if the spec supports this due to row planar. Maybe smarter handling of row chunking + panic!("This parser cannot handle width less than 8") + } let mut nv: Vec = vec![0; width]; for color_row in buffer.chunks(width / 8) { diff --git a/src/terminal/main.rs b/src/terminal/main.rs index 59e24dd..a0f90a8 100644 --- a/src/terminal/main.rs +++ b/src/terminal/main.rs @@ -23,6 +23,8 @@ pub fn main() -> Result<(), Box> { let parser = ParserType::type_str(&args.image_parser); let image = file_data.parse(parser, args.width); + let _ = file_data.previews(ParserType::EGARowPlanar); + let image_data = if args.tile_height.is_some() { image::tile(image.data(), args.tile_height.unwrap()) } else { diff --git a/src/webc/main.rs b/src/webc/main.rs index a3453d8..6cc397c 100644 --- a/src/webc/main.rs +++ b/src/webc/main.rs @@ -25,19 +25,23 @@ pub fn png(data: &[u8]) -> String { #[wasm_bindgen] pub fn previews(data: &[u8]) -> JsValue { let file_data = Raw::new(data); - let palette = palette_from_abbr("cga0"); let mut hm = HashMap::new(); - let images: Vec = file_data - .previews() + //hm.insert("CGA".to_string(), preview(&file_data, ParserType::CGA)); + hm.insert("EGARowPlanar".to_string(), preview(&file_data, ParserType::EGARowPlanar)); + JsValue::from_serde(&hm).unwrap() +} + +pub fn preview(data: &Raw, parser: ParserType) -> Vec { + let palette = parser.image_type().default_color_palette(); + data.previews(parser) .iter() .map(|p| { format!( "data:application/png;base64,{}", STANDARD.encode(png::write2(p.data(), palette.clone())) ) - }).collect(); - hm.insert("CGA".to_string(), images); - JsValue::from_serde(&hm).unwrap() + }) + .collect() } fn main() {}