@jimp/jpeg-js 0.0.0-stage → 2.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 (225) hide show
  1. package/LICENSE +129 -0
  2. package/LICENSE-APACHE-2.0 +202 -0
  3. package/NOTICE +27 -0
  4. package/README.ijg +260 -0
  5. package/README.md +261 -2
  6. package/dist/browser/compat.d.ts +44 -0
  7. package/dist/browser/compat.d.ts.map +1 -0
  8. package/dist/browser/compat.js +42 -0
  9. package/dist/browser/compat.js.map +1 -0
  10. package/dist/browser/decode.d.ts +73 -0
  11. package/dist/browser/decode.d.ts.map +1 -0
  12. package/dist/browser/decode.js +82 -0
  13. package/dist/browser/decode.js.map +1 -0
  14. package/dist/browser/decoder/arithmetic.d.ts +66 -0
  15. package/dist/browser/decoder/arithmetic.d.ts.map +1 -0
  16. package/dist/browser/decoder/arithmetic.js +374 -0
  17. package/dist/browser/decoder/arithmetic.js.map +1 -0
  18. package/dist/browser/decoder/decoder.d.ts +87 -0
  19. package/dist/browser/decoder/decoder.d.ts.map +1 -0
  20. package/dist/browser/decoder/decoder.js +869 -0
  21. package/dist/browser/decoder/decoder.js.map +1 -0
  22. package/dist/browser/decoder/huffman.d.ts +64 -0
  23. package/dist/browser/decoder/huffman.d.ts.map +1 -0
  24. package/dist/browser/decoder/huffman.js +260 -0
  25. package/dist/browser/decoder/huffman.js.map +1 -0
  26. package/dist/browser/decoder/idct.d.ts +20 -0
  27. package/dist/browser/decoder/idct.d.ts.map +1 -0
  28. package/dist/browser/decoder/idct.js +222 -0
  29. package/dist/browser/decoder/idct.js.map +1 -0
  30. package/dist/browser/decoder/output.d.ts +17 -0
  31. package/dist/browser/decoder/output.d.ts.map +1 -0
  32. package/dist/browser/decoder/output.js +351 -0
  33. package/dist/browser/decoder/output.js.map +1 -0
  34. package/dist/browser/decoder/scan-tables.d.ts +7 -0
  35. package/dist/browser/decoder/scan-tables.d.ts.map +1 -0
  36. package/dist/browser/decoder/scan-tables.js +13 -0
  37. package/dist/browser/decoder/scan-tables.js.map +1 -0
  38. package/dist/browser/decoder/scan.d.ts +57 -0
  39. package/dist/browser/decoder/scan.d.ts.map +1 -0
  40. package/dist/browser/decoder/scan.js +381 -0
  41. package/dist/browser/decoder/scan.js.map +1 -0
  42. package/dist/browser/decoder/smoothing.d.ts +28 -0
  43. package/dist/browser/decoder/smoothing.d.ts.map +1 -0
  44. package/dist/browser/decoder/smoothing.js +345 -0
  45. package/dist/browser/decoder/smoothing.js.map +1 -0
  46. package/dist/browser/decoder/standard-tables.d.ts +19 -0
  47. package/dist/browser/decoder/standard-tables.d.ts.map +1 -0
  48. package/dist/browser/decoder/standard-tables.js +50 -0
  49. package/dist/browser/decoder/standard-tables.js.map +1 -0
  50. package/dist/browser/encode.d.ts +57 -0
  51. package/dist/browser/encode.d.ts.map +1 -0
  52. package/dist/browser/encode.js +57 -0
  53. package/dist/browser/encode.js.map +1 -0
  54. package/dist/browser/encoder/encoder.d.ts +65 -0
  55. package/dist/browser/encoder/encoder.d.ts.map +1 -0
  56. package/dist/browser/encoder/encoder.js +713 -0
  57. package/dist/browser/encoder/encoder.js.map +1 -0
  58. package/dist/browser/encoder/huffman.d.ts +19 -0
  59. package/dist/browser/encoder/huffman.d.ts.map +1 -0
  60. package/dist/browser/encoder/huffman.js +135 -0
  61. package/dist/browser/encoder/huffman.js.map +1 -0
  62. package/dist/browser/encoder/progressive.d.ts +20 -0
  63. package/dist/browser/encoder/progressive.d.ts.map +1 -0
  64. package/dist/browser/encoder/progressive.js +315 -0
  65. package/dist/browser/encoder/progressive.js.map +1 -0
  66. package/dist/browser/encoder/writer.d.ts +18 -0
  67. package/dist/browser/encoder/writer.d.ts.map +1 -0
  68. package/dist/browser/encoder/writer.js +71 -0
  69. package/dist/browser/encoder/writer.js.map +1 -0
  70. package/dist/browser/errors.d.ts +27 -0
  71. package/dist/browser/errors.d.ts.map +1 -0
  72. package/dist/browser/errors.js +28 -0
  73. package/dist/browser/errors.js.map +1 -0
  74. package/dist/browser/index.d.ts +12 -0
  75. package/dist/browser/index.d.ts.map +1 -0
  76. package/dist/browser/index.js +8 -0
  77. package/dist/browser/index.js.map +1 -0
  78. package/dist/browser/package.json +3 -0
  79. package/dist/commonjs/compat.d.ts +44 -0
  80. package/dist/commonjs/compat.d.ts.map +1 -0
  81. package/dist/commonjs/compat.js +46 -0
  82. package/dist/commonjs/compat.js.map +1 -0
  83. package/dist/commonjs/decode.d.ts +73 -0
  84. package/dist/commonjs/decode.d.ts.map +1 -0
  85. package/dist/commonjs/decode.js +86 -0
  86. package/dist/commonjs/decode.js.map +1 -0
  87. package/dist/commonjs/decoder/arithmetic.d.ts +66 -0
  88. package/dist/commonjs/decoder/arithmetic.d.ts.map +1 -0
  89. package/dist/commonjs/decoder/arithmetic.js +380 -0
  90. package/dist/commonjs/decoder/arithmetic.js.map +1 -0
  91. package/dist/commonjs/decoder/decoder.d.ts +87 -0
  92. package/dist/commonjs/decoder/decoder.d.ts.map +1 -0
  93. package/dist/commonjs/decoder/decoder.js +873 -0
  94. package/dist/commonjs/decoder/decoder.js.map +1 -0
  95. package/dist/commonjs/decoder/huffman.d.ts +64 -0
  96. package/dist/commonjs/decoder/huffman.d.ts.map +1 -0
  97. package/dist/commonjs/decoder/huffman.js +265 -0
  98. package/dist/commonjs/decoder/huffman.js.map +1 -0
  99. package/dist/commonjs/decoder/idct.d.ts +20 -0
  100. package/dist/commonjs/decoder/idct.d.ts.map +1 -0
  101. package/dist/commonjs/decoder/idct.js +226 -0
  102. package/dist/commonjs/decoder/idct.js.map +1 -0
  103. package/dist/commonjs/decoder/output.d.ts +17 -0
  104. package/dist/commonjs/decoder/output.d.ts.map +1 -0
  105. package/dist/commonjs/decoder/output.js +354 -0
  106. package/dist/commonjs/decoder/output.js.map +1 -0
  107. package/dist/commonjs/decoder/scan-tables.d.ts +7 -0
  108. package/dist/commonjs/decoder/scan-tables.d.ts.map +1 -0
  109. package/dist/commonjs/decoder/scan-tables.js +16 -0
  110. package/dist/commonjs/decoder/scan-tables.js.map +1 -0
  111. package/dist/commonjs/decoder/scan.d.ts +57 -0
  112. package/dist/commonjs/decoder/scan.d.ts.map +1 -0
  113. package/dist/commonjs/decoder/scan.js +385 -0
  114. package/dist/commonjs/decoder/scan.js.map +1 -0
  115. package/dist/commonjs/decoder/smoothing.d.ts +28 -0
  116. package/dist/commonjs/decoder/smoothing.d.ts.map +1 -0
  117. package/dist/commonjs/decoder/smoothing.js +349 -0
  118. package/dist/commonjs/decoder/smoothing.js.map +1 -0
  119. package/dist/commonjs/decoder/standard-tables.d.ts +19 -0
  120. package/dist/commonjs/decoder/standard-tables.d.ts.map +1 -0
  121. package/dist/commonjs/decoder/standard-tables.js +53 -0
  122. package/dist/commonjs/decoder/standard-tables.js.map +1 -0
  123. package/dist/commonjs/encode.d.ts +57 -0
  124. package/dist/commonjs/encode.d.ts.map +1 -0
  125. package/dist/commonjs/encode.js +60 -0
  126. package/dist/commonjs/encode.js.map +1 -0
  127. package/dist/commonjs/encoder/encoder.d.ts +65 -0
  128. package/dist/commonjs/encoder/encoder.d.ts.map +1 -0
  129. package/dist/commonjs/encoder/encoder.js +720 -0
  130. package/dist/commonjs/encoder/encoder.js.map +1 -0
  131. package/dist/commonjs/encoder/huffman.d.ts +19 -0
  132. package/dist/commonjs/encoder/huffman.d.ts.map +1 -0
  133. package/dist/commonjs/encoder/huffman.js +139 -0
  134. package/dist/commonjs/encoder/huffman.js.map +1 -0
  135. package/dist/commonjs/encoder/progressive.d.ts +20 -0
  136. package/dist/commonjs/encoder/progressive.d.ts.map +1 -0
  137. package/dist/commonjs/encoder/progressive.js +318 -0
  138. package/dist/commonjs/encoder/progressive.js.map +1 -0
  139. package/dist/commonjs/encoder/writer.d.ts +18 -0
  140. package/dist/commonjs/encoder/writer.d.ts.map +1 -0
  141. package/dist/commonjs/encoder/writer.js +75 -0
  142. package/dist/commonjs/encoder/writer.js.map +1 -0
  143. package/dist/commonjs/errors.d.ts +27 -0
  144. package/dist/commonjs/errors.d.ts.map +1 -0
  145. package/dist/commonjs/errors.js +35 -0
  146. package/dist/commonjs/errors.js.map +1 -0
  147. package/dist/commonjs/index.d.ts +12 -0
  148. package/dist/commonjs/index.d.ts.map +1 -0
  149. package/dist/commonjs/index.js +19 -0
  150. package/dist/commonjs/index.js.map +1 -0
  151. package/dist/commonjs/package.json +3 -0
  152. package/dist/esm/compat.d.ts +44 -0
  153. package/dist/esm/compat.d.ts.map +1 -0
  154. package/dist/esm/compat.js +42 -0
  155. package/dist/esm/compat.js.map +1 -0
  156. package/dist/esm/decode.d.ts +73 -0
  157. package/dist/esm/decode.d.ts.map +1 -0
  158. package/dist/esm/decode.js +82 -0
  159. package/dist/esm/decode.js.map +1 -0
  160. package/dist/esm/decoder/arithmetic.d.ts +66 -0
  161. package/dist/esm/decoder/arithmetic.d.ts.map +1 -0
  162. package/dist/esm/decoder/arithmetic.js +374 -0
  163. package/dist/esm/decoder/arithmetic.js.map +1 -0
  164. package/dist/esm/decoder/decoder.d.ts +87 -0
  165. package/dist/esm/decoder/decoder.d.ts.map +1 -0
  166. package/dist/esm/decoder/decoder.js +869 -0
  167. package/dist/esm/decoder/decoder.js.map +1 -0
  168. package/dist/esm/decoder/huffman.d.ts +64 -0
  169. package/dist/esm/decoder/huffman.d.ts.map +1 -0
  170. package/dist/esm/decoder/huffman.js +260 -0
  171. package/dist/esm/decoder/huffman.js.map +1 -0
  172. package/dist/esm/decoder/idct.d.ts +20 -0
  173. package/dist/esm/decoder/idct.d.ts.map +1 -0
  174. package/dist/esm/decoder/idct.js +222 -0
  175. package/dist/esm/decoder/idct.js.map +1 -0
  176. package/dist/esm/decoder/output.d.ts +17 -0
  177. package/dist/esm/decoder/output.d.ts.map +1 -0
  178. package/dist/esm/decoder/output.js +351 -0
  179. package/dist/esm/decoder/output.js.map +1 -0
  180. package/dist/esm/decoder/scan-tables.d.ts +7 -0
  181. package/dist/esm/decoder/scan-tables.d.ts.map +1 -0
  182. package/dist/esm/decoder/scan-tables.js +13 -0
  183. package/dist/esm/decoder/scan-tables.js.map +1 -0
  184. package/dist/esm/decoder/scan.d.ts +57 -0
  185. package/dist/esm/decoder/scan.d.ts.map +1 -0
  186. package/dist/esm/decoder/scan.js +381 -0
  187. package/dist/esm/decoder/scan.js.map +1 -0
  188. package/dist/esm/decoder/smoothing.d.ts +28 -0
  189. package/dist/esm/decoder/smoothing.d.ts.map +1 -0
  190. package/dist/esm/decoder/smoothing.js +345 -0
  191. package/dist/esm/decoder/smoothing.js.map +1 -0
  192. package/dist/esm/decoder/standard-tables.d.ts +19 -0
  193. package/dist/esm/decoder/standard-tables.d.ts.map +1 -0
  194. package/dist/esm/decoder/standard-tables.js +50 -0
  195. package/dist/esm/decoder/standard-tables.js.map +1 -0
  196. package/dist/esm/encode.d.ts +57 -0
  197. package/dist/esm/encode.d.ts.map +1 -0
  198. package/dist/esm/encode.js +57 -0
  199. package/dist/esm/encode.js.map +1 -0
  200. package/dist/esm/encoder/encoder.d.ts +65 -0
  201. package/dist/esm/encoder/encoder.d.ts.map +1 -0
  202. package/dist/esm/encoder/encoder.js +713 -0
  203. package/dist/esm/encoder/encoder.js.map +1 -0
  204. package/dist/esm/encoder/huffman.d.ts +19 -0
  205. package/dist/esm/encoder/huffman.d.ts.map +1 -0
  206. package/dist/esm/encoder/huffman.js +135 -0
  207. package/dist/esm/encoder/huffman.js.map +1 -0
  208. package/dist/esm/encoder/progressive.d.ts +20 -0
  209. package/dist/esm/encoder/progressive.d.ts.map +1 -0
  210. package/dist/esm/encoder/progressive.js +315 -0
  211. package/dist/esm/encoder/progressive.js.map +1 -0
  212. package/dist/esm/encoder/writer.d.ts +18 -0
  213. package/dist/esm/encoder/writer.d.ts.map +1 -0
  214. package/dist/esm/encoder/writer.js +71 -0
  215. package/dist/esm/encoder/writer.js.map +1 -0
  216. package/dist/esm/errors.d.ts +27 -0
  217. package/dist/esm/errors.d.ts.map +1 -0
  218. package/dist/esm/errors.js +28 -0
  219. package/dist/esm/errors.js.map +1 -0
  220. package/dist/esm/index.d.ts +12 -0
  221. package/dist/esm/index.d.ts.map +1 -0
  222. package/dist/esm/index.js +8 -0
  223. package/dist/esm/index.js.map +1 -0
  224. package/dist/esm/package.json +3 -0
  225. package/package.json +99 -4
