Skip to content

Commit 3606df5

Browse files
committedAug 31, 2021
cleanup browser example
1 parent 4f3eba3 commit 3606df5

File tree

2 files changed

+23
-174
lines changed

2 files changed

+23
-174
lines changed
 

‎index.html

+18-150
Original file line numberDiff line numberDiff line change
@@ -35,112 +35,32 @@
3535
<br><br>
3636
<label><input type="checkbox" name="slow" id="slow" value="value"> Simulate slow chunks</label>
3737
<br>
38-
<label><input type="color" name="fillColor" id="fillColor" value="#000000" onchange="redraw()"> background color</label>
39-
<br>
40-
<label><input type="number" name="dx" id="dx" value="0" onchange="redraw()"> dx</label>
41-
<br>
42-
<label><input type="number" name="dy" id="dy" value="0" onchange="redraw()"> dy</label>
43-
<br>
44-
<label><input type="number" name="sx" id="sx" value="0" onchange="redraw()"> sx</label>
45-
<br>
46-
<label><input type="number" name="sy" id="sy" value="0" onchange="redraw()"> sy</label>
47-
<br>
48-
<label><input type="number" name="swidth" id="swidth" value="0" onchange="redraw()"> swidth</label>
49-
<br>
50-
<label><input type="number" name="sheight" id="sheight" value="0" onchange="redraw()"> sheight</label>
38+
<label><input type="color" name="fillColor" id="fillColor" value="#000000"> background color</label>
5139
<br><br>
5240
<span id="stats"></span>
5341
<br><br>
54-
Default decoder:<br>
55-
<canvas id="output" style="border: 1px solid black"></canvas>
56-
<br><br>
57-
L2 decoder (wasm):<br>
5842
<canvas id="output_wasm" style="border: 1px solid black"></canvas>
59-
<br><br>
60-
Reencoded with img2sixel:<br>
61-
<canvas id="output2" style="border: 1px solid black"></canvas>
62-
<script src="/dist/bundle.js"></script>
63-
64-
<script id="sampsa" type="application/json"></script>
43+
<script src="/dist/full.umd.js"></script>
6544
<script>
6645

6746
let drawHandle = null;
68-
let imgS = null;
69-
let imgWasm = null;
70-
let wasmDecoder = null;
71-
sixel.DecoderAsync().then(dec => wasmDecoder = dec);
47+
let decoder = null;
48+
sixel.DecoderAsync().then(dec => decoder = dec);
7249

7350
function drawImageWasm() {
74-
const data = wasmDecoder.data32;
75-
const width = wasmDecoder.width;
76-
const height = wasmDecoder.height;
77-
if (!height || !width) {
51+
if (!decoder.height || !decoder.width) {
7852
return;
7953
}
8054
const canvas = document.getElementById('output_wasm');
8155
const ctx = canvas.getContext('2d');
8256
// resize canvas to show full image
83-
canvas.width = width;
84-
canvas.height = height;
85-
const target = new ImageData(width, height);
86-
new Uint32Array(target.data.buffer).set(data);
57+
canvas.width = decoder.width;
58+
canvas.height = decoder.height;
59+
const target = new ImageData(decoder.width, decoder.height);
60+
new Uint32Array(target.data.buffer).set(decoder.data32);
8761
ctx.putImageData(target, 0, 0);
8862
}
8963

90-
/**
91-
* example how to get the img data
92-
*/
93-
function drawImage(img) {
94-
if (!img.height || !img.width) {
95-
return;
96-
}
97-
const canvas = document.getElementById('output');
98-
const ctx = canvas.getContext('2d');
99-
// resize canvas to show full image
100-
canvas.width = img.width;
101-
canvas.height = img.height;
102-
// grab imagedata
103-
// const target = ctx.getImageData(0, 0, img.width, img.height);
104-
const target = new ImageData(img.width, img.height);
105-
img.toPixelData(
106-
// target metrics
107-
target.data, img.width, img.height,
108-
// dx, dy
109-
parseInt(document.getElementById('dx').value), parseInt(document.getElementById('dy').value),
110-
// sx, sy,
111-
parseInt(document.getElementById('sx').value), parseInt(document.getElementById('sy').value),
112-
// width, height
113-
parseInt(document.getElementById('swidth').value), parseInt(document.getElementById('sheight').value),
114-
// fill color
115-
sixel.toRGBA8888(...hexColorToRGB(document.getElementById('fillColor').value)));
116-
ctx.putImageData(target, 0, 0);
117-
118-
// test encoding by re-encoding the output above
119-
//const reEncoded = sixel.image2sixel(target.data, img.width, img.height);
120-
//const dim = new sixel.DimensionDecoder().decodeString(reEncoded.slice(7, -2));
121-
//if (sixel.canUseWasm(dim.width, dim.height, 4096)) {
122-
// wasmDecoder.init(dim.width, dim.height, 0)
123-
// wasmDecoder.decodeString(reEncoded.slice(7, -2));
124-
// const canvas2 = document.getElementById('output2');
125-
// canvas2.width = dim.width;
126-
// canvas2.height = dim.height;
127-
// const ctx2 = canvas2.getContext('2d');
128-
// const target2 = ctx.getImageData(0, 0, dim.width, dim.height);
129-
// new Uint32Array(target2.data.buffer).set(wasmDecoder.data32);
130-
// ctx2.putImageData(target2, 0, 0);
131-
//} else {
132-
// const six2 = new sixel.SixelDecoder();
133-
// six2.decodeString(reEncoded.slice(7, -2)); // strip off enclosing escape sequence
134-
// const canvas2 = document.getElementById('output2');
135-
// canvas2.width = six2.width;
136-
// canvas2.height = six2.height;
137-
// const ctx2 = canvas2.getContext('2d');
138-
// const target2 = ctx.getImageData(0, 0, six2.width, six2.height);
139-
// six2.toPixelData(target2.data, six2.width, six2.height);
140-
// ctx2.putImageData(target2, 0, 0);
141-
//}
142-
}
143-
14464
function hexColorToRGB(color) {
14565
const value = parseInt(color.slice(1), 16);
14666
return [
@@ -151,92 +71,40 @@
15171
]
15272
}
15373

154-
function redraw() {
155-
if (imgS) drawImage(imgS);
156-
}
157-
15874
async function setImage(s) {
15975
clearInterval(drawHandle);
16076

161-
// create image
162-
const img = new sixel.SixelDecoder();
163-
wasmDecoder.init(sixel.toRGBA8888(...hexColorToRGB(document.getElementById('fillColor').value)));
164-
imgS = img;
77+
// initialize decoder
78+
decoder.init(sixel.toRGBA8888(...hexColorToRGB(document.getElementById('fillColor').value)));
16579

16680
// read in
167-
let start;
16881
if (s === 'wiki') {
169-
start = new Date();
170-
img.decodeString('#0;2;0;0;0#1;2;100;100;0#2;2;0;100;0#1~~@@vv@@~~@@~~~~@@vv@@~~@@~~~~@@vv@@~~@@~~$#2??}}GG}}??}}????}}GG}}??}}????}}GG}}??}}??$-#1!14!14!14A');
171-
wasmDecoder.decodeString('#0;2;0;0;0#1;2;100;100;0#2;2;0;100;0#1~~@@vv@@~~@@~~~~@@vv@@~~@@~~~~@@vv@@~~@@~~$#2??}}GG}}??}}????}}GG}}??}}????}}GG}}??}}??$-#1!42A');
82+
decoder.decodeString('#0;2;0;0;0#1;2;100;100;0#2;2;0;100;0#1~~@@vv@@~~@@~~~~@@vv@@~~@@~~~~@@vv@@~~@@~~$#2??}}GG}}??}}????}}GG}}??}}????}}GG}}??}}??$-#1!42A');
17283
} else {
17384
const response = await fetch('/testfiles/' + s);
17485
const bytes = new Uint8Array(await response.arrayBuffer());
175-
//decodeWasm(bytes);
176-
start = new Date();
17786
if (document.getElementById('slow').checked) {
178-
let localHandle = drawHandle = setInterval(() => {
179-
drawImage(img);
180-
drawImageWasm();
181-
}, 100);
87+
let localHandle = drawHandle = setInterval(() => drawImageWasm(), 100);
18288
let i = 0;
18389
const endTens = bytes.length - (bytes.length % 10);
18490
while (i < endTens) {
18591
if (drawHandle !== localHandle) return;
186-
img.decode(bytes, i, i + 10);
187-
wasmDecoder.decode(bytes, i, i + 10);
188-
document.getElementById('stats').innerText = 'width: ' + img.width + '\nheight: ' + img.height;
189-
document.getElementById('swidth').value = img.width;
190-
document.getElementById('sheight').value = img.height;
92+
decoder.decode(bytes, i, i + 10);
93+
document.getElementById('stats').innerText = 'width: ' + decoder.width + '\nheight: ' + decoder.height;
19194
await new Promise(resolve => setTimeout(resolve, 1));
19295
i += 10;
19396
}
19497
if (bytes.length % 10) {
195-
img.decode(bytes, endTens, bytes.length);
196-
wasmDecoder.decode(bytes, endTens, bytes.length);
197-
//decodeWasm(bytes.subarray(endTens, endTens + bytes.length));
98+
decoder.decode(bytes, endTens, bytes.length);
19899
}
199100
clearInterval(drawHandle);
200101
} else {
201-
img.decode(bytes);
202-
//decodeWasm(bytes);
203-
wasmDecoder.decode(bytes);
204-
205-
//import('/dist/decode.es6.js').then(m => m.sixelDecodeAsync(bytes)).then(r => {
206-
////sixel_new.sixelDecodeAsync(bytes).then(r => {
207-
// console.log(r);
208-
// const data = r.data32;
209-
// const width = r.width;
210-
// const height = r.height;
211-
// if (!height || !width) {
212-
// return;
213-
// }
214-
// const canvas = document.getElementById('output_wasm');
215-
// const ctx = canvas.getContext('2d');
216-
// // resize canvas to show full image
217-
// canvas.width = width;
218-
// canvas.height = height;
219-
// const target = new ImageData(width, height);
220-
// new Uint32Array(target.data.buffer).set(data);
221-
// ctx.putImageData(target, 0, 0);
222-
//});
102+
decoder.decode(bytes);
223103
}
224104
}
225-
document.getElementById('stats').innerText = 'width: ' + img.width + '\nheight: ' + img.height;
226-
document.getElementById('swidth').value = img.width;
227-
document.getElementById('sheight').value = img.height;
228-
229-
230-
console.log('read & conversion time', (new Date()) - start);
231-
232-
// output
233-
start = new Date();
234-
drawImage(img);
105+
document.getElementById('stats').innerText = 'width: ' + decoder.width + '\nheight: ' + decoder.height;
235106
drawImageWasm();
236-
console.log('output to canvas time', (new Date()) - start);
237107
}
238-
239-
240108
</script>
241109
</body>
242110
</html>

‎webpack.config.js

+5-24
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,5 @@
11
const path = require('path');
22

3-
4-
// FIXME: to be removed when done with decode|encode|full bundle
5-
const test_bundle = {
6-
entry: './src/index.ts',
7-
module: {
8-
rules: [
9-
{
10-
test: /\.tsx?$/,
11-
use: 'ts-loader',
12-
exclude: /node_modules/
13-
}
14-
]
15-
},
16-
resolve: {
17-
extensions: [ '.tsx', '.ts', '.js' ]
18-
},
19-
output: {
20-
filename: 'bundle.js',
21-
path: path.resolve(__dirname, 'dist'),
22-
library: ['sixel']
23-
}
24-
};
25-
263
const full_esm = {
274
entry: `./lib-esm/index.js`,
285
devtool: 'source-map',
@@ -161,4 +138,8 @@ const encode_umd = {
161138
mode: 'production'
162139
};
163140

164-
module.exports = [test_bundle, full_esm, full_umd, decode_esm, decode_umd, encode_esm, encode_umd];
141+
module.exports = [
142+
full_esm, full_umd,
143+
decode_esm, decode_umd,
144+
encode_esm, encode_umd
145+
];

0 commit comments

Comments
 (0)
Please sign in to comment.