Skip to content

Commit ba79ac2

Browse files
committed
minor
1 parent 8afe238 commit ba79ac2

File tree

3 files changed

+26
-14
lines changed

3 files changed

+26
-14
lines changed

app/script.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,18 @@ function resetOutput() {
6767
footer.style.top = '-3rem';
6868
}
6969

70-
async function showImage(imBlob, format) {
70+
/**
71+
* Prepare the output area: display the image, and set up the download button.
72+
* @param {string} type - 'encoded' / 'decoded'
73+
* @param {Uint8Array} imBlob - The image data as a Uint8Array
74+
* @param {string} format - The image format, 'png'/'jpeg'
75+
*/
76+
async function showImage(type, imBlob, format) {
7177
resetOutput();
7278
ensureOutput();
7379

7480
hintLabel.textContent = '';
75-
const blob = new Blob([imBlob], { type: 'image/png' });
81+
const blob = new Blob([imBlob], { type: `image/${format}` });
7682
const url = URL.createObjectURL(blob);
7783

7884
const imgElem = document.createElement('img');
@@ -84,8 +90,8 @@ async function showImage(imBlob, format) {
8490
downloadBtn.onclick = () => {
8591
const a = document.createElement('a');
8692
a.href = url;
87-
const timeName = new Date().toISOString().replace(/[:.]/g, '-');
88-
a.download = `img-${timeName}.${format}`;
93+
const uuid = crypto.randomUUID().slice(0, 8);
94+
a.download = `${type}-${uuid}.${format}`;
8995
document.body.appendChild(a);
9096
a.click();
9197
document.body.removeChild(a);
@@ -130,9 +136,11 @@ worker.onmessage = async (event) => {
130136
if (event.data.error) {
131137
await showError(`Error: ${event.data.error}`);
132138
console.error(event.data.error);
139+
hintLabel.textContent = '';
133140
return;
134141
}
135142
await showImage(
143+
event.data.type,
136144
event.data.buffer,
137145
event.data.format
138146
);
@@ -154,10 +162,10 @@ worker.onmessage = async (event) => {
154162

155163
fileInput.addEventListener('change', ()=>{
156164
resetOutput();
157-
hintLabel.textContent = '(The selected image)';
158165
const file = fileInput.files[0];
159166
if (file) {
160167
ensureOutput();
168+
hintLabel.textContent = '(The selected image)';
161169
const imgElem = document.createElement('img');
162170
imgElem.src = URL.createObjectURL(file);
163171
imgElem.alt = 'Selected Image';

app/worker.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,19 @@ self.onmessage = async function(event) {
1212
try {
1313
if (type === 'encode') {
1414
const encoded = encode(buffer, secret, maxSide, outputAs);
15-
self.postMessage({ type: 'encoded', buffer: encoded, format: outputAs });
15+
self.postMessage({
16+
type: 'encoded',
17+
buffer: encoded,
18+
format: outputAs
19+
});
1620
}
1721
if (type === 'decode') {
1822
const decoded = decode(buffer, secret, maxSide, outputAs);
19-
self.postMessage({ type: 'decoded', buffer: decoded, format: outputAs });
23+
self.postMessage({
24+
type: 'decoded',
25+
buffer: decoded,
26+
format: outputAs
27+
});
2028
}
2129
}
2230
catch (error) {

src/lib.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@ mod logistic_map;
22

33
use std::collections::hash_map::DefaultHasher;
44
use std::hash::{Hash, Hasher};
5-
use std::u8;
6-
use wasm_bindgen::prelude::*;
75
use image::{self, ImageEncoder};
6+
use wasm_bindgen::prelude::*;
87

9-
// https://rustwasm.github.io/wasm-bindgen/examples/console-log.html
108
#[wasm_bindgen]
119
extern "C" {
1210
#[wasm_bindgen(js_namespace = console)]
@@ -19,7 +17,6 @@ macro_rules! console_log {
1917
pub struct ImageOptions {
2018
pub width: u32,
2119
pub height: u32,
22-
pub channels: u32,
2320
}
2421

2522
fn str2f(s: &str) -> f64 {
@@ -65,7 +62,6 @@ fn img2vec(im: &[u8], limit_max_side: Option<u32>) -> Result<(Vec<u8>, ImageOpti
6562
Ok((pixels, ImageOptions {
6663
width: rgb.width(),
6764
height: rgb.height(),
68-
channels: 3,
6965
}))
7066
}
7167

@@ -75,10 +71,10 @@ enum ImageType {
7571
}
7672

7773
fn vec2imblob(pixels: &Vec<u8>, im_opt: ImageOptions, limit_max_side: Option<u32>, im_type: ImageType) -> Result<Box<[u8]>, String> {
78-
let ImageOptions { mut width, mut height , channels} = im_opt;
74+
let ImageOptions { mut width, mut height } = im_opt;
7975
let mut img = image::RgbImage::new(width, height);
8076

81-
for (i, pixel) in pixels.chunks(channels as usize).enumerate() {
77+
for (i, pixel) in pixels.chunks(3).enumerate() {
8278
let x = (i % width as usize) as u32;
8379
let y = (i / width as usize) as u32;
8480
img.put_pixel(x, y, image::Rgb([

0 commit comments

Comments
 (0)