Skip to content

Commit

Permalink
setting up for multiple previews
Browse files Browse the repository at this point in the history
  • Loading branch information
knzai committed Aug 3, 2024
1 parent 93c3164 commit 541acb9
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
37 changes: 37 additions & 0 deletions src/file_data.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use factor::factor::factor;

use crate::image::Image;
use crate::parser::ParserType;

pub struct Raw(Vec<u8>);

impl Raw {
pub fn new(data: &[u8]) -> Self {
Self(data.to_owned())
}
fn byte_count(&self) -> usize {
self.0.len()
}
fn cga_count(&self) -> usize {
self.byte_count() * 4
}
fn ega_count(&self) -> usize {
self.byte_count() * 2
}
pub fn cga_possible(&self) -> bool {
self.cga_count() <= 64_000
}
pub fn cga_fullscreen(&self) -> bool {
self.cga_count() == 64_000
}
pub fn cga_widths(&self) -> Vec<i64> {
factor(self.cga_count().try_into().unwrap())
}
pub fn ega_widths(&self) -> Vec<i64> {
factor(self.ega_count().try_into().unwrap())
}

pub fn parse(&self, parser: ParserType, width: usize) -> Image {
Image(parser.process_input(&self.0, width))
}
}
8 changes: 4 additions & 4 deletions src/terminal/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use std::path::Path;
use clap::Parser;

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

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

let image_data = if args.tile_height.is_some() {
image::tile(image.data(), args.tile_height.unwrap())
Expand Down
6 changes: 3 additions & 3 deletions src/wasm/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use web_sys::HtmlInputElement;
use yew::{html, Callback, Component, Context, Event, Html, Properties, SubmitEvent, TargetCast};

use crate::color::palette::palette_from_abbr;
use crate::file_data;
use crate::image::tile;
use crate::image::Image;
use crate::parser::ParserType;
use crate::png;

Expand Down Expand Up @@ -39,9 +39,9 @@ impl ImageFile<'_> {
if self.file_input.mime_type.contains("image") {
self.file_input.data.clone()
} else {
let file_data = file_data::Raw::new(&self.file_input.data);
let parser = ParserType::CGA;
let parsed = parser.process_input(&self.file_input.data, self.width);
let image = Image(parsed);
let image = file_data.parse(parser, self.width);
let palette = palette_from_abbr("cga0");
let mut bytes: Vec<u8> = Vec::new();

Expand Down

0 comments on commit 541acb9

Please sign in to comment.