imagescript 3.3.0 → 3.5.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 (67) hide show
  1. package/README.md +32 -8
  2. package/THIRD_PARTY_NOTICES.md +204 -2
  3. package/dist/animation.d.ts +18 -8
  4. package/dist/animation.js +23 -2
  5. package/dist/codecs/av1/cdef.js +211 -0
  6. package/dist/codecs/av1/data.js +141 -0
  7. package/dist/codecs/av1/decode.js +1769 -0
  8. package/dist/codecs/av1/encode.js +1512 -0
  9. package/dist/codecs/av1/fdct-lee.js +156 -0
  10. package/dist/codecs/av1/fdct.js +185 -0
  11. package/dist/codecs/av1/filmgrain.js +219 -0
  12. package/dist/codecs/av1/frame.js +114 -0
  13. package/dist/codecs/av1/ipred.js +491 -0
  14. package/dist/codecs/av1/itx.js +965 -0
  15. package/dist/codecs/av1/loopfilter.js +149 -0
  16. package/dist/codecs/av1/msac-enc.js +126 -0
  17. package/dist/codecs/av1/msac.js +161 -0
  18. package/dist/codecs/av1/obu-enc.js +153 -0
  19. package/dist/codecs/av1/obu.js +516 -0
  20. package/dist/codecs/av1/palette.js +110 -0
  21. package/dist/codecs/av1/postfilter.js +28 -0
  22. package/dist/codecs/av1/restoration.js +193 -0
  23. package/dist/codecs/av1/tables.js +160 -0
  24. package/dist/codecs/avif-enc.js +210 -0
  25. package/dist/codecs/avif.js +581 -0
  26. package/dist/codecs/bmp.js +317 -0
  27. package/dist/codecs/hdr.d.ts +25 -0
  28. package/dist/codecs/hdr.js +201 -0
  29. package/dist/codecs/ico.js +48 -0
  30. package/dist/codecs/jpeg.js +2 -1
  31. package/dist/codecs/jxl/bits.js +126 -0
  32. package/dist/codecs/jxl/blend.js +117 -0
  33. package/dist/codecs/jxl/color.js +134 -0
  34. package/dist/codecs/jxl/dct.js +369 -0
  35. package/dist/codecs/jxl/entropy.js +560 -0
  36. package/dist/codecs/jxl/fast-lossless.js +805 -0
  37. package/dist/codecs/jxl/features.js +325 -0
  38. package/dist/codecs/jxl/filters.js +153 -0
  39. package/dist/codecs/jxl/frame-header.js +195 -0
  40. package/dist/codecs/jxl/frame.js +319 -0
  41. package/dist/codecs/jxl/headers.js +249 -0
  42. package/dist/codecs/jxl/idct.js +484 -0
  43. package/dist/codecs/jxl/modular.js +715 -0
  44. package/dist/codecs/jxl/quant.js +219 -0
  45. package/dist/codecs/jxl/upsample.js +52 -0
  46. package/dist/codecs/jxl/vardct.js +472 -0
  47. package/dist/codecs/jxl.js +90 -0
  48. package/dist/codecs/netpbm.js +239 -0
  49. package/dist/codecs/qoi.js +150 -0
  50. package/dist/codecs/vp8-enc-dsp.js +932 -0
  51. package/dist/codecs/vp8-enc-tables.js +78 -0
  52. package/dist/codecs/vp8-enc.js +1930 -0
  53. package/dist/codecs/webp.js +36 -2
  54. package/dist/image-type.d.ts +15 -1
  55. package/dist/image-type.js +45 -2
  56. package/dist/image.d.ts +69 -6
  57. package/dist/image.js +56 -3
  58. package/dist/index.d.ts +3 -2
  59. package/dist/index.js +2 -2
  60. package/dist/render/hinting.js +26 -18
  61. package/dist/render/layout.js +1 -1
  62. package/dist/render/svg.js +1 -1
  63. package/dist/render/unicode.js +1 -9
  64. package/dist/util/bytes.js +9 -0
  65. package/dist/util/resample.js +4 -2
  66. package/dist/version.js +1 -1
  67. package/package.json +25 -2
package/README.md CHANGED
@@ -26,11 +26,23 @@ deno add jsr:@matmen/imagescript # Deno (npm:imagescript works too)
26
26
  - JPEGs (baseline and progressive; grayscale, RGB, CMYK)
27
27
  - TIFFs (strips and tiles; uncompressed, LZW, deflate, PackBits and JPEG compressed; BigTIFF)
28
28
  - WebPs (lossy, lossless and with alpha; bit-exact with libwebp)
29
+ - BMPs (1-64 bit, with any channel masks, alpha, RLE4 and RLE8, embedded JPEGs and PNGs; also OS/2 bitmaps)
30
+ - ICOs and CURs (the largest image: bitmaps with their masks, or PNGs)
31
+ - QOIs
32
+ - Netpbm images: PBM, PGM, PPM (plain and raw, up to 16 bits), PAM and PFM
33
+ - Radiance HDRs (RGBE and XYZE, every orientation), tone mapped to sRGB like Reinhard's operator does (or clamped,
34
+ `toneMapping`)
29
35
  - TGAs (color mapped, truecolor and greyscale; uncompressed and run length encoded)
36
+ - AVIFs (4:2:0, 4:2:2, 4:4:4 and monochrome, 8-12 bit; grids, alpha, crop, rotation and mirroring; the first frame
37
+ of image sequences; bit-exact with dav1d and libavif), HDR ones (PQ, HLG) tone mapped to sRGB
38
+ - JPEG XLs (Modular and VarDCT, lossless and lossy, every coding tool: progressive passes and LF frames, patches,
39
+ splines, noise, restoration filters, upsampling, JPEG recompression, layers and blending, alpha and spot colors,
40
+ CMYK; animations; within 1 of libjxl's djxl on the conformance suite), XYB ones as sRGB, HDR ones tone mapped to
41
+ sRGB
30
42
  - JPEGs, TIFFs, PNGs and WebPs are rotated as their Exif (or, for PNGs, XMP) orientation says
31
43
  - images larger than 268 megapixels (`maxPixels`) are rejected before decoding, so small files declaring huge images
32
44
  can't exhaust the memory
33
- - [Decoding animations](https://imagescript.matmen.dev/classes/Animation.html#decode) (GIF, APNG and WebP) with all
45
+ - [Decoding animations](https://imagescript.matmen.dev/classes/Animation.html#decode) (GIF, APNG, WebP and JPEG XL) with all
34
46
  frames, durations and disposal modes
35
47
  - [Rendering SVGs](https://imagescript.matmen.dev/classes/Image.html#rendersvg) (shapes, paths, gradients, strokes, clip paths,
36
48
  masks, CSS; no text, images or filters)
@@ -53,10 +65,18 @@ deno add jsr:@matmen/imagescript # Deno (npm:imagescript works too)
53
65
  , [dominantColor](https://imagescript.matmen.dev/classes/Image.html#dominantcolor), ...)
54
66
  - Encoding images as [PNGs](https://imagescript.matmen.dev/classes/Image.html#encode) (smallest lossless color type)
55
67
  , [JPEGs](https://imagescript.matmen.dev/classes/Image.html#encodejpeg)
56
- , lossless [WebPs](https://imagescript.matmen.dev/classes/Image.html#encodewebp)
68
+ , lossless and lossy [WebPs](https://imagescript.matmen.dev/classes/Image.html#encodewebp) (lossy ones byte for
69
+ byte like libwebp's cwebp)
70
+ , [AVIFs](https://imagescript.matmen.dev/classes/Image.html#encodeavif) (lossy and lossless, with alpha)
71
+ , lossless [JPEG XLs](https://imagescript.matmen.dev/classes/Image.html#encodejxl) (byte for byte like libjxl's
72
+ `cjxl -e 1`)
73
+ , [BMPs](https://imagescript.matmen.dev/classes/Image.html#encodebmp)
74
+ , [QOIs](https://imagescript.matmen.dev/classes/Image.html#encodeqoi)
75
+ , [Netpbm images](https://imagescript.matmen.dev/classes/Image.html#encodepnm) (PBM, PGM, PPM or PAM, whichever is
76
+ smallest)
57
77
  and [TGAs](https://imagescript.matmen.dev/classes/Image.html#encodetga)
58
78
  - Encoding animations as [GIF](https://imagescript.matmen.dev/classes/Animation.html#encodegif),
59
- [APNG](https://imagescript.matmen.dev/classes/Animation.html#encodeapng) and lossless
79
+ [APNG](https://imagescript.matmen.dev/classes/Animation.html#encodeapng) and lossless or lossy
60
80
  [WebP](https://imagescript.matmen.dev/classes/Animation.html#encodewebp), storing only what changes from frame to frame
61
81
 
62
82
  ---
@@ -81,9 +101,8 @@ const { Image } = require('imagescript');
81
101
  All coordinates are 0-based, `(0, 0)` is the top left pixel.
82
102
 
83
103
  A bigger example is the [README image generation](https://github.com/matmen/ImageScript/blob/master/tests/readme.js).
84
- Upgrading from 1.x? Version 3 has breaking changes (0-based coordinates, removed lossy WebP encoding, Node.js 22.13+, ...),
85
- see the [changelog](https://github.com/matmen/ImageScript/blob/master/CHANGELOG.md). There is no 2.x release, the number was skipped because of the old experimental
86
- `imagescript/v2` API.
104
+ Upgrading from 1.x? Version 3 has breaking changes (0-based coordinates, lossy WebP encoding as `encodeWebP({ quality })`,
105
+ Node.js 22.13+, ...), see the [changelog](https://github.com/matmen/ImageScript/blob/master/CHANGELOG.md).
87
106
 
88
107
  [![Example](https://github.com/matmen/ImageScript/raw/master/tests/targets/readme.png)](https://github.com/matmen/ImageScript/blob/master/tests/readme.js)
89
108
 
@@ -103,10 +122,15 @@ The deflate/inflate implementation is derived from [fflate](https://github.com/1
103
122
  The JPEG codec is based in part on the work of the Independent JPEG Group (libjpeg) and on
104
123
  [pdf.js](https://github.com/mozilla/pdf.js), the glyph rasterizer is translated from
105
124
  [font-rs](https://github.com/raphlinus/font-rs). The WebP codecs follow [RFC 9649](https://www.rfc-editor.org/rfc/rfc9649) and [RFC 6386](https://www.rfc-editor.org/rfc/rfc6386)
106
- and reproduce [libwebp](https://chromium.googlesource.com/webm/libwebp)'s color conversion, the TrueType hinting follows
125
+ and reproduce [libwebp](https://chromium.googlesource.com/webm/libwebp)'s color conversion, the lossy WebP encoder is
126
+ ported from libwebp's, the TrueType hinting follows
107
127
  [FreeType](https://freetype.org)'s interpreter (portions of this software are copyright © 2026 The FreeType Project),
108
128
  the text shaping is ported from [HarfBuzz](https://github.com/harfbuzz/harfbuzz) and uses data of the
109
- [Unicode Character Database](https://www.unicode.org/ucd/).
129
+ [Unicode Character Database](https://www.unicode.org/ucd/). The AV1 decoder is ported from
130
+ [dav1d](https://code.videolan.org/videolan/dav1d), AVIFs are converted to RGB like
131
+ [libavif](https://github.com/AOMediaCodec/libavif) does, the JPEG XL decoder is ported from
132
+ [jxl-oxide](https://github.com/tirr-c/jxl-oxide) and [libjxl](https://github.com/libjxl/libjxl), the JPEG XL encoder
133
+ from libjxl's fast lossless encoder, the AV1 encoder mirrors the decoder and uses libaom's symbol encoder.
110
134
  See [THIRD_PARTY_NOTICES.md](THIRD_PARTY_NOTICES.md) for their licenses.
111
135
 
112
136
  ---
@@ -12,8 +12,15 @@ following projects, under their own licenses. Each derived file says so in its h
12
12
  | [font-rs](https://github.com/raphlinus/font-rs) by Raph Levien | `src/render/text.ts` (the `Rasterizer` class, also used for SVGs) | Apache-2.0 |
13
13
  | [RFC 9649](https://www.rfc-editor.org/rfc/rfc9649) (WebP Image Format) | `src/codecs/vp8l.ts` (lossless bitstream, short distance code table), `src/codecs/webp.ts` (container) | BSD-3-Clause (Revised BSD) |
14
14
  | [RFC 6386](https://www.rfc-editor.org/rfc/rfc6386) (VP8 Data Format and Decoding Guide, with the libvpx reference decoder) | `src/codecs/vp8-tables.ts` (probability and quantizer tables), `src/codecs/vp8.ts` (decoding procedures) | BSD-3-Clause |
15
- | [libwebp](https://chromium.googlesource.com/webm/libwebp) | `src/codecs/vp8.ts` (YUV to RGB conversion, "fancy" chroma upsampling), `src/codecs/webp.ts` (animation frame blending) | BSD-3-Clause |
15
+ | [libwebp](https://chromium.googlesource.com/webm/libwebp) | `src/codecs/vp8.ts` (YUV to RGB conversion, "fancy" chroma upsampling), `src/codecs/webp.ts` (animation frame blending), `src/codecs/vp8-enc.ts`, `src/codecs/vp8-enc-dsp.ts` (the lossy encoder: RGB to YUV conversion, analysis, segments, rate-distortion optimized mode decisions, quantization, token probabilities, bitstream, alpha filtering), `src/codecs/vp8-enc-tables.ts` (its tables, generated from libwebp by `scripts/vp8-enc-tables.py`) | BSD-3-Clause |
16
+ | [dav1d](https://code.videolan.org/videolan/dav1d) by VideoLAN, the dav1d authors and Two Orioles | `src/codecs/av1/*` (the AV1 decoder, and the encoder's syntax, contexts and reconstruction mirrored from it: bitstream parsing, symbol decoding, prediction, inverse transforms, loop filters, film grain; the tables in `src/codecs/av1/data.ts`, generated from dav1d by `scripts/av1-tables.c`) | BSD-2-Clause |
17
+ | [libaom](https://aomedia.googlesource.com/aom/) by the Alliance for Open Media | `src/codecs/av1/msac-enc.ts` (the symbol encoder), `src/codecs/av1/fdct.ts` (the forward Walsh-Hadamard transform), `src/codecs/av1/encode.ts` (the loop filter level from the quantizer), `src/codecs/avif-enc.ts` (the quantizer to qindex table) | BSD-2-Clause |
18
+ | [libavif](https://github.com/AOMediaCodec/libavif) by Joe Drago | `src/codecs/avif.ts` (YUV to RGB conversion with bilinear chroma upsampling, alpha range expansion and unpremultiplication) | BSD-2-Clause |
19
+ | [jxl-oxide](https://github.com/tirr-c/jxl-oxide) by Wonwoo Choi | `src/codecs/jxl/*` (the JPEG XL decoder: bit reader, header and frame header fields, entropy decoding, VarDCT, dequantization matrices, transforms, restoration filters, upsampling, the parsing of patches, splines and noise) | MIT (or Apache-2.0) |
20
+ | [libjxl](https://github.com/libjxl/libjxl) by the JPEG XL Project Authors | `src/codecs/jxl/modular.ts` (Modular decoding: MA trees, predictors, the self-correcting predictor, transforms), `src/codecs/jxl/frame.ts` (the sections of frames, the order of the rendering stages, reference frames), `src/codecs/jxl/blend.ts`, `src/codecs/jxl/color.ts` (XYB to RGB), `src/codecs/jxl/features.ts` (rendering patches, splines and noise), `src/codecs/jxl/fast-lossless.ts` (the lossless encoder: palette detection, YCoCg, the gradient predictor, prefix codes, runs as LZ77, the headers) | BSD-3-Clause |
21
+ | [JPEG XL conformance](https://github.com/libjxl/conformance) by the JPEG XL Project Authors | `tests/jxl/alpha-*.jxl`, `animation-spline.jxl`, `blendmodes.jxl`, `mul-no-extra-channels.jxl` (test files) | BSD-3-Clause |
16
22
  | [FreeType](https://freetype.org) | `src/render/hinting.ts` (behavior and parts of the logic of the TrueType interpreter) | FreeType License |
23
+ | [QOI](https://github.com/phoboslab/qoi) by Dominic Szablewski | `src/codecs/qoi.ts` (the reference encoder's choices of operations) | MIT |
17
24
  | [Pillow](https://github.com/python-pillow/Pillow) | `src/util/resample.ts` (filter coefficients, box, bilinear and bicubic filters) | MIT-CMU |
18
25
  | [HarfBuzz](https://github.com/harfbuzz/harfbuzz) | `src/render/buffer.ts`, `src/render/layout.ts`, `src/render/shape.ts`, `src/render/shapers/*` (OpenType shaping: buffer, GSUB/GPOS, normalization, fallback positioning, the script shapers and syllable grammars), `scripts/unicode.js` (shaping category tables) | Old MIT |
19
26
  | [Universal Shaping Engine data](https://github.com/harfbuzz/harfbuzz/tree/main/src/ms-use) by Microsoft | `scripts/ms-use` (category overrides, invalid vowel sequences), the tables generated from them in `src/render/unicode-data.ts` | MIT |
@@ -85,7 +92,8 @@ one decode function per scan type). The `Rasterizer` class in `src/render/text.t
85
92
 
86
93
  The VP8 decoder follows the procedures of RFC 6386 and its reference decoder, its constant tables are extracted from
87
94
  the RFC. To decode exactly like libwebp, the YUV to RGB conversion, the chroma upsampling and the blending of animation
88
- frames reproduce libwebp's integer arithmetic.
95
+ frames reproduce libwebp's integer arithmetic. The lossy encoder is ported from libwebp's (version 1.6.0, its default
96
+ settings) and writes the same VP8 bitstreams as cwebp.
89
97
 
90
98
  ```
91
99
  RFC 6386 reference decoder: Copyright (c) 2010, 2011, Google Inc. All rights reserved.
@@ -148,6 +156,174 @@ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWIS
148
156
  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
149
157
  ```
150
158
 
159
+ ## dav1d
160
+
161
+ The AV1 decoder is ported from dav1d 1.5.1 and decodes exactly like it (the frames of its intra coding tools, with
162
+ every post filter and film grain); its tables are generated from dav1d's. The ported files are Copyright © 2018-2021,
163
+ VideoLAN and dav1d authors, and Copyright © 2018, Two Orioles, LLC, under dav1d's license. AV1 is covered by the
164
+ [Alliance for Open Media Patent License 1.0](https://aomedia.org/license/patent-license/).
165
+
166
+ ```
167
+ Copyright © 2018-2019, VideoLAN and dav1d authors
168
+ All rights reserved.
169
+
170
+ Redistribution and use in source and binary forms, with or without
171
+ modification, are permitted provided that the following conditions are met:
172
+
173
+ 1. Redistributions of source code must retain the above copyright notice, this
174
+ list of conditions and the following disclaimer.
175
+
176
+ 2. Redistributions in binary form must reproduce the above copyright notice,
177
+ this list of conditions and the following disclaimer in the documentation
178
+ and/or other materials provided with the distribution.
179
+
180
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
181
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
182
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
183
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
184
+ ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
185
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
186
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
187
+ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
188
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
189
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
190
+ ```
191
+
192
+ ## libaom
193
+
194
+ The AV1 encoder's symbol encoder and forward Walsh-Hadamard transform are ported from libaom, the AV1 reference
195
+ implementation, as are its guess of the loop filter level and the quantizer table of the quality. AV1 is covered by the
196
+ [Alliance for Open Media Patent License 1.0](https://aomedia.org/license/patent-license/).
197
+
198
+ ```
199
+ Copyright (c) 2016, Alliance for Open Media. All rights reserved.
200
+
201
+ Redistribution and use in source and binary forms, with or without
202
+ modification, are permitted provided that the following conditions
203
+ are met:
204
+
205
+ 1. Redistributions of source code must retain the above copyright
206
+ notice, this list of conditions and the following disclaimer.
207
+
208
+ 2. Redistributions in binary form must reproduce the above copyright
209
+ notice, this list of conditions and the following disclaimer in
210
+ the documentation and/or other materials provided with the
211
+ distribution.
212
+
213
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
214
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
215
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
216
+ FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
217
+ COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
218
+ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
219
+ BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
220
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
221
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
222
+ LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
223
+ ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
224
+ POSSIBILITY OF SUCH DAMAGE.
225
+ ```
226
+
227
+ ## libavif
228
+
229
+ The conversion of decoded AVIFs to RGB reproduces libavif's reference conversion (in floating point, with bilinear
230
+ chroma upsampling), so the pixels are the ones libavif gives.
231
+
232
+ ```
233
+ Copyright 2019 Joe Drago. All rights reserved.
234
+
235
+ Redistribution and use in source and binary forms, with or without
236
+ modification, are permitted provided that the following conditions are met:
237
+
238
+ 1. Redistributions of source code must retain the above copyright notice, this
239
+ list of conditions and the following disclaimer.
240
+
241
+ 2. Redistributions in binary form must reproduce the above copyright notice,
242
+ this list of conditions and the following disclaimer in the documentation
243
+ and/or other materials provided with the distribution.
244
+
245
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
246
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
247
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
248
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
249
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
250
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
251
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
252
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
253
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
254
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
255
+ ```
256
+
257
+ ## jxl-oxide
258
+
259
+ The structure of the JPEG XL decoder and most of its parts are ported from jxl-oxide (which may be used under the MIT
260
+ or the Apache-2.0 license; the MIT license applies here).
261
+
262
+ ```
263
+ Copyright (c) Wonwoo Choi
264
+
265
+ Permission is hereby granted, free of charge, to any
266
+ person obtaining a copy of this software and associated
267
+ documentation files (the "Software"), to deal in the
268
+ Software without restriction, including without
269
+ limitation the rights to use, copy, modify, merge,
270
+ publish, distribute, sublicense, and/or sell copies of
271
+ the Software, and to permit persons to whom the Software
272
+ is furnished to do so, subject to the following
273
+ conditions:
274
+
275
+ The above copyright notice and this permission notice
276
+ shall be included in all copies or substantial portions
277
+ of the Software.
278
+
279
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
280
+ ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
281
+ TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
282
+ PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
283
+ SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
284
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
285
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
286
+ IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
287
+ DEALINGS IN THE SOFTWARE.
288
+ ```
289
+
290
+ ## libjxl, JPEG XL conformance
291
+
292
+ The Modular decoding, the rendering pipeline, blending, the XYB conversion and the rendering of patches, splines and
293
+ noise follow libjxl, the JPEG XL reference implementation, and the lossless encoder is ported from its fast lossless
294
+ encoder; tests/jxl includes files of its conformance suite. JPEG XL
295
+ is covered by Google's patent grant for libjxl (the "Additional IP Rights Grant" in libjxl's PATENTS file).
296
+
297
+ ```
298
+ Copyright (c) the JPEG XL Project Authors.
299
+ All rights reserved.
300
+
301
+ Redistribution and use in source and binary forms, with or without
302
+ modification, are permitted provided that the following conditions are met:
303
+
304
+ 1. Redistributions of source code must retain the above copyright notice, this
305
+ list of conditions and the following disclaimer.
306
+
307
+ 2. Redistributions in binary form must reproduce the above copyright notice,
308
+ this list of conditions and the following disclaimer in the documentation
309
+ and/or other materials provided with the distribution.
310
+
311
+ 3. Neither the name of the copyright holder nor the names of its
312
+ contributors may be used to endorse or promote products derived from
313
+ this software without specific prior written permission.
314
+
315
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
316
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
317
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
318
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
319
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
320
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
321
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
322
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
323
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
324
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
325
+ ```
326
+
151
327
  ## FreeType
152
328
 
153
329
  Portions of this software are copyright © 2026 The FreeType Project (https://freetype.org). All rights reserved.
@@ -159,6 +335,32 @@ interpolation of untouched points (IUP), the handling of near-perpendicular free
159
335
  tolerance of invalid references. FreeType is used under the FreeType License, included as
160
336
  [licenses/FTL.txt](licenses/FTL.txt).
161
337
 
338
+ ## QOI
339
+
340
+ ```
341
+ MIT License
342
+
343
+ Copyright (c) 2021, 2022 Dominic Szablewski
344
+
345
+ Permission is hereby granted, free of charge, to any person obtaining a copy
346
+ of this software and associated documentation files (the "Software"), to deal
347
+ in the Software without restriction, including without limitation the rights
348
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
349
+ copies of the Software, and to permit persons to whom the Software is
350
+ furnished to do so, subject to the following conditions:
351
+
352
+ The above copyright notice and this permission notice shall be included in all
353
+ copies or substantial portions of the Software.
354
+
355
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
356
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
357
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
358
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
359
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
360
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
361
+ SOFTWARE.
362
+ ```
363
+
162
364
  ## Pillow
163
365
 
164
366
  ```
@@ -18,7 +18,7 @@ export interface AnimationDecodeOptions {
18
18
  */
19
19
  maxPixels?: number;
20
20
  }
21
- /** Options of {@link Animation.encodeWebP}, and those all animation encoders have */
21
+ /** The options all animation encoders have */
22
22
  export interface AnimationEncodeOptions {
23
23
  /** Metadata to include, see {@link Metadata} */
24
24
  metadata?: Metadata;
@@ -29,6 +29,16 @@ export interface AnimationEncodeOptions {
29
29
  */
30
30
  optimize?: boolean;
31
31
  }
32
+ /** Options of {@link Animation.encodeWebP} */
33
+ export interface WebPAnimationEncodeOptions extends AnimationEncodeOptions {
34
+ /**
35
+ * Lossy frames (0-100, like libwebp's quality: 75 is its default), much smaller than lossless ones. Without it the
36
+ * frames are lossless
37
+ */
38
+ quality?: number;
39
+ /** How much effort lossy frames take (0-6), see {@link WebPEncodeOptions.effort}. Default: `4` */
40
+ effort?: number;
41
+ }
32
42
  /** Options of {@link Animation.encodeGIF} */
33
43
  export interface GIFEncodeOptions extends AnimationEncodeOptions {
34
44
  /** 1-100, scales the palette size from 8 colors (1) to 256 colors (90 and above). Default: `95` */
@@ -126,20 +136,20 @@ export declare class Animation extends Array<Frame> {
126
136
  */
127
137
  encodeAPNG(compression?: number): Promise<Uint8Array>;
128
138
  /**
129
- * Encodes the animation as lossless animated WebP. Each frame is stored as the area that changed since the frame
130
- * before, identical frames are merged (unless the `optimize` option is false): the file shows exactly the same
131
- * animation, but doesn't necessarily decode to the same frames
132
- * @param options See {@link AnimationEncodeOptions}
139
+ * Encodes the animation as animated WebP, lossless or (with a quality) lossy. Each frame is stored as the area that
140
+ * changed since the frame before, identical frames are merged (unless the `optimize` option is false): the file
141
+ * shows the same animation, but doesn't necessarily decode to the same frames
142
+ * @param options See {@link WebPAnimationEncodeOptions}
133
143
  */
134
- encodeWebP({ metadata, optimize }?: AnimationEncodeOptions): Promise<Uint8Array>;
144
+ encodeWebP({ metadata, optimize, quality, effort }?: WebPAnimationEncodeOptions): Promise<Uint8Array>;
135
145
  /**
136
- * Decodes an animated GIF, APNG or WebP into fully composited frames (still images become a single frame)
146
+ * Decodes an animated GIF, APNG, WebP or JPEG XL into fully composited frames (still images become a single frame)
137
147
  * @param input The binary data to decode
138
148
  * @param options See {@link AnimationDecodeOptions}
139
149
  */
140
150
  static decode(input: BinaryData, options?: AnimationDecodeOptions): Promise<Animation>;
141
151
  /**
142
- * Decodes an animated GIF, APNG or WebP into fully composited frames (still images become a single frame)
152
+ * Decodes an animated GIF, APNG, WebP or JPEG XL into fully composited frames (still images become a single frame)
143
153
  * @param input The binary data to decode
144
154
  * @param onlyExtractFirstFrame Whether to stop decoding after the first frame
145
155
  */
package/dist/animation.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import * as gifCodec from "./codecs/gif.js";
2
2
  import * as png from "./codecs/png.js";
3
3
  import * as webp from "./codecs/webp.js";
4
+ import * as jxl from "./codecs/jxl.js";
4
5
  import { view } from "./util/mem.js";
5
6
  import { Image } from "./image.js";
6
7
  import { Frame } from "./frame.js";
@@ -123,12 +124,19 @@ export class Animation extends Array {
123
124
  : this.__apngFrames__().map(({ blend: _, ...f }) => f);
124
125
  return png.encodeAnimation(frames, this.width, this.height, toPlays(this.loopCount), compression, pngText(resolveMetadata(metadata)));
125
126
  }
126
- async encodeWebP({ metadata = {}, optimize = true } = {}) {
127
+ async encodeWebP({ metadata = {}, optimize = true, quality, effort = 4 } = {}) {
127
128
  this.__validate__();
128
129
  if (!this.length)
129
130
  throw new Error('An animation needs at least one frame');
131
+ if (quality !== undefined) {
132
+ const q = finite(quality, 'quality');
133
+ if (q < 0 || q > 100)
134
+ throw new RangeError(`quality must be 0-100, not ${q}`);
135
+ if (!Number.isInteger(effort) || effort < 0 || effort > 6)
136
+ throw new RangeError(`effort must be an integer 0-6, not ${effort}`);
137
+ }
130
138
  const frames = optimize ? this.__optimized__({ replace: true, even: true, alpha: 'frame' }) : this.__webpFrames__();
131
- return webp.encodeAnimation(frames, this.width, this.height, toPlays(this.loopCount), exifData(resolveMetadata(metadata)));
139
+ return webp.encodeAnimation(frames, this.width, this.height, toPlays(this.loopCount), exifData(resolveMetadata(metadata)), quality, effort);
132
140
  }
133
141
  __webpFrames__() {
134
142
  const width = this.width, height = this.height;
@@ -192,6 +200,19 @@ export class Animation extends Array {
192
200
  const frames = canvasFrames(decoded.width, decoded.height, png.compositeAnimation(decoded));
193
201
  return new Animation(frames, decoded.frames.length > 1 ? fromPlays(decoded.plays) : -1);
194
202
  }
203
+ if (type === 'jxl') {
204
+ const frames = [];
205
+ let loopCount = -1;
206
+ for (const decoded of jxl.frames(data, maxPixels)) {
207
+ const frame = new Frame(decoded.width, decoded.height, decoded.duration, 0, 0, Frame.DISPOSAL_BACKGROUND);
208
+ frame.bitmap = decoded.pixels;
209
+ frames.push(frame.__orient__(decoded.orientation));
210
+ loopCount = fromPlays(decoded.loopCount);
211
+ if (onlyExtractFirstFrame)
212
+ break;
213
+ }
214
+ return new Animation(frames, frames.length > 1 ? loopCount : -1);
215
+ }
195
216
  if (type)
196
217
  return new Animation([Frame.from(await Image.decode(data, { maxPixels }), 0)]);
197
218
  throw new Error('Unsupported image type');
@@ -0,0 +1,211 @@
1
+ /*!
2
+ * The AV1 constrained directional enhancement filter, as the AV1 specification describes it (section 7.15) and dav1d
3
+ * implements it (src/cdef_tmpl.c). Copyright (c) 2018-2021, VideoLAN and dav1d authors, Copyright (c) 2018, Two
4
+ * Orioles, LLC, BSD-2-Clause License (see THIRD_PARTY_NOTICES.md)
5
+ */
6
+ const DIV_TABLE = [0, 840, 420, 280, 210, 168, 140, 120, 105];
7
+ const UV_DIR = [[[0, 1, 2, 3, 4, 5, 6, 7], [1, 2, 2, 2, 3, 4, 6, 0]], [[7, 0, 2, 4, 5, 6, 6, 6], [0, 1, 2, 3, 4, 5, 6, 7]]];
8
+ const PRI_TAPS = [[4, 2], [3, 3]], SEC_TAPS = [2, 1];
9
+ const floorLog2 = (v) => 31 - Math.clz32(v);
10
+ export function cdefActive(f) {
11
+ const c = f.hdr.cdef;
12
+ return f.seq.cdef && (c.yStrengths.some(s => s) || c.uvStrengths.some(s => s));
13
+ }
14
+ export function cdef(f, src, dirs) {
15
+ const c = f.hdr.cdef;
16
+ const coeffShift = f.bitdepth - 8;
17
+ const s4 = f.b4Stride;
18
+ for (let r = 0; r < f.bh; r += 2) {
19
+ for (let col = 0; col < f.bw; col += 2) {
20
+ const idx = f.cdefIdx[(r >> 4) * (s4 >> 4) + (col >> 4)];
21
+ if (idx === -1)
22
+ continue;
23
+ const o = r * s4 + col;
24
+ if (f.skip[o] && f.skip[o + s4] && f.skip[o + 1] && f.skip[o + s4 + 1])
25
+ continue;
26
+ const usesDirection = c.yStrengths[idx] >> 2 || (f.planes.length > 1 && c.uvStrengths[idx] >> 2);
27
+ let yDir = 0, variance = 0;
28
+ if (usesDirection) {
29
+ const k = (r >> 1) * (s4 >> 1) + (col >> 1);
30
+ if (dirs && dirs[2 * k] >= 0) {
31
+ yDir = dirs[2 * k];
32
+ variance = dirs[2 * k + 1];
33
+ }
34
+ else {
35
+ yDir = direction(src[0], r * 4 * f.strides[0] + col * 4, f.strides[0], coeffShift);
36
+ variance = directionVariance;
37
+ if (dirs) {
38
+ dirs[2 * k] = yDir;
39
+ dirs[2 * k + 1] = variance;
40
+ }
41
+ }
42
+ }
43
+ const ys = c.yStrengths[idx];
44
+ let priStr = (ys >> 2) << coeffShift;
45
+ const secY = ys & 3;
46
+ const secStr = (secY === 3 ? 4 : secY) << coeffShift;
47
+ const dir = priStr ? yDir : 0;
48
+ const varStr = variance >> 6 ? Math.min(floorLog2(variance >> 6), 12) : 0;
49
+ priStr = variance ? (priStr * (4 + varStr) + 8) >> 4 : 0;
50
+ filterBlock(f, src, 0, r, col, priStr, secStr, c.damping + coeffShift, dir);
51
+ if (f.planes.length === 1)
52
+ continue;
53
+ const uvs = c.uvStrengths[idx];
54
+ const uvPri = (uvs >> 2) << coeffShift, secUv = uvs & 3;
55
+ const uvSec = (secUv === 3 ? 4 : secUv) << coeffShift;
56
+ const uvDir = uvPri ? UV_DIR[f.ssx][f.ssy][yDir] : 0;
57
+ for (let p = 1; p < 3; p++)
58
+ filterBlock(f, src, p, r, col, uvPri, uvSec, c.damping + coeffShift - 1, uvDir);
59
+ }
60
+ }
61
+ }
62
+ const partial = new Int32Array(8 * 15), cost = new Float64Array(8);
63
+ let directionVariance = 0;
64
+ function direction(luma, o, ls, coeffShift) {
65
+ partial.fill(0);
66
+ for (let i = 0; i < 8; i++) {
67
+ const row = o + i * ls, ih = i >> 1;
68
+ for (let j = 0; j < 8; j++) {
69
+ const x = (luma[row + j] >> coeffShift) - 128, jh = j >> 1;
70
+ partial[i + j] += x;
71
+ partial[15 + i + jh] += x;
72
+ partial[30 + i] += x;
73
+ partial[45 + 3 + i - jh] += x;
74
+ partial[60 + 7 + i - j] += x;
75
+ partial[75 + 3 - ih + j] += x;
76
+ partial[90 + j] += x;
77
+ partial[105 + ih + j] += x;
78
+ }
79
+ }
80
+ cost.fill(0);
81
+ for (let i = 0; i < 8; i++) {
82
+ cost[2] += partial[30 + i] * partial[30 + i];
83
+ cost[6] += partial[90 + i] * partial[90 + i];
84
+ }
85
+ cost[2] *= DIV_TABLE[8];
86
+ cost[6] *= DIV_TABLE[8];
87
+ for (let i = 0; i < 7; i++) {
88
+ cost[0] += (partial[i] * partial[i] + partial[14 - i] * partial[14 - i]) * DIV_TABLE[i + 1];
89
+ cost[4] += (partial[60 + i] * partial[60 + i] + partial[60 + 14 - i] * partial[60 + 14 - i]) * DIV_TABLE[i + 1];
90
+ }
91
+ cost[0] += partial[7] * partial[7] * DIV_TABLE[8];
92
+ cost[4] += partial[67] * partial[67] * DIV_TABLE[8];
93
+ for (let i = 1; i < 8; i += 2) {
94
+ const p = i * 15;
95
+ for (let j = 0; j < 5; j++)
96
+ cost[i] += partial[p + 3 + j] * partial[p + 3 + j];
97
+ cost[i] *= DIV_TABLE[8];
98
+ for (let j = 0; j < 3; j++)
99
+ cost[i] += (partial[p + j] * partial[p + j] + partial[p + 10 - j] * partial[p + 10 - j]) * DIV_TABLE[2 * j + 2];
100
+ }
101
+ let bestCost = 0, yDir = 0;
102
+ for (let i = 0; i < 8; i++) {
103
+ if (cost[i] > bestCost) {
104
+ bestCost = cost[i];
105
+ yDir = i;
106
+ }
107
+ }
108
+ directionVariance = Math.floor((bestCost - cost[(yDir + 4) & 7]) / 1024);
109
+ return yDir;
110
+ }
111
+ function filterBlock(f, src, plane, r, c, priStr, secStr, damping, dir) {
112
+ if (!priStr && !secStr)
113
+ return;
114
+ const subX = plane ? f.ssx : 0, subY = plane ? f.ssy : 0;
115
+ const s = src[plane], dst = f.planes[plane], stride = f.strides[plane];
116
+ const x0 = (c * 4) >> subX, y0 = (r * 4) >> subY, w = 8 >> subX, h = 8 >> subY;
117
+ const maxX = (f.bw * 4) >> subX, maxY = (f.bh * 4) >> subY;
118
+ const priTaps = PRI_TAPS[(priStr >> (f.bitdepth - 8)) & 1];
119
+ const priShift = priStr ? Math.max(0, damping - floorLog2(priStr)) : 0, secShift = secStr ? Math.max(0, damping - floorLog2(secStr)) : 0;
120
+ for (let n = 0; n < 6; n++) {
121
+ const t = n >> 1, k = n & 1;
122
+ const d = (t === 0 ? dir : t === 1 ? dir + 6 : dir + 2) & 7;
123
+ const dy = DIRECTIONS[d * 4 + k * 2], dx = DIRECTIONS[d * 4 + k * 2 + 1];
124
+ offsets[n] = dy * stride + dx;
125
+ dys[n] = dy;
126
+ dxs[n] = dx;
127
+ taps[n] = t ? SEC_TAPS[k] : priTaps[k];
128
+ strengths[n] = t ? secStr : priStr;
129
+ shifts[n] = t ? secShift : priShift;
130
+ }
131
+ if (x0 >= 2 && y0 >= 2 && x0 + w + 2 <= maxX && y0 + h + 2 <= maxY) {
132
+ const p0 = offsets[0], p1 = offsets[1], s0 = offsets[2], s1 = offsets[3], s2 = offsets[4], s3 = offsets[5];
133
+ const t0 = priTaps[0], t1 = priTaps[1];
134
+ const off = (1 << f.bitdepth) - 1;
135
+ const P = constrainTable(priStr, priShift, f.bitdepth), S = constrainTable(secStr, secShift, f.bitdepth);
136
+ for (let i = 0; i < h; i++) {
137
+ for (let j = 0, o = (y0 + i) * stride + x0; j < w; j++, o++) {
138
+ const x = s[o];
139
+ const a0 = s[o + p0], b0 = s[o - p0], a1 = s[o + p1], b1 = s[o - p1];
140
+ const c0 = s[o + s0], d0 = s[o - s0], c1 = s[o + s1], d1 = s[o - s1];
141
+ const e0 = s[o + s2], f0 = s[o - s2], e1 = s[o + s3], f1 = s[o - s3];
142
+ const ox = off - x;
143
+ const sum = t0 * (P[a0 + ox] + P[b0 + ox]) + t1 * (P[a1 + ox] + P[b1 + ox]) +
144
+ 2 * (S[c0 + ox] + S[d0 + ox] + S[e0 + ox] + S[f0 + ox]) + S[c1 + ox] + S[d1 + ox] + S[e1 + ox] + S[f1 + ox];
145
+ let v = x + ((8 + sum - (sum < 0 ? 1 : 0)) >> 4);
146
+ if (v > x) {
147
+ if (v > a0 && v > b0 && v > a1 && v > b1 && v > c0 && v > d0 && v > c1 && v > d1 && v > e0 && v > f0 && v > e1 && v > f1)
148
+ v = Math.max(x, a0, b0, a1, b1, c0, d0, c1, d1, e0, f0, e1, f1);
149
+ }
150
+ else if (v < x) {
151
+ if (v < a0 && v < b0 && v < a1 && v < b1 && v < c0 && v < d0 && v < c1 && v < d1 && v < e0 && v < f0 && v < e1 && v < f1)
152
+ v = Math.min(x, a0, b0, a1, b1, c0, d0, c1, d1, e0, f0, e1, f1);
153
+ }
154
+ dst[o] = v;
155
+ }
156
+ }
157
+ return;
158
+ }
159
+ for (let i = 0; i < h; i++) {
160
+ const y = y0 + i;
161
+ for (let j = 0; j < w; j++) {
162
+ const xx = x0 + j, o = y * stride + xx;
163
+ const x = s[o];
164
+ let sum = 0, max = x, min = x;
165
+ for (let t = 0; t < 6; t++) {
166
+ for (let sign = -1; sign <= 1; sign += 2) {
167
+ const py = y + sign * dys[t], px = xx + sign * dxs[t];
168
+ if (py < 0 || px < 0 || py >= maxY || px >= maxX)
169
+ continue;
170
+ const p = s[o + sign * offsets[t]];
171
+ sum += taps[t] * constrain(p - x, strengths[t], shifts[t]);
172
+ if (p > max)
173
+ max = p;
174
+ if (p < min)
175
+ min = p;
176
+ }
177
+ }
178
+ const v = x + ((8 + sum - (sum < 0 ? 1 : 0)) >> 4);
179
+ dst[o] = v < min ? min : v > max ? max : v;
180
+ }
181
+ }
182
+ }
183
+ const tables = new Map();
184
+ function constrainTable(strength, shift, bitdepth) {
185
+ const key = (strength * 32 + shift) * 16 + bitdepth;
186
+ let t = tables.get(key);
187
+ if (!t) {
188
+ const off = (1 << bitdepth) - 1;
189
+ t = new Int16Array(2 * off + 1);
190
+ for (let d = -off; d <= off; d++)
191
+ t[d + off] = constrain(d, strength, shift);
192
+ tables.set(key, t);
193
+ }
194
+ return t;
195
+ }
196
+ function constrain(diff, strength, shift) {
197
+ if (!strength)
198
+ return 0;
199
+ const a = diff < 0 ? -diff : diff;
200
+ let v = strength - (a >> shift);
201
+ if (v > a)
202
+ v = a;
203
+ if (v <= 0)
204
+ return 0;
205
+ return diff < 0 ? -v : v;
206
+ }
207
+ const DIRECTIONS = new Int8Array([
208
+ -1, 1, -2, 2, 0, 1, -1, 2, 0, 1, 0, 2, 0, 1, 1, 2,
209
+ 1, 1, 2, 2, 1, 0, 2, 1, 1, 0, 2, 0, 1, 0, 2, -1
210
+ ]);
211
+ const offsets = new Int32Array(6), dys = new Int32Array(6), dxs = new Int32Array(6), taps = new Int32Array(6), strengths = new Int32Array(6), shifts = new Int32Array(6);