Skip to content

Commit 8306674

Browse files
committed
port JPEG decoding to Deno
1 parent 99909e9 commit 8306674

2 files changed

Lines changed: 61 additions & 63 deletions

File tree

‎ImageScript.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -958,7 +958,7 @@ export class Image {
958958
}
959959

960960
/**
961-
* Decodes a PNG image
961+
* Decodes an image (PNG or JPEG)
962962
* @param {Buffer|Uint8Array} buffer The binary data to decode
963963
* @return {Promise<Image>} The decoded image
964964
*/

‎utils/wasm/jpeg.js‎

Lines changed: 60 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
const {readFile} = require('fs').promises;
2-
const {join} = require('path');
1+
let wasm;
32

4-
let wasm = new Promise(async resolve => {
5-
const module = new WebAssembly.Module(await readFile(join(__dirname, './jpeg.wasm')));
3+
{
4+
const module = new WebAssembly.Module(await fetch('https://github.com/matmen/ImageScript/raw/deno/utils/wasm/jpeg.wasm').then(r => r.arrayBuffer()));
65
const instance = new WebAssembly.Instance(module);
7-
const wasm = instance.exports;
86

9-
resolve(wasm);
10-
});
7+
wasm = instance.exports;
8+
}
119

1210
let cachegetUint8Memory0 = null;
1311

@@ -53,60 +51,60 @@ function getArrayU8FromWasm0(ptr, len) {
5351
return getUint8Memory0().subarray(ptr / 1, ptr / 1 + len);
5452
}
5553

56-
module.exports = {
57-
/**
58-
* @param {number} ptr
59-
* @param {Uint8Array} buffer
60-
* @param {number} _width
61-
* @param {number} _height
62-
* @returns {number}
63-
*/
64-
async decode(ptr, buffer, _width, _height) {
65-
wasm = await wasm;
66-
const ptr0 = passArray8ToWasm0(buffer, wasm.__wbindgen_malloc);
67-
return wasm.decode(ptr, ptr0, WASM_VECTOR_LEN, _width, _height);
68-
},
69-
/**
70-
* @param {number} id
71-
* @returns {Uint16Array}
72-
*/
73-
meta(id) {
74-
try {
75-
const retptr = wasm.__wbindgen_export_1.value - 16;
76-
wasm.__wbindgen_export_1.value = retptr;
77-
wasm.meta(retptr, id);
78-
const r0 = getInt32Memory0()[retptr / 4];
79-
const r1 = getInt32Memory0()[retptr / 4 + 1];
80-
const v0 = getArrayU16FromWasm0(r0, r1).slice();
81-
wasm.__wbindgen_free(r0, r1 * 2);
82-
return v0;
83-
} finally {
84-
wasm.__wbindgen_export_1.value += 16;
85-
}
86-
},
87-
/**
88-
* @param {number} id
89-
* @returns {Uint8Array}
90-
*/
91-
buffer(id) {
92-
try {
93-
const retptr = wasm.__wbindgen_export_1.value - 16;
94-
wasm.__wbindgen_export_1.value = retptr;
95-
wasm.buffer(retptr, id);
96-
const r0 = getInt32Memory0()[retptr / 4];
97-
const r1 = getInt32Memory0()[retptr / 4 + 1];
98-
const v0 = getArrayU8FromWasm0(r0, r1).slice();
99-
wasm.__wbindgen_free(r0, r1 * 1);
100-
return v0;
101-
} finally {
102-
wasm.__wbindgen_export_1.value += 16;
103-
}
104-
},
105-
/**
106-
* @param {number} id
107-
*/
108-
free(id) {
109-
wasm.free(id);
54+
/**
55+
* @param {number} ptr
56+
* @param {Uint8Array} buffer
57+
* @param {number} _width
58+
* @param {number} _height
59+
* @returns {number}
60+
*/
61+
export async function decode(ptr, buffer, _width, _height) {
62+
wasm = await wasm;
63+
const ptr0 = passArray8ToWasm0(buffer, wasm.__wbindgen_malloc);
64+
return wasm.decode(ptr, ptr0, WASM_VECTOR_LEN, _width, _height);
65+
}
66+
67+
/**
68+
* @param {number} id
69+
* @returns {Uint16Array}
70+
*/
71+
export function meta(id) {
72+
try {
73+
const retptr = wasm.__wbindgen_export_1.value - 16;
74+
wasm.__wbindgen_export_1.value = retptr;
75+
wasm.meta(retptr, id);
76+
const r0 = getInt32Memory0()[retptr / 4];
77+
const r1 = getInt32Memory0()[retptr / 4 + 1];
78+
const v0 = getArrayU16FromWasm0(r0, r1).slice();
79+
wasm.__wbindgen_free(r0, r1 * 2);
80+
return v0;
81+
} finally {
82+
wasm.__wbindgen_export_1.value += 16;
83+
}
84+
}
85+
86+
/**
87+
* @param {number} id
88+
* @returns {Uint8Array}
89+
*/
90+
export function buffer(id) {
91+
try {
92+
const retptr = wasm.__wbindgen_export_1.value - 16;
93+
wasm.__wbindgen_export_1.value = retptr;
94+
wasm.buffer(retptr, id);
95+
const r0 = getInt32Memory0()[retptr / 4];
96+
const r1 = getInt32Memory0()[retptr / 4 + 1];
97+
const v0 = getArrayU8FromWasm0(r0, r1).slice();
98+
wasm.__wbindgen_free(r0, r1 * 1);
99+
return v0;
100+
} finally {
101+
wasm.__wbindgen_export_1.value += 16;
110102
}
111-
};
103+
}
112104

105+
/**
106+
* @param {number} id
107+
*/
108+
export function free(id) {
109+
wasm.free(id);
110+
}

0 commit comments

Comments
 (0)