heic2any-lite 0.0.0-stage → 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.
Files changed (38) hide show
  1. package/LICENSE +40 -0
  2. package/README.md +163 -3
  3. package/dist/esm/chunks/chunk-3VHOSGIF.mjs +25 -0
  4. package/dist/esm/chunks/chunk-SERODLKX.mjs +29 -0
  5. package/dist/esm/chunks/chunk-SOBDVI65.mjs +40 -0
  6. package/dist/esm/chunks/decodeHeic-WSJQEMAF.mjs +3705 -0
  7. package/dist/esm/chunks/encodeGif-G4GNU4NT.mjs +418 -0
  8. package/dist/esm/chunks/encodePng-BTPSEWL3.mjs +354 -0
  9. package/dist/esm/chunks/workerDecoder-OABQOUTK.mjs +136 -0
  10. package/dist/esm/chunks/yielder-J65S6IXC.mjs +8 -0
  11. package/dist/esm/index.mjs +187 -0
  12. package/dist/heic2any.js +5050 -0
  13. package/dist/heic2any.min.js +17 -0
  14. package/dist/types/color.d.ts +7 -0
  15. package/dist/types/decodeHeic.d.ts +22 -0
  16. package/dist/types/errors.d.ts +22 -0
  17. package/dist/types/gif/encodeGif.d.ts +12 -0
  18. package/dist/types/gif/neuQuant.d.ts +23 -0
  19. package/dist/types/heic2any.d.ts +21 -0
  20. package/dist/types/heif/decodeImage.d.ts +17 -0
  21. package/dist/types/heif/heifFile.d.ts +70 -0
  22. package/dist/types/hevc/bitReader.d.ts +20 -0
  23. package/dist/types/hevc/cabac.d.ts +43 -0
  24. package/dist/types/hevc/decoder.d.ts +20 -0
  25. package/dist/types/hevc/intraPrediction.d.ts +11 -0
  26. package/dist/types/hevc/loopFilter.d.ts +39 -0
  27. package/dist/types/hevc/parameterSets.d.ts +94 -0
  28. package/dist/types/hevc/tables.d.ts +32 -0
  29. package/dist/types/hevc/transform.d.ts +21 -0
  30. package/dist/types/index.d.ts +13 -0
  31. package/dist/types/png/deflateJoin.d.ts +19 -0
  32. package/dist/types/png/encodePng.d.ts +35 -0
  33. package/dist/types/worker/createDecodeWorker.d.ts +1 -0
  34. package/dist/types/worker/decodeWorker.d.ts +1 -0
  35. package/dist/types/worker/workerDecoder.d.ts +18 -0
  36. package/dist/types/worker/workerTask.d.ts +18 -0
  37. package/dist/types/yielder.d.ts +5 -0
  38. package/package.json +88 -6
package/LICENSE ADDED
@@ -0,0 +1,40 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 kanha vishwakarma
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
23
+ ===========================================================================
24
+ Third-party notices
25
+ ===========================================================================
26
+
27
+ heic2any-lite reproduces the public API, option handling, colour conversion and
28
+ error messages of heic2any, and the GIF output of gifshot/omggif as used by
29
+ heic2any. Those projects are MIT licensed:
30
+
31
+ - heic2any: Copyright (c) 2020 Alex Corvi (MIT License)
32
+ https://github.com/alexcorvi/heic2any
33
+ - gifshot: Copyright (c) 2017 Yahoo Inc. (MIT License)
34
+ https://github.com/yahoo/gifshot
35
+ - omggif: Copyright (c) 2013 Dean McNamee (MIT License)
36
+ https://github.com/deanm/omggif
37
+
38
+ src/gif/neuQuant.ts is derived from the NeuQuant Neural-Net Quantization
39
+ Algorithm, Copyright (c) 1994 Anthony Dekker, used under its original terms:
40
+ the copyright notice must remain intact (see the header of that file).
package/README.md CHANGED
@@ -1,3 +1,163 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
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.
@@ -0,0 +1,25 @@
1
+ /*! heic2any-lite v1.1.0 | MIT | NeuQuant (c) 1994 Anthony Dekker */
2
+ var __defProp = Object.defineProperty;
3
+ var __defProps = Object.defineProperties;
4
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
5
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
8
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
9
+ var __spreadValues = (a, b) => {
10
+ for (var prop in b || (b = {}))
11
+ if (__hasOwnProp.call(b, prop))
12
+ __defNormalProp(a, prop, b[prop]);
13
+ if (__getOwnPropSymbols)
14
+ for (var prop of __getOwnPropSymbols(b)) {
15
+ if (__propIsEnum.call(b, prop))
16
+ __defNormalProp(a, prop, b[prop]);
17
+ }
18
+ return a;
19
+ };
20
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
21
+
22
+ export {
23
+ __spreadValues,
24
+ __spreadProps
25
+ };
@@ -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
+ };
@@ -0,0 +1,40 @@
1
+ /*! heic2any-lite v1.1.0 | MIT | NeuQuant (c) 1994 Anthony Dekker */
2
+
3
+ // src/errors.ts
4
+ var ERROR_CODES = {
5
+ UNKNOWN: 0,
6
+ USER: 1,
7
+ LIBHEIF: 2,
8
+ GIF: 3,
9
+ DOM: 4,
10
+ CANVAS: 5
11
+ };
12
+ var PREFIXES = ["ERR_USER", "ERR_LIBHEIF", "ERR_GIF", "ERR_DOM", "ERR_CANVAS"];
13
+ function toHeic2AnyError(message) {
14
+ let text;
15
+ if (!message) {
16
+ text = "ERR_UNKNOWN";
17
+ } else if (typeof message !== "string") {
18
+ const m = message;
19
+ text = m.toString ? m.toString() : JSON.stringify(message);
20
+ } else {
21
+ text = message;
22
+ }
23
+ let code = 0;
24
+ PREFIXES.forEach((prefix, index) => {
25
+ if (text.indexOf(prefix) === 0) code = index + 1;
26
+ });
27
+ return { code, message: text };
28
+ }
29
+ var HeicDecodeError = class extends Error {
30
+ constructor(message) {
31
+ super(message);
32
+ this.name = "HeicDecodeError";
33
+ }
34
+ };
35
+
36
+ export {
37
+ ERROR_CODES,
38
+ toHeic2AnyError,
39
+ HeicDecodeError
40
+ };