heic-to 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/worker.js ADDED
@@ -0,0 +1,49 @@
1
+ import buildLibheif from "./lib/libheif";
2
+
3
+ const libheif = buildLibheif()
4
+
5
+ const decodeBuffer = async (buffer) => {
6
+ const decoder = new libheif.HeifDecoder();
7
+ const data = decoder.decode(buffer);
8
+
9
+ if (!data.length) {
10
+ throw new Error('HEIF image not found');
11
+ }
12
+
13
+ const image = data[0]
14
+
15
+ const width = image.get_width();
16
+ const height = image.get_height();
17
+
18
+ const whiteImage = new ImageData(width, height)
19
+ for (let i = 0; i < width * height; i++) {
20
+ whiteImage.data[i * 4 + 3] = 255;
21
+ }
22
+
23
+ const imageData = await new Promise((resolve, reject) => {
24
+ image.display(whiteImage, (displayData) => {
25
+ if (!displayData) {
26
+ return reject(new Error('HEIF processing error'));
27
+ }
28
+
29
+ resolve(displayData);
30
+ });
31
+ });
32
+
33
+ return imageData;
34
+ };
35
+
36
+ onmessage = async (message) => {
37
+ const id = message.data.id;
38
+
39
+ try {
40
+ const imageData = await decodeBuffer(message.data.buffer)
41
+ postMessage({ id, imageData, error: "" });
42
+ } catch (e) {
43
+ postMessage({
44
+ id,
45
+ imageData: null,
46
+ error: e && e.toString ? e.toString() : e,
47
+ });
48
+ }
49
+ };