heic2any-lite 1.0.0 → 1.1.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/README.md +163 -139
- package/dist/esm/chunks/{chunk-OUHVJDKC.mjs → chunk-3VHOSGIF.mjs} +1 -1
- package/dist/esm/chunks/chunk-SERODLKX.mjs +29 -0
- package/dist/esm/chunks/{chunk-VI5DOPZT.mjs → chunk-SOBDVI65.mjs} +1 -1
- package/dist/esm/chunks/{decodeHeic-IME6HOYH.mjs → decodeHeic-WSJQEMAF.mjs} +6 -25
- package/dist/esm/chunks/{encodeGif-SLDKQRZA.mjs → encodeGif-G4GNU4NT.mjs} +2 -2
- package/dist/esm/chunks/encodePng-BTPSEWL3.mjs +354 -0
- package/dist/esm/chunks/workerDecoder-OABQOUTK.mjs +136 -0
- package/dist/esm/chunks/yielder-J65S6IXC.mjs +8 -0
- package/dist/esm/index.mjs +57 -19
- package/dist/heic2any.js +510 -56
- package/dist/heic2any.min.js +2 -2
- package/dist/types/png/deflateJoin.d.ts +19 -0
- package/dist/types/png/encodePng.d.ts +35 -0
- package/dist/types/worker/workerDecoder.d.ts +6 -1
- package/dist/types/worker/workerTask.d.ts +18 -0
- package/dist/types/yielder.d.ts +5 -0
- package/package.json +88 -74
- package/dist/esm/chunks/workerDecoder-GXKGQLF4.mjs +0 -95
package/README.md
CHANGED
|
@@ -1,139 +1,163 @@
|
|
|
1
|
-
# heic2any-lite
|
|
2
|
-
|
|
3
|
-
Convert HEIC/HEIF images (iPhone photos) to **PNG, JPEG or GIF in the browser**.
|
|
4
|
-
|
|
5
|
-
A drop-in replacement for [`heic2any`](https://github.com/alexcorvi/heic2any): same function, same options, same
|
|
6
|
-
results and the same error objects. Underneath it has its own HEIF parser and HEVC decoder written in TypeScript,
|
|
7
|
-
with no dependencies and no WebAssembly.
|
|
8
|
-
|
|
9
|
-
| | `heic2any` 0.0.4 | `heic2any-lite` |
|
|
10
|
-
| ----------------------- | ----------------------------------- | --------------------------------------------------- |
|
|
11
|
-
| Size (minified) | 1.35 MB |
|
|
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
|
-
const
|
|
44
|
-
const
|
|
45
|
-
const
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
<script>
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
|
61
|
-
|
|
|
62
|
-
| `
|
|
63
|
-
| `
|
|
64
|
-
| `
|
|
65
|
-
| `
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
|
73
|
-
|
|
|
74
|
-
|
|
|
75
|
-
|
|
|
76
|
-
|
|
|
77
|
-
|
|
|
78
|
-
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
-
|
|
104
|
-
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
the
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
1
|
+
# heic2any-lite
|
|
2
|
+
|
|
3
|
+
Convert HEIC/HEIF images (iPhone photos) to **PNG, JPEG or GIF in the browser**.
|
|
4
|
+
|
|
5
|
+
A drop-in replacement for [`heic2any`](https://github.com/alexcorvi/heic2any): same function, same options, same
|
|
6
|
+
results and the same error objects. Underneath it has its own HEIF parser and HEVC decoder written in TypeScript,
|
|
7
|
+
with no dependencies and no WebAssembly.
|
|
8
|
+
|
|
9
|
+
| | `heic2any` 0.0.4 | `heic2any-lite` |
|
|
10
|
+
| ----------------------- | ----------------------------------- | --------------------------------------------------- |
|
|
11
|
+
| Size (minified) | 1.35 MB | 127 KB; the ESM entry is ~6 KB, the rest loads on first use |
|
|
12
|
+
| PNG files | canvas encoder (RGBA, light compression) | own lossless encoder: same pixels, ~35% smaller files |
|
|
13
|
+
| On import | starts a Web Worker, sets `window.__heic2any__worker` | does nothing (SSR-safe, tree-shakeable) |
|
|
14
|
+
| Decoding | Web Worker (libde265 via asm.js) | Web Worker, started on first use, closed after 30 s idle |
|
|
15
|
+
| CSP blocks `blob:` workers | conversion fails | falls back to the main thread, yielding so the page stays responsive |
|
|
16
|
+
| Pixel accuracy | its libde265 build mis-decodes some samples | bit-exact with the browser's hardware HEVC decoder |
|
|
17
|
+
| Module formats | UMD | ESM (code-split) + UMD (CommonJS / AMD / `<script>`) |
|
|
18
|
+
| Decode without a DOM | no | `decodeHeicToRgba()` works in workers and Node |
|
|
19
|
+
|
|
20
|
+
## Install
|
|
21
|
+
|
|
22
|
+
```sh
|
|
23
|
+
npm install heic2any-lite
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Migrating from heic2any
|
|
27
|
+
|
|
28
|
+
Change the import. Nothing else changes.
|
|
29
|
+
|
|
30
|
+
```diff
|
|
31
|
+
- import heic2any from 'heic2any';
|
|
32
|
+
+ import heic2any from 'heic2any-lite';
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
CommonJS (`require('heic2any-lite')` and `require(...).default`), AMD and the `window.heic2any` global work the
|
|
36
|
+
same way as before.
|
|
37
|
+
|
|
38
|
+
## Usage
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
import heic2any from 'heic2any-lite';
|
|
42
|
+
|
|
43
|
+
const png = (await heic2any({ blob: file })) as Blob; // PNG
|
|
44
|
+
const jpeg = (await heic2any({ blob: file, toType: 'image/jpeg', quality: 0.8 })) as Blob;
|
|
45
|
+
const all = (await heic2any({ blob: file, multiple: true })) as Blob[]; // every image in the file
|
|
46
|
+
const gif = (await heic2any({ blob: file, toType: 'image/gif', gifInterval: 0.5 })) as Blob; // animated if several
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
Plain `<script>` tag (no bundler):
|
|
50
|
+
|
|
51
|
+
```html
|
|
52
|
+
<script src="https://cdn.jsdelivr.net/npm/heic2any-lite@1/dist/heic2any.min.js"></script>
|
|
53
|
+
<script>
|
|
54
|
+
heic2any({ blob }).then((png) => { /* ... */ });
|
|
55
|
+
</script>
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### `heic2any(options): Promise<Blob | Blob[]>`
|
|
59
|
+
|
|
60
|
+
| Option | Type | Default | Meaning |
|
|
61
|
+
| ------------- | -------- | ------------- | ------- |
|
|
62
|
+
| `blob` | `Blob` | (required) | The HEIC/HEIF file. |
|
|
63
|
+
| `toType` | `string` | `'image/png'` | `'image/png'`, `'image/jpeg'` or `'image/gif'`. Any other value gives PNG. |
|
|
64
|
+
| `quality` | `number` | `0.92` | JPEG quality, 0 to 1. Values outside that range use 0.92. |
|
|
65
|
+
| `multiple` | `true` | (not set) | Resolve with an array holding every top-level image instead of only the first. |
|
|
66
|
+
| `gifInterval` | `number` | `0.4` | Seconds per frame when `toType` is `'image/gif'`. Every image in the file becomes a frame. |
|
|
67
|
+
|
|
68
|
+
It resolves with a `Blob`, or `Blob[]` when `multiple` is set (a GIF is always one `Blob`).
|
|
69
|
+
|
|
70
|
+
It rejects with `{ code, message }`, like heic2any:
|
|
71
|
+
|
|
72
|
+
| `code` | `ERROR_CODES` | Example `message` |
|
|
73
|
+
| ------ | ------------- | ----------------- |
|
|
74
|
+
| 1 | `USER` | `ERR_USER Image is already browser readable: image/png` |
|
|
75
|
+
| 2 | `LIBHEIF` | `ERR_LIBHEIF format not supported` |
|
|
76
|
+
| 3 | `GIF` | `ERR_GIF ...` |
|
|
77
|
+
| 4 | `DOM` | `ERR_DOM ...` |
|
|
78
|
+
| 5 | `CANVAS` | `ERR_CANVAS Error on converting imagedata to blob: ...` |
|
|
79
|
+
| 0 | `UNKNOWN` | anything else |
|
|
80
|
+
|
|
81
|
+
```ts
|
|
82
|
+
import heic2any, { ERROR_CODES, type Heic2AnyError } from 'heic2any-lite';
|
|
83
|
+
|
|
84
|
+
try {
|
|
85
|
+
await heic2any({ blob: file });
|
|
86
|
+
} catch (e) {
|
|
87
|
+
if ((e as Heic2AnyError).code === ERROR_CODES.USER) {
|
|
88
|
+
// already a PNG/JPEG/GIF: use the file as it is
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### `decodeHeicToRgba(data, options?): Promise<RgbaImage[]>`
|
|
94
|
+
|
|
95
|
+
Decodes every top-level image to `{ width, height, data: Uint8ClampedArray }` RGBA pixels, with no canvas.
|
|
96
|
+
Use it in a Web Worker, in Node, or to draw the pixels yourself. Pass `{ yieldBetweenTiles: false }` when you
|
|
97
|
+
are not on a page's main thread.
|
|
98
|
+
|
|
99
|
+
## What it decodes
|
|
100
|
+
|
|
101
|
+
- HEIC/HEIF still images: single images, grids (`grid`, the format iPhones use), overlays (`iovl`) and derived
|
|
102
|
+
images (`iden`), with rotation (`irot`), mirroring (`imir`) and cropping (`clap`) applied.
|
|
103
|
+
- HEVC Main, Main Still Picture and Main 10 (8 to 12 bit, converted to 8-bit output), 4:2:0 and monochrome.
|
|
104
|
+
- Like heic2any, it ignores alpha and depth auxiliary images and EXIF orientation.
|
|
105
|
+
- It does not support 4:2:2/4:4:4 chroma or the HEVC range-extension coding tools. These files reject with
|
|
106
|
+
`ERR_LIBHEIF Error while processing single image ...` (heic2any cannot convert them either).
|
|
107
|
+
|
|
108
|
+
## Output compared with heic2any
|
|
109
|
+
|
|
110
|
+
The API, option handling, colour conversion (same coefficients, nearest chroma sample) and error objects all
|
|
111
|
+
reproduce heic2any. The decoded pixels are **not** byte-identical to heic2any, deliberately. The libde265 build
|
|
112
|
+
inside heic2any 0.0.4 mis-decodes some samples along sharp edges (about 4% of samples in the test image are off
|
|
113
|
+
by up to 22 luma / 63 chroma levels). This library matches the browser's own hardware HEVC decoder exactly, and
|
|
114
|
+
`npm run test:browser` checks that. In practice the two outputs look the same (PSNR ≈ 45 dB).
|
|
115
|
+
|
|
116
|
+
### PNG size
|
|
117
|
+
|
|
118
|
+
heic2any makes PNGs with `canvas.toBlob()`. Browsers tune that encoder for speed: it always stores an alpha
|
|
119
|
+
channel and compresses lightly. This library encodes PNGs itself, in the worker. It writes RGB when every pixel
|
|
120
|
+
is opaque (always true for HEIC photos) and picks the best PNG filter for every row. Deflate is the browser's
|
|
121
|
+
own `CompressionStream`, so no compression code is bundled. The pixels are exactly the same. Only the file is
|
|
122
|
+
smaller:
|
|
123
|
+
|
|
124
|
+
| Photo (Edge 158) | HEIC | PNG from canvas | PNG from this library |
|
|
125
|
+
| ----------------------- | ------- | --------------- | --------------------- |
|
|
126
|
+
| iPhone, 5712×4284 | 2.47 MB | 29.0 MB | 19.1 MB (−34%) |
|
|
127
|
+
| 4000×3000 | 1.44 MB | 19.0 MB | 12.5 MB (−34%) |
|
|
128
|
+
|
|
129
|
+
This compression needs more CPU than the canvas's, so the image is cut into strips that are compressed in
|
|
130
|
+
parallel on a small worker pool (one worker per core minus one, at most 8). The strips are joined into the single
|
|
131
|
+
stream PNG requires, so the file is byte-identical whatever the core count. For the 24 MP photo above, encoding
|
|
132
|
+
takes about 1.4 s with 8 workers, about the same as the canvas, and about 2.6 s on a 4-core device (canvas: about
|
|
133
|
+
1 s). Decoding the HEIC (3 to 5 s) is unchanged. Browsers without `CompressionStream`
|
|
134
|
+
(before Chrome 80, Firefox 113 and Safari 16.4) get the canvas PNG as before. A PNG is still several times
|
|
135
|
+
bigger than the HEIC, because the HEIC is lossy and the PNG is not. If you don't need lossless output,
|
|
136
|
+
`toType: 'image/jpeg'` at the default quality of 0.92 is about the size of the HEIC (3.05 MB for the first
|
|
137
|
+
photo).
|
|
138
|
+
|
|
139
|
+
## Content-Security-Policy
|
|
140
|
+
|
|
141
|
+
Decoding runs in a Web Worker created from a `blob:` URL, as heic2any's does. If your CSP has `worker-src`
|
|
142
|
+
(or `script-src` as its fallback) without `blob:`, the worker cannot start. The library then decodes on the main
|
|
143
|
+
thread and pauses between tiles so the page stays responsive. Add `blob:` to `worker-src` to keep decoding off
|
|
144
|
+
the main thread.
|
|
145
|
+
|
|
146
|
+
## Development
|
|
147
|
+
|
|
148
|
+
```sh
|
|
149
|
+
npm install
|
|
150
|
+
npm test # unit tests (jest): HEIF parser, HEVC decoder, PNG and GIF encoders, worker protocol, API
|
|
151
|
+
npm run typecheck
|
|
152
|
+
npm run build # dist/esm (ESM), dist/heic2any.js + .min.js (UMD), dist/types
|
|
153
|
+
npm run test:dist # the built UMD and ESM files, in Node
|
|
154
|
+
npm run test:browser # real Edge/Chrome: worker, CSP fallback, hardware-decoder bit-exactness,
|
|
155
|
+
# comparison with the original heic2any (installed as a devDependency)
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
`npm publish` runs the type-check, the unit tests, the build and the dist tests first (`prepublishOnly`).
|
|
159
|
+
|
|
160
|
+
## License
|
|
161
|
+
|
|
162
|
+
MIT © Darshit Mehta & kanha vishwakarma. It reproduces the API of [heic2any](https://github.com/alexcorvi/heic2any) and the GIF
|
|
163
|
+
output of gifshot/omggif (all MIT). See [LICENSE](LICENSE) for the third-party notices.
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/*! heic2any-lite v1.
|
|
1
|
+
/*! heic2any-lite v1.1.0 | MIT | NeuQuant (c) 1994 Anthony Dekker */
|
|
2
2
|
var __defProp = Object.defineProperty;
|
|
3
3
|
var __defProps = Object.defineProperties;
|
|
4
4
|
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/*! heic2any-lite v1.1.0 | MIT | NeuQuant (c) 1994 Anthony Dekker */
|
|
2
|
+
|
|
3
|
+
// src/yielder.ts
|
|
4
|
+
function createYielder(enabled) {
|
|
5
|
+
if (!enabled) return { yieldNow: () => Promise.resolve(), dispose: () => void 0 };
|
|
6
|
+
if (typeof MessageChannel !== "undefined" && typeof window !== "undefined") {
|
|
7
|
+
const channel = new MessageChannel();
|
|
8
|
+
const queue = [];
|
|
9
|
+
channel.port1.onmessage = () => {
|
|
10
|
+
var _a;
|
|
11
|
+
return (_a = queue.shift()) == null ? void 0 : _a();
|
|
12
|
+
};
|
|
13
|
+
return {
|
|
14
|
+
yieldNow: () => new Promise((resolve) => {
|
|
15
|
+
queue.push(resolve);
|
|
16
|
+
channel.port2.postMessage(0);
|
|
17
|
+
}),
|
|
18
|
+
dispose: () => {
|
|
19
|
+
channel.port1.close();
|
|
20
|
+
channel.port2.close();
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
return { yieldNow: () => new Promise((resolve) => setTimeout(resolve, 0)), dispose: () => void 0 };
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export {
|
|
28
|
+
createYielder
|
|
29
|
+
};
|
|
@@ -1,11 +1,14 @@
|
|
|
1
|
-
/*! heic2any-lite v1.
|
|
1
|
+
/*! heic2any-lite v1.1.0 | MIT | NeuQuant (c) 1994 Anthony Dekker */
|
|
2
2
|
import {
|
|
3
3
|
HeicDecodeError
|
|
4
|
-
} from "./chunk-
|
|
4
|
+
} from "./chunk-SOBDVI65.mjs";
|
|
5
|
+
import {
|
|
6
|
+
createYielder
|
|
7
|
+
} from "./chunk-SERODLKX.mjs";
|
|
5
8
|
import {
|
|
6
9
|
__spreadProps,
|
|
7
10
|
__spreadValues
|
|
8
|
-
} from "./chunk-
|
|
11
|
+
} from "./chunk-3VHOSGIF.mjs";
|
|
9
12
|
|
|
10
13
|
// src/color.ts
|
|
11
14
|
var Y_TERM = new Float64Array(256);
|
|
@@ -3663,28 +3666,6 @@ function isUsableImage(file, id) {
|
|
|
3663
3666
|
var MSG_FORMAT_NOT_SUPPORTED = "ERR_LIBHEIF format not supported";
|
|
3664
3667
|
var MSG_DECODE_FAILED = "ERR_LIBHEIF Error while processing single image and generating image data, could not ensure image";
|
|
3665
3668
|
var MSG_NO_VALID_IMAGES = "ERR_LIBHEIF Heic doesn't contain valid images";
|
|
3666
|
-
function createYielder(enabled) {
|
|
3667
|
-
if (!enabled) return { yieldNow: () => Promise.resolve(), dispose: () => void 0 };
|
|
3668
|
-
if (typeof MessageChannel !== "undefined" && typeof window !== "undefined") {
|
|
3669
|
-
const channel = new MessageChannel();
|
|
3670
|
-
const queue = [];
|
|
3671
|
-
channel.port1.onmessage = () => {
|
|
3672
|
-
var _a;
|
|
3673
|
-
return (_a = queue.shift()) == null ? void 0 : _a();
|
|
3674
|
-
};
|
|
3675
|
-
return {
|
|
3676
|
-
yieldNow: () => new Promise((resolve) => {
|
|
3677
|
-
queue.push(resolve);
|
|
3678
|
-
channel.port2.postMessage(0);
|
|
3679
|
-
}),
|
|
3680
|
-
dispose: () => {
|
|
3681
|
-
channel.port1.close();
|
|
3682
|
-
channel.port2.close();
|
|
3683
|
-
}
|
|
3684
|
-
};
|
|
3685
|
-
}
|
|
3686
|
-
return { yieldNow: () => new Promise((resolve) => setTimeout(resolve, 0)), dispose: () => void 0 };
|
|
3687
|
-
}
|
|
3688
3669
|
async function decodeHeicToRgba(buffer, options = {}) {
|
|
3689
3670
|
const data = buffer instanceof Uint8Array ? buffer : new Uint8Array(buffer);
|
|
3690
3671
|
let file;
|