imagescript 4.0.0 → 4.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -10
- package/THIRD_PARTY_NOTICES.md +6 -4
- package/dist/animation-encoder.js +15 -9
- package/dist/animation.d.ts +4 -3
- package/dist/animation.js +5 -3
- package/dist/codecs/jpeg.js +928 -37
- package/dist/codecs/png.js +100 -23
- package/dist/codecs/quantize-rgba.js +315 -0
- package/dist/codecs/quantize.js +31 -11
- package/dist/codecs/tiff.js +127 -45
- package/dist/codecs/vp8.js +341 -20
- package/dist/codecs/vp8l.js +288 -86
- package/dist/codecs/zlib.js +45 -0
- package/dist/image.d.ts +41 -2
- package/dist/image.js +18 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -20,11 +20,9 @@
|
|
|
20
20
|
|
|
21
21
|
---
|
|
22
22
|
|
|
23
|
-
**ImageScript** decodes, edits and encodes images in pure TypeScript: no WebAssembly, no native binaries,
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
definitions (TypeScript 5.7 or newer; CommonJS projects need `module: nodenext` and 5.8 or newer to `import` it (`module: node16` can't). With older versions and `skipLibCheck`, the encoders' return types become `any`). Large AVIF and JPEG XL operations, and lossless WebP encoding, can use multiple CPU cores when the runtime
|
|
27
|
-
supports it.
|
|
23
|
+
**ImageScript** decodes, edits and encodes images in pure TypeScript: no WebAssembly, no native binaries, no runtime
|
|
24
|
+
dependencies. It runs on Node.js 22.13+, Deno, Bun, browsers and edge runtimes, and ships with TypeScript type
|
|
25
|
+
definitions.
|
|
28
26
|
|
|
29
27
|
```sh
|
|
30
28
|
npm install imagescript # Node.js, Bun and bundlers
|
|
@@ -59,10 +57,11 @@ import { Image } from 'https://cdn.jsdelivr.net/npm/imagescript@4/+esm';
|
|
|
59
57
|
### Threads and bundlers
|
|
60
58
|
|
|
61
59
|
The codecs that can use worker threads (large AVIF and JPEG XL images, lossless WebP; the `threads` option) do so in
|
|
62
|
-
Node.js, Deno and Bun, and in browsers on cross-origin isolated pages.
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
60
|
+
Node.js, Deno and Bun, and in browsers on cross-origin isolated pages. Large PNGs are compressed in parallel on the zlib
|
|
61
|
+
threads of Node.js (4 unless `UV_THREADPOOL_SIZE` says otherwise) and Bun instead, without a `threads` option. Where
|
|
62
|
+
workers can't start, the work runs on the calling thread: the output is the same, only slower, and nothing warns about
|
|
63
|
+
it. Browsers refuse workers from another origin, so the jsDelivr import above runs single-threaded; serve the package
|
|
64
|
+
from your own origin (on a cross-origin isolated page) to get threads.
|
|
66
65
|
|
|
67
66
|
Bundlers that write one file (esbuild, Rollup, `bun build`) don't copy the worker's module: serve `dist/util/worker.js` next to the
|
|
68
67
|
bundle, or the same single-threaded fallback is used. webpack and Vite emit it.
|
|
@@ -122,7 +121,7 @@ compares ImageScript's speed and file sizes with Jimp, sharp and other libraries
|
|
|
122
121
|
### Credits
|
|
123
122
|
|
|
124
123
|
ImageScript builds on [fflate](https://github.com/101arrowz/fflate), libjpeg, [pdf.js](https://github.com/mozilla/pdf.js),
|
|
125
|
-
[font-rs](https://github.com/raphlinus/font-rs), [libwebp](https://chromium.googlesource.com/webm/libwebp),
|
|
124
|
+
[font-rs](https://github.com/raphlinus/font-rs), [mozjpeg](https://github.com/mozilla/mozjpeg), [libwebp](https://chromium.googlesource.com/webm/libwebp),
|
|
126
125
|
[HarfBuzz](https://github.com/harfbuzz/harfbuzz), [dav1d](https://code.videolan.org/videolan/dav1d),
|
|
127
126
|
[libavif](https://github.com/AOMediaCodec/libavif), libaom, [jxl-oxide](https://github.com/tirr-c/jxl-oxide) and
|
|
128
127
|
[libjxl](https://github.com/libjxl/libjxl). The TrueType hinting follows [FreeType](https://freetype.org): portions of
|
package/THIRD_PARTY_NOTICES.md
CHANGED
|
@@ -7,7 +7,8 @@ following projects, under their own licenses. Each derived file says so in its h
|
|
|
7
7
|
|---|---|---|
|
|
8
8
|
| [fflate](https://github.com/101arrowz/fflate) by Arjun Barrett | `src/codecs/zlib.ts` (deflate/inflate) | MIT |
|
|
9
9
|
| [Fast CRC32 in JavaScript](https://gist.github.com/101arrowz/e58695f7ccfdf74f60ba22018093edea) by Arjun Barrett | `src/codecs/crc.ts` (slice-by-16 CRC-32) | MIT |
|
|
10
|
-
| [libjpeg](https://ijg.org/) / [libjpeg-turbo](https://libjpeg-turbo.org/) by the Independent JPEG Group | `src/codecs/jpeg.ts` (IDCT, FDCT, upsampling, color conversion, progressive
|
|
10
|
+
| [libjpeg](https://ijg.org/) / [libjpeg-turbo](https://libjpeg-turbo.org/) by the Independent JPEG Group | `src/codecs/jpeg.ts` (IDCT, FDCT, upsampling, color conversion, progressive decoding and encoding, optimal Huffman tables) | IJG |
|
|
11
|
+
| [mozjpeg](https://github.com/mozilla/mozjpeg) by Mozilla | `src/codecs/jpeg.ts` (the scans of progressive files, chosen from its scan search) | IJG |
|
|
11
12
|
| [pdf.js](https://github.com/mozilla/pdf.js) (forked from [jpgjs](https://github.com/notmasteryet/jpgjs) by notmasteryet) | `src/codecs/jpeg.ts` (structure of the scan decoder) | Apache-2.0 |
|
|
12
13
|
| [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
14
|
| [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) |
|
|
@@ -66,9 +67,10 @@ This software is based in part on the work of the Independent JPEG Group.
|
|
|
66
67
|
|
|
67
68
|
The JPEG codec translates parts of libjpeg (as shipped by libjpeg-turbo) to TypeScript: the integer inverse DCT
|
|
68
69
|
(`jidctint.c`), the floating point forward DCT (`jfdctflt.c`), fancy upsampling (`jdsample.c`), the YCbCr to RGB
|
|
69
|
-
tables (`jdcolor.c`), the progressive AC refinement (`jdphuff.c`)
|
|
70
|
-
(`
|
|
71
|
-
|
|
70
|
+
tables (`jdcolor.c`), the progressive AC refinement (`jdphuff.c`), the progressive encoder (`jcphuff.c`) and the
|
|
71
|
+
optimal Huffman table generation (`jchuff.c`). The scans of progressive files are a fixed choice from
|
|
72
|
+
the ones mozjpeg's `jpeg_search_progression` tries (`jcparam.c`). The translations were rewritten for TypeScript's numbers and typed arrays; the
|
|
73
|
+
rest of the codec is ImageScript's own. libjpeg's README with its copyright and terms is included unaltered as
|
|
72
74
|
[licenses/README.ijg](licenses/README.ijg).
|
|
73
75
|
|
|
74
76
|
## pdf.js, font-rs
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { availableThreads } from "./util/threads.js";
|
|
1
2
|
import * as gifCodec from "./codecs/gif.js";
|
|
2
3
|
import { paletteOptions } from "./codecs/quantize.js";
|
|
3
4
|
import * as png from "./codecs/png.js";
|
|
@@ -7,7 +8,7 @@ import { Frame } from "./frame.js";
|
|
|
7
8
|
import { Optimizer, Compositor, coverCanvas, fillUnchanged } from "./util/optimize.js";
|
|
8
9
|
import { compressAsync, decompressAsync } from "./codecs/zlib.js";
|
|
9
10
|
import { resolveMetadata, pngText, exifData, gifComments } from "./metadata.js";
|
|
10
|
-
import { instance, checkOptions, finite, nonNegative, canvasSize, compressionLevel, loopCount, toPlays, fitLoops, fitDuration, maxDuration, boolean } from "./util/options.js";
|
|
11
|
+
import { instance, checkOptions, finite, nonNegative, canvasSize, compressionLevel, checkThreads, loopCount, toPlays, fitLoops, fitDuration, maxDuration, boolean } from "./util/options.js";
|
|
11
12
|
import { checkFormatSize } from "./util/limits.js";
|
|
12
13
|
function clipToCanvas(frame, width, height) {
|
|
13
14
|
const { x, y, width: w, height: h } = frame;
|
|
@@ -143,8 +144,11 @@ export class AnimationEncoder {
|
|
|
143
144
|
}
|
|
144
145
|
}, optimize);
|
|
145
146
|
}
|
|
146
|
-
static __apng__(width, height, { loopCount: count = Infinity, metadata = {}, optimize = true, compression: compressionOption = 4 }) {
|
|
147
|
+
static __apng__(width, height, { loopCount: count = Infinity, metadata = {}, optimize = true, compression: compressionOption = 4, threads = Math.max(1, Math.min(availableThreads(), 8)), colors, dither = 'floyd-steinberg', ditherStrength = 0.85 }) {
|
|
147
148
|
const compression = compressionLevel(compressionOption);
|
|
149
|
+
checkThreads(threads);
|
|
150
|
+
const palette = paletteOptions(colors, dither, ditherStrength);
|
|
151
|
+
const lossy = palette.colors === undefined ? undefined : { colors: palette.colors, dither: palette.dither, strength: palette.strength };
|
|
148
152
|
boolean(optimize, 'optimize');
|
|
149
153
|
const loops = fitLoops(loopCount(count), 'APNG');
|
|
150
154
|
[width, height] = canvas(width, height, 'APNG');
|
|
@@ -154,7 +158,7 @@ export class AnimationEncoder {
|
|
|
154
158
|
let blended = false;
|
|
155
159
|
return new AnimationEncoder(width, height, {
|
|
156
160
|
name: 'APNG',
|
|
157
|
-
optimize: { replace: true, even: false, alpha: () => cost.cost(), maxDuration: maxDuration('APNG') },
|
|
161
|
+
optimize: { replace: true, even: false, alpha: () => lossy ? 1 : cost.cost(), maxDuration: maxDuration('APNG') },
|
|
158
162
|
async frame(f, optimized) {
|
|
159
163
|
let frame;
|
|
160
164
|
if (optimized) {
|
|
@@ -171,7 +175,7 @@ export class AnimationEncoder {
|
|
|
171
175
|
stored.push({ frame: rest, packed: await compressAsync(pixels, 1) });
|
|
172
176
|
},
|
|
173
177
|
async file(optimizer) {
|
|
174
|
-
const fill = !!optimizer && optimizer.opaque && blended && optimizer.denseTotal < optimizer.chosenTotal * cost.cost();
|
|
178
|
+
const fill = !!optimizer && optimizer.opaque && blended && optimizer.denseTotal < optimizer.chosenTotal * (lossy ? 1 : cost.cost());
|
|
175
179
|
const frames = async function* () {
|
|
176
180
|
const shown32 = new Int32Array(width * height);
|
|
177
181
|
for (const [i, { frame, packed }] of stored.entries()) {
|
|
@@ -183,12 +187,14 @@ export class AnimationEncoder {
|
|
|
183
187
|
yield f;
|
|
184
188
|
}
|
|
185
189
|
};
|
|
186
|
-
const
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
+
const each = async function* () {
|
|
191
|
+
for await (const f of frames())
|
|
192
|
+
yield f.pixels;
|
|
193
|
+
};
|
|
194
|
+
const { format, plan } = await png.chooseFormat(each, lossy);
|
|
195
|
+
const encoded = [];
|
|
190
196
|
for await (const { pixels, ...f } of frames())
|
|
191
|
-
encoded.push({ ...f, data: await png.frameData(pixels, f.width, f.height, format, compression) });
|
|
197
|
+
encoded.push({ ...f, data: await png.frameData(plan ? png.quantizePixels(pixels, f.width, f.height, plan) : pixels, f.width, f.height, format, compression, threads) });
|
|
192
198
|
return png.animationFile(encoded, width, height, toPlays(loops), format, text);
|
|
193
199
|
}
|
|
194
200
|
}, optimize);
|
package/dist/animation.d.ts
CHANGED
|
@@ -202,10 +202,11 @@ export declare class Animation {
|
|
|
202
202
|
*/
|
|
203
203
|
encodeGIF(options?: GIFAnimationEncodeOptions): Promise<Uint8Array<ArrayBuffer>>;
|
|
204
204
|
/**
|
|
205
|
-
* Encodes the animation as APNG (animated PNG, lossless and with full alpha
|
|
205
|
+
* Encodes the animation as APNG (animated PNG, lossless and with full alpha, unless `colors` makes it a palette
|
|
206
|
+
* shared by all frames). Viewers without APNG support show
|
|
206
207
|
* the first frame. Each frame is stored as the area that changed since the frame before, identical frames are
|
|
207
|
-
* merged (unless the `optimize` option is false): the file shows
|
|
208
|
-
*
|
|
208
|
+
* merged (unless the `optimize` option is false): the file shows the same animation, but doesn't necessarily decode
|
|
209
|
+
* to the same frames (with `colors`, the same up to the palette)
|
|
209
210
|
* @param options See {@link APNGEncodeOptions}; the loop count is the animation's {@link loopCount} (the streaming
|
|
210
211
|
* {@link apngEncoder} takes it as an option)
|
|
211
212
|
* @throws `RangeError` if the animation has no frames, the loop count or a frame's duration is more than APNG stores
|
package/dist/animation.js
CHANGED
|
@@ -334,14 +334,16 @@ export class Animation {
|
|
|
334
334
|
}
|
|
335
335
|
async encodeAPNG(options = {}) {
|
|
336
336
|
checkOptions(options, 'encodeAPNG({ compression: 9 })');
|
|
337
|
-
const { compression = 4, metadata = {}, optimize = true } = options;
|
|
337
|
+
const { compression = 4, threads = Math.max(1, Math.min(availableThreads(), 8)), colors, dither = 'floyd-steinberg', ditherStrength = 0.85, metadata = {}, optimize = true } = options;
|
|
338
|
+
checkThreads(threads);
|
|
339
|
+
const palette = paletteOptions(colors, dither, ditherStrength);
|
|
338
340
|
boolean(optimize, 'optimize');
|
|
339
341
|
const loops = fitLoops(this.loopCount, 'APNG');
|
|
340
342
|
this.__validate__('APNG');
|
|
341
343
|
const frames = optimize
|
|
342
|
-
? this.__optimized__({ replace: true, even: false, alpha: frames => png.alphaCost(frames.map(f => f.pixels)), maxDuration: maxDuration('APNG') }).map(f => ({ ...f, dispose: f.dispose ? 1 : 0 }))
|
|
344
|
+
? this.__optimized__({ replace: true, even: false, alpha: frames => palette.colors === undefined ? png.alphaCost(frames.map(f => f.pixels)) : 1, maxDuration: maxDuration('APNG') }).map(f => ({ ...f, dispose: f.dispose ? 1 : 0 }))
|
|
343
345
|
: this.__apngFrames__().map(({ blend: _, ...f }) => f);
|
|
344
|
-
return png.encodeAnimation(frames, this.width, this.height, toPlays(loops), compressionLevel(compression), pngText(resolveMetadata(metadata)));
|
|
346
|
+
return png.encodeAnimation(frames, this.width, this.height, toPlays(loops), compressionLevel(compression), pngText(resolveMetadata(metadata)), threads, palette.colors === undefined ? undefined : { colors: palette.colors, dither: palette.dither, strength: palette.strength });
|
|
345
347
|
}
|
|
346
348
|
async encodeWebP(options = {}) {
|
|
347
349
|
checkOptions(options, 'animation.encodeWebP({ quality: 80 })');
|