Skip to content

Commit 8536ab9

Browse files
committed
the beginnings of preview/wizard mode
1 parent f639381 commit 8536ab9

File tree

5 files changed

+47
-22
lines changed

5 files changed

+47
-22
lines changed

src/file_data.rs

+7
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,11 @@ impl Raw {
2727
pub fn parse(&self, parser: ParserType, width: usize) -> Image {
2828
Image(parser.process_input(&self.0, width))
2929
}
30+
31+
pub fn previews(&self) -> Vec<Image> {
32+
self.widths(ImageType::CGA)
33+
.iter()
34+
.map(|w| Image(ParserType::CGA.process_input(&self.0, *w as usize)))
35+
.collect()
36+
}
3037
}

src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ impl ImageType {
6464
pub fn widths(&self, byte_count: usize) -> Vec<i64> {
6565
factor(self.pixel_count(byte_count).try_into().unwrap())
6666
.into_iter()
67-
.filter(|&x| x < 80)
67+
.filter(|&x| x > 4 && x < 80)
6868
.collect()
6969
}
7070
}

src/wasm/image.rs

+38-14
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
use base64::{engine::general_purpose::STANDARD, Engine};
22

3-
use web_sys::HtmlInputElement;
3+
use web_sys::{HtmlElement, 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;
87
use crate::image::tile;
98
use crate::parser::ParserType;
10-
use crate::png;
9+
use crate::{file_data, png};
1110

1211
use crate::wasm::FileUpload;
1312

@@ -42,6 +41,28 @@ impl ImageComponent {
4241
};
4342
format!("data:application/png;base64,{}", STANDARD.encode(data))
4443
}
44+
45+
pub fn previews(&self, file: &FileUpload) -> Html {
46+
if file.mime_type.contains("image") {
47+
"".into()
48+
} else {
49+
let file_data = file_data::Raw::new(&file.data);
50+
file_data
51+
.previews()
52+
.iter()
53+
.map(|p| {
54+
let palette = palette_from_abbr("cga0");
55+
let mut bytes: Vec<u8> = Vec::new();
56+
57+
let _ = png::write_to(&mut bytes, tile(p.data(), self.height), palette.clone());
58+
let src = format!("data:application/png;base64,{}", STANDARD.encode(bytes));
59+
html! {
60+
<img src={ src } />
61+
}
62+
})
63+
.collect()
64+
}
65+
}
4566
}
4667

4768
impl Component for ImageComponent {
@@ -76,18 +97,21 @@ impl Component for ImageComponent {
7697
let file = &ctx.props().file;
7798

7899
html! {
79-
<div class="preview-tile ">
80-
<form onsubmit={noop}>
81-
<label for="width">{"[Tile] Width"}</label>
82-
<input name="width" type="number" value={self.width.to_string()} onchange={ctx.link().callback(Msg::Width)} />
83-
<label for="height">{"[Tile] Height"}</label>
84-
<input name="height" type="number" value={self.height.to_string()} onchange={ctx.link().callback(Msg::Height)} />
85-
</form>
86-
<p class="preview-name">{ file.name.to_string() }</p>
87-
<div class=".preview-media">
88-
<img src={ self.src(file) } />
100+
<>
101+
<div class="preview-tile ">
102+
<div class=".preview-media">
103+
<p class="preview-name">{ file.name.to_string() }</p>
104+
<img src={ self.src(file) } />
105+
</div>
106+
<form onsubmit={noop}>
107+
<label for="width">{"[Tile] Width"}</label>
108+
<input name="width" type="number" value={self.width.to_string()} onchange={ctx.link().callback(Msg::Width)} />
109+
<label for="height">{"[Tile] Height"}</label>
110+
<input name="height" type="number" value={self.height.to_string()} onchange={ctx.link().callback(Msg::Height)} />
111+
</form>
89112
</div>
90-
</div>
113+
{self.previews(file)}
114+
</>
91115
}
92116
}
93117
}

src/wasm/main.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ impl Component for App {
4141
<div id="wrapper">
4242
<h1>{ "Process your CGA/EGAs" }</h1>
4343
<FileInput accept="image/png,.bin,.cga,.ega" onload={ctx.link().callback( Msg::Loaded )} children={None}/>
44-
<div id="preview-area">
45-
{{ images }}
46-
</div>
44+
<div id="preview-area">{{ images }}</div>
4745
</div>
4846
}
4947
}

src/wasm/styles.css

-4
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@ body {
1111
color: #fcfcfc;
1212
}
1313

14-
img {
15-
display: block;
16-
}
17-
1814
p {
1915
text-align: center;
2016
}

0 commit comments

Comments
 (0)