/
index.html
469 lines (423 loc) · 16.5 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
<!DOCTYPE HTML>
<html>
<head>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<script type="text/javascript" src="../../dist/charlsjs.js"></script>
</head>
<body>
<div class="container">
<div class="page-header">
<h1>JPEG-LS Decoding with WebAssembly (v<span id="version"></span>)</div></h1>
<p class="lead">
Select an image or drag and drop a JPEG-LS file. Files dropped here remain local in your browser, they are not uploaded anywhere.
</p>
<div>
<select id="imageSelector">
<option value="../../test/fixtures/jls/CT1.JLS" selected>CT1</option>
<option value="../../test/fixtures/jls/CT2.JLS">CT2</option>
<option value="../../test/fixtures/jls/MG1.JLS">MG1</option>
<option value="../../test/fixtures/jls/MR1.JLS">MR1</option>
<option value="../../test/fixtures/jls/MR2.JLS">MR2</option>
<option value="../../test/fixtures/jls/MR3.JLS">MR3</option>
<option value="../../test/fixtures/jls/MR4.JLS">MR4</option>
<option value="../../test/fixtures/jls/NM1.JLS">NM1</option>
<option value="../../test/fixtures/jls/RG1.JLS">RG1</option>
<option value="../../test/fixtures/jls/RG2.JLS">RG2</option>
<option value="../../test/fixtures/jls/RG3.JLS">RG3</option>
<option value="../../test/fixtures/jls/SC1.JLS">SC1</option>
<option value="../../test/fixtures/jls/XA1.JLS">XA1</option>
</select>
<button id="benchmark">Benchmark</button>
<label><input id="signed" type="checkbox" value="">Signed Data - check to properly display signed DICOM images</label>
</div>
<div class="row">
<label><input id="visualizeDeltas" type="checkbox" value="">Visualize Deltas</label>
</div>
<div class="row">
<div class="col-md-4">Status:<span id="status"></span></div>
</div>
<div class="row">
<div class="col-md-4">Encode Time: <span id="encodeTime"></span></div>
<div class="col-md-4">Decode Time: <span id="decodeTime"></span></div>
<div class="col-md-4">Display Time: <span id="displayTime"></span></div>
</div>
<div class="row">
<div class="col-md-4">Encoded Size: <span id="encodedSize"></span></div>
<div class="col-md-4">Decoded Size: <span id="decodedSize"></span></div>
<div class="col-md-4">Compression Ratio: <span id="compressionRatio"></span></div>
</div>
<div class="row">
<div class="col-md-4">Resolution: <span id="resolution"></span></div>
<div class="col-md-4">Pixel Format: <span id="pixelFormat"></span></div>
<div class="col-md-4">Component Count: <span id="componentCount"></span></div>
</div>
<div class="row">
<div class="col-md-4">Min Pixel: <span id="minPixel"></span></div>
<div class="col-md-4">Max Pixel: <span id="maxPixel"></span></div>
<div class="col-md-4">Dynamic Range: <span id="dynamicRange"></span></div>
</div>
<hr>
<div class="row">
<div class="col-md-4">Interleave Mode: <span id="interleaveMode"></span></div>
<div class="col-md-4">NEAR Parameter: <span id="nearLossless"></span></div>
</div>
<hr>
<div class="row">
<div class="col-md-12">
<b>Encoding Parameters</b>
</div>
</div>
<div class="row">
<div class="col-md-12">
<span>NEAR Parameter: </span><span id="nearParameter">0</span>
<input type="range" class="custom-range" id="nearParameterRange" min="0" max="100" value="0">
</div>
</div>
<div class="row">
<div class="col-md-12">
<button id="download">Download</button>
</div>
</div>
<hr>
<div class="row">
<div class="col-md-6">
<canvas id='myCanvas' width="512" height="512"/>
</div>
</div>
</body>
<script>
let decoder = undefined;
let encoder = undefined;
let encodedBitStream = undefined;
let uncompressedImageFrame = undefined;
let frameInfo = undefined;
let quantization = 0;
let minMax = undefined;
let interleaveMode = undefined;
function getMinMax(frameInfo, pixelData) {
const numPixels = frameInfo.width * frameInfo.height * frameInfo.componentCount;
let min = pixelData[0];
let max = pixelData[0];
for(let i=0; i < numPixels; i++) {
if(pixelData[i] < min) {
min = pixelData[i];
}
if(pixelData[i] > max) {
max = pixelData[i];
}
}
return {min, max};
}
function getPixelData(frameInfo, decodedBuffer, signed) {
if(frameInfo.bitsPerSample > 8) {
if(signed) {
return new Int16Array(decodedBuffer.buffer, decodedBuffer.byteOffset, decodedBuffer.byteLength / 2);
} else {
return new Uint16Array(decodedBuffer.buffer, decodedBuffer.byteOffset, decodedBuffer.byteLength / 2);
}
} else {
return decodedBuffer;
}
}
function colorToCanvas(frameInfo, pixelData, imageData, interleaveMode) {
const planeSize = frameInfo.width * frameInfo.height;
const shift = frameInfo.bitsPerSample > 8 ? frameInfo.bitsPerSample - 8 : 0;
let outOffset = 0;
let inOffset = 0;
for(var y=0; y < frameInfo.height; y++) {
for (var x = 0; x < frameInfo.width; x++) {
if (interleaveMode === 0) { // planar (RRRRGGGGBBBB)
imageData.data[outOffset++] = pixelData[inOffset] >> shift;
imageData.data[outOffset++] = pixelData[inOffset + planeSize] >> shift;
imageData.data[outOffset++] = pixelData[inOffset + (planeSize * 2)] >> shift;
imageData.data[outOffset++] = 255;
inOffset++;
}
else if (interleaveMode === 1) { // line (actually RGB)
imageData.data[outOffset++] = pixelData[inOffset++] >> shift;
imageData.data[outOffset++] = pixelData[inOffset++] >> shift;
imageData.data[outOffset++] = pixelData[inOffset++] >> shift;
imageData.data[outOffset++] = 255;
}
else if (interleaveMode === 2) { // pixel/sample (actually RGB)
imageData.data[outOffset++] = pixelData[inOffset++] >> shift;
imageData.data[outOffset++] = pixelData[inOffset++] >> shift;
imageData.data[outOffset++] = pixelData[inOffset++] >> shift;
imageData.data[outOffset++] = 255;
}
}
}
}
function grayToCanvas(frameInfo, pixelData, imageData, signed) {
var outOffset = 0;
var planeSize = frameInfo.width * frameInfo.height;
var inOffset = 0;
minMax = getMinMax(frameInfo, pixelData);
$('#minPixel').text('' + minMax.min);
$('#maxPixel').text('' + minMax.max);
//console.log(minMax);
let dynamicRange = minMax.max - minMax.min;
$('#dynamicRange').text('' + dynamicRange);
//console.log('dynamicRange=', dynamicRange);
let bitsOfData = 1;
while(dynamicRange > 1) {
dynamicRange = dynamicRange >> 1;
bitsOfData++;
}
//console.log('bitsOfData = ', bitsOfData);
let bitShift = bitsOfData - 8;
const offset = -minMax.min;
//console.log('bitShift=', bitShift);
//console.log('offset=', offset);
for(var y=0; y < frameInfo.height; y++) {
for (var x = 0; x < frameInfo.width; x++) {
if(frameInfo.bitsPerSample <= 8) {
const value = pixelData[inOffset++];
imageData.data[outOffset] = value;
imageData.data[outOffset + 1] = value;
imageData.data[outOffset + 2] = value;
imageData.data[outOffset + 3] = 255;
outOffset += 4;
}
else // bitsPerSample > 8
{
// Do a simple transformation to display 16 bit data:
// * Offset the pixels so the smallest value is 0
// * Shift the pixels to display the most significant 8 bits
const fullPixel = pixelData[inOffset++] + offset;
const value = (fullPixel >> bitShift);
imageData.data[outOffset] = value;
imageData.data[outOffset + 1] = value;
imageData.data[outOffset + 2] = value;
imageData.data[outOffset + 3] = 255;
outOffset += 4;
}
}
}
}
function deltasToCanvas(frameInfo, pixelData, imageData, signed) {
if(!uncompressedImageFrame) {
return;
}
const deltas = new Int32Array(frameInfo.height * frameInfo.width);
const uif = getPixelData(frameInfo, uncompressedImageFrame, signed);
let inOffset = 0;
let outOffset = 0;
for(var y=0; y < frameInfo.height; y++) {
for (var x = 0; x < frameInfo.width; x++) {
const unc = uif[inOffset];
const comp = pixelData[inOffset];
deltas[inOffset++] = Math.abs(comp - unc);
}
}
const deltaMinMax = getMinMax(frameInfo, deltas);
console.log('deltas min/max', deltaMinMax);
inOffset = 0;
for(var y=0; y < frameInfo.height; y++) {
for (var x = 0; x < frameInfo.width; x++) {
if(uncompressedImageFrame) {
const delta = deltas[inOffset];
inOffset++;
imageData.data[outOffset] = delta;
imageData.data[outOffset + 1] = delta;
imageData.data[outOffset + 2] = delta;
imageData.data[outOffset + 3] = 255;
outOffset += 4;
}
}
}
}
function display(frameInfo, decodedBuffer, interleaveMode) {
// Use the signed option for monochrome (1 component) image. Multi component images are always unsigned.
const signed = $('#signed').is(":checked") && frameInfo.componentCount === 1;
const pixelData = getPixelData(frameInfo, decodedBuffer, signed);
const begin = performance.now(); // performance.now() returns value in milliseconds
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
c.width = frameInfo.width;
c.height = frameInfo.height;
var myImageData = ctx.createImageData(frameInfo.width, frameInfo.height);
const visualizeDeltas = $('#visualizeDeltas').is(":checked");
if(frameInfo.componentCount > 1) {
colorToCanvas(frameInfo, pixelData, myImageData, interleaveMode);
} else {
if(visualizeDeltas) {
deltasToCanvas(frameInfo, pixelData, myImageData, signed);
} else {
grayToCanvas(frameInfo, pixelData, myImageData, signed);
}
}
ctx.putImageData(myImageData, 0, 0);
const end = performance.now();
$('#displayTime').text((end-begin).toFixed(2) + ' ms');
}
function decode(iterations = 1) {
$('#encodedSize').text('' + encodedBitStream.length.toLocaleString() + ' bytes');
// Setup
const encodedBuffer = decoder.getEncodedBuffer(encodedBitStream.length);
let begin = performance.now(); // performance.now() returns value in milliseconds
encodedBuffer.set(encodedBitStream);
let end = performance.now();
// Decode
begin = performance.now(); // performance.now() returns value in milliseconds
for(let i=0; i < iterations; i++) {
decoder.decode();
}
end = performance.now();
$('#decodeTime').text(((end-begin) / iterations).toFixed(2) + ' ms');
frameInfo = decoder.getFrameInfo();
interleaveMode = decoder.getInterleaveMode();
const nearLossless = decoder.getNearLossless();
// Display image properties
$('#resolution').text(''+frameInfo.width + 'x' + frameInfo.height);
$('#pixelFormat').text(''+frameInfo.bitsPerSample +' bpp');
$('#componentCount').text(''+frameInfo.componentCount);
$('#interleaveMode').text(''+interleaveMode);
$('#nearLossless').text(''+nearLossless);
// Display Image
var decodedBuffer = decoder.getDecodedBuffer();
$('#decodedSize').text(''+decodedBuffer.length.toLocaleString() + " bytes");
$('#compressionRatio').text('' + (decodedBuffer.length / encodedBitStream.length).toFixed(2) + ":1");
display(frameInfo, decodedBuffer, interleaveMode);
}
function encode(NEARParameter, iterations = 1) {
// Setup buffer
const decodedBytes = encoder.getDecodedBuffer(frameInfo);
decodedBytes.set(uncompressedImageFrame);
encoder.setNearLossless(NEARParameter);
encoder.setInterleaveMode(interleaveMode)
// Do the encode
begin = performance.now(); // performance.now() returns value in milliseconds
for(let i=0; i < iterations; i++) {
encoder.encode();
}
end = performance.now();
$('#encodeTime').text(((end-begin) / iterations).toFixed(2) + ' ms');
}
function load(url) {
if(url === '../../test/fixtures/jls/CT1.JLS' ||
url === '../../test/fixtures/jls/CT2.JLS') {
$('#signed').prop('checked', true);
} else {
$('#signed').prop('checked', false);
}
fetch(url)
.then((response) => {
return response.arrayBuffer();
})
.then((arrayBuffer) => {
// {
encodedBitStream = new Uint8Array(arrayBuffer);
decode();
uncompressedImageFrame = new Uint8Array(decoder.getDecodedBuffer().length);
uncompressedImageFrame.set(decoder.getDecodedBuffer()); //}
//catch(ex) {
// $('#status').text('Exception thrown while parsing ' + ex);
// }
});//.catch(function() {
//$//('#status').text('error loading ' + url);
//});
}
function reset() {
minMax = undefined;
const c = document.getElementById("myCanvas");
const ctx = c.getContext("2d");
ctx.fillRect(0,0,c.width, c.height);
$('#status').text('');
$('#encodedSize').text('');
$('#decodedSize').text('');
$('#compressionRatio').text('');
$('#decodeTime').text('');
$('#displayTime').text('');
$('#resolution').text('');
$('#pixelFormat').text('');
$('#componentCount').text('');
$('#interleaveMode').text('');
$('#nearLossless').text('');
}
Module.onRuntimeInitialized = async _ => {
$("#version").text(Module.getVersion())
decoder = new Module.JpegLSDecoder();
encoder = new Module.JpegLSEncoder();
load('../../test/fixtures/jls/CT1.JLS');
//load('../../test/fixtures/artificial-line.jls');
$('#imageSelector').change(function(e) {
reset();
load(e.target.options[e.target.selectedIndex].value);
});
$('#nearParameterRange').on('input', function(e) {
NEARParameter = parseFloat($(this).val());
$('#nearParameter').text('' + NEARParameter);
encode(NEARParameter);
// Get the encoded bytes and display them
const encodedBytes = encoder.getEncodedBuffer();
encodedBitStream = encodedBytes;
decode();
});
$('#benchmark').click(function(e) {
$('#status').text('Please wait while benchmark runs....');
setTimeout(() => {
decode(50)
$('#status').text('');
}, 1);
});
// set an event handler for the signed checkbox
$('#signed').change(function() {
console.log('checked toggle')
// this will contain a reference to the checkbox
if (this.checked) {
decode();
} else {
decode();
}
});
$('#visualizeDeltas').change(function() {
// this will contain a reference to the checkbox
if (this.checked) {
decode();
} else {
decode();
}
});
// this function gets called once the user drops the file onto the div
function handleFileSelect(evt) {
evt.stopPropagation();
evt.preventDefault();
// Get the FileList object that contains the list of files that were dropped
var files = evt.dataTransfer.files;
// this UI is only built for a single file so just dump the first one
var file = files[0];
var fileReader = new FileReader();
fileReader.onload = function (e) {
var fileAsArrayBuffer = e.target.result;
encodedBitStream = new Uint8Array(fileAsArrayBuffer);
reset();
try {
decode();
}
catch(ex) {
$('#status').text('Exception thrown while parsing ' + ex);
}
};
fileReader.readAsArrayBuffer(file);
}
function handleDragOver(evt) {
evt.stopPropagation();
evt.preventDefault();
evt.dataTransfer.dropEffect = 'copy'; // Explicitly show this is a copy.
}
// Setup the dnd listeners.
var dropZone = document.getElementById('myCanvas');
dropZone.addEventListener('dragover', handleDragOver, false);
dropZone.addEventListener('drop', handleFileSelect, false);
$('#download').click(function() {
// download the de-identified DICOM P10 bytestream
var blob = new Blob([encodedBitStream], {type: "image/jls"});
var url = window.URL.createObjectURL(blob);
window.open(url);
window.URL.revokeObjectURL(url);
});
}
</script>
</html>