From 8536ab94bd02b717b6f4c28032b5f2f6afc13ad1 Mon Sep 17 00:00:00 2001 From: Kenzi Connor Date: Sun, 4 Aug 2024 10:40:46 -0700 Subject: [PATCH] the beginnings of preview/wizard mode --- src/file_data.rs | 7 ++++++ src/lib.rs | 2 +- src/wasm/image.rs | 52 +++++++++++++++++++++++++++++++++------------ src/wasm/main.rs | 4 +--- src/wasm/styles.css | 4 ---- 5 files changed, 47 insertions(+), 22 deletions(-) diff --git a/src/file_data.rs b/src/file_data.rs index fea064a..0a1a66f 100644 --- a/src/file_data.rs +++ b/src/file_data.rs @@ -27,4 +27,11 @@ impl Raw { pub fn parse(&self, parser: ParserType, width: usize) -> Image { Image(parser.process_input(&self.0, width)) } + + pub fn previews(&self) -> Vec { + self.widths(ImageType::CGA) + .iter() + .map(|w| Image(ParserType::CGA.process_input(&self.0, *w as usize))) + .collect() + } } diff --git a/src/lib.rs b/src/lib.rs index 91de810..900f3a4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -64,7 +64,7 @@ impl ImageType { pub fn widths(&self, byte_count: usize) -> Vec { factor(self.pixel_count(byte_count).try_into().unwrap()) .into_iter() - .filter(|&x| x < 80) + .filter(|&x| x > 4 && x < 80) .collect() } } diff --git a/src/wasm/image.rs b/src/wasm/image.rs index 44796b4..bf85cc1 100644 --- a/src/wasm/image.rs +++ b/src/wasm/image.rs @@ -1,13 +1,12 @@ use base64::{engine::general_purpose::STANDARD, Engine}; -use web_sys::HtmlInputElement; +use web_sys::{HtmlElement, 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::parser::ParserType; -use crate::png; +use crate::{file_data, png}; use crate::wasm::FileUpload; @@ -42,6 +41,28 @@ impl ImageComponent { }; format!("data:application/png;base64,{}", STANDARD.encode(data)) } + + pub fn previews(&self, file: &FileUpload) -> Html { + if file.mime_type.contains("image") { + "".into() + } else { + let file_data = file_data::Raw::new(&file.data); + file_data + .previews() + .iter() + .map(|p| { + let palette = palette_from_abbr("cga0"); + let mut bytes: Vec = Vec::new(); + + let _ = png::write_to(&mut bytes, tile(p.data(), self.height), palette.clone()); + let src = format!("data:application/png;base64,{}", STANDARD.encode(bytes)); + html! { + + } + }) + .collect() + } + } } impl Component for ImageComponent { @@ -76,18 +97,21 @@ impl Component for ImageComponent { let file = &ctx.props().file; html! { -
-
- - - - -
-

{ file.name.to_string() }

-
- + <> +
+
+

{ file.name.to_string() }

+ +
+
+ + + + +
-
+ {self.previews(file)} + } } } diff --git a/src/wasm/main.rs b/src/wasm/main.rs index 4df408d..de656de 100644 --- a/src/wasm/main.rs +++ b/src/wasm/main.rs @@ -41,9 +41,7 @@ impl Component for App {

{ "Process your CGA/EGAs" }

-
- {{ images }} -
+
{{ images }}
} } diff --git a/src/wasm/styles.css b/src/wasm/styles.css index dd5be8a..599f0a1 100644 --- a/src/wasm/styles.css +++ b/src/wasm/styles.css @@ -11,10 +11,6 @@ body { color: #fcfcfc; } -img { - display: block; -} - p { text-align: center; }