Skip to content

Commit

Permalink
stick the fullscreen 320 back on there
Browse files Browse the repository at this point in the history
  • Loading branch information
knzai committed Aug 11, 2024
1 parent 9cac811 commit b08434d
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 25 deletions.
25 changes: 18 additions & 7 deletions src/file_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,24 @@ impl Raw {
Image(parser.process_input(&self.0, width))
}

pub fn previews(&self, parser: ParserType) -> Vec<Image> {
// if let Some(width) = width {
// }else {
self.widths(parser.image_type())
pub fn height_previews(&self, parser: ParserType, width: usize) -> Vec<Image> {
let image = self.parse(parser, width);

Check warning on line 36 in src/file_data.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unused variable: `image`

Check failure on line 36 in src/file_data.rs

View workflow job for this annotation

GitHub Actions / Check and lint

unused variable: `image`

Check warning on line 36 in src/file_data.rs

View workflow job for this annotation

GitHub Actions / Check and lint

unused variable: `image`
let mut previews = self
.heights(parser.image_type(), width)
.iter()
.map(|w| Image(parser.process_input(&self.0, *w as usize)))
.collect()
// }
.map(|h| self.parse(parser, width))

Check warning on line 40 in src/file_data.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unused variable: `h`

Check failure on line 40 in src/file_data.rs

View workflow job for this annotation

GitHub Actions / Check and lint

unused variable: `h`

Check warning on line 40 in src/file_data.rs

View workflow job for this annotation

GitHub Actions / Check and lint

unused variable: `h`
.collect::<Vec<Image>>();
previews.push(self.parse(parser, 320));
previews
}

pub fn width_previews(&self, parser: ParserType) -> Vec<Image> {
let mut previews = self
.widths(parser.image_type())
.iter()
.map(|w| self.parse(parser, *w as usize))
.collect::<Vec<Image>>();
previews.push(self.parse(parser, 320));
previews
}
}
7 changes: 7 additions & 0 deletions src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ impl Image {
"".to_string()
}
}

pub fn sprites(&self, height: usize) -> Vec<Image> {
self.data()
.chunks(height)
.map(|chunk| Image(chunk.to_vec()))
.collect()
}
}

pub fn tile<T: std::clone::Clone>(data: Vec<Vec<T>>, tile_height: usize) -> Vec<Vec<T>> {
Expand Down
3 changes: 0 additions & 3 deletions src/terminal/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
let file_data = file_data::Raw::new(&std::fs::read(Path::new(&args.image))?);
let parser = ParserType::type_str(&args.image_parser);
let image = file_data.parse(parser, args.width);

let _ = file_data.previews(ParserType::EGARowPlanar);

let image_data = if args.tile_height.is_some() {
image::tile(image.data(), args.tile_height.unwrap())
} else {
Expand Down
3 changes: 2 additions & 1 deletion src/wasm/file-byte-reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ class FileByteReader extends HTMLInputElement {
}

onFileLoad(event) {
this.emit('loaded', new Int8Array(event.target.result));
this.data = new Int8Array(event.target.result);
this.emit('loaded', this.data);
}

onChange() {
Expand Down
47 changes: 34 additions & 13 deletions src/wasm/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,55 +3,76 @@
<head>
<link data-trunk rel="css" href="./styles.css" />
<link
data-trunk
rel="rust" href="../../Cargo.toml"
data-bin="cega-wasm"
data-cargo-no-default-features
data-cargo-features="wasm"
data-trunk
rel="rust" href="../../Cargo.toml"
data-bin="cega-wasm"
data-cargo-no-default-features
data-cargo-features="wasm"
/>
<script data-trunk src="./file-byte-reader.js"></script>
<script>
let $ = (query, ele = document) => ele.querySelector(query);
const D = document;
const $ = (q, e = D) => e.querySelector(q);

function fileLoaded(array) {
let wrapper = $('preview-wrapper');
wrapper.textContent = "";

let parser_images = window.wasmBindings.previews(array);
for (const [key, images] of Object.entries(parser_images)) {
const container = document.createElement("preview-area");
const container = D.createElement("preview-area");
container.innerHTML = `<h3>${key}</h3>`
images.forEach(src => container.appendChild(new PreviewItem(src)));
wrapper.appendChild(container);
}
}

document.addEventListener("DOMContentLoaded", () => {
D.addEventListener("DOMContentLoaded", () => {
$('#file-input').addEventListener("file-byte-reader:loaded",
e => fileLoaded(e.detail)
e => {
if (file = e.target.files[0]) {
$('form').style.display = "block";
$('#file-name').value = file.name;
fileLoaded(e.detail)
}
}
);
});

class PreviewItem extends HTMLElement {
constructor(src) {
super();
const shadow = this.attachShadow({mode: "open"});
const fragment = document.createRange().createContextualFragment('<h3></h3><img/>');
const fragment = D.createRange().createContextualFragment('<h3></h3><img/>');
const h3 = $('h3', fragment);
this.img = $('img', fragment);

this.img.onload = function () {
h3.innerText = this.width;
}
this.img.src = src
this.img.src = src;
this.addEventListener('click', this.onClick);
shadow.appendChild(fragment);
}
onClick() {
$('#width').value = this.img.width;
}
}
customElements.define("preview-item", PreviewItem);
</script>
</head>
<body>
<h1>Process your CGA/EGAs</h1>
<input is="file-byte-reader" id="file-input" multiple="true" accept=".bin,.cga,.ega,.cega" />
<input is="file-byte-reader" id="file-input" multiple="false" accept=".bin,.cga,.ega,.cega" />
<form style="display:none;">
<label for="file-name">File name: </label><input name="file-name" id="file-name" />
<label for="parser">Parser: </label>
<select name="parser" id="parser">
<option disabled selected value></option>
<option value="CGA">CGA</option>
<option value="EGARowPlanar">EGARowPlanar</option>
</select>
<label for="width">Width: </label><input name="width" id="width" />
</form>
<preview-wrapper></preview-wrapper>
</body>
</html>
2 changes: 1 addition & 1 deletion src/wasm/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub fn previews(data: &[u8]) -> JsValue {

pub fn preview(data: &Raw, parser: ParserType) -> Vec<String> {
let palette = parser.image_type().default_color_palette();
data.previews(parser)
data.width_previews(parser)
.iter()
.map(|p| {
format!(
Expand Down

0 comments on commit b08434d

Please sign in to comment.