Skip to content

Commit

Permalink
wip - gotta figure out some issues with EGA multi-width display
Browse files Browse the repository at this point in the history
  • Loading branch information
knzai committed Aug 10, 2024
1 parent 764701e commit e5e9d27
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 13 deletions.
6 changes: 3 additions & 3 deletions src/file_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ impl Raw {
Image(parser.process_input(&self.0, width))
}

pub fn previews(&self) -> Vec<Image> {
pub fn previews(&self, parser: ParserType) -> Vec<Image> {
// 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()
// }
}
Expand Down
19 changes: 15 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -62,17 +71,19 @@ impl ImageType {
}

pub fn widths(&self, byte_count: usize) -> Vec<i64> {
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<i64> {
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<i64> {
pub fn factors(num: usize, lower: usize, upper: usize) -> Vec<i64> {
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()
}
}
4 changes: 4 additions & 0 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ impl ProcessBinary for EGARowPlanar {
impl EGARowPlanar {
fn words_to_bytes_row(&self, buffer: &[u8]) -> Vec<u8> {
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<u8> = vec![0; width];

for color_row in buffer.chunks(width / 8) {
Expand Down
2 changes: 2 additions & 0 deletions src/terminal/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
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 {
Expand Down
16 changes: 10 additions & 6 deletions src/webc/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> = 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<String> {
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() {}

0 comments on commit e5e9d27

Please sign in to comment.