package/README.md CHANGED
@@ -1,3 +1,262 @@
1
- # Temporary Holding Version
1
+ <div align="center">
2
+ <img width="140" alt=""
3
+ src="https://raw.githubusercontent.com/jimp-dev/jimp/main/.github/assets/jimpy.png">
4
+ <h1>@jimp/jpeg-js</h1>
5
+ <p>A maintained fork of jpeg-js: JPEG encoder and decoder in pure JavaScript.</p>
6
+ </div>
2
7
 
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.
8
+ ## Based on jpeg-js / pdf.js / as3corelib
9
+
10
+ This package is a fork of [jpeg-js](https://github.com/jpeg-js/jpeg-js) by
11
+ Eugene Ware and contributors (BSD 3-Clause). Its decoder comes from
12
+ [jpgjs](https://github.com/notmasteryet/jpgjs) by notmasteryet, which became
13
+ the JPEG decoder of [pdf.js](https://github.com/mozilla/pdf.js) (Apache 2.0).
14
+ Its encoder is the JPGEncoder of Adobe's as3corelib (BSD 3-Clause), ported to
15
+ JavaScript by Andreas Ritter. The entropy decoding, inverse DCT, upsampling
16
+ and colour conversion are ports of
17
+ [libjpeg-turbo](https://github.com/libjpeg-turbo/libjpeg-turbo), so decoded
18
+ pixels match it exactly: this software is based in part on the work of the
19
+ Independent JPEG Group. See [LICENSE](./LICENSE), [NOTICE](./NOTICE) and
20
+ [README.ijg](./README.ijg).
21
+
22
+ ## Why
23
+
24
+ jpeg-js hasn't had a release since 2022. It crashes with `TypeError`s on
25
+ malformed files, decodes colours a few levels off from other software,
26
+ allocates hundreds of megabytes for an ordinary photo, and its encoder writes
27
+ large files. @jimp/jpeg-js keeps its API and fixes all of that:
28
+
29
+ - **Exact:** decoded pixels are identical to libjpeg-turbo's (what Chrome,
30
+ Firefox, Pillow, sharp and ImageMagick use) for every JPEG in a corpus of
31
+ 158, from baseline to progressive, every chroma subsampling, CMYK, YCCK,
32
+ 12-bit and arithmetic-coded.
33
+ - **Robust:** malformed input only ever throws a `JpegError`, never hangs, and
34
+ can't make the decoder allocate more than `maxMemoryUsageInMB`. The decoder
35
+ is fuzzed in its tests.
36
+ - **Small files:** 4:2:0 chroma subsampling, optimized Huffman tables and
37
+ progressive encoding make files about 20% smaller than jpeg-js's at the
38
+ same SSIM.
39
+ - **Fast and lean:** about twice as fast as jpeg-js, with a third of its peak
40
+ memory when decoding.
41
+ - **Anywhere:** ESM and CommonJS, TypeScript types, and no dependencies. It
42
+ works in browsers without a `Buffer` polyfill.
43
+
44
+ ## Install
45
+
46
+ ```sh
47
+ npm install @jimp/jpeg-js
48
+ ```
49
+
50
+ ## Usage
51
+
52
+ ```ts
53
+ import { readFileSync, writeFileSync } from "node:fs";
54
+ import { decodeJpeg, encodeJpeg } from "@jimp/jpeg-js";
55
+
56
+ // { width, height, data: Uint8Array of RGBA pixels, exifBuffer?, comments? }
57
+ const image = decodeJpeg(readFileSync("photo.jpg"));
58
+
59
+ // RGBA (4 bytes per pixel), RGB (3) or greyscale (1) pixels
60
+ const jpeg = encodeJpeg(image, { quality: 80, progressive: true });
61
+
62
+ writeFileSync("small.jpg", jpeg);
63
+ ```
64
+
65
+ ### As a drop-in replacement for jpeg-js
66
+
67
+ `decode` and `encode` take the same arguments as jpeg-js's and return the
68
+ same objects, with `Buffer`s in Node.js:
69
+
70
+ ```js
71
+ const jpeg = require("@jimp/jpeg-js");
72
+
73
+ const raw = jpeg.decode(readFileSync("photo.jpg"), { useTArray: true });
74
+ const { data } = jpeg.encode(raw, 50);
75
+ ```
76
+
77
+ Pixels decode to the same as libjpeg-turbo rather than jpeg-js's, which were
78
+ a few levels off, and the encoder writes smaller files: see
79
+ [Differences from jpeg-js](#differences-from-jpeg-js).
80
+
81
+ ## API
82
+
83
+ ### `decodeJpeg(data, options?)`
84
+
85
+ Decodes a JPEG from a `Uint8Array` (or `Buffer`), `ArrayBuffer`, other typed
86
+ array, `DataView` or array of bytes. Returns
87
+ `{ width, height, data, exifBuffer?, comments? }`, where `data` is a
88
+ `Uint8Array` of RGBA pixels, row by row.
89
+
90
+ | Option | Default | Description |
91
+ | -------------------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
92
+ | `formatAsRGBA` | `true` | RGBA pixels (alpha is always 255); `false` for RGB. |
93
+ | `colorTransform` | auto | Whether the data is YCbCr (or YCCK) to convert to RGB (or CMYK). By default it's picked like libjpeg does, from the JFIF and Adobe markers and the component IDs. |
94
+ | `tolerantDecoding` | `true` | Recover from corrupt data like libjpeg does: junk between segments, a marker that lost its 0xFF, wrong segment lengths, bad Huffman codes, misplaced restart markers. `false` throws instead. |
95
+ | `allowTruncated` | `false` | Decode a JPEG whose image data was cut off, with the rest grey (or unrefined, if progressive), like browsers show it. Otherwise it throws. A missing EOI marker is always fine. |
96
+ | `maxResolutionInMP` | `100` | Throw a `JpegLimitError` for larger images, in megapixels. |
97
+ | `maxMemoryUsageInMB` | `512` | Throw a `JpegLimitError` if decoding would take more memory than this, including the pixels. |
98
+ | `tables` | | The DQT and DHT tables of an "abbreviated" JPEG that leaves them out (e.g. a TIFF's JPEGTables), as a JPEG of only SOI, tables and EOI. |
99
+
100
+ The decoder doesn't rotate images by their EXIF orientation; read it from
101
+ `exifBuffer` if you need it.
102
+
103
+ ### `encodeJpeg(image, options?)`
104
+
105
+ Encodes `{ width, height, data }`, where `data` is RGBA, RGB or greyscale
106
+ pixels (told apart by its length), and returns a `Uint8Array`. Greyscale
107
+ input makes a one-component JPEG. Throws a `TypeError` or `RangeError` for
108
+ input that can't be encoded.
109
+
110
+ | Option | Default | Description |
111
+ | ------------------- | ------------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------- |
112
+ | `quality` | `75` | 1 to 100. Scales the quantization tables of the JPEG standard like libjpeg. |
113
+ | `chromaSubsampling` | `"4:2:0"` below quality 90, else `"4:4:4"` | `"4:4:4"`, `"4:2:2"` or `"4:2:0"`: how much to reduce the resolution of the colour channels. |
114
+ | `optimizeHuffman` | `true` | Huffman tables built for the image instead of the standard's examples, like libjpeg's `optimize_coding`: 5-15% smaller, with the same pixels. |
115
+ | `progressive` | `false` | A progressive JPEG with libjpeg's default scans, shown at low quality first while it loads. A few percent smaller, but slower to encode and decode. |
116
+ | `comments` | | Strings to write in COM segments. |
117
+ | `exif` | | EXIF data for an APP1 segment. "Exif\0" is added if it doesn't start with "Exif", so a decoded `exifBuffer` can be passed back. |
118
+
119
+ Every file has a JFIF header.
120
+
121
+ ### `decode(data, options?)` and `encode(image, quality?)`
122
+
123
+ jpeg-js's API. `decode` takes the options of `decodeJpeg` plus `useTArray`,
124
+ and returns the pixels as a `Buffer` unless `useTArray` is `true` (or there's
125
+ no `Buffer`, as in browsers). `encode` takes `comments` and `exifBuffer` on
126
+ the image, a quality that defaults to 50, and returns
127
+ `{ data, width, height }`. The default export is `{ decode, encode }`.
128
+
129
+ ### Errors
130
+
131
+ Everything the decoder throws extends `JpegError`:
132
+
133
+ - `JpegFormatError`: not a JPEG, or corrupt or truncated beyond recovery;
134
+ - `JpegUnsupportedError`: a lossless or hierarchical JPEG, or sample
135
+ precision other than 8 or 12 bits;
136
+ - `JpegLimitError`: over `maxResolutionInMP` or `maxMemoryUsageInMB`. The
137
+ messages are jpeg-js's, e.g. `maxResolutionInMP limit exceeded by 3MP`.
138
+
139
+ ## Differences from jpeg-js
140
+
141
+ ### Decoding
142
+
143
+ The decoder is rebuilt on pdf.js's marker parser, with libjpeg-turbo's
144
+ Huffman, arithmetic and progressive decoding, islow IDCT, block smoothing,
145
+ "fancy" upsampling and colour conversion. jpeg-js upsampled chroma by
146
+ repeating pixels and used a less exact IDCT, so its colours were up to tens of
147
+ levels off along edges. Sequential JPEGs are decoded a row of MCUs at a time
148
+ into one buffer per component, where jpeg-js allocated a typed array for each
149
+ 8x8 block.
150
+
151
+ ### Encoding
152
+
153
+ The encoder subsamples chroma, optimizes its Huffman tables, can write
154
+ progressive JPEGs, accepts RGB and greyscale input, repeats the last row and
155
+ column of the image to fill edge blocks, and writes into a `Uint8Array`
156
+ instead of an array of numbers.
157
+
158
+ ### jpeg-js issues
159
+
160
+ Every open issue on jpeg-js as of November 2026:
161
+
162
+ | Issue | Status |
163
+ | -------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------- |
164
+ | [#3](https://github.com/jpeg-js/jpeg-js/issues/3) | Done: `encodeJpeg` takes RGBA, RGB or greyscale pixels. |
165
+ | [#12](https://github.com/jpeg-js/jpeg-js/issues/12) | Done: `progressive: true`. |
166
+ | [#17](https://github.com/jpeg-js/jpeg-js/issues/17) | Fixed: fancy upsampling, identical to libjpeg-turbo. |
167
+ | [#21](https://github.com/jpeg-js/jpeg-js/issues/21) | Fixed: restart intervals that end part-way through a scan, extra restart markers and resynchronization on corrupt data. |
168
+ | [#27](https://github.com/jpeg-js/jpeg-js/issues/27) | Not applicable (project governance). |
169
+ | [#30](https://github.com/jpeg-js/jpeg-js/issues/30) | Done: the parser is pdf.js's current one, with libjpeg-turbo's decoding pipeline. |
170
+ | [#31](https://github.com/jpeg-js/jpeg-js/issues/31) | Done: `chromaSubsampling`, 4:2:0 by default below quality 90. |
171
+ | [#37](https://github.com/jpeg-js/jpeg-js/issues/37) | Fixed: the encoder validates its input, and the decoder rejects a file too small for the image it claims before allocating it. |
172
+ | [#42](https://github.com/jpeg-js/jpeg-js/issues/42) | Done: `JpegError` and its subclasses. |
173
+ | [#43](https://github.com/jpeg-js/jpeg-js/issues/43) | Partly: JPEG-in-TIFF works with `tables` (the JPEGTables tag). Old-style JPEG (OJPEG) isn't supported. |
174
+ | [#63](https://github.com/jpeg-js/jpeg-js/issues/63) | Fixed: SOS before SOF throws a `JpegFormatError`. |
175
+ | [#70](https://github.com/jpeg-js/jpeg-js/issues/70) | Partly: decoding takes about a third of the memory. There's no option to decode into a buffer you provide. |
176
+ | [#77](https://github.com/jpeg-js/jpeg-js/issues/77) | Fixed: decoded pixels are identical to libjpeg-turbo's. |
177
+ | [#81](https://github.com/jpeg-js/jpeg-js/issues/81) | Fixed: a progressive JPEG cut off between scans decodes, smoothed like libjpeg-turbo does. |
178
+ | [#83](https://github.com/jpeg-js/jpeg-js/issues/83) | Done: ESM, CommonJS and browser builds, with types. |
179
+ | [#85](https://github.com/jpeg-js/jpeg-js/issues/85) | Done: the `tables` option. |
180
+ | [#94](https://github.com/jpeg-js/jpeg-js/issues/94) | Fixed: SOS before SOF throws a `JpegFormatError`. |
181
+ | [#95](https://github.com/jpeg-js/jpeg-js/issues/95) | Fixed: an unknown component in SOS throws a `JpegFormatError`. |
182
+ | [#96](https://github.com/jpeg-js/jpeg-js/issues/96) | Fixed: a missing quantization table throws a `JpegFormatError`. |
183
+ | [#97](https://github.com/jpeg-js/jpeg-js/issues/97) | Fixed: a missing Huffman table throws a `JpegFormatError` (in sequential JPEGs, the standard tables are used, like libjpeg). |
184
+ | [#107](https://github.com/jpeg-js/jpeg-js/issues/107) | Fixed in jpeg-js 0.4.4; sampling factors are validated here too. |
185
+ | [#109](https://github.com/jpeg-js/jpeg-js/issues/109) | Couldn't reproduce: the attached file decodes. Junk where a marker should be is skipped. |
186
+ | [#112](https://github.com/jpeg-js/jpeg-js/issues/112) | Fixed: throws a `JpegFormatError`. |
187
+ | [#113](https://github.com/jpeg-js/jpeg-js/issues/113) | Not a codec bug: EXIF orientation isn't applied by the decoder. Jimp applies it, once. Pass `exifBuffer` back to keep it. |
188
+ | [#115](https://github.com/jpeg-js/jpeg-js/issues/115) | Fixed: the colour space is picked like libjpeg, so an Adobe transform of 0 means RGB. An explicit `colorTransform` always wins. |
189
+ | [#117](https://github.com/jpeg-js/jpeg-js/issues/117) | Fixed: extra restart markers are skipped. |
190
+ | [Jimp #153](https://github.com/jimp-dev/jimp/issues/153) | Fixed: decoding a 21 MP photo peaks at 336 MB instead of 957 MB, and a 3.3 MP one at 89 MB instead of 231 MB. |
191
+
192
+ ## Conformance
193
+
194
+ The tests decode a corpus of 159 JPEGs and compare them with libjpeg-turbo
195
+ 3.2's djpeg, with its default islow IDCT and fancy upsampling (the references
196
+ were made with SIMD disabled; its NEON build gives the same output for every
197
+ file): libjpeg-turbo's test images, jpeg-js's fixtures, JPEGs from
198
+ pdf.js's test files, the images attached to jpeg-js issues, and generated ones
199
+ covering 4:4:4, 4:2:2, 4:2:0, 4:4:0, 4:1:1, 4:1:0 and mixed sampling,
200
+ baseline, progressive and arithmetic coding, restart intervals, greyscale,
201
+ RGB, CMYK, YCCK, 12-bit samples, 16-bit quantization tables, non-interleaved
202
+ scans, sizes from 1x1 that don't fill whole MCUs, and progressive JPEGs cut
203
+ off after a scan. The tolerance is 0: all 158 are identical to djpeg's output,
204
+ except for a lossless JPEG, which is rejected with a `JpegUnsupportedError`.
205
+ See
206
+ [test/corpus](./test/corpus/README.md).
207
+
208
+ ## Benchmarks
209
+
210
+ On an Apple Silicon Mac with Node.js 24, against jpeg-js 0.4.4, with
211
+ `bench/speed.mjs` and `bench/quality.mjs` (see [bench/](./bench/README.md)).
212
+ Times are the median of 3 to 5 runs; memory is the peak resident set size
213
+ minus that of an idle Node.js process. The photos are from Jimp's tests; the
214
+ 21 MP one is a 3x enlargement of the 2376x1408 one.
215
+
216
+ ### Decoding
217
+
218
+ | Image | jpeg-js | @jimp/jpeg-js |
219
+ | ----------------------------- | -------------- | -------------- |
220
+ | 640x426, 4:4:4 | 10 ms, 65 MB | 3.3 ms, 27 MB |
221
+ | 2376x1408, 4:2:0 | 57 ms, 231 MB | 26 ms, 89 MB |
222
+ | 2376x1408, 4:2:0, progressive | 70 ms, 231 MB | 46 ms, 112 MB |
223
+ | 10000x783, 4:4:4 | 379 ms, 468 MB | 210 ms, 183 MB |
224
+ | 7128x4224 (21 MP), 4:2:0 | 421 ms, 957 MB | 225 ms, 336 MB |
225
+
226
+ ### Encoding
227
+
228
+ Encoding the decoded image at quality 75 and 100, with the default options
229
+ (memory includes decoding the input):
230
+
231
+ | Image | jpeg-js, q75 | @jimp/jpeg-js, q75 | jpeg-js, q100 | @jimp/jpeg-js, q100 |
232
+ | ----------------- | ----------------------- | ----------------------- | ------------------------ | ----------------------- |
233
+ | 640x426 | 15 ms, 113 MB, 52 KB | 4 ms, 24 MB, 36 KB | 17 ms, 141 MB, 165 KB | 7 ms, 29 MB, 143 KB |
234
+ | 2376x1408 | 59 ms, 168 MB, 246 KB | 36 ms, 44 MB, 189 KB | 78 ms, 234 MB, 892 KB | 57 ms, 66 MB, 732 KB |
235
+ | 10000x783 | 259 ms, 261 MB, 1749 KB | 121 ms, 113 MB, 1440 KB | 587 ms, 580 MB, 10697 KB | 332 ms, 199 MB, 9790 KB |
236
+ | 7128x4224 (21 MP) | 440 ms, 344 MB, 1338 KB | 328 ms, 195 MB, 817 KB | 578 ms, 549 MB, 5048 KB | 421 ms, 251 MB, 4144 KB |
237
+
238
+ ### File size at equal quality
239
+
240
+ Nine lossless photos (seven from the Kodak set, and two from Jimp's tests)
241
+ were encoded by both, decoded with libjpeg-turbo, and compared with the
242
+ original. For each quality of jpeg-js, the table gives how much smaller
243
+ @jimp/jpeg-js's file is at the same SSIM (on luma) or PSNR (on RGB),
244
+ interpolated between its own qualities. 4:2:0 subsampling costs little SSIM
245
+ but some PSNR, which is why the two differ below quality 90.
246
+
247
+ | jpeg-js quality | Same SSIM | Same SSIM, progressive | Same PSNR | Same PSNR, progressive |
248
+ | --------------- | --------- | ---------------------- | --------- | ---------------------- |
249
+ | 50 | -20.4% | -20.6% | -8.8% | -9.8% |
250
+ | 75 | -18.5% | -20.4% | -3.9% | -7.0% |
251
+ | 85 | -18.6% | -21.7% | -4.5% | -8.2% |
252
+ | 90 | -3.6% | -7.2% | -3.1% | -6.8% |
253
+ | 95 | -4.6% | -9.1% | -4.9% | -9.4% |
254
+ | 100 | | | -12.2% | -16.4% |
255
+
256
+ At the same quality setting, quality 100 files are 11% smaller (3.5 MB
257
+ instead of 4.0 MB for the nine) with the same PSNR and SSIM.
258
+
259
+ ## License
260
+
261
+ BSD 3-Clause AND Apache 2.0 AND IJG: see [LICENSE](./LICENSE) for which
262
+ files are under which license.
@@ -0,0 +1,44 @@
1
+ /**
2
+ * The API of jpeg-js, so @jimp/jpeg-js is a drop-in replacement for it:
3
+ * `decode(data, options)` and `encode(image, quality)`.
4
+ */
5
+ import { type DecodeJpegOptions, type JpegData } from "./decode.js";
6
+ import { type RawImageData } from "./encode.js";
7
+ export interface DecodeOptions extends DecodeJpegOptions {
8
+ /**
9
+ * Return the pixels as a `Uint8Array` instead of a Node.js `Buffer`. Where
10
+ * there is no `Buffer` (browsers) it's always a `Uint8Array`.
11
+ * @default false
12
+ */
13
+ useTArray?: boolean;
14
+ }
15
+ export interface DecodedImage<T extends Uint8Array = Uint8Array> {
16
+ width: number;
17
+ height: number;
18
+ data: T;
19
+ exifBuffer?: Uint8Array;
20
+ comments?: string[];
21
+ }
22
+ /**
23
+ * Decodes a JPEG to RGBA (or RGB) pixels, like jpeg-js's `decode`. See
24
+ * `decodeJpeg` for the errors it throws.
25
+ */
26
+ export declare function decode(jpegData: JpegData, options?: DecodeOptions): DecodedImage;
27
+ export interface EncodeInput extends RawImageData {
28
+ /** Text to write in COM segments, one per string. */
29
+ comments?: string[];
30
+ /** EXIF data to write in an APP1 segment, see `EncodeJpegOptions.exif`. */
31
+ exifBuffer?: JpegData;
32
+ }
33
+ export interface EncodedImage<T extends Uint8Array = Uint8Array> {
34
+ width: number;
35
+ height: number;
36
+ data: T;
37
+ }
38
+ /**
39
+ * Encodes RGBA (or RGB, or greyscale) pixels as a JPEG of the given quality
40
+ * (1..100), like jpeg-js's `encode`.
41
+ * @param quality defaults to 50, like jpeg-js
42
+ */
43
+ export declare function encode(image: EncodeInput, quality?: number): EncodedImage;
44
+ //# sourceMappingURL=compat.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"compat.d.ts","sourceRoot":"","sources":["../../src/compat.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAc,KAAK,iBAAiB,EAAE,KAAK,QAAQ,EAAE,MAAM,aAAa,CAAC;AAChF,OAAO,EAAc,KAAK,YAAY,EAAE,MAAM,aAAa,CAAC;AAsB5D,MAAM,WAAW,aAAc,SAAQ,iBAAiB;IACtD;;;;OAIG;IACH,SAAS,CAAC,EAAE,OAAO,CAAC;CACrB;AAED,MAAM,WAAW,YAAY,CAAC,CAAC,SAAS,UAAU,GAAG,UAAU;IAC7D,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,CAAC,CAAC;IACR,UAAU,CAAC,EAAE,UAAU,CAAC;IACxB,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAC;CACrB;AAED;;;GAGG;AACH,wBAAgB,MAAM,CACpB,QAAQ,EAAE,QAAQ,EAClB,OAAO,GAAE,aAAkB,GAC1B,YAAY,CASd;AAED,MAAM,WAAW,WAAY,SAAQ,YAAY;IAC/C,qDAAqD;IACrD,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAC;IACpB,2EAA2E;IAC3E,UAAU,CAAC,EAAE,QAAQ,CAAC;CACvB;AAED,MAAM,WAAW,YAAY,CAAC,CAAC,SAAS,UAAU,GAAG,UAAU;IAC7D,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,CAAC,CAAC;CACT;AAED;;;;GAIG;AACH,wBAAgB,MAAM,CAAC,KAAK,EAAE,WAAW,EAAE,OAAO,SAAK,GAAG,YAAY,CAQrE"}
@@ -0,0 +1,42 @@
1
+ /**
2
+ * The API of jpeg-js, so @jimp/jpeg-js is a drop-in replacement for it:
3
+ * `decode(data, options)` and `encode(image, quality)`.
4
+ */
5
+ import { decodeJpeg } from "./decode.js";
6
+ import { encodeJpeg } from "./encode.js";
7
+ /**
8
+ * Like jpeg-js, Node.js gets a `Buffer`. It's found at runtime, so bundlers
9
+ * don't add a Buffer polyfill for browsers, which get a `Uint8Array`.
10
+ */
11
+ function toBuffer(data) {
12
+ const Buffer = globalThis.Buffer;
13
+ return Buffer
14
+ ? Buffer.from(data.buffer, data.byteOffset, data.byteLength)
15
+ : data;
16
+ }
17
+ /**
18
+ * Decodes a JPEG to RGBA (or RGB) pixels, like jpeg-js's `decode`. See
19
+ * `decodeJpeg` for the errors it throws.
20
+ */
21
+ export function decode(jpegData, options = {}) {
22
+ const { useTArray = false, ...decodeOptions } = options;
23
+ const image = decodeJpeg(jpegData, decodeOptions);
24
+ if (!useTArray) {
25
+ image.data = toBuffer(image.data);
26
+ }
27
+ return image;
28
+ }
29
+ /**
30
+ * Encodes RGBA (or RGB, or greyscale) pixels as a JPEG of the given quality
31
+ * (1..100), like jpeg-js's `encode`.
32
+ * @param quality defaults to 50, like jpeg-js
33
+ */
34
+ export function encode(image, quality = 50) {
35
+ const data = encodeJpeg(image, {
36
+ quality,
37
+ comments: image.comments,
38
+ exif: image.exifBuffer,
39
+ });
40
+ return { data: toBuffer(data), width: image.width, height: image.height };
41
+ }
42
+ //# sourceMappingURL=compat.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"compat.js","sourceRoot":"","sources":["../../src/compat.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,UAAU,EAAyC,MAAM,aAAa,CAAC;AAChF,OAAO,EAAE,UAAU,EAAqB,MAAM,aAAa,CAAC;AAU5D;;;GAGG;AACH,SAAS,QAAQ,CAAC,IAAgB;IAChC,MAAM,MAAM,GAAI,UAA6C,CAAC,MAAM,CAAC;IAErE,OAAO,MAAM;QACX,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,MAAqB,EAAE,IAAI,CAAC,UAAU,EAAE,IAAI,CAAC,UAAU,CAAC;QAC3E,CAAC,CAAC,IAAI,CAAC;AACX,CAAC;AAmBD;;;GAGG;AACH,MAAM,UAAU,MAAM,CACpB,QAAkB,EAClB,OAAO,GAAkB,EAAE;IAE3B,MAAM,EAAE,SAAS,GAAG,KAAK,EAAE,GAAG,aAAa,EAAE,GAAG,OAAO,CAAC;IACxD,MAAM,KAAK,GAAG,UAAU,CAAC,QAAQ,EAAE,aAAa,CAAC,CAAC;IAElD,IAAI,CAAC,SAAS,EAAE,CAAC;QACf,KAAK,CAAC,IAAI,GAAG,QAAQ,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IACpC,CAAC;IAED,OAAO,KAAK,CAAC;AACf,CAAC;AAeD;;;;GAIG;AACH,MAAM,UAAU,MAAM,CAAC,KAAkB,EAAE,OAAO,GAAG,EAAE;IACrD,MAAM,IAAI,GAAG,UAAU,CAAC,KAAK,EAAE;QAC7B,OAAO;QACP,QAAQ,EAAE,KAAK,CAAC,QAAQ;QACxB,IAAI,EAAE,KAAK,CAAC,UAAU;KACvB,CAAC,CAAC;IAEH,OAAO,EAAE,IAAI,EAAE,QAAQ,CAAC,IAAI,CAAC,EAAE,KAAK,EAAE,KAAK,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,CAAC,MAAM,EAAE,CAAC;AAC5E,CAAC","sourcesContent":["/**\n * The API of jpeg-js, so @jimp/jpeg-js is a drop-in replacement for it:\n * `decode(data, options)` and `encode(image, quality)`.\n */\n\nimport { decodeJpeg, type DecodeJpegOptions, type JpegData } from \"./decode.js\";\nimport { encodeJpeg, type RawImageData } from \"./encode.js\";\n\ninterface NodeBuffer extends Uint8Array {\n toString(encoding?: string): string;\n}\n\ninterface BufferConstructor {\n from(buffer: ArrayBuffer, byteOffset: number, length: number): NodeBuffer;\n}\n\n/**\n * Like jpeg-js, Node.js gets a `Buffer`. It's found at runtime, so bundlers\n * don't add a Buffer polyfill for browsers, which get a `Uint8Array`.\n */\nfunction toBuffer(data: Uint8Array): Uint8Array {\n const Buffer = (globalThis as { Buffer?: BufferConstructor }).Buffer;\n\n return Buffer\n ? Buffer.from(data.buffer as ArrayBuffer, data.byteOffset, data.byteLength)\n : data;\n}\n\nexport interface DecodeOptions extends DecodeJpegOptions {\n /**\n * Return the pixels as a `Uint8Array` instead of a Node.js `Buffer`. Where\n * there is no `Buffer` (browsers) it's always a `Uint8Array`.\n * @default false\n */\n useTArray?: boolean;\n}\n\nexport interface DecodedImage<T extends Uint8Array = Uint8Array> {\n width: number;\n height: number;\n data: T;\n exifBuffer?: Uint8Array;\n comments?: string[];\n}\n\n/**\n * Decodes a JPEG to RGBA (or RGB) pixels, like jpeg-js's `decode`. See\n * `decodeJpeg` for the errors it throws.\n */\nexport function decode(\n jpegData: JpegData,\n options: DecodeOptions = {},\n): DecodedImage {\n const { useTArray = false, ...decodeOptions } = options;\n const image = decodeJpeg(jpegData, decodeOptions);\n\n if (!useTArray) {\n image.data = toBuffer(image.data);\n }\n\n return image;\n}\n\nexport interface EncodeInput extends RawImageData {\n /** Text to write in COM segments, one per string. */\n comments?: string[];\n /** EXIF data to write in an APP1 segment, see `EncodeJpegOptions.exif`. */\n exifBuffer?: JpegData;\n}\n\nexport interface EncodedImage<T extends Uint8Array = Uint8Array> {\n width: number;\n height: number;\n data: T;\n}\n\n/**\n * Encodes RGBA (or RGB, or greyscale) pixels as a JPEG of the given quality\n * (1..100), like jpeg-js's `encode`.\n * @param quality defaults to 50, like jpeg-js\n */\nexport function encode(image: EncodeInput, quality = 50): EncodedImage {\n const data = encodeJpeg(image, {\n quality,\n comments: image.comments,\n exif: image.exifBuffer,\n });\n\n return { data: toBuffer(data), width: image.width, height: image.height };\n}\n"]}
@@ -0,0 +1,73 @@
1
+ /** Bytes of a JPEG: a Uint8Array (or Node Buffer), ArrayBuffer, other typed array or DataView, or an array of byte values. */
2
+ export type JpegData = ArrayBufferView | ArrayBuffer | ArrayLike<number>;
3
+ export interface DecodeJpegOptions {
4
+ /**
5
+ * Whether the data is YCbCr (or YCCK for 4 components) and must be
6
+ * converted to RGB (or CMYK). By default this is picked from the JFIF and
7
+ * Adobe markers and the component IDs, like libjpeg does.
8
+ */
9
+ colorTransform?: boolean;
10
+ /**
11
+ * Decode to RGBA (4 bytes per pixel, alpha always 255) instead of RGB.
12
+ * @default true
13
+ */
14
+ formatAsRGBA?: boolean;
15
+ /**
16
+ * Recover from corrupt data like libjpeg does: skip junk between segments,
17
+ * fix bad segment lengths, and decode damaged entropy-coded data as well as
18
+ * possible. When false, any of these throws a `JpegFormatError`.
19
+ * @default true
20
+ */
21
+ tolerantDecoding?: boolean;
22
+ /**
23
+ * Decode a JPEG whose image data was cut off, leaving the missing part grey
24
+ * (or unrefined, for progressive JPEGs), like browsers show it. Otherwise
25
+ * a truncated JPEG throws a `JpegFormatError`. A JPEG that only lacks its
26
+ * end of image marker, or a few bits of its last row, always decodes.
27
+ * @default false
28
+ */
29
+ allowTruncated?: boolean;
30
+ /**
31
+ * Throw a `JpegLimitError` instead of decoding an image of more than this
32
+ * many megapixels.
33
+ * @default 100
34
+ */
35
+ maxResolutionInMP?: number;
36
+ /**
37
+ * Throw a `JpegLimitError` instead of decoding an image that needs more than
38
+ * about this many mebibytes of memory, including the decoded pixels.
39
+ * @default 512
40
+ */
41
+ maxMemoryUsageInMB?: number;
42
+ /**
43
+ * The DQT and DHT tables of an "abbreviated" JPEG, which leaves them out
44
+ * because they're stored elsewhere (e.g. the JPEGTables tag of a TIFF), as
45
+ * a JPEG made of only SOI, tables and EOI.
46
+ */
47
+ tables?: JpegData;
48
+ }
49
+ export interface JpegImage {
50
+ width: number;
51
+ height: number;
52
+ /** RGBA or RGB pixels, row by row. */
53
+ data: Uint8Array;
54
+ /**
55
+ * The EXIF data of the APP1 segment, from its second padding byte (the
56
+ * byte after "Exif\0"), like jpeg-js.
57
+ */
58
+ exifBuffer?: Uint8Array;
59
+ /** The text of each COM segment, if there are any. */
60
+ comments?: string[];
61
+ }
62
+ /** Views (or copies, for an array of numbers) `data` as bytes. */
63
+ export declare function toBytes(data: JpegData, name?: string): Uint8Array;
64
+ /**
65
+ * Decodes a JPEG to RGBA (or RGB) pixels.
66
+ *
67
+ * Throws a `JpegFormatError` for data that isn't a JPEG or is corrupt, a
68
+ * `JpegUnsupportedError` for JPEG features this decoder doesn't support (like
69
+ * lossless and hierarchical JPEGs), and a `JpegLimitError` when a limit is
70
+ * exceeded. All of them extend `JpegError`.
71
+ */
72
+ export declare function decodeJpeg(data: JpegData, options?: DecodeJpegOptions): JpegImage;
73
+ //# sourceMappingURL=decode.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"decode.d.ts","sourceRoot":"","sources":["../../src/decode.ts"],"names":[],"mappings":"AAGA,8HAA8H;AAC9H,MAAM,MAAM,QAAQ,GAAG,eAAe,GAAG,WAAW,GAAG,SAAS,CAAC,MAAM,CAAC,CAAC;AAEzE,MAAM,WAAW,iBAAiB;IAChC;;;;OAIG;IACH,cAAc,CAAC,EAAE,OAAO,CAAC;IACzB;;;OAGG;IACH,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB;;;;;OAKG;IACH,gBAAgB,CAAC,EAAE,OAAO,CAAC;IAC3B;;;;;;OAMG;IACH,cAAc,CAAC,EAAE,OAAO,CAAC;IACzB;;;;OAIG;IACH,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B;;;;OAIG;IACH,kBAAkB,CAAC,EAAE,MAAM,CAAC;IAC5B;;;;OAIG;IACH,MAAM,CAAC,EAAE,QAAQ,CAAC;CACnB;AAED,MAAM,WAAW,SAAS;IACxB,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,sCAAsC;IACtC,IAAI,EAAE,UAAU,CAAC;IACjB;;;OAGG;IACH,UAAU,CAAC,EAAE,UAAU,CAAC;IACxB,sDAAsD;IACtD,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAC;CACrB;AAED,kEAAkE;AAClE,wBAAgB,OAAO,CAAC,IAAI,EAAE,QAAQ,EAAE,IAAI,SAAS,GAAG,UAAU,CAwBjE;AAUD;;;;;;;GAOG;AACH,wBAAgB,UAAU,CACxB,IAAI,EAAE,QAAQ,EACd,OAAO,GAAE,iBAAsB,GAC9B,SAAS,CA2DX"}
@@ -0,0 +1,82 @@
1
+ import { JpegDecoder } from "./decoder/decoder.js";
2
+ import { JpegError, JpegFormatError, JpegLimitError } from "./errors.js";
3
+ /** Views (or copies, for an array of numbers) `data` as bytes. */
4
+ export function toBytes(data, name = "data") {
5
+ if (data instanceof Uint8Array) {
6
+ return data;
7
+ }
8
+ if (ArrayBuffer.isView(data)) {
9
+ return new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
10
+ }
11
+ if (data instanceof ArrayBuffer) {
12
+ return new Uint8Array(data);
13
+ }
14
+ if (data !== null &&
15
+ typeof data === "object" &&
16
+ typeof data.length === "number") {
17
+ return Uint8Array.from(data);
18
+ }
19
+ throw new TypeError(`Expected the ${name} to be a Uint8Array, ArrayBuffer, typed array or array of bytes`);
20
+ }
21
+ const defaults = {
22
+ formatAsRGBA: true,
23
+ tolerantDecoding: true,
24
+ allowTruncated: false,
25
+ maxResolutionInMP: 100,
26
+ maxMemoryUsageInMB: 512,
27
+ };
28
+ /**
29
+ * Decodes a JPEG to RGBA (or RGB) pixels.
30
+ *
31
+ * Throws a `JpegFormatError` for data that isn't a JPEG or is corrupt, a
32
+ * `JpegUnsupportedError` for JPEG features this decoder doesn't support (like
33
+ * lossless and hierarchical JPEGs), and a `JpegLimitError` when a limit is
34
+ * exceeded. All of them extend `JpegError`.
35
+ */
36
+ export function decodeJpeg(data, options = {}) {
37
+ const opts = { ...defaults };
38
+ for (const [key, value] of Object.entries(options)) {
39
+ if (value !== undefined) {
40
+ opts[key] = value;
41
+ }
42
+ }
43
+ const bytes = toBytes(data);
44
+ const decoder = new JpegDecoder({
45
+ ...opts,
46
+ colorTransform: options.colorTransform,
47
+ tables: options.tables === undefined
48
+ ? undefined
49
+ : toBytes(options.tables, "tables"),
50
+ });
51
+ try {
52
+ decoder.parse(bytes);
53
+ const channels = opts.formatAsRGBA ? 4 : 3;
54
+ const size = decoder.width * decoder.height * channels;
55
+ let pixels;
56
+ try {
57
+ pixels = new Uint8Array(size);
58
+ }
59
+ catch {
60
+ throw new JpegLimitError(`Could not allocate enough memory for the image. Required: ${size}`);
61
+ }
62
+ decoder.getPixels(pixels, channels);
63
+ const image = {
64
+ width: decoder.width,
65
+ height: decoder.height,
66
+ exifBuffer: decoder.exif,
67
+ data: pixels,
68
+ };
69
+ if (decoder.comments.length > 0) {
70
+ image.comments = decoder.comments;
71
+ }
72
+ return image;
73
+ }
74
+ catch (error) {
75
+ if (error instanceof JpegError) {
76
+ throw error;
77
+ }
78
+ // A bug, but keep the promise that only JpegErrors are thrown
79
+ throw new JpegFormatError(`Failed to decode JPEG: ${error instanceof Error ? error.message : String(error)}`, { cause: error });
80
+ }
81
+ }
82
+ //# sourceMappingURL=decode.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"decode.js","sourceRoot":"","sources":["../../src/decode.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AACnD,OAAO,EAAE,SAAS,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAkEzE,kEAAkE;AAClE,MAAM,UAAU,OAAO,CAAC,IAAc,EAAE,IAAI,GAAG,MAAM;IACnD,IAAI,IAAI,YAAY,UAAU,EAAE,CAAC;QAC/B,OAAO,IAAI,CAAC;IACd,CAAC;IAED,IAAI,WAAW,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC;QAC7B,OAAO,IAAI,UAAU,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,UAAU,EAAE,IAAI,CAAC,UAAU,CAAC,CAAC;IACvE,CAAC;IAED,IAAI,IAAI,YAAY,WAAW,EAAE,CAAC;QAChC,OAAO,IAAI,UAAU,CAAC,IAAI,CAAC,CAAC;IAC9B,CAAC;IAED,IACE,IAAI,KAAK,IAAI;QACb,OAAO,IAAI,KAAK,QAAQ;QACxB,OAAQ,IAA0B,CAAC,MAAM,KAAK,QAAQ,EACtD,CAAC;QACD,OAAO,UAAU,CAAC,IAAI,CAAC,IAAyB,CAAC,CAAC;IACpD,CAAC;IAED,MAAM,IAAI,SAAS,CACjB,gBAAgB,IAAI,iEAAiE,CACtF,CAAC;AACJ,CAAC;AAED,MAAM,QAAQ,GAAG;IACf,YAAY,EAAE,IAAI;IAClB,gBAAgB,EAAE,IAAI;IACtB,cAAc,EAAE,KAAK;IACrB,iBAAiB,EAAE,GAAG;IACtB,kBAAkB,EAAE,GAAG;CACxB,CAAC;AAEF;;;;;;;GAOG;AACH,MAAM,UAAU,UAAU,CACxB,IAAc,EACd,OAAO,GAAsB,EAAE;IAE/B,MAAM,IAAI,GAAG,EAAE,GAAG,QAAQ,EAAE,CAAC;IAE7B,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC;QACnD,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;YACvB,IAAgC,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;QACjD,CAAC;IACH,CAAC;IAED,MAAM,KAAK,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAC5B,MAAM,OAAO,GAAG,IAAI,WAAW,CAAC;QAC9B,GAAG,IAAI;QACP,cAAc,EAAE,OAAO,CAAC,cAAc;QACtC,MAAM,EACJ,OAAO,CAAC,MAAM,KAAK,SAAS;YAC1B,CAAC,CAAC,SAAS;YACX,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,MAAM,EAAE,QAAQ,CAAC;KACxC,CAAC,CAAC;IAEH,IAAI,CAAC;QACH,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;QAErB,MAAM,QAAQ,GAAG,IAAI,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;QAC3C,MAAM,IAAI,GAAG,OAAO,CAAC,KAAK,GAAG,OAAO,CAAC,MAAM,GAAG,QAAQ,CAAC;QACvD,IAAI,MAAkB,CAAC;QAEvB,IAAI,CAAC;YACH,MAAM,GAAG,IAAI,UAAU,CAAC,IAAI,CAAC,CAAC;QAChC,CAAC;QAAC,MAAM,CAAC;YACP,MAAM,IAAI,cAAc,CACtB,6DAA6D,IAAI,EAAE,CACpE,CAAC;QACJ,CAAC;QAED,OAAO,CAAC,SAAS,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;QAEpC,MAAM,KAAK,GAAc;YACvB,KAAK,EAAE,OAAO,CAAC,KAAK;YACpB,MAAM,EAAE,OAAO,CAAC,MAAM;YACtB,UAAU,EAAE,OAAO,CAAC,IAAI;YACxB,IAAI,EAAE,MAAM;SACb,CAAC;QAEF,IAAI,OAAO,CAAC,QAAQ,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YAChC,KAAK,CAAC,QAAQ,GAAG,OAAO,CAAC,QAAQ,CAAC;QACpC,CAAC;QAED,OAAO,KAAK,CAAC;IACf,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,KAAK,YAAY,SAAS,EAAE,CAAC;YAC/B,MAAM,KAAK,CAAC;QACd,CAAC;QAED,8DAA8D;QAC9D,MAAM,IAAI,eAAe,CACvB,0BAA0B,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,EAClF,EAAE,KAAK,EAAE,KAAK,EAAE,CACjB,CAAC;IACJ,CAAC;AACH,CAAC","sourcesContent":["import { JpegDecoder } from \"./decoder/decoder.js\";\nimport { JpegError, JpegFormatError, JpegLimitError } from \"./errors.js\";\n\n/** Bytes of a JPEG: a Uint8Array (or Node Buffer), ArrayBuffer, other typed array or DataView, or an array of byte values. */\nexport type JpegData = ArrayBufferView | ArrayBuffer | ArrayLike<number>;\n\nexport interface DecodeJpegOptions {\n /**\n * Whether the data is YCbCr (or YCCK for 4 components) and must be\n * converted to RGB (or CMYK). By default this is picked from the JFIF and\n * Adobe markers and the component IDs, like libjpeg does.\n */\n colorTransform?: boolean;\n /**\n * Decode to RGBA (4 bytes per pixel, alpha always 255) instead of RGB.\n * @default true\n */\n formatAsRGBA?: boolean;\n /**\n * Recover from corrupt data like libjpeg does: skip junk between segments,\n * fix bad segment lengths, and decode damaged entropy-coded data as well as\n * possible. When false, any of these throws a `JpegFormatError`.\n * @default true\n */\n tolerantDecoding?: boolean;\n /**\n * Decode a JPEG whose image data was cut off, leaving the missing part grey\n * (or unrefined, for progressive JPEGs), like browsers show it. Otherwise\n * a truncated JPEG throws a `JpegFormatError`. A JPEG that only lacks its\n * end of image marker, or a few bits of its last row, always decodes.\n * @default false\n */\n allowTruncated?: boolean;\n /**\n * Throw a `JpegLimitError` instead of decoding an image of more than this\n * many megapixels.\n * @default 100\n */\n maxResolutionInMP?: number;\n /**\n * Throw a `JpegLimitError` instead of decoding an image that needs more than\n * about this many mebibytes of memory, including the decoded pixels.\n * @default 512\n */\n maxMemoryUsageInMB?: number;\n /**\n * The DQT and DHT tables of an \"abbreviated\" JPEG, which leaves them out\n * because they're stored elsewhere (e.g. the JPEGTables tag of a TIFF), as\n * a JPEG made of only SOI, tables and EOI.\n */\n tables?: JpegData;\n}\n\nexport interface JpegImage {\n width: number;\n height: number;\n /** RGBA or RGB pixels, row by row. */\n data: Uint8Array;\n /**\n * The EXIF data of the APP1 segment, from its second padding byte (the\n * byte after \"Exif\\0\"), like jpeg-js.\n */\n exifBuffer?: Uint8Array;\n /** The text of each COM segment, if there are any. */\n comments?: string[];\n}\n\n/** Views (or copies, for an array of numbers) `data` as bytes. */\nexport function toBytes(data: JpegData, name = \"data\"): Uint8Array {\n if (data instanceof Uint8Array) {\n return data;\n }\n\n if (ArrayBuffer.isView(data)) {\n return new Uint8Array(data.buffer, data.byteOffset, data.byteLength);\n }\n\n if (data instanceof ArrayBuffer) {\n return new Uint8Array(data);\n }\n\n if (\n data !== null &&\n typeof data === \"object\" &&\n typeof (data as ArrayLike<number>).length === \"number\"\n ) {\n return Uint8Array.from(data as ArrayLike<number>);\n }\n\n throw new TypeError(\n `Expected the ${name} to be a Uint8Array, ArrayBuffer, typed array or array of bytes`,\n );\n}\n\nconst defaults = {\n formatAsRGBA: true,\n tolerantDecoding: true,\n allowTruncated: false,\n maxResolutionInMP: 100,\n maxMemoryUsageInMB: 512,\n};\n\n/**\n * Decodes a JPEG to RGBA (or RGB) pixels.\n *\n * Throws a `JpegFormatError` for data that isn't a JPEG or is corrupt, a\n * `JpegUnsupportedError` for JPEG features this decoder doesn't support (like\n * lossless and hierarchical JPEGs), and a `JpegLimitError` when a limit is\n * exceeded. All of them extend `JpegError`.\n */\nexport function decodeJpeg(\n data: JpegData,\n options: DecodeJpegOptions = {},\n): JpegImage {\n const opts = { ...defaults };\n\n for (const [key, value] of Object.entries(options)) {\n if (value !== undefined) {\n (opts as Record<string, unknown>)[key] = value;\n }\n }\n\n const bytes = toBytes(data);\n const decoder = new JpegDecoder({\n ...opts,\n colorTransform: options.colorTransform,\n tables:\n options.tables === undefined\n ? undefined\n : toBytes(options.tables, \"tables\"),\n });\n\n try {\n decoder.parse(bytes);\n\n const channels = opts.formatAsRGBA ? 4 : 3;\n const size = decoder.width * decoder.height * channels;\n let pixels: Uint8Array;\n\n try {\n pixels = new Uint8Array(size);\n } catch {\n throw new JpegLimitError(\n `Could not allocate enough memory for the image. Required: ${size}`,\n );\n }\n\n decoder.getPixels(pixels, channels);\n\n const image: JpegImage = {\n width: decoder.width,\n height: decoder.height,\n exifBuffer: decoder.exif,\n data: pixels,\n };\n\n if (decoder.comments.length > 0) {\n image.comments = decoder.comments;\n }\n\n return image;\n } catch (error) {\n if (error instanceof JpegError) {\n throw error;\n }\n\n // A bug, but keep the promise that only JpegErrors are thrown\n throw new JpegFormatError(\n `Failed to decode JPEG: ${error instanceof Error ? error.message : String(error)}`,\n { cause: error },\n );\n }\n}\n"]}
@@ -0,0 +1,66 @@
1
+ import { BitReader } from "./huffman.js";
2
+ /** Conditioning of an arithmetic table, from a DAC marker. */
3
+ export interface ArithmeticConditioning {
4
+ /** Lower and upper bounds of the DC difference categories (L and U). */
5
+ dcLower: number[];
6
+ dcUpper: number[];
7
+ /** Coefficient index where AC magnitude contexts switch (Kx). */
8
+ acK: number[];
9
+ }
10
+ export declare function defaultConditioning(): ArithmeticConditioning;
11
+ export interface ArithmeticComponent {
12
+ coefficients: Int16Array;
13
+ /** Conditioning table numbers (DAC) of the component in this scan. */
14
+ dcArithmetic: number;
15
+ acArithmetic: number;
16
+ pred: number;
17
+ /** DC context of the last difference (F.1.4.4.1.2). */
18
+ dcContext: number;
19
+ }
20
+ /**
21
+ * The arithmetic decoder's registers and statistics, reading bytes from the
22
+ * scan. Like libjpeg, data past a marker reads as zeros, which is how a
23
+ * scan ends in arithmetic coding.
24
+ */
25
+ export declare class ArithmeticReader extends BitReader {
26
+ /** Code register, interval size and bit counter (ITU T.81 D.2). */
27
+ c: number;
28
+ a: number;
29
+ ct: number;
30
+ /** Set on corrupt data, until the next restart: blocks are left as is. */
31
+ failed: boolean;
32
+ readonly dcStats: Uint8Array<ArrayBuffer>[];
33
+ readonly acStats: Uint8Array<ArrayBuffer>[];
34
+ /** Statistics bin with a fixed probability of 0.5. */
35
+ readonly fixed: Uint8Array<ArrayBuffer>;
36
+ private readByte;
37
+ /** Decodes one binary decision with the statistics bin `stats[bin]`. */
38
+ decodeBit(stats: Uint8Array, bin: number): number;
39
+ /** Starts decoding afresh, at the start of a scan or restart interval. */
40
+ reset(): void;
41
+ }
42
+ /** Arithmetic-coded block decoding, like jdarith.c's decode_mcu*(). */
43
+ export declare class ArithmeticBlockDecoder {
44
+ readonly reader: ArithmeticReader;
45
+ readonly conditioning: ArithmeticConditioning;
46
+ readonly spectralStart: number;
47
+ readonly spectralEnd: number;
48
+ readonly approxLow: number;
49
+ readonly tolerant: boolean;
50
+ constructor(reader: ArithmeticReader, conditioning: ArithmeticConditioning, spectralStart: number, spectralEnd: number, approxLow: number, tolerant: boolean);
51
+ private fail;
52
+ /** Decodes a DC difference and updates the component's prediction. */
53
+ private decodeDC;
54
+ /**
55
+ * Decodes AC coefficients `start`..`end`. Returns false on corrupt data.
56
+ */
57
+ private decodeAC;
58
+ decodeBaseline(component: ArithmeticComponent, offset: number): void;
59
+ decodeDCFirst(component: ArithmeticComponent, offset: number): void;
60
+ decodeDCRefine(component: ArithmeticComponent, offset: number): void;
61
+ decodeACFirst(component: ArithmeticComponent, offset: number): void;
62
+ decodeACRefine(component: ArithmeticComponent, offset: number): void;
63
+ /** Resets the statistics of the scan's components, as a restart does. */
64
+ resetStatistics(components: ArithmeticComponent[], progressive: boolean, approxHigh: number): void;
65
+ }
66
+ //# sourceMappingURL=arithmetic.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"arithmetic.d.ts","sourceRoot":"","sources":["../../../src/decoder/arithmetic.ts"],"names":[],"mappings":"AAmBA,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AA0DzC,8DAA8D;AAC9D,MAAM,WAAW,sBAAsB;IACrC,wEAAwE;IACxE,OAAO,EAAE,MAAM,EAAE,CAAC;IAClB,OAAO,EAAE,MAAM,EAAE,CAAC;IAClB,iEAAiE;IACjE,GAAG,EAAE,MAAM,EAAE,CAAC;CACf;AAED,wBAAgB,mBAAmB,IAAI,sBAAsB,CAE5D;AAED,MAAM,WAAW,mBAAmB;IAClC,YAAY,EAAE,UAAU,CAAC;IACzB,sEAAsE;IACtE,YAAY,EAAE,MAAM,CAAC;IACrB,YAAY,EAAE,MAAM,CAAC;IACrB,IAAI,EAAE,MAAM,CAAC;IACb,uDAAuD;IACvD,SAAS,EAAE,MAAM,CAAC;CACnB;AAED;;;;GAIG;AACH,qBAAa,gBAAiB,SAAQ,SAAS;IAC7C,mEAAmE;IACnE,CAAC,SAAK;IACN,CAAC,SAAK;IACN,EAAE,SAAO;IACT,0EAA0E;IAC1E,MAAM,UAAS;IACf,QAAQ,CAAC,OAAO,4BAGd;IACF,QAAQ,CAAC,OAAO,4BAGd;IACF,sDAAsD;IACtD,QAAQ,CAAC,KAAK,0BAAsB;IAEpC,OAAO,CAAC,QAAQ;IAsChB,wEAAwE;IACxE,SAAS,CAAC,KAAK,EAAE,UAAU,EAAE,GAAG,EAAE,MAAM,UAsDvC;IAED,0EAA0E;IAC1E,KAAK,SAKJ;CACF;AAED,uEAAuE;AACvE,qBAAa,sBAAsB;IAE/B,QAAQ,CAAC,MAAM,EAAE,gBAAgB;IACjC,QAAQ,CAAC,YAAY,EAAE,sBAAsB;IAC7C,QAAQ,CAAC,aAAa,EAAE,MAAM;IAC9B,QAAQ,CAAC,WAAW,EAAE,MAAM;IAC5B,QAAQ,CAAC,SAAS,EAAE,MAAM;IAC1B,QAAQ,CAAC,QAAQ,EAAE,OAAO;IAN5B,YACW,MAAM,EAAE,gBAAgB,EACxB,YAAY,EAAE,sBAAsB,EACpC,aAAa,EAAE,MAAM,EACrB,WAAW,EAAE,MAAM,EACnB,SAAS,EAAE,MAAM,EACjB,QAAQ,EAAE,OAAO,EACxB;IAEJ,OAAO,CAAC,IAAI;IAQZ,sEAAsE;IACtE,OAAO,CAAC,QAAQ;IAyDhB;;OAEG;IACH,OAAO,CAAC,QAAQ;IAgEhB,cAAc,CAAC,SAAS,EAAE,mBAAmB,EAAE,MAAM,EAAE,MAAM,QAQ5D;IAED,aAAa,CAAC,SAAS,EAAE,mBAAmB,EAAE,MAAM,EAAE,MAAM,QAM3D;IAED,cAAc,CAAC,SAAS,EAAE,mBAAmB,EAAE,MAAM,EAAE,MAAM,QAI5D;IAED,aAAa,CAAC,SAAS,EAAE,mBAAmB,EAAE,MAAM,EAAE,MAAM,QAU3D;IAED,cAAc,CAAC,SAAS,EAAE,mBAAmB,EAAE,MAAM,EAAE,MAAM,QAuD5D;IAED,yEAAyE;IACzE,eAAe,CACb,UAAU,EAAE,mBAAmB,EAAE,EACjC,WAAW,EAAE,OAAO,EACpB,UAAU,EAAE,MAAM,QAiBnB;CACF"}