imagescript 3.0.0 → 3.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 +19 -7
- package/THIRD_PARTY_NOTICES.md +82 -0
- package/dist/animation.d.ts +55 -0
- package/dist/animation.js +167 -0
- package/dist/codecs/png.d.ts +58 -5
- package/dist/codecs/png.js +276 -54
- package/dist/codecs/tga.d.ts +16 -0
- package/dist/codecs/tga.js +203 -0
- package/dist/codecs/vp8-tables.d.ts +10 -0
- package/dist/codecs/vp8-tables.js +17 -0
- package/dist/codecs/vp8.d.ts +8 -0
- package/dist/codecs/vp8.js +711 -0
- package/dist/codecs/vp8l.d.ts +62 -0
- package/dist/codecs/vp8l.js +909 -0
- package/dist/codecs/webp.d.ts +67 -0
- package/dist/codecs/webp.js +280 -0
- package/dist/frame.d.ts +10 -10
- package/dist/gif.d.ts +7 -20
- package/dist/gif.js +5 -78
- package/dist/image-type.d.ts +5 -1
- package/dist/image-type.js +16 -1
- package/dist/image.d.ts +34 -2
- package/dist/image.js +40 -2
- package/dist/index.d.ts +8 -4
- package/dist/index.js +8 -1
- package/dist/render/font.d.ts +29 -0
- package/dist/render/font.js +99 -2
- package/dist/render/hinting.d.ts +110 -0
- package/dist/render/hinting.js +1236 -0
- package/dist/render/text.d.ts +3 -1
- package/dist/render/text.js +32 -7
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/licenses/FTL.txt +169 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
|
|
10
10
|
**ImageScript** is a zero-dependency alternative to common JavaScript bitmap image manipulation tools. Every codec and
|
|
11
11
|
renderer is written in TypeScript: no WebAssembly, no native binaries, no npm dependencies, no install scripts. The
|
|
12
|
-
package is about
|
|
12
|
+
package is about 140 kB (compressed) and runs in Node.js 22.13 or newer (including Alpine/musl, ARM and bundled
|
|
13
13
|
serverless functions), Deno, Bun and browsers. It works with both `import` and `require()`, type definitions are included.
|
|
14
14
|
|
|
15
15
|
```sh
|
|
@@ -25,11 +25,15 @@ deno add jsr:@matmen/imagescript # Deno (npm:imagescript works too)
|
|
|
25
25
|
- PNGs (grayscale, RGB, indexed colors, 1-16 bit, interlaced) with and without alpha channels
|
|
26
26
|
- JPEGs (baseline and progressive; grayscale, RGB, CMYK)
|
|
27
27
|
- TIFFs (strips and tiles; uncompressed, LZW, deflate, PackBits and JPEG compressed; BigTIFF)
|
|
28
|
-
-
|
|
29
|
-
-
|
|
28
|
+
- WebPs (lossy, lossless and with alpha; bit-exact with libwebp)
|
|
29
|
+
- TGAs (color mapped, truecolor and greyscale; uncompressed and run length encoded)
|
|
30
|
+
- JPEGs, TIFFs, PNGs and WebPs are rotated as their Exif (or, for PNGs, XMP) orientation says
|
|
31
|
+
- [Decoding animations](https://imagescript.matmen.dev/classes/Animation.html#decode) (GIF, APNG and WebP) with all
|
|
32
|
+
frames, durations and disposal modes
|
|
30
33
|
- [Rendering SVGs](https://imagescript.matmen.dev/classes/Image.html#rendersvg) (shapes, paths, gradients, strokes, clip paths,
|
|
31
34
|
masks, CSS; no text, images or filters)
|
|
32
|
-
- [Rendering vector fonts](https://imagescript.matmen.dev/classes/Image.html#rendertext) (TrueType and OpenType, with kerning
|
|
35
|
+
- [Rendering vector fonts](https://imagescript.matmen.dev/classes/Image.html#rendertext) (TrueType and OpenType, with kerning,
|
|
36
|
+
outlines and TrueType hinting)
|
|
33
37
|
- [Resizing](https://imagescript.matmen.dev/classes/Image.html#resize) (bilinear, bicubic, area averaging and nearest neighbor)
|
|
34
38
|
- Image manipulation functions ([crop](https://imagescript.matmen.dev/classes/Image.html#crop)
|
|
35
39
|
, [rotate](https://imagescript.matmen.dev/classes/Image.html#rotate)
|
|
@@ -39,12 +43,17 @@ deno add jsr:@matmen/imagescript # Deno (npm:imagescript works too)
|
|
|
39
43
|
, [sharpen](https://imagescript.matmen.dev/classes/Image.html#sharpen)
|
|
40
44
|
, [brightness](https://imagescript.matmen.dev/classes/Image.html#brightness)
|
|
41
45
|
, [hueShift](https://imagescript.matmen.dev/classes/Image.html#hueshift), ...)
|
|
42
|
-
- Compositing with opacity and blend modes ([composite](https://imagescript.matmen.dev/classes/Image.html#composite))
|
|
46
|
+
- Compositing with opacity and blend modes ([composite](https://imagescript.matmen.dev/classes/Image.html#composite)),
|
|
47
|
+
removing backgrounds ([colorToAlpha](https://imagescript.matmen.dev/classes/Image.html#colortoalpha))
|
|
43
48
|
- Color information functions ([averageColor](https://imagescript.matmen.dev/classes/Image.html#averagecolor)
|
|
44
49
|
, [dominantColor](https://imagescript.matmen.dev/classes/Image.html#dominantcolor), ...)
|
|
45
50
|
- Encoding images as [PNGs](https://imagescript.matmen.dev/classes/Image.html#encode) (smallest lossless color type)
|
|
46
51
|
, [JPEGs](https://imagescript.matmen.dev/classes/Image.html#encodejpeg)
|
|
47
|
-
|
|
52
|
+
, lossless [WebPs](https://imagescript.matmen.dev/classes/Image.html#encodewebp)
|
|
53
|
+
and [TGAs](https://imagescript.matmen.dev/classes/Image.html#encodetga)
|
|
54
|
+
- Encoding animations as [GIF](https://imagescript.matmen.dev/classes/Animation.html#encodegif),
|
|
55
|
+
[APNG](https://imagescript.matmen.dev/classes/Animation.html#encodeapng) and lossless
|
|
56
|
+
[WebP](https://imagescript.matmen.dev/classes/Animation.html#encodewebp)
|
|
48
57
|
|
|
49
58
|
---
|
|
50
59
|
|
|
@@ -82,7 +91,10 @@ The deflate/inflate implementation is derived from [fflate](https://github.com/1
|
|
|
82
91
|
[Fast CRC32 in JavaScript](https://gist.github.com/101arrowz/e58695f7ccfdf74f60ba22018093edea), both by Arjun Barrett.
|
|
83
92
|
The JPEG codec is based in part on the work of the Independent JPEG Group (libjpeg) and on
|
|
84
93
|
[pdf.js](https://github.com/mozilla/pdf.js), the glyph rasterizer is translated from
|
|
85
|
-
[font-rs](https://github.com/raphlinus/font-rs).
|
|
94
|
+
[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)
|
|
95
|
+
and reproduce [libwebp](https://chromium.googlesource.com/webm/libwebp)'s color conversion, the TrueType hinting follows
|
|
96
|
+
[FreeType](https://freetype.org)'s interpreter (portions of this software are copyright © 2026 The FreeType Project).
|
|
97
|
+
See [THIRD_PARTY_NOTICES.md](THIRD_PARTY_NOTICES.md) for their licenses.
|
|
86
98
|
|
|
87
99
|
---
|
|
88
100
|
|
package/THIRD_PARTY_NOTICES.md
CHANGED
|
@@ -10,6 +10,10 @@ following projects, under their own licenses. Each derived file says so in its h
|
|
|
10
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 refinement, optimal Huffman tables) | IJG |
|
|
11
11
|
| [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
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
|
+
| [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
|
+
| [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 |
|
|
16
|
+
| [FreeType](https://freetype.org) | `src/render/hinting.ts` (behavior and parts of the logic of the TrueType interpreter) | FreeType License |
|
|
13
17
|
|
|
14
18
|
## fflate, Fast CRC32 in JavaScript
|
|
15
19
|
|
|
@@ -72,3 +76,81 @@ The scan decoder of `src/codecs/jpeg.ts` follows the structure of pdf.js' `src/c
|
|
|
72
76
|
one decode function per scan type). The `Rasterizer` class in `src/render/text.ts` is a translation of font-rs'
|
|
73
77
|
`Raster` from Rust to TypeScript (`draw_line`, `draw_quad`, `accumulate`); its `cubic` method is adapted from
|
|
74
78
|
`draw_quad`. The full license text is in [licenses/Apache-2.0.txt](licenses/Apache-2.0.txt).
|
|
79
|
+
|
|
80
|
+
## RFC 6386, libwebp
|
|
81
|
+
|
|
82
|
+
The VP8 decoder follows the procedures of RFC 6386 and its reference decoder, its constant tables are extracted from
|
|
83
|
+
the RFC. To decode exactly like libwebp, the YUV to RGB conversion, the chroma upsampling and the blending of animation
|
|
84
|
+
frames reproduce libwebp's integer arithmetic.
|
|
85
|
+
|
|
86
|
+
```
|
|
87
|
+
RFC 6386 reference decoder: Copyright (c) 2010, 2011, Google Inc. All rights reserved.
|
|
88
|
+
libwebp: Copyright (c) 2010, Google Inc. All rights reserved.
|
|
89
|
+
|
|
90
|
+
Redistribution and use in source and binary forms, with or without
|
|
91
|
+
modification, are permitted provided that the following conditions are
|
|
92
|
+
met:
|
|
93
|
+
|
|
94
|
+
* Redistributions of source code must retain the above copyright
|
|
95
|
+
notice, this list of conditions and the following disclaimer.
|
|
96
|
+
|
|
97
|
+
* Redistributions in binary form must reproduce the above copyright
|
|
98
|
+
notice, this list of conditions and the following disclaimer in
|
|
99
|
+
the documentation and/or other materials provided with the
|
|
100
|
+
distribution.
|
|
101
|
+
|
|
102
|
+
* Neither the name of Google nor the names of its contributors may
|
|
103
|
+
be used to endorse or promote products derived from this software
|
|
104
|
+
without specific prior written permission.
|
|
105
|
+
|
|
106
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
107
|
+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
108
|
+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
109
|
+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
110
|
+
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
111
|
+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
112
|
+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
113
|
+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
114
|
+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
115
|
+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
116
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
The RFC's reference decoder additionally comes with Google's patent grant for VP8 (RFC 6386 section 20.27).
|
|
120
|
+
|
|
121
|
+
## RFC 9649
|
|
122
|
+
|
|
123
|
+
The WebP container and the lossless (VP8L) codec follow RFC 9649, the short distance code table is taken from it.
|
|
124
|
+
|
|
125
|
+
```
|
|
126
|
+
Copyright (c) 2024 IETF Trust and the persons identified as authors of the code. All rights reserved.
|
|
127
|
+
|
|
128
|
+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
|
|
129
|
+
following conditions are met:
|
|
130
|
+
|
|
131
|
+
- Redistributions of source code must retain the above copyright notice, this list of conditions and the following
|
|
132
|
+
disclaimer.
|
|
133
|
+
- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
|
|
134
|
+
following disclaimer in the documentation and/or other materials provided with the distribution.
|
|
135
|
+
- Neither the name of Internet Society, IETF or IETF Trust, nor the names of specific contributors, may be used to
|
|
136
|
+
endorse or promote products derived from this software without specific prior written permission.
|
|
137
|
+
|
|
138
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
|
139
|
+
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
140
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
141
|
+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
142
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
143
|
+
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
144
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
## FreeType
|
|
148
|
+
|
|
149
|
+
Portions of this software are copyright © 2026 The FreeType Project (https://freetype.org). All rights reserved.
|
|
150
|
+
|
|
151
|
+
The TrueType hinting interpreter in `src/render/hinting.ts` is written after the TrueType instruction specification.
|
|
152
|
+
Where the specification leaves details open, its behavior and parts of its logic follow FreeType's interpreter
|
|
153
|
+
(`src/truetype/ttinterp.c`): the fixed point rounding, measuring original distances on the unscaled outline, the
|
|
154
|
+
interpolation of untouched points (IUP), the handling of near-perpendicular freedom and projection vectors, and the
|
|
155
|
+
tolerance of invalid references. FreeType is used under the FreeType License, included as
|
|
156
|
+
[licenses/FTL.txt](licenses/FTL.txt).
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { BinaryData } from './util/mem.js';
|
|
2
|
+
import { ResizeMode } from './image.js';
|
|
3
|
+
import { Frame } from './frame.js';
|
|
4
|
+
/**
|
|
5
|
+
* An animation: an array of frames that can be decoded from and encoded to GIF, APNG and WebP.
|
|
6
|
+
* Array methods like map and filter return plain arrays.
|
|
7
|
+
*/
|
|
8
|
+
export declare class Animation extends Array<Frame> {
|
|
9
|
+
/** How many times to repeat the animation (-1 = forever, 0 = play once) */
|
|
10
|
+
loopCount: number;
|
|
11
|
+
/**
|
|
12
|
+
* Creates a new animation
|
|
13
|
+
* @param frames The frames to create the animation from
|
|
14
|
+
* @param loopCount How many times to repeat the animation (-1 = forever, 0 = play once)
|
|
15
|
+
*/
|
|
16
|
+
constructor(frames: Frame[], loopCount?: number);
|
|
17
|
+
/** The animation's width */
|
|
18
|
+
get width(): number;
|
|
19
|
+
/** The animation's height */
|
|
20
|
+
get height(): number;
|
|
21
|
+
/** The animation's duration (in ms) */
|
|
22
|
+
get duration(): number;
|
|
23
|
+
toString(): string;
|
|
24
|
+
/**
|
|
25
|
+
* Returns a new animation of the same class containing a range of this one's frames
|
|
26
|
+
* (same semantics as Array.prototype.slice)
|
|
27
|
+
*/
|
|
28
|
+
slice(start?: number, end?: number): this;
|
|
29
|
+
/**
|
|
30
|
+
* Resizes the animation to the given dimensions, scaling every frame and its offsets proportionally.
|
|
31
|
+
* Use {@link Image.RESIZE_AUTO} as either width or height to automatically preserve the aspect ratio.
|
|
32
|
+
*/
|
|
33
|
+
resize(width: number, height: number, mode?: ResizeMode): this;
|
|
34
|
+
/**
|
|
35
|
+
* Encodes the animation as GIF
|
|
36
|
+
* @param quality 1-100, scales the palette size from 8 colors (1) to 256 colors (90 and above)
|
|
37
|
+
*/
|
|
38
|
+
encodeGIF(quality?: number): Promise<Uint8Array>;
|
|
39
|
+
/**
|
|
40
|
+
* Encodes the animation as APNG (animated PNG, lossless and with full alpha). Viewers without APNG support show
|
|
41
|
+
* the first frame
|
|
42
|
+
* @param compression The zlib compression level (0-9), higher is smaller but slower
|
|
43
|
+
*/
|
|
44
|
+
encodeAPNG(compression?: number): Promise<Uint8Array>;
|
|
45
|
+
/**
|
|
46
|
+
* Encodes the animation as lossless animated WebP
|
|
47
|
+
*/
|
|
48
|
+
encodeWebP(): Promise<Uint8Array>;
|
|
49
|
+
/**
|
|
50
|
+
* Decodes an animated GIF, APNG or WebP into fully composited frames (still images become a single frame)
|
|
51
|
+
* @param input The binary data to decode
|
|
52
|
+
* @param onlyExtractFirstFrame Whether to stop decoding after the first frame
|
|
53
|
+
*/
|
|
54
|
+
static decode(input: BinaryData, onlyExtractFirstFrame?: boolean): Promise<Animation>;
|
|
55
|
+
}
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import * as gifCodec from "./codecs/gif.js";
|
|
2
|
+
import * as png from "./codecs/png.js";
|
|
3
|
+
import * as webp from "./codecs/webp.js";
|
|
4
|
+
import { view } from "./util/mem.js";
|
|
5
|
+
import { Image } from "./image.js";
|
|
6
|
+
import { Frame } from "./frame.js";
|
|
7
|
+
import { ImageType } from "./image-type.js";
|
|
8
|
+
const toPlays = (loopCount) => loopCount < 0 ? 0 : loopCount + 1;
|
|
9
|
+
const fromPlays = (plays) => plays === 0 ? -1 : plays - 1;
|
|
10
|
+
function canvasFrames(width, height, frames) {
|
|
11
|
+
return frames.map(({ pixels, duration }) => {
|
|
12
|
+
const frame = new Frame(width, height, duration, 0, 0, Frame.DISPOSAL_BACKGROUND);
|
|
13
|
+
frame.bitmap.set(pixels);
|
|
14
|
+
return frame;
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
export class Animation extends Array {
|
|
18
|
+
loopCount;
|
|
19
|
+
constructor(frames, loopCount = -1) {
|
|
20
|
+
super();
|
|
21
|
+
for (const frame of frames)
|
|
22
|
+
this.push(frame);
|
|
23
|
+
for (let i = 0; i < this.length; i++)
|
|
24
|
+
if (!(this[i] instanceof Frame))
|
|
25
|
+
throw new TypeError(`Frame ${i} is not an instance of Frame`);
|
|
26
|
+
if (loopCount < -1 || isNaN(loopCount))
|
|
27
|
+
throw new RangeError('Invalid loop count');
|
|
28
|
+
this.loopCount = loopCount;
|
|
29
|
+
}
|
|
30
|
+
static get [Symbol.species]() {
|
|
31
|
+
return Array;
|
|
32
|
+
}
|
|
33
|
+
get width() {
|
|
34
|
+
let max = 0;
|
|
35
|
+
for (const frame of this)
|
|
36
|
+
max = Math.max(max, frame.width + frame.xOffset);
|
|
37
|
+
return max;
|
|
38
|
+
}
|
|
39
|
+
get height() {
|
|
40
|
+
let max = 0;
|
|
41
|
+
for (const frame of this)
|
|
42
|
+
max = Math.max(max, frame.height + frame.yOffset);
|
|
43
|
+
return max;
|
|
44
|
+
}
|
|
45
|
+
get duration() {
|
|
46
|
+
let duration = 0;
|
|
47
|
+
for (const frame of this)
|
|
48
|
+
duration += frame.duration;
|
|
49
|
+
return duration;
|
|
50
|
+
}
|
|
51
|
+
toString() {
|
|
52
|
+
return `Animation<${this.width}x${this.height}x${this.duration}ms>`;
|
|
53
|
+
}
|
|
54
|
+
slice(start, end) {
|
|
55
|
+
const Class = this.constructor;
|
|
56
|
+
return new Class(Array.prototype.slice.call(this, start, end), this.loopCount);
|
|
57
|
+
}
|
|
58
|
+
resize(width, height, mode = Image.RESIZE_BILINEAR) {
|
|
59
|
+
const originalWidth = this.width, originalHeight = this.height;
|
|
60
|
+
if (width === Image.RESIZE_AUTO && height === Image.RESIZE_AUTO)
|
|
61
|
+
throw new Error('RESIZE_AUTO can only be used for either width or height, not for both');
|
|
62
|
+
if (width === Image.RESIZE_AUTO)
|
|
63
|
+
width = originalWidth / originalHeight * height;
|
|
64
|
+
if (height === Image.RESIZE_AUTO)
|
|
65
|
+
height = originalHeight / originalWidth * width;
|
|
66
|
+
const sx = width / originalWidth, sy = height / originalHeight;
|
|
67
|
+
for (const frame of this) {
|
|
68
|
+
const left = Math.round(frame.xOffset * sx), top = Math.round(frame.yOffset * sy);
|
|
69
|
+
const right = Math.round((frame.xOffset + frame.width) * sx), bottom = Math.round((frame.yOffset + frame.height) * sy);
|
|
70
|
+
Image.prototype.resize.call(frame, Math.max(1, right - left), Math.max(1, bottom - top), mode);
|
|
71
|
+
frame.xOffset = left;
|
|
72
|
+
frame.yOffset = top;
|
|
73
|
+
}
|
|
74
|
+
return this;
|
|
75
|
+
}
|
|
76
|
+
__validate__() {
|
|
77
|
+
for (const frame of this)
|
|
78
|
+
if (!(frame instanceof Frame))
|
|
79
|
+
throw new Error('Animation contains invalid frames');
|
|
80
|
+
}
|
|
81
|
+
async encodeGIF(quality = 95) {
|
|
82
|
+
this.__validate__();
|
|
83
|
+
const frames = [];
|
|
84
|
+
for (const frame of this)
|
|
85
|
+
frames.push({
|
|
86
|
+
x: frame.xOffset,
|
|
87
|
+
y: frame.yOffset,
|
|
88
|
+
width: frame.width,
|
|
89
|
+
height: frame.height,
|
|
90
|
+
delay: Math.round(frame.duration / 10),
|
|
91
|
+
dispose: frame.__disposal_id__,
|
|
92
|
+
pixels: new Uint8Array(frame.bitmap.buffer, frame.bitmap.byteOffset, frame.bitmap.byteLength)
|
|
93
|
+
});
|
|
94
|
+
return gifCodec.encode({ width: this.width, height: this.height, loopCount: this.loopCount, frames }, quality);
|
|
95
|
+
}
|
|
96
|
+
async encodeAPNG(compression = 1) {
|
|
97
|
+
this.__validate__();
|
|
98
|
+
if (!this.length)
|
|
99
|
+
throw new Error('An animation needs at least one frame');
|
|
100
|
+
return png.encodeAnimation(this.__apngFrames__(), this.width, this.height, toPlays(this.loopCount), compression);
|
|
101
|
+
}
|
|
102
|
+
async encodeWebP() {
|
|
103
|
+
this.__validate__();
|
|
104
|
+
if (!this.length)
|
|
105
|
+
throw new Error('An animation needs at least one frame');
|
|
106
|
+
const width = this.width, height = this.height;
|
|
107
|
+
const fullFrames = this.some(f => f.disposalMode === 'previous' || (f.disposalMode === 'background' && (f.xOffset & 1 || f.yOffset & 1)));
|
|
108
|
+
if (fullFrames) {
|
|
109
|
+
const canvases = png.compositeAnimation({ width, height, frames: this.__apngFrames__() });
|
|
110
|
+
return webp.encodeAnimation(canvases.map(c => ({ x: 0, y: 0, width, height, duration: c.duration, dispose: true, pixels: c.pixels })), width, height, toPlays(this.loopCount));
|
|
111
|
+
}
|
|
112
|
+
const frames = Array.from(this, frame => {
|
|
113
|
+
let { xOffset: x, yOffset: y, width: w, height: h } = frame;
|
|
114
|
+
let pixels = new Uint8Array(frame.bitmap.buffer, frame.bitmap.byteOffset, frame.bitmap.byteLength);
|
|
115
|
+
if (x & 1 || y & 1) {
|
|
116
|
+
const dx = x & 1, dy = y & 1;
|
|
117
|
+
const padded = new Uint8Array((w + dx) * (h + dy) * 4);
|
|
118
|
+
for (let row = 0; row < h; row++)
|
|
119
|
+
padded.set(pixels.subarray(row * w * 4, (row + 1) * w * 4), ((row + dy) * (w + dx) + dx) * 4);
|
|
120
|
+
pixels = padded;
|
|
121
|
+
x -= dx;
|
|
122
|
+
y -= dy;
|
|
123
|
+
w += dx;
|
|
124
|
+
h += dy;
|
|
125
|
+
}
|
|
126
|
+
return { x, y, width: w, height: h, duration: frame.duration, dispose: frame.disposalMode === 'background', pixels };
|
|
127
|
+
});
|
|
128
|
+
return webp.encodeAnimation(frames, width, height, toPlays(this.loopCount));
|
|
129
|
+
}
|
|
130
|
+
__apngFrames__() {
|
|
131
|
+
const dispose = [0, 0, 2, 1];
|
|
132
|
+
return Array.from(this, frame => ({
|
|
133
|
+
x: frame.xOffset,
|
|
134
|
+
y: frame.yOffset,
|
|
135
|
+
width: frame.width,
|
|
136
|
+
height: frame.height,
|
|
137
|
+
duration: frame.duration,
|
|
138
|
+
dispose: dispose[frame.__disposal_id__],
|
|
139
|
+
blend: 1,
|
|
140
|
+
pixels: new Uint8Array(frame.bitmap.buffer, frame.bitmap.byteOffset, frame.bitmap.byteLength)
|
|
141
|
+
}));
|
|
142
|
+
}
|
|
143
|
+
static async decode(input, onlyExtractFirstFrame = false) {
|
|
144
|
+
const data = view(input);
|
|
145
|
+
const type = ImageType.getType(data);
|
|
146
|
+
if (type === 'gif')
|
|
147
|
+
return Animation.__fromGIF__(data, onlyExtractFirstFrame, Animation);
|
|
148
|
+
if (type === 'webp') {
|
|
149
|
+
const decoded = webp.decodeAnimation(data, onlyExtractFirstFrame);
|
|
150
|
+
const frames = canvasFrames(decoded.width, decoded.height, webp.composite(decoded));
|
|
151
|
+
return new Animation(frames, fromPlays(decoded.loopCount));
|
|
152
|
+
}
|
|
153
|
+
if (type === 'png') {
|
|
154
|
+
const decoded = await png.decodeAnimation(data, onlyExtractFirstFrame);
|
|
155
|
+
const frames = canvasFrames(decoded.width, decoded.height, png.compositeAnimation(decoded));
|
|
156
|
+
return new Animation(frames, decoded.frames.length > 1 ? fromPlays(decoded.plays) : -1);
|
|
157
|
+
}
|
|
158
|
+
if (type)
|
|
159
|
+
return new Animation([Frame.from(await Image.decode(data), 0)]);
|
|
160
|
+
throw new Error('Unsupported image type');
|
|
161
|
+
}
|
|
162
|
+
static __fromGIF__(data, onlyExtractFirstFrame, Class) {
|
|
163
|
+
const decoded = gifCodec.decode(data, onlyExtractFirstFrame);
|
|
164
|
+
const frames = gifCodec.composite(decoded).map(({ pixels, delay }) => ({ pixels, duration: 10 * delay }));
|
|
165
|
+
return new Class(canvasFrames(decoded.width, decoded.height, frames), decoded.loopCount);
|
|
166
|
+
}
|
|
167
|
+
}
|
package/dist/codecs/png.d.ts
CHANGED
|
@@ -27,18 +27,71 @@ export interface Decoded {
|
|
|
27
27
|
*/
|
|
28
28
|
export declare function encode(data: Uint8Array, { text, width, height, channels, depth, level, palette, transparency, filter: filterRows }: EncodeOptions): Promise<Uint8Array>;
|
|
29
29
|
/**
|
|
30
|
-
* Encodes 8-bit RGBA pixels into a PNG with the smallest lossless color type
|
|
31
|
-
* a palette for up to 256 colors, otherwise greyscale or RGB, with alpha only if a pixel isn't opaque.
|
|
30
|
+
* Encodes 8-bit RGBA pixels into a PNG with the smallest lossless color type (see {@link pixelFormat}).
|
|
32
31
|
* Rows of non-palette images are paeth filtered (unless the data isn't compressed), palette indices aren't
|
|
33
32
|
* filtered (like libpng recommends)
|
|
34
33
|
*/
|
|
35
34
|
export declare function encodeRGBA(rgba: Uint8Array, width: number, height: number, level: number, text?: EncodeOptions['text']): Promise<Uint8Array>;
|
|
35
|
+
export interface APNGEncodeFrame {
|
|
36
|
+
x: number;
|
|
37
|
+
y: number;
|
|
38
|
+
width: number;
|
|
39
|
+
height: number;
|
|
40
|
+
/** milliseconds (stored as a fraction of up to 65535/65535 seconds) */
|
|
41
|
+
duration: number;
|
|
42
|
+
/** 0 none, 1 background (transparent), 2 previous */
|
|
43
|
+
dispose: number;
|
|
44
|
+
/** RGBA */
|
|
45
|
+
pixels: Uint8Array;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Encodes frames into an APNG. Frames are alpha blended onto the canvas ("over"); the first frame is also the
|
|
49
|
+
* default image that viewers without APNG support show
|
|
50
|
+
* @param plays How often to play the animation, 0 = forever
|
|
51
|
+
*/
|
|
52
|
+
export declare function encodeAnimation(frames: APNGEncodeFrame[], width: number, height: number, plays: number, level: number): Promise<Uint8Array>;
|
|
36
53
|
/**
|
|
37
|
-
* Decodes a PNG into 8-bit RGBA pixel data
|
|
54
|
+
* Decodes a PNG into 8-bit RGBA pixel data (for APNGs, the default image)
|
|
38
55
|
* Supports all color types, bit depths, tRNS transparency and adam7 interlacing
|
|
39
56
|
* The orientation comes from the eXIf chunk, or, if there is none, from the Exif text ImageMagick wrote before eXIf
|
|
40
57
|
* existed ("Raw profile type exif") or from the XMP packet
|
|
41
|
-
* @param {Uint8Array} array The PNG data
|
|
42
|
-
* @return {{width: number, height: number, pixels: Uint8Array, orientation: number}}
|
|
43
58
|
*/
|
|
44
59
|
export declare function decode(array: Uint8Array): Promise<Decoded>;
|
|
60
|
+
export interface APNGFrame {
|
|
61
|
+
x: number;
|
|
62
|
+
y: number;
|
|
63
|
+
width: number;
|
|
64
|
+
height: number;
|
|
65
|
+
/** milliseconds */
|
|
66
|
+
duration: number;
|
|
67
|
+
/** 0 none, 1 background (transparent), 2 previous */
|
|
68
|
+
dispose: number;
|
|
69
|
+
/** 0 source (replace), 1 over */
|
|
70
|
+
blend: number;
|
|
71
|
+
pixels: Uint8Array;
|
|
72
|
+
}
|
|
73
|
+
/** Whether the PNG is an APNG (has an acTL chunk) */
|
|
74
|
+
export declare function isAnimated(array: Uint8Array): boolean;
|
|
75
|
+
/**
|
|
76
|
+
* Decodes an APNG (https://wiki.mozilla.org/APNG_Specification) into its (uncomposited) frames; a plain PNG becomes
|
|
77
|
+
* a single frame
|
|
78
|
+
* @return plays: 0 = forever
|
|
79
|
+
*/
|
|
80
|
+
export declare function decodeAnimation(array: Uint8Array, onlyFirstFrame?: boolean): Promise<{
|
|
81
|
+
width: number;
|
|
82
|
+
height: number;
|
|
83
|
+
plays: number;
|
|
84
|
+
frames: APNGFrame[];
|
|
85
|
+
}>;
|
|
86
|
+
/**
|
|
87
|
+
* Composites APNG frames onto the canvas, as the APNG specification describes (the integer "over" operation of its
|
|
88
|
+
* blend_op section, https://wiki.mozilla.org/APNG_Specification)
|
|
89
|
+
*/
|
|
90
|
+
export declare function compositeAnimation({ width, height, frames }: {
|
|
91
|
+
width: number;
|
|
92
|
+
height: number;
|
|
93
|
+
frames: APNGFrame[];
|
|
94
|
+
}): Array<{
|
|
95
|
+
pixels: Uint8Array;
|
|
96
|
+
duration: number;
|
|
97
|
+
}>;
|