Skip to content

Commit 47f181b

Browse files
committed
TIFF decoding
(cherry picked from commit 7da8d4b)
1 parent ab2f4f0 commit 47f181b

3 files changed

Lines changed: 120 additions & 1 deletion

File tree

‎ImageScript.js‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import * as fontlib from './utils/wasm/font.js';
44
import * as svglib from './utils/wasm/svg.js';
55
import * as jpeglib from './utils/wasm/jpeg.js';
66

7+
const tifflib = require('./utils/wasm/tiff');
8+
79
/**
810
* Represents an image; provides utility functions
911
*/
@@ -1093,7 +1095,7 @@ export class Image {
10931095
}
10941096

10951097
/**
1096-
* Decodes an image (PNG or JPEG)
1098+
* Decodes an image (PNG, JPEG or TIFF)
10971099
* @param {Buffer|Uint8Array} data The binary data to decode
10981100
* @return {Promise<Image>} The decoded image
10991101
*/
@@ -1140,6 +1142,15 @@ export class Image {
11401142
image.bitmap[i + 3] = 0xff;
11411143
}
11421144
}
1145+
} else if (view.getUint32(0, false) === 0x49492a00) {
1146+
const status = await tifflib.decode(0, data);
1147+
if (status === 1) throw new Error('Failed decoding TIFF image');
1148+
const meta = tifflib.meta(0);
1149+
const buffer = tifflib.buffer(0);
1150+
tifflib.free(0);
1151+
1152+
image = new this(...meta);
1153+
image.bitmap.set(buffer);
11431154
} else throw new Error('Unsupported image type');
11441155

11451156
return image;

‎utils/wasm/tiff.js‎

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
const {readFile} = require('fs').promises;
2+
const {join} = require('path');
3+
4+
let wasm;
5+
6+
let cachegetUint8Memory0 = null;
7+
8+
function getUint8Memory0() {
9+
if (cachegetUint8Memory0 === null || cachegetUint8Memory0.buffer !== wasm.memory.buffer) {
10+
cachegetUint8Memory0 = new Uint8Array(wasm.memory.buffer);
11+
}
12+
return cachegetUint8Memory0;
13+
}
14+
15+
let WASM_VECTOR_LEN = 0;
16+
17+
function passArray8ToWasm0(arg, malloc) {
18+
const ptr = malloc(arg.length * 1);
19+
getUint8Memory0().set(arg, ptr / 1);
20+
WASM_VECTOR_LEN = arg.length;
21+
return ptr;
22+
}
23+
24+
let cachegetInt32Memory0 = null;
25+
26+
function getInt32Memory0() {
27+
if (cachegetInt32Memory0 === null || cachegetInt32Memory0.buffer !== wasm.memory.buffer) {
28+
cachegetInt32Memory0 = new Int32Array(wasm.memory.buffer);
29+
}
30+
return cachegetInt32Memory0;
31+
}
32+
33+
let cachegetUint32Memory0 = null;
34+
35+
function getUint32Memory0() {
36+
if (cachegetUint32Memory0 === null || cachegetUint32Memory0.buffer !== wasm.memory.buffer) {
37+
cachegetUint32Memory0 = new Uint32Array(wasm.memory.buffer);
38+
}
39+
return cachegetUint32Memory0;
40+
}
41+
42+
function getArrayU32FromWasm0(ptr, len) {
43+
return getUint32Memory0().subarray(ptr / 4, ptr / 4 + len);
44+
}
45+
46+
function getArrayU8FromWasm0(ptr, len) {
47+
return getUint8Memory0().subarray(ptr / 1, ptr / 1 + len);
48+
}
49+
50+
module.exports = {
51+
/**
52+
* @param {number} ptr
53+
* @param {Uint8Array} buffer
54+
* @returns {number}
55+
*/
56+
async decode(ptr, buffer) {
57+
if (!wasm) {
58+
const module = new WebAssembly.Module(await readFile(join(__dirname, './tiff.wasm')));
59+
const instance = new WebAssembly.Instance(module);
60+
wasm = instance.exports;
61+
}
62+
63+
const ptr0 = passArray8ToWasm0(buffer, wasm.__wbindgen_malloc);
64+
return wasm.decode(ptr, ptr0, WASM_VECTOR_LEN);
65+
},
66+
/**
67+
* @param {number} id
68+
* @returns {Uint32Array}
69+
*/
70+
meta(id) {
71+
try {
72+
const retptr = wasm.__wbindgen_export_1.value - 16;
73+
wasm.__wbindgen_export_1.value = retptr;
74+
wasm.meta(retptr, id);
75+
const r0 = getInt32Memory0()[retptr / 4];
76+
const r1 = getInt32Memory0()[retptr / 4 + 1];
77+
const v0 = getArrayU32FromWasm0(r0, r1).slice();
78+
wasm.__wbindgen_free(r0, r1 * 4);
79+
return v0;
80+
} finally {
81+
wasm.__wbindgen_export_1.value += 16;
82+
}
83+
},
84+
/**
85+
* @param {number} id
86+
* @returns {Uint8Array}
87+
*/
88+
buffer(id) {
89+
try {
90+
const retptr = wasm.__wbindgen_export_1.value - 16;
91+
wasm.__wbindgen_export_1.value = retptr;
92+
wasm.buffer(retptr, id);
93+
const r0 = getInt32Memory0()[retptr / 4];
94+
const r1 = getInt32Memory0()[retptr / 4 + 1];
95+
const v0 = getArrayU8FromWasm0(r0, r1).slice();
96+
wasm.__wbindgen_free(r0, r1 * 1);
97+
return v0;
98+
} finally {
99+
wasm.__wbindgen_export_1.value += 16;
100+
}
101+
},
102+
/**
103+
* @param {number} id
104+
*/
105+
free(id) {
106+
wasm.free(id);
107+
}
108+
}

‎utils/wasm/tiff.wasm‎

279 KB
Binary file not shown.

0 commit comments

Comments
 (0)