Skip to content

Commit 577325b

Browse files
committed
wip - gotta figure out some issues with EGA multi-width display
1 parent 764701e commit 577325b

File tree

5 files changed

+37
-13
lines changed

5 files changed

+37
-13
lines changed

src/file_data.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ impl Raw {
3232
Image(parser.process_input(&self.0, width))
3333
}
3434

35-
pub fn previews(&self) -> Vec<Image> {
35+
pub fn previews(&self, parser: ParserType) -> Vec<Image> {
3636
// if let Some(width) = width {
3737
// }else {
38-
self.widths(ImageType::CGA)
38+
self.widths(parser.image_type())
3939
.iter()
40-
.map(|w| Image(ParserType::CGA.process_input(&self.0, *w as usize)))
40+
.map(|w| Image(parser.process_input(&self.0, *w as usize)))
4141
.collect()
4242
// }
4343
}

src/lib.rs

+15-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
use factor::factor::factor;
44

5+
use crate::color::palette::palette_from_abbr;
6+
57
pub mod color;
68
pub mod file_data;
79
pub mod image;
@@ -34,6 +36,13 @@ pub enum ImageType {
3436
}
3537

3638
impl ImageType {
39+
pub fn default_color_palette(&self) -> ColorPalette {
40+
match self {
41+
Self::CGA => palette_from_abbr("cga0"),
42+
Self::EGA => palette_from_abbr("ega"),
43+
}
44+
}
45+
3746
pub fn palette_length(&self) -> usize {
3847
match self {
3948
Self::CGA => 4,
@@ -62,17 +71,19 @@ impl ImageType {
6271
}
6372

6473
pub fn widths(&self, byte_count: usize) -> Vec<i64> {
65-
Self::factors(self.pixel_count(byte_count), 80)
74+
let widths = Self::factors(self.pixel_count(byte_count), 8, 80);
75+
println!("{:?}", widths);
76+
widths
6677
}
6778

6879
pub fn heights(&self, byte_count: usize, width: usize) -> Vec<i64> {
69-
Self::factors(self.pixel_count(byte_count) / width, 50)
80+
Self::factors(self.pixel_count(byte_count) / width, 4, 50)
7081
}
7182

72-
pub fn factors(num: usize, upper: usize) -> Vec<i64> {
83+
pub fn factors(num: usize, lower: usize, upper: usize) -> Vec<i64> {
7384
factor(num.try_into().unwrap())
7485
.into_iter()
75-
.filter(|&x| x > 4 && x <= upper.try_into().unwrap())
86+
.filter(|&x| x >= lower.try_into().unwrap() && x <= upper.try_into().unwrap())
7687
.collect()
7788
}
7889
}

src/parser.rs

+4
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ impl ProcessBinary for EGARowPlanar {
125125
impl EGARowPlanar {
126126
fn words_to_bytes_row(&self, buffer: &[u8]) -> Vec<u8> {
127127
let width = buffer.len() * 2;
128+
if width < 8 {
129+
//TODO don't know if the spec supports this due to row planar. Maybe smarter handling of row chunking
130+
panic!("This parser cannot handle width less than 8")
131+
}
128132
let mut nv: Vec<u8> = vec![0; width];
129133

130134
for color_row in buffer.chunks(width / 8) {

src/terminal/main.rs

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
2323
let parser = ParserType::type_str(&args.image_parser);
2424
let image = file_data.parse(parser, args.width);
2525

26+
let _ = file_data.previews(ParserType::EGARowPlanar);
27+
2628
let image_data = if args.tile_height.is_some() {
2729
image::tile(image.data(), args.tile_height.unwrap())
2830
} else {

src/webc/main.rs

+13-6
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,26 @@ pub fn png(data: &[u8]) -> String {
2525
#[wasm_bindgen]
2626
pub fn previews(data: &[u8]) -> JsValue {
2727
let file_data = Raw::new(data);
28-
let palette = palette_from_abbr("cga0");
2928
let mut hm = HashMap::new();
30-
let images: Vec<String> = file_data
31-
.previews()
29+
//hm.insert("CGA".to_string(), preview(&file_data, ParserType::CGA));
30+
hm.insert(
31+
"EGARowPlanar".to_string(),
32+
preview(&file_data, ParserType::EGARowPlanar),
33+
);
34+
JsValue::from_serde(&hm).unwrap()
35+
}
36+
37+
pub fn preview(data: &Raw, parser: ParserType) -> Vec<String> {
38+
let palette = parser.image_type().default_color_palette();
39+
data.previews(parser)
3240
.iter()
3341
.map(|p| {
3442
format!(
3543
"data:application/png;base64,{}",
3644
STANDARD.encode(png::write2(p.data(), palette.clone()))
3745
)
38-
}).collect();
39-
hm.insert("CGA".to_string(), images);
40-
JsValue::from_serde(&hm).unwrap()
46+
})
47+
.collect()
4148
}
4249

4350
fn main() {}

0 commit comments

Comments
 (0)