4
Emscripten: OpenJPEG Demo
6
<script type="text/javascript">
10
<script src="openjpeg.cc.js"></script>
11
<script src="syntensity_lobby.j2k.js"></script>
12
<script type="text/javascript">
13
// Wrapper around OpenJPEG
14
function j2k_to_image(data) {
16
_STDIO.prepare('image.j2k', data);
17
callMain(['-i', 'image.j2k', '-o', 'image.raw']);
18
return _STDIO.streams[_STDIO.filenames['image.raw']].data;
21
var imageWidth, imageHeight;
23
// print function which the runtime will call. We figure out the image dimensions from there
24
function print(text) {
25
document.getElementById('output').innerHTML += text + '<br>';
26
var m = /Component 0 characteristics: (\d+)x(\d+)x(\d+) unsigned/.exec(text);
30
document.getElementById('output').innerHTML += '<b>(Image dimensions: ' + [imageWidth, imageHeight] + ')</b><br>';
33
function render(url) {
34
imageWidth = imageHeight = null;
36
// Demo image by default
39
// If given a URL, fetch it
40
if (url && url[0] != '(') {
42
var xhr = new XMLHttpRequest();
43
xhr.open("GET", url, false);
45
var buffer = xhr.mozResponseArrayBuffer;
46
if (buffer) data = new Uint8Array(buffer);
48
alert('Could not load URL: ' + e);
53
document.getElementById('output').innerHTML = '';
54
output = j2k_to_image(data);
55
if (!(imageWidth && imageHeight)) { // We should have figured these values out
56
alert('An error occurred.');
60
var canvas = document.getElementById('canvas');
61
canvas.width = imageWidth;
62
canvas.height = imageHeight;
64
var ctx = canvas.getContext('2d');
65
var image = ctx.getImageData(0, 0, canvas.width, canvas.height);
67
var componentSize = canvas.width*canvas.height;
68
for (var y = 0; y < canvas.height; y++) {
69
for (var x = 0; x < canvas.width; x++) {
70
var value = output[y*canvas.width + x];
71
var base = (y*canvas.width + x)*4;
72
image.data[base + 0] = output[0*componentSize + y*canvas.width + x];
73
image.data[base + 1] = output[1*componentSize + y*canvas.width + x];
74
image.data[base + 2] = output[2*componentSize + y*canvas.width + x];
75
image.data[base + 3] = 255;
78
ctx.putImageData(image, 0, 0);
83
<h1>JPEG 2000 on the Web</h1>
84
<p>This is a demo of decoding <a href="http://en.wikipedia.org/wiki/JPEG_2000">JPEG 2000</a> images entirely in
85
JavaScript. It uses <a href="http://www.openjpeg.org/">OpenJPEG</a>, an open source library for JPEG 2000 images,
86
which was compiled to JavaScript using <a href="http://emscripten.org">Emscripten</a>.</p>
88
decoded into pixel data, it is rendered using a Canvas element. This demo should therefore work in any web
89
browser that supports the Canvas element, whether or not that web browser natively supports the JPEG 2000 format.</p>
90
<p><b>Click 'Go!'</b> to render a demo image (from <a href="http://syntensity.com/toplevel/screenshots/">here</a>)
91
which has been encoded into a JSON object.
92
You can also change the URL to that of a binary JPEG 2000 image, which will be
93
downloaded and rendered, but I am not aware of a cross-browser way to receive binary data, so it uses
94
a <a href="https://developer.mozilla.org/en/using_xmlhttprequest#Receiving_binary_data_using_JavaScript_typed_arrays">typed array
95
property of XHRs</a> (which will only work on Firefox 4).</p>
97
<canvas id='canvas' width=1 height=1></canvas>
99
<form onsubmit="render(texty.value); return false">
100
JPEG 2000 URL: <input type="text" name="texty" size=60 value="(replace this with a URL of a JPEG 2000 file, or just click 'Go!')" onclick="if (value[0] === '(') value=''"><br>
101
<input type="submit" value="Go!">
104
<div id="output" style="font-family: Courier New,Courier,monospace;"></div>