35
35
< br > < br >
36
36
< label > < input type ="checkbox " name ="slow " id ="slow " value ="value "> Simulate slow chunks</ label >
37
37
< 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 >
51
39
< br > < br >
52
40
< span id ="stats "> </ span >
53
41
< br > < br >
54
- Default decoder:< br >
55
- < canvas id ="output " style ="border: 1px solid black "> </ canvas >
56
- < br > < br >
57
- L2 decoder (wasm):< br >
58
42
< 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 >
65
44
< script >
66
45
67
46
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 ) ;
72
49
73
50
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 ) {
78
52
return ;
79
53
}
80
54
const canvas = document . getElementById ( 'output_wasm' ) ;
81
55
const ctx = canvas . getContext ( '2d' ) ;
82
56
// 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 ) ;
87
61
ctx . putImageData ( target , 0 , 0 ) ;
88
62
}
89
63
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
-
144
64
function hexColorToRGB ( color ) {
145
65
const value = parseInt ( color . slice ( 1 ) , 16 ) ;
146
66
return [
151
71
]
152
72
}
153
73
154
- function redraw ( ) {
155
- if ( imgS ) drawImage ( imgS ) ;
156
- }
157
-
158
74
async function setImage ( s ) {
159
75
clearInterval ( drawHandle ) ;
160
76
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 ) ) ) ;
165
79
166
80
// read in
167
- let start ;
168
81
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' ) ;
172
83
} else {
173
84
const response = await fetch ( '/testfiles/' + s ) ;
174
85
const bytes = new Uint8Array ( await response . arrayBuffer ( ) ) ;
175
- //decodeWasm(bytes);
176
- start = new Date ( ) ;
177
86
if ( document . getElementById ( 'slow' ) . checked ) {
178
- let localHandle = drawHandle = setInterval ( ( ) => {
179
- drawImage ( img ) ;
180
- drawImageWasm ( ) ;
181
- } , 100 ) ;
87
+ let localHandle = drawHandle = setInterval ( ( ) => drawImageWasm ( ) , 100 ) ;
182
88
let i = 0 ;
183
89
const endTens = bytes . length - ( bytes . length % 10 ) ;
184
90
while ( i < endTens ) {
185
91
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 ;
191
94
await new Promise ( resolve => setTimeout ( resolve , 1 ) ) ;
192
95
i += 10 ;
193
96
}
194
97
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 ) ;
198
99
}
199
100
clearInterval ( drawHandle ) ;
200
101
} 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 ) ;
223
103
}
224
104
}
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 ;
235
106
drawImageWasm ( ) ;
236
- console . log ( 'output to canvas time' , ( new Date ( ) ) - start ) ;
237
107
}
238
-
239
-
240
108
</ script >
241
109
</ body >
242
110
</ html >
0 commit comments