heic-to 1.1.13 → 1.1.14
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/README.md +1 -0
- package/dist/csp/heic-to.js +1 -1
- package/dist/csp/heic-to.min.js +1 -1
- package/dist/heic-to.js +1 -1
- package/dist/heic-to.min.js +1 -1
- package/dist/iife/heic-to.js +27 -9
- package/package.json +1 -1
- package/src/index.js +27 -8
- package/src/worker.js +33 -21
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -42,14 +42,21 @@ const decodeBuffer = async (buffer) => {
|
|
|
42
42
|
worker.postMessage(message);
|
|
43
43
|
const handleEvent = (event) => {
|
|
44
44
|
if (event.data.id === id) {
|
|
45
|
-
event.currentTarget.removeEventListener("message", handleEvent)
|
|
45
|
+
event.currentTarget.removeEventListener("message", handleEvent);
|
|
46
|
+
event.currentTarget.removeEventListener("error", handleError);
|
|
46
47
|
if (event.data.error) {
|
|
47
48
|
return reject(event.data.error);
|
|
48
49
|
}
|
|
49
50
|
return resolve(event.data.imageData);
|
|
50
51
|
}
|
|
51
52
|
}
|
|
53
|
+
const handleError = (event) => {
|
|
54
|
+
event.currentTarget.removeEventListener("message", handleEvent);
|
|
55
|
+
event.currentTarget.removeEventListener("error", handleError);
|
|
56
|
+
return reject(event.data);
|
|
57
|
+
}
|
|
52
58
|
worker.addEventListener("message", handleEvent);
|
|
59
|
+
worker.addEventListener("error", handleError);
|
|
53
60
|
});
|
|
54
61
|
}
|
|
55
62
|
|
|
@@ -65,15 +72,27 @@ const encodeByCanvas = async (imageBuffer) => {
|
|
|
65
72
|
return canvas;
|
|
66
73
|
};
|
|
67
74
|
|
|
75
|
+
const releaseCanvas = (canvas) => {
|
|
76
|
+
canvas.width = 1;
|
|
77
|
+
canvas.height = 1;
|
|
78
|
+
const ctx = canvas.getContext('2d');
|
|
79
|
+
ctx && ctx.clearRect(0, 0, 1, 1);
|
|
80
|
+
}
|
|
81
|
+
|
|
68
82
|
const heicTo = async ({blob, type, quality}) => {
|
|
69
83
|
const imageBuffer = await blob.arrayBuffer()
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
84
|
+
let canvas;
|
|
85
|
+
try {
|
|
86
|
+
canvas = await encodeByCanvas(imageBuffer);
|
|
87
|
+
return await new Promise((resolve, reject) => canvas.toBlob(blob => {
|
|
88
|
+
if (blob != null)
|
|
89
|
+
resolve(blob);
|
|
90
|
+
else
|
|
91
|
+
reject(`Can't convert canvas to blob.`);
|
|
92
|
+
}, type, quality));
|
|
93
|
+
} finally {
|
|
94
|
+
if (canvas) releaseCanvas(canvas);
|
|
95
|
+
}
|
|
77
96
|
};
|
|
78
97
|
|
|
79
98
|
export {
|
package/src/worker.js
CHANGED
|
@@ -3,41 +3,53 @@ import buildLibheif from LIB_HEIF_PATH;
|
|
|
3
3
|
const libheif = buildLibheif()
|
|
4
4
|
|
|
5
5
|
const decodeBuffer = async (buffer) => {
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
let decoder, data;
|
|
7
|
+
try {
|
|
8
|
+
decoder = new libheif.HeifDecoder();
|
|
9
|
+
data = decoder.decode(buffer);
|
|
8
10
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
11
|
+
if (!data.length) {
|
|
12
|
+
throw new Error('HEIF image not found');
|
|
13
|
+
}
|
|
12
14
|
|
|
13
|
-
|
|
15
|
+
const image = data[0]
|
|
14
16
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
+
const width = image.get_width();
|
|
18
|
+
const height = image.get_height();
|
|
17
19
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
20
|
+
const whiteImage = new ImageData(width, height)
|
|
21
|
+
for (let i = 0; i < width * height; i++) {
|
|
22
|
+
whiteImage.data[i * 4 + 3] = 255;
|
|
23
|
+
}
|
|
22
24
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
25
|
+
const imageData = await new Promise((resolve, reject) => {
|
|
26
|
+
image.display(whiteImage, (displayData) => {
|
|
27
|
+
if (!displayData) {
|
|
28
|
+
return reject(new Error('HEIF processing error'));
|
|
29
|
+
}
|
|
28
30
|
|
|
29
|
-
|
|
31
|
+
resolve(displayData);
|
|
32
|
+
});
|
|
30
33
|
});
|
|
31
|
-
});
|
|
32
34
|
|
|
33
|
-
|
|
35
|
+
return imageData;
|
|
36
|
+
} finally {
|
|
37
|
+
if (data && data.length) {
|
|
38
|
+
for (let i = 0; i < data.length; i++) {
|
|
39
|
+
data[i].free();
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
if (decoder && decoder.decoder) {
|
|
43
|
+
libheif.heif_context_free(decoder.decoder);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
34
46
|
};
|
|
35
47
|
|
|
36
48
|
onmessage = async (message) => {
|
|
37
49
|
const id = message.data.id;
|
|
38
50
|
|
|
39
51
|
try {
|
|
40
|
-
const imageData = await decodeBuffer(message.data.buffer)
|
|
52
|
+
const imageData = await decodeBuffer(message.data.buffer);
|
|
41
53
|
postMessage({ id, imageData, error: "" });
|
|
42
54
|
} catch (e) {
|
|
43
55
|
postMessage({
|