-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.html
More file actions
68 lines (59 loc) · 2.21 KB
/
index.html
File metadata and controls
68 lines (59 loc) · 2.21 KB
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
<html>
<body>
<script>
let mem, wasmInstance;
function fetchAndInstantiateWasm (imports) {
return fetch('jpeg-decode.wasm').then(response =>
response.arrayBuffer()
)
.then(bytes => WebAssembly.compile(bytes))
.then(module =>
WebAssembly.instantiate(module, imports || {})
)
.then(instance => instance.exports)
.then(instance => {
mem = instance.memory;
wasmInstance = instance;
});
}
function writeBytes(byteArray, offset) {
console.log('Write bytes to offset: ',offset);
const outBuf = new Uint8Array(mem.buffer, offset, byteArray.length);
for (let i = 0; i < byteArray.length; i++) {
outBuf[i] = byteArray[i];
}
}
function readBytes(offset, length) {
console.log('Read bytes from offset: ', offset);
const outBuf = new Uint8Array(mem.buffer, offset, length);
let result = new Uint8Array(length);
for (let i = 0; i < outBuf.length; i++) {
result[i] = outBuf[i];
}
return result;
}
function convertBytesWithWASM(jpegBytes) {
console.log('Convert bytes: ', jpegBytes);
writeBytes(jpegBytes, wasmInstance.getInOffset(jpegBytes.length));
let resultPointer = wasmInstance.jpeg_decode();
let resultSize = wasmInstance.getResultSize();
console.log('Result bytes: ', readBytes(resultPointer, resultSize));
}
function test() {
var xhr = new XMLHttpRequest();
xhr.open('get', 'test.jpg');
xhr.responseType = 'blob';
xhr.onload = function(){
var fr = new FileReader();
fr.onload = function(){
let arr = new Uint8Array(this.result);
convertBytesWithWASM(arr);
};
fr.readAsArrayBuffer(xhr.response);
}
xhr.send();
}
fetchAndInstantiateWasm();
</script>
</body>
</html>