Skip to content

Commit 541acb9

Browse files
committed
setting up for multiple previews
1 parent 93c3164 commit 541acb9

File tree

3 files changed

+44
-7
lines changed

3 files changed

+44
-7
lines changed

src/file_data.rs

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
use factor::factor::factor;
2+
3+
use crate::image::Image;
4+
use crate::parser::ParserType;
5+
6+
pub struct Raw(Vec<u8>);
7+
8+
impl Raw {
9+
pub fn new(data: &[u8]) -> Self {
10+
Self(data.to_owned())
11+
}
12+
fn byte_count(&self) -> usize {
13+
self.0.len()
14+
}
15+
fn cga_count(&self) -> usize {
16+
self.byte_count() * 4
17+
}
18+
fn ega_count(&self) -> usize {
19+
self.byte_count() * 2
20+
}
21+
pub fn cga_possible(&self) -> bool {
22+
self.cga_count() <= 64_000
23+
}
24+
pub fn cga_fullscreen(&self) -> bool {
25+
self.cga_count() == 64_000
26+
}
27+
pub fn cga_widths(&self) -> Vec<i64> {
28+
factor(self.cga_count().try_into().unwrap())
29+
}
30+
pub fn ega_widths(&self) -> Vec<i64> {
31+
factor(self.ega_count().try_into().unwrap())
32+
}
33+
34+
pub fn parse(&self, parser: ParserType, width: usize) -> Image {
35+
Image(parser.process_input(&self.0, width))
36+
}
37+
}

src/terminal/main.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use std::path::Path;
66
use clap::Parser;
77

88
use cega::color::palette::palette_from_abbr;
9-
//use cega::file_data::Raw;
10-
use cega::image::{self, Image};
9+
use cega::file_data;
10+
use cega::image;
1111
use cega::parser::ParserType;
1212
#[cfg(feature = "png")]
1313
use cega::png;
@@ -19,9 +19,9 @@ use cega::ImageType;
1919
pub fn main() -> Result<(), Box<dyn std::error::Error>> {
2020
let args = args::Args::parse();
2121

22-
let file_data = &std::fs::read(Path::new(&args.image))?;
22+
let file_data = file_data::Raw::new(&std::fs::read(Path::new(&args.image))?);
2323
let parser = ParserType::type_str(&args.image_parser);
24-
let image = Image(parser.process_input(&file_data, args.width));
24+
let image = file_data.parse(parser, args.width);
2525

2626
let image_data = if args.tile_height.is_some() {
2727
image::tile(image.data(), args.tile_height.unwrap())

src/wasm/image.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use web_sys::HtmlInputElement;
44
use yew::{html, Callback, Component, Context, Event, Html, Properties, SubmitEvent, TargetCast};
55

66
use crate::color::palette::palette_from_abbr;
7+
use crate::file_data;
78
use crate::image::tile;
8-
use crate::image::Image;
99
use crate::parser::ParserType;
1010
use crate::png;
1111

@@ -39,9 +39,9 @@ impl ImageFile<'_> {
3939
if self.file_input.mime_type.contains("image") {
4040
self.file_input.data.clone()
4141
} else {
42+
let file_data = file_data::Raw::new(&self.file_input.data);
4243
let parser = ParserType::CGA;
43-
let parsed = parser.process_input(&self.file_input.data, self.width);
44-
let image = Image(parsed);
44+
let image = file_data.parse(parser, self.width);
4545
let palette = palette_from_abbr("cga0");
4646
let mut bytes: Vec<u8> = Vec::new();
4747

0 commit comments

Comments
 (0)