imagescript 3.16.1 → 3.18.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 +4 -4
- package/dist/animation-encoder.d.ts +37 -36
- package/dist/animation-encoder.js +8 -5
- package/dist/animation.d.ts +61 -14
- package/dist/animation.js +51 -19
- package/dist/codecs/gif.js +1 -1
- package/dist/codecs/jxl.js +1 -1
- package/dist/codecs/png.js +3 -2
- package/dist/codecs/webp.js +6 -2
- package/dist/colors.d.ts +79 -0
- package/dist/colors.js +161 -0
- package/dist/frame.d.ts +80 -15
- package/dist/frame.js +17 -16
- package/dist/gainmap.d.ts +16 -0
- package/dist/gainmap.js +3 -2
- package/dist/gif.d.ts +2 -1
- package/dist/image-type.d.ts +52 -13
- package/dist/image.d.ts +322 -119
- package/dist/image.js +115 -195
- package/dist/index.d.ts +5 -4
- package/dist/index.js +3 -2
- package/dist/render/svg.js +2 -2
- package/dist/text-layout.d.ts +4 -2
- package/dist/version.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -30,8 +30,8 @@ Without a bundler, import it from jsDelivr in the browser:
|
|
|
30
30
|
import { Image } from 'https://cdn.jsdelivr.net/npm/imagescript@3/+esm';
|
|
31
31
|
|
|
32
32
|
const image = await Image.decode(await fetch('photo.jpg').then(r => r.arrayBuffer()));
|
|
33
|
-
image.resize(256
|
|
34
|
-
const url = URL.createObjectURL(new Blob([await image.
|
|
33
|
+
image.resize({ width: 256 });
|
|
34
|
+
const url = URL.createObjectURL(new Blob([await image.encodePNG()], { type: 'image/png' }));
|
|
35
35
|
document.body.append(Object.assign(document.createElement('img'), { src: url }));
|
|
36
36
|
</script>
|
|
37
37
|
```
|
|
@@ -45,8 +45,8 @@ import { Image } from 'imagescript';
|
|
|
45
45
|
import { readFile, writeFile } from 'node:fs/promises';
|
|
46
46
|
|
|
47
47
|
const image = await Image.decode(await readFile('input.jpg'));
|
|
48
|
-
image.resize(256
|
|
49
|
-
await writeFile('output.png', await image.
|
|
48
|
+
image.resize({ width: 256 }).roundCorners(16);
|
|
49
|
+
await writeFile('output.png', await image.encodePNG());
|
|
50
50
|
```
|
|
51
51
|
|
|
52
52
|
<p align="center">
|
|
@@ -1,39 +1,32 @@
|
|
|
1
1
|
import { Image } from './image.js';
|
|
2
2
|
import { Frame, FrameOptions } from './frame.js';
|
|
3
|
-
import {
|
|
4
|
-
/**
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
export
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
quality?: number;
|
|
31
|
-
}
|
|
32
|
-
/** Options of {@link Animation.apngEncoder} */
|
|
33
|
-
export interface APNGAnimationEncoderOptions extends AnimationEncoderOptions {
|
|
34
|
-
/** The zlib compression level (0-9), higher is smaller but slower. Default: `4` */
|
|
35
|
-
compression?: number;
|
|
36
|
-
}
|
|
3
|
+
import type { AnimationOptions, AnimationEncodeOptions, WebPAnimationEncodeOptions, GIFEncodeOptions, APNGEncodeOptions } from './animation.js';
|
|
4
|
+
/**
|
|
5
|
+
* The options all animation encoders of {@link Animation.webpEncoder} and the others have
|
|
6
|
+
* @deprecated {@link Animation.webpEncoder} and the others take the options of {@link Animation.encodeWebP} and the
|
|
7
|
+
* others ({@link AnimationEncodeOptions}) with the {@link AnimationOptions.loopCount}, this will be removed in 4.0
|
|
8
|
+
* @group Deprecated
|
|
9
|
+
*/
|
|
10
|
+
export type AnimationEncoderOptions = AnimationEncodeOptions & AnimationOptions;
|
|
11
|
+
/**
|
|
12
|
+
* Options of {@link Animation.webpEncoder}
|
|
13
|
+
* @deprecated Use {@link WebPAnimationEncodeOptions}, with {@link AnimationOptions.loopCount}: this will be removed
|
|
14
|
+
* in 4.0
|
|
15
|
+
* @group Deprecated
|
|
16
|
+
*/
|
|
17
|
+
export type WebPAnimationEncoderOptions = WebPAnimationEncodeOptions & AnimationOptions;
|
|
18
|
+
/**
|
|
19
|
+
* Options of {@link Animation.gifEncoder}
|
|
20
|
+
* @deprecated Use {@link GIFEncodeOptions}, with {@link AnimationOptions.loopCount}: this will be removed in 4.0
|
|
21
|
+
* @group Deprecated
|
|
22
|
+
*/
|
|
23
|
+
export type GIFAnimationEncoderOptions = GIFEncodeOptions & AnimationOptions;
|
|
24
|
+
/**
|
|
25
|
+
* Options of {@link Animation.apngEncoder}
|
|
26
|
+
* @deprecated Use {@link APNGEncodeOptions}, with {@link AnimationOptions.loopCount}: this will be removed in 4.0
|
|
27
|
+
* @group Deprecated
|
|
28
|
+
*/
|
|
29
|
+
export type APNGAnimationEncoderOptions = APNGEncodeOptions & AnimationOptions;
|
|
37
30
|
/**
|
|
38
31
|
* Encodes an animation a frame at a time, without keeping all frames in memory: see {@link Animation.webpEncoder},
|
|
39
32
|
* {@link Animation.gifEncoder} and {@link Animation.apngEncoder}. The file is the one an {@link Animation} of the
|
|
@@ -43,14 +36,22 @@ export interface APNGAnimationEncoderOptions extends AnimationEncoderOptions {
|
|
|
43
36
|
* format depends on all of them
|
|
44
37
|
*/
|
|
45
38
|
export declare class AnimationEncoder {
|
|
46
|
-
readonly width: number;
|
|
47
|
-
readonly height: number;
|
|
48
39
|
private readonly format;
|
|
49
40
|
private optimizer;
|
|
50
41
|
private queue;
|
|
51
42
|
private count;
|
|
52
43
|
private finished;
|
|
44
|
+
/** The canvas width */
|
|
45
|
+
readonly width: number;
|
|
46
|
+
/** The canvas height */
|
|
47
|
+
readonly height: number;
|
|
53
48
|
/** How many frames were added */
|
|
49
|
+
get frameCount(): number;
|
|
50
|
+
/**
|
|
51
|
+
* How many frames were added
|
|
52
|
+
* @deprecated Use {@link AnimationEncoder.frameCount}, `frames` will be removed in 4.0
|
|
53
|
+
* @group Deprecated
|
|
54
|
+
*/
|
|
54
55
|
get frames(): number;
|
|
55
56
|
/**
|
|
56
57
|
* Adds the next frame: a {@link Frame} (with its duration, offsets and disposal) or an image (a frame of 100 ms by
|
|
@@ -7,21 +7,24 @@ import { Optimizer, Compositor, coverCanvas, fillUnchanged } from "./util/optimi
|
|
|
7
7
|
import { compressAsync, decompressAsync } from "./codecs/zlib.js";
|
|
8
8
|
import { resolveMetadata, pngText, exifData, gifComments } from "./metadata.js";
|
|
9
9
|
import { finite, compressionLevel } from "./util/options.js";
|
|
10
|
-
const toPlays = (loopCount) => loopCount < 0 ? 0 : loopCount + 1;
|
|
10
|
+
const toPlays = (loopCount) => loopCount < 0 || loopCount === Infinity ? 0 : loopCount + 1;
|
|
11
11
|
export class AnimationEncoder {
|
|
12
|
-
width;
|
|
13
|
-
height;
|
|
14
12
|
format;
|
|
15
13
|
optimizer;
|
|
16
14
|
queue = Promise.resolve();
|
|
17
15
|
count = 0;
|
|
18
16
|
finished = false;
|
|
17
|
+
width;
|
|
18
|
+
height;
|
|
19
19
|
constructor(width, height, format, optimize) {
|
|
20
|
+
this.format = format;
|
|
20
21
|
this.width = width;
|
|
21
22
|
this.height = height;
|
|
22
|
-
this.format = format;
|
|
23
23
|
this.optimizer = optimize ? new Optimizer(width, height, format.optimize) : null;
|
|
24
24
|
}
|
|
25
|
+
get frameCount() {
|
|
26
|
+
return this.count;
|
|
27
|
+
}
|
|
25
28
|
get frames() {
|
|
26
29
|
return this.count;
|
|
27
30
|
}
|
|
@@ -171,7 +174,7 @@ export class AnimationEncoder {
|
|
|
171
174
|
function canvas(width, height, loopCount) {
|
|
172
175
|
if (!Number.isInteger(width) || !Number.isInteger(height) || width < 1 || height < 1)
|
|
173
176
|
throw new RangeError(`Invalid canvas size: ${width}x${height}`);
|
|
174
|
-
if (!Number.isInteger(loopCount) || loopCount < -1)
|
|
177
|
+
if (loopCount !== Infinity && (!Number.isInteger(loopCount) || loopCount < -1))
|
|
175
178
|
throw new RangeError('Invalid loop count');
|
|
176
179
|
}
|
|
177
180
|
function webpFrame(f) {
|
package/dist/animation.d.ts
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
import { BinaryData } from './util/mem.js';
|
|
2
|
-
import {
|
|
2
|
+
import type { ToneMapping } from './codecs/hdr.js';
|
|
3
|
+
import { ResizeMode, ResizeOptions, ResizeToOptions } from './image.js';
|
|
3
4
|
import { Frame } from './frame.js';
|
|
4
5
|
import { Metadata } from './metadata.js';
|
|
5
|
-
import { AnimationEncoder
|
|
6
|
+
import { AnimationEncoder } from './animation-encoder.js';
|
|
6
7
|
/** Options of {@link Animation} */
|
|
7
8
|
export interface AnimationOptions {
|
|
8
|
-
/**
|
|
9
|
+
/**
|
|
10
|
+
* How many times to repeat the animation (`Infinity` = forever, 0 = play once, 1 = play twice, ...). `-1` still
|
|
11
|
+
* means forever, it will be removed in 4.0. Default: forever
|
|
12
|
+
*/
|
|
9
13
|
loopCount?: number;
|
|
10
14
|
}
|
|
11
15
|
/** Options of {@link Animation.decode} */
|
|
@@ -22,6 +26,22 @@ export interface AnimationDecodeOptions {
|
|
|
22
26
|
applyColorProfile?: boolean;
|
|
23
27
|
/** At most this many worker threads decode in parallel, like `threads` of {@link Image.decode}. Default: the processors available, at most 8 */
|
|
24
28
|
threads?: number;
|
|
29
|
+
/**
|
|
30
|
+
* Whether to rotate/mirror the frames as the orientation tag (Exif, JPEG XL's header, PNG's eXIf chunk or XMP)
|
|
31
|
+
* says, like `applyOrientation` of {@link Image.decode}: APNGs, animated WebPs and JPEG XLs. Default: `true`
|
|
32
|
+
*/
|
|
33
|
+
applyOrientation?: boolean;
|
|
34
|
+
/**
|
|
35
|
+
* How HDR (PQ or HLG) JPEG XL animations, whose light can be brighter than (SDR) white, become 8 bit sRGB, like
|
|
36
|
+
* `toneMapping` of {@link Image.decode}: `'reinhard'` compresses the brightest parts, `'clamp'` clamps everything
|
|
37
|
+
* brighter than white. Default: `'reinhard'`
|
|
38
|
+
*/
|
|
39
|
+
toneMapping?: ToneMapping;
|
|
40
|
+
/**
|
|
41
|
+
* Whether to read the gain map (jhgm box) of JPEG XL animations into {@link Image.gainMap} of every frame, like
|
|
42
|
+
* `gainMap` of {@link Image.decode}. Default: `true`
|
|
43
|
+
*/
|
|
44
|
+
gainMap?: boolean;
|
|
25
45
|
}
|
|
26
46
|
/** The options all animation encoders have */
|
|
27
47
|
export interface AnimationEncodeOptions {
|
|
@@ -67,7 +87,11 @@ export interface APNGEncodeOptions extends AnimationEncodeOptions {
|
|
|
67
87
|
* plain arrays.
|
|
68
88
|
*/
|
|
69
89
|
export declare class Animation extends Array<Frame> {
|
|
70
|
-
/**
|
|
90
|
+
/**
|
|
91
|
+
* How many times to repeat the animation (`Infinity` = forever, 0 = play once, 1 = play twice, ...). `-1` still
|
|
92
|
+
* means forever, it will be removed in 4.0, when `Infinity` is what animations that loop forever have (animations
|
|
93
|
+
* that loop forever have `-1` until then, also the decoded ones)
|
|
94
|
+
*/
|
|
71
95
|
loopCount: number;
|
|
72
96
|
/**
|
|
73
97
|
* Creates a new animation
|
|
@@ -78,7 +102,8 @@ export declare class Animation extends Array<Frame> {
|
|
|
78
102
|
/**
|
|
79
103
|
* Creates a new animation
|
|
80
104
|
* @param frames The frames to create the animation from
|
|
81
|
-
* @param loopCount How many times to repeat the animation (
|
|
105
|
+
* @param loopCount How many times to repeat the animation (`Infinity` = forever, 0 = play once, 1 = play twice, ...)
|
|
106
|
+
* @deprecated Pass {@link AnimationOptions} instead, the positional arguments will be removed in 4.0
|
|
82
107
|
*/
|
|
83
108
|
constructor(frames: Frame[], loopCount?: number);
|
|
84
109
|
/**
|
|
@@ -86,7 +111,10 @@ export declare class Animation extends Array<Frame> {
|
|
|
86
111
|
* setting an index), and can be replaced as a whole
|
|
87
112
|
*/
|
|
88
113
|
get frames(): Frame[];
|
|
89
|
-
/**
|
|
114
|
+
/**
|
|
115
|
+
* Replaces the animation's frames
|
|
116
|
+
* @param frames The new frames (copied into the animation, the array itself isn't kept)
|
|
117
|
+
*/
|
|
90
118
|
set frames(frames: Frame[]);
|
|
91
119
|
/** The animation's width */
|
|
92
120
|
get width(): number;
|
|
@@ -99,20 +127,36 @@ export declare class Animation extends Array<Frame> {
|
|
|
99
127
|
/**
|
|
100
128
|
* Returns a new animation of the same class containing a range of this one's frames
|
|
101
129
|
* (same semantics as Array.prototype.slice)
|
|
130
|
+
* @param start The index of the first frame (negative counts from the end). Default: `0`
|
|
131
|
+
* @param end The index after the last frame (negative counts from the end). Default: the frame count
|
|
102
132
|
* @deprecated Use `new Animation(animation.frames.slice(start, end), animation.loopCount)`: in 4.0 animations
|
|
103
133
|
* aren't arrays anymore
|
|
134
|
+
* @group Deprecated
|
|
104
135
|
*/
|
|
105
136
|
slice(start?: number, end?: number): this;
|
|
106
137
|
/**
|
|
107
138
|
* Resizes the animation to the given dimensions, scaling every frame and its offsets proportionally.
|
|
108
|
-
*
|
|
139
|
+
* Pass {@link ResizeToOptions} with only a width or a height to keep the aspect ratio.
|
|
140
|
+
* @param width The new width
|
|
141
|
+
* @param height The new height
|
|
142
|
+
* @param options See {@link ResizeOptions}
|
|
109
143
|
*/
|
|
110
144
|
resize(width: number, height: number, options?: ResizeOptions): this;
|
|
111
145
|
/**
|
|
112
146
|
* Resizes the animation to the given dimensions, scaling every frame and its offsets proportionally.
|
|
113
|
-
*
|
|
147
|
+
* Pass {@link ResizeToOptions} with only a width or a height to keep the aspect ratio.
|
|
148
|
+
* @param width The new width
|
|
149
|
+
* @param height The new height
|
|
150
|
+
* @param mode The resizing mode to use. Default: {@link ResizeModes.BILINEAR}
|
|
151
|
+
* @deprecated Pass {@link ResizeOptions} with `mode` instead, the positional mode will be removed in 4.0
|
|
114
152
|
*/
|
|
115
153
|
resize(width: number, height: number, mode?: ResizeMode): this;
|
|
154
|
+
/**
|
|
155
|
+
* Resizes the animation, scaling every frame and its offsets proportionally: to the given width or height (the
|
|
156
|
+
* other side keeps the aspect ratio), or to both
|
|
157
|
+
* @param options The size and how to resize, see {@link ResizeToOptions}
|
|
158
|
+
*/
|
|
159
|
+
resize(options: ResizeToOptions): this;
|
|
116
160
|
/**
|
|
117
161
|
* Encodes the animation as GIF. Like all encoders, it stores each frame as the area that changed since the frame
|
|
118
162
|
* before and merges identical frames (unless the `optimize` option is false), so the file decodes to what the
|
|
@@ -127,6 +171,7 @@ export declare class Animation extends Array<Frame> {
|
|
|
127
171
|
* animation shows, but not necessarily to the same frames. Semi-transparent pixels become opaque or transparent
|
|
128
172
|
* after the frames are composited
|
|
129
173
|
* @param quality 1-100, scales the palette size from 8 colors (1) to 256 colors (90 and above)
|
|
174
|
+
* @deprecated Pass {@link GIFEncodeOptions} instead, the positional arguments will be removed in 4.0
|
|
130
175
|
*/
|
|
131
176
|
encodeGIF(quality?: number): Promise<Uint8Array>;
|
|
132
177
|
/**
|
|
@@ -143,6 +188,7 @@ export declare class Animation extends Array<Frame> {
|
|
|
143
188
|
* merged (unless the `optimize` option is false): the file shows exactly the same animation, but doesn't
|
|
144
189
|
* necessarily decode to the same frames
|
|
145
190
|
* @param compression The zlib compression level (0-9), higher is smaller but slower. Default: `4`
|
|
191
|
+
* @deprecated Pass {@link APNGEncodeOptions} instead, the positional arguments will be removed in 4.0
|
|
146
192
|
*/
|
|
147
193
|
encodeAPNG(compression?: number): Promise<Uint8Array>;
|
|
148
194
|
/**
|
|
@@ -158,26 +204,26 @@ export declare class Animation extends Array<Frame> {
|
|
|
158
204
|
* with `{ duration }`), then `await encoder.finish()` for the file
|
|
159
205
|
* @param width The canvas width
|
|
160
206
|
* @param height The canvas height
|
|
161
|
-
* @param options See {@link
|
|
207
|
+
* @param options See {@link WebPAnimationEncodeOptions}, and {@link AnimationOptions.loopCount}
|
|
162
208
|
*/
|
|
163
|
-
static webpEncoder(width: number, height: number, options?:
|
|
209
|
+
static webpEncoder(width: number, height: number, options?: WebPAnimationEncodeOptions & AnimationOptions): AnimationEncoder;
|
|
164
210
|
/**
|
|
165
211
|
* An encoder that makes an animated GIF a frame at a time, like {@link Animation.encodeGIF} (the same file),
|
|
166
212
|
* without keeping the frames in memory: `await encoder.add(frame)` for each frame, then `await encoder.finish()`
|
|
167
213
|
* @param width The canvas width
|
|
168
214
|
* @param height The canvas height
|
|
169
|
-
* @param options See {@link
|
|
215
|
+
* @param options See {@link GIFEncodeOptions}, and {@link AnimationOptions.loopCount}
|
|
170
216
|
*/
|
|
171
|
-
static gifEncoder(width: number, height: number, options?:
|
|
217
|
+
static gifEncoder(width: number, height: number, options?: GIFEncodeOptions & AnimationOptions): AnimationEncoder;
|
|
172
218
|
/**
|
|
173
219
|
* An encoder that makes an APNG a frame at a time, like {@link Animation.encodeAPNG} (the same file), keeping the
|
|
174
220
|
* frames only compressed (the pixel format depends on all of them): `await encoder.add(frame)` for each frame,
|
|
175
221
|
* then `await encoder.finish()`
|
|
176
222
|
* @param width The canvas width
|
|
177
223
|
* @param height The canvas height
|
|
178
|
-
* @param options See {@link
|
|
224
|
+
* @param options See {@link APNGEncodeOptions}, and {@link AnimationOptions.loopCount}
|
|
179
225
|
*/
|
|
180
|
-
static apngEncoder(width: number, height: number, options?:
|
|
226
|
+
static apngEncoder(width: number, height: number, options?: APNGEncodeOptions & AnimationOptions): AnimationEncoder;
|
|
181
227
|
/**
|
|
182
228
|
* Decodes an animated GIF, APNG, WebP or JPEG XL into fully composited frames (still images become a single frame)
|
|
183
229
|
* @param input The binary data to decode
|
|
@@ -188,6 +234,7 @@ export declare class Animation extends Array<Frame> {
|
|
|
188
234
|
* Decodes an animated GIF, APNG, WebP or JPEG XL into fully composited frames (still images become a single frame)
|
|
189
235
|
* @param input The binary data to decode
|
|
190
236
|
* @param onlyExtractFirstFrame Whether to stop decoding after the first frame
|
|
237
|
+
* @deprecated Pass {@link AnimationDecodeOptions} instead, the positional arguments will be removed in 4.0
|
|
191
238
|
*/
|
|
192
239
|
static decode(input: BinaryData, onlyExtractFirstFrame?: boolean): Promise<Animation>;
|
|
193
240
|
}
|
package/dist/animation.js
CHANGED
|
@@ -5,19 +5,20 @@ import * as webp from "./codecs/webp.js";
|
|
|
5
5
|
import * as jxl from "./codecs/jxl.js";
|
|
6
6
|
import { availableThreads } from "./util/threads.js";
|
|
7
7
|
import { view } from "./util/mem.js";
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
8
|
+
import { GainMap } from "./gainmap.js";
|
|
9
|
+
import { Image, ResizeModes, resizeArguments } from "./image.js";
|
|
10
|
+
import { Frame, FrameDisposalModes } from "./frame.js";
|
|
10
11
|
import { ImageType } from "./image-type.js";
|
|
11
12
|
import { optimize } from "./util/optimize.js";
|
|
12
13
|
import { resolveMetadata, pngText, exifData, gifComments } from "./metadata.js";
|
|
13
14
|
import { isOptions, finite, compressionLevel } from "./util/options.js";
|
|
14
15
|
import { MAX_PIXELS, decoding } from "./util/limits.js";
|
|
15
16
|
import { AnimationEncoder } from "./animation-encoder.js";
|
|
16
|
-
const toPlays = (loopCount) => loopCount < 0 ? 0 : loopCount + 1;
|
|
17
|
+
const toPlays = (loopCount) => loopCount < 0 || loopCount === Infinity ? 0 : loopCount + 1;
|
|
17
18
|
const fromPlays = (plays) => plays === 0 ? -1 : plays - 1;
|
|
18
19
|
function canvasFrames(width, height, frames) {
|
|
19
20
|
return frames.map(({ pixels, duration }) => {
|
|
20
|
-
const frame = new Frame(width, height, duration, 0, 0,
|
|
21
|
+
const frame = new Frame(width, height, duration, 0, 0, FrameDisposalModes.BACKGROUND);
|
|
21
22
|
frame.bitmap = pixels;
|
|
22
23
|
return frame;
|
|
23
24
|
});
|
|
@@ -32,7 +33,7 @@ export class Animation extends Array {
|
|
|
32
33
|
for (let i = 0; i < this.length; i++)
|
|
33
34
|
if (!(this[i] instanceof Frame))
|
|
34
35
|
throw new TypeError(`Frame ${i} is not an instance of Frame`);
|
|
35
|
-
if (finite(loopCount, 'loop count') < -1 || !Number.isInteger(Number(loopCount)))
|
|
36
|
+
if (loopCount !== Infinity && (finite(loopCount, 'loop count') < -1 || !Number.isInteger(Number(loopCount))))
|
|
36
37
|
throw new RangeError(`Invalid loop count: ${loopCount}`);
|
|
37
38
|
this.loopCount = loopCount;
|
|
38
39
|
}
|
|
@@ -77,19 +78,21 @@ export class Animation extends Array {
|
|
|
77
78
|
const Class = this.constructor;
|
|
78
79
|
return new Class(Array.prototype.slice.call(this, start, end), this.loopCount);
|
|
79
80
|
}
|
|
80
|
-
resize(
|
|
81
|
+
resize(widthOrOptions, heightArgument, modeArgument) {
|
|
82
|
+
let [width, height, modeOrOptions] = resizeArguments(widthOrOptions, heightArgument, modeArgument);
|
|
81
83
|
const originalWidth = this.width, originalHeight = this.height;
|
|
82
|
-
if (width ===
|
|
84
|
+
if (width === ResizeModes.AUTO && height === ResizeModes.AUTO)
|
|
83
85
|
throw new Error('RESIZE_AUTO can only be used for either width or height, not for both');
|
|
84
|
-
if (width ===
|
|
86
|
+
if (width === ResizeModes.AUTO)
|
|
85
87
|
width = originalWidth / originalHeight * height;
|
|
86
|
-
if (height ===
|
|
88
|
+
if (height === ResizeModes.AUTO)
|
|
87
89
|
height = originalHeight / originalWidth * width;
|
|
88
90
|
const sx = width / originalWidth, sy = height / originalHeight;
|
|
89
91
|
for (const frame of this) {
|
|
90
92
|
const left = Math.round(frame.xOffset * sx), top = Math.round(frame.yOffset * sy);
|
|
91
93
|
const right = Math.round((frame.xOffset + frame.width) * sx), bottom = Math.round((frame.yOffset + frame.height) * sy);
|
|
92
|
-
Image.prototype.resize
|
|
94
|
+
Image.prototype.resize
|
|
95
|
+
.call(frame, Math.max(1, right - left), Math.max(1, bottom - top), modeOrOptions);
|
|
93
96
|
frame.xOffset = left;
|
|
94
97
|
frame.yOffset = top;
|
|
95
98
|
}
|
|
@@ -191,11 +194,20 @@ export class Animation extends Array {
|
|
|
191
194
|
throw new RangeError(`threads must be a positive integer, not ${threads}`);
|
|
192
195
|
if (!(Number(maxPixels) > 0))
|
|
193
196
|
throw new RangeError(`maxPixels must be a positive number, not ${maxPixels}`);
|
|
197
|
+
const { toneMapping = 'reinhard' } = onlyFirstFrameOrOptions;
|
|
198
|
+
if (toneMapping !== 'reinhard' && toneMapping !== 'clamp')
|
|
199
|
+
throw new TypeError(`toneMapping must be 'reinhard' or 'clamp', not ${String(toneMapping)}`);
|
|
194
200
|
}
|
|
195
201
|
return decoding(() => Animation.__decode__(input, onlyFirstFrameOrOptions));
|
|
196
202
|
}
|
|
197
203
|
static async __decode__(input, onlyFirstFrameOrOptions) {
|
|
198
|
-
const { onlyFirstFrame: onlyExtractFirstFrame = false, maxPixels = MAX_PIXELS, applyColorProfile = true, threads = Math.max(1, Math.min(availableThreads(), 8)) } = isOptions(onlyFirstFrameOrOptions) ? onlyFirstFrameOrOptions : { onlyFirstFrame: onlyFirstFrameOrOptions };
|
|
204
|
+
const { onlyFirstFrame: onlyExtractFirstFrame = false, maxPixels = MAX_PIXELS, applyColorProfile = true, threads = Math.max(1, Math.min(availableThreads(), 8)), applyOrientation = true, toneMapping = 'reinhard', gainMap = true } = isOptions(onlyFirstFrameOrOptions) ? onlyFirstFrameOrOptions : { onlyFirstFrame: onlyFirstFrameOrOptions };
|
|
205
|
+
const orient = (frames, orientation) => {
|
|
206
|
+
if (applyOrientation && orientation > 1)
|
|
207
|
+
for (const frame of frames)
|
|
208
|
+
frame.__orient__(orientation);
|
|
209
|
+
return frames;
|
|
210
|
+
};
|
|
199
211
|
const profile = (frames, icc, space) => {
|
|
200
212
|
const transform = !applyColorProfile ? null : icc ? profileTransform(icc) : space ? colorSpaceTransform(space.xy, space.curve) : null;
|
|
201
213
|
if (transform)
|
|
@@ -209,35 +221,55 @@ export class Animation extends Array {
|
|
|
209
221
|
return Animation.__fromGIF__(data, onlyExtractFirstFrame, Animation, maxPixels);
|
|
210
222
|
if (type === 'webp') {
|
|
211
223
|
const decoded = webp.decodeAnimation(data, onlyExtractFirstFrame, maxPixels);
|
|
212
|
-
const frames = profile(canvasFrames(decoded.width, decoded.height, webp.composite(decoded)), decoded.icc);
|
|
224
|
+
const frames = orient(profile(canvasFrames(decoded.width, decoded.height, webp.composite(decoded)), decoded.icc), decoded.orientation);
|
|
213
225
|
return new Animation(frames, fromPlays(decoded.loopCount));
|
|
214
226
|
}
|
|
215
227
|
if (type === 'png' && !png.isAnimated(data)) {
|
|
216
|
-
const image = await Image.decode(data, { maxPixels, applyOrientation
|
|
228
|
+
const image = await Image.decode(data, { maxPixels, applyOrientation, applyColorProfile });
|
|
217
229
|
return new Animation(canvasFrames(image.width, image.height, [{ pixels: image.bitmap, duration: 0 }]), -1);
|
|
218
230
|
}
|
|
219
231
|
if (type === 'png') {
|
|
220
232
|
const decoded = await png.decodeAnimation(data, onlyExtractFirstFrame, maxPixels);
|
|
221
|
-
const frames = profile(canvasFrames(decoded.width, decoded.height, png.compositeAnimation(decoded)), decoded.icc);
|
|
233
|
+
const frames = orient(profile(canvasFrames(decoded.width, decoded.height, png.compositeAnimation(decoded)), decoded.icc), decoded.orientation);
|
|
222
234
|
return new Animation(frames, decoded.frames.length > 1 ? fromPlays(decoded.plays) : -1);
|
|
223
235
|
}
|
|
224
236
|
if (type === 'jxl') {
|
|
225
237
|
const frames = [];
|
|
226
238
|
let loopCount = -1, icc, space;
|
|
227
|
-
|
|
228
|
-
|
|
239
|
+
let orientation = 1;
|
|
240
|
+
for await (const decoded of jxl.frames(data, maxPixels, toneMapping, threads)) {
|
|
241
|
+
const frame = new Frame(decoded.width, decoded.height, decoded.duration, 0, 0, FrameDisposalModes.BACKGROUND);
|
|
229
242
|
frame.bitmap = decoded.pixels;
|
|
230
|
-
frames.push(frame
|
|
243
|
+
frames.push(frame);
|
|
244
|
+
orientation = decoded.orientation;
|
|
231
245
|
loopCount = fromPlays(decoded.loopCount);
|
|
232
246
|
icc = decoded.icc;
|
|
233
247
|
space = decoded.colorSpace;
|
|
234
248
|
if (onlyExtractFirstFrame)
|
|
235
249
|
break;
|
|
236
250
|
}
|
|
237
|
-
|
|
251
|
+
const map = gainMap && data[0] !== 0xff ? await jxl.decodeGainMap(data, maxPixels).catch(() => undefined) : undefined;
|
|
252
|
+
if (map)
|
|
253
|
+
for (const frame of frames) {
|
|
254
|
+
const image = new Image(map.width, map.height);
|
|
255
|
+
image.bitmap = map.pixels.slice();
|
|
256
|
+
frame.gainMap = GainMap.__from_pixels__(image, map.params);
|
|
257
|
+
}
|
|
258
|
+
for (const frame of orient(profile(frames, icc, space), orientation)) {
|
|
259
|
+
const mapOfFrame = frame.gainMap;
|
|
260
|
+
if (!mapOfFrame || !applyOrientation || orientation <= 1)
|
|
261
|
+
continue;
|
|
262
|
+
try {
|
|
263
|
+
mapOfFrame.image = mapOfFrame.image.__orient__(orientation);
|
|
264
|
+
}
|
|
265
|
+
catch {
|
|
266
|
+
frame.gainMap = null;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
return new Animation(frames, frames.length > 1 ? loopCount : -1);
|
|
238
270
|
}
|
|
239
271
|
if (type)
|
|
240
|
-
return new Animation([Frame.from(await Image.decode(data, { maxPixels, applyColorProfile }), 0)]);
|
|
272
|
+
return new Animation([Frame.from(await Image.decode(data, { maxPixels, applyOrientation, applyColorProfile, toneMapping, gainMap, threads }), 0)]);
|
|
241
273
|
throw new Error('Unsupported image type');
|
|
242
274
|
}
|
|
243
275
|
static __fromGIF__(data, onlyExtractFirstFrame, Class, maxPixels = MAX_PIXELS) {
|
package/dist/codecs/gif.js
CHANGED
|
@@ -29,7 +29,7 @@ export function header(width, height, loopCount, comments) {
|
|
|
29
29
|
out.bytes(0x21, 0xff, 0x0b);
|
|
30
30
|
out.string('NETSCAPE2.0');
|
|
31
31
|
out.bytes(0x03, 0x01);
|
|
32
|
-
out.u16(loopCount === -1 ? 0 : loopCount);
|
|
32
|
+
out.u16(loopCount === -1 || loopCount === Infinity ? 0 : loopCount);
|
|
33
33
|
out.bytes(0x00);
|
|
34
34
|
}
|
|
35
35
|
for (const comment of comments) {
|
package/dist/codecs/jxl.js
CHANGED
|
@@ -157,7 +157,7 @@ export async function decode(data, maxPixels = MAX_PIXELS, toneMapping = 'reinha
|
|
|
157
157
|
}
|
|
158
158
|
throw new Error('JPEG XL: no frame shown');
|
|
159
159
|
}
|
|
160
|
-
async function decodeGainMap(data, maxPixels) {
|
|
160
|
+
export async function decodeGainMap(data, maxPixels) {
|
|
161
161
|
for (const { type, content: c } of boxesOf(data)) {
|
|
162
162
|
if (type !== 'jhgm')
|
|
163
163
|
continue;
|
package/dist/codecs/png.js
CHANGED
|
@@ -543,10 +543,11 @@ export function isAnimated(array) {
|
|
|
543
543
|
export async function decodeAnimation(array, onlyFirstFrame = false, maxPixels = MAX_PIXELS) {
|
|
544
544
|
const p = parse(array);
|
|
545
545
|
const { width, height } = p.header;
|
|
546
|
+
const orientation = p.exif ? exifOrientation(p.exif) : await textOrientation(p.text);
|
|
546
547
|
checkPixels(width, height, maxPixels, !p.animation || p.animationError || onlyFirstFrame ? 1 : Math.max(1, p.frames.length));
|
|
547
548
|
if (!p.animation || p.animationError) {
|
|
548
549
|
const pixels = await decodeImageData(p, p.idat, width, height);
|
|
549
|
-
return { width, height, plays: 0, frames: [{ x: 0, y: 0, width, height, duration: 0, dispose: 0, blend: 0, pixels }], icc: await profile(p.iccp) };
|
|
550
|
+
return { width, height, plays: 0, frames: [{ x: 0, y: 0, width, height, duration: 0, dispose: 0, blend: 0, pixels }], icc: await profile(p.iccp), orientation };
|
|
550
551
|
}
|
|
551
552
|
const frames = [];
|
|
552
553
|
for (const { control: c, data } of p.frames) {
|
|
@@ -561,7 +562,7 @@ export async function decodeAnimation(array, onlyFirstFrame = false, maxPixels =
|
|
|
561
562
|
if (onlyFirstFrame)
|
|
562
563
|
break;
|
|
563
564
|
}
|
|
564
|
-
return { width, height, plays: p.animation.plays, frames, icc: await profile(p.iccp) };
|
|
565
|
+
return { width, height, plays: p.animation.plays, frames, icc: await profile(p.iccp), orientation };
|
|
565
566
|
}
|
|
566
567
|
export function compositeAnimation({ width, height, frames }) {
|
|
567
568
|
const canvas = new Uint8Array(width * height * 4);
|
package/dist/codecs/webp.js
CHANGED
|
@@ -141,7 +141,7 @@ export function decodeAnimation(data, onlyFirstFrame = false, maxPixels = MAX_PI
|
|
|
141
141
|
const header = list[0];
|
|
142
142
|
if (!header || header.type !== VP8X || !list.some(c => c.type === ANMF)) {
|
|
143
143
|
const { width, height, pixels } = decodeBitstream(list, maxPixels);
|
|
144
|
-
return { width, height, loopCount: 0, background: 0, frames: [{ x: 0, y: 0, width, height, duration: 0, blend: false, dispose: false, pixels }], icc: list.find(c => c.type === ICCP)?.data };
|
|
144
|
+
return { width, height, loopCount: 0, background: 0, frames: [{ x: 0, y: 0, width, height, duration: 0, blend: false, dispose: false, pixels }], icc: list.find(c => c.type === ICCP)?.data, orientation: orientationOf(list) };
|
|
145
145
|
}
|
|
146
146
|
if (header.data.length < 10)
|
|
147
147
|
throw new Error('WebP: invalid VP8X chunk');
|
|
@@ -176,7 +176,11 @@ export function decodeAnimation(data, onlyFirstFrame = false, maxPixels = MAX_PI
|
|
|
176
176
|
}
|
|
177
177
|
if (!frames.length)
|
|
178
178
|
throw new Error('WebP: animation without frames');
|
|
179
|
-
return { width, height, loopCount, background, frames, icc: list.find(c => c.type === ICCP)?.data };
|
|
179
|
+
return { width, height, loopCount, background, frames, icc: list.find(c => c.type === ICCP)?.data, orientation: orientationOf(list) };
|
|
180
|
+
}
|
|
181
|
+
function orientationOf(list) {
|
|
182
|
+
const exif = list.find(c => c.type === EXIF);
|
|
183
|
+
return exif ? exifOrientation(exif.data) : 1;
|
|
180
184
|
}
|
|
181
185
|
export function composite({ width, height, frames }) {
|
|
182
186
|
const canvas = new Uint8Array(width * height * 4);
|
package/dist/colors.d.ts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import type { Color } from './image.js';
|
|
2
|
+
/** Clamps and rounds a channel value to 0..255 */
|
|
3
|
+
export declare function channel(value: number): number;
|
|
4
|
+
export declare function wrapHue(h: number): number;
|
|
5
|
+
export declare function hue2rgb(p: number, q: number, t: number): number;
|
|
6
|
+
/**
|
|
7
|
+
* Converts an RGBA value to an array of HSLA values
|
|
8
|
+
* @param r {number} (0..255)
|
|
9
|
+
* @param g {number} (0..255)
|
|
10
|
+
* @param b {number} (0..255)
|
|
11
|
+
* @param a {number} (0..255)
|
|
12
|
+
* @returns The HSLA values ([H, S, L, A])
|
|
13
|
+
*/
|
|
14
|
+
export declare function rgbaToHSLA(r: number, g: number, b: number, a: number): [h: number, s: number, l: number, a: number];
|
|
15
|
+
/**
|
|
16
|
+
* Conversions between RGBA, HSLA and the 32-bit color values ({@link Color}, `0xRRGGBBAA`) that the image methods take
|
|
17
|
+
* and return, and gradients between colors
|
|
18
|
+
*/
|
|
19
|
+
export declare class Colors {
|
|
20
|
+
private constructor();
|
|
21
|
+
/**
|
|
22
|
+
* Converts RGBA components to an RGBA value, values are rounded and clamped to 0..255
|
|
23
|
+
* @param r red (0..255)
|
|
24
|
+
* @param g green (0..255)
|
|
25
|
+
* @param b blue (0..255)
|
|
26
|
+
* @param a alpha (0..255)
|
|
27
|
+
* @returns RGBA value
|
|
28
|
+
*/
|
|
29
|
+
static fromRGBA(r: number, g: number, b: number, a: number): Color;
|
|
30
|
+
/**
|
|
31
|
+
* Converts RGB components to an RGBA value (assuming alpha = 255)
|
|
32
|
+
* @param r red (0..255)
|
|
33
|
+
* @param g green (0..255)
|
|
34
|
+
* @param b blue (0..255)
|
|
35
|
+
* @returns RGBA value
|
|
36
|
+
*/
|
|
37
|
+
static fromRGB(r: number, g: number, b: number): Color;
|
|
38
|
+
/**
|
|
39
|
+
* Converts HSLA colors to RGBA colors
|
|
40
|
+
* @param h hue (0..1)
|
|
41
|
+
* @param s saturation (0..1)
|
|
42
|
+
* @param l lightness (0..1)
|
|
43
|
+
* @param a opacity (0..1)
|
|
44
|
+
* @returns color
|
|
45
|
+
*/
|
|
46
|
+
static fromHSLA(h: number, s: number, l: number, a: number): Color;
|
|
47
|
+
/**
|
|
48
|
+
* Converts HSL colors to RGBA colors (assuming an opacity of 255)
|
|
49
|
+
* @param h hue (0..1)
|
|
50
|
+
* @param s saturation (0..1)
|
|
51
|
+
* @param l lightness (0..1)
|
|
52
|
+
* @returns color
|
|
53
|
+
*/
|
|
54
|
+
static fromHSL(h: number, s: number, l: number): Color;
|
|
55
|
+
/**
|
|
56
|
+
* Converts a color value to an array of RGBA values
|
|
57
|
+
* @param color The color value to convert
|
|
58
|
+
* @returns The RGBA values ([R, G, B, A])
|
|
59
|
+
*/
|
|
60
|
+
static toRGBA(color: Color): [r: number, g: number, b: number, a: number];
|
|
61
|
+
/**
|
|
62
|
+
* Converts a color value to an array of RGB values (ignoring the colors alpha)
|
|
63
|
+
* @param color The color value to convert
|
|
64
|
+
* @returns The RGB values ([R, G, B])
|
|
65
|
+
*/
|
|
66
|
+
static toRGB(color: Color): [r: number, g: number, b: number];
|
|
67
|
+
/**
|
|
68
|
+
* Converts a color to an array of HSLA values
|
|
69
|
+
* @param color The color value to convert
|
|
70
|
+
* @returns The HSLA values ([H, S, L, A]), H and S and L and A in 0..1
|
|
71
|
+
*/
|
|
72
|
+
static toHSLA(color: Color): [h: number, s: number, l: number, a: number];
|
|
73
|
+
/**
|
|
74
|
+
* Creates a multi-point gradient generator
|
|
75
|
+
* @param colors The gradient points to use (e.g. `{0: 0xff0000ff, 1: 0x00ff00ff}`)
|
|
76
|
+
* @return The gradient generator. The function argument is the position in the gradient (0..1).
|
|
77
|
+
*/
|
|
78
|
+
static gradient(colors: Record<number, Color>): (position: number) => Color;
|
|
79
|
+
}
|