Repository navigation
Expand file tree
/
Copy pathanimation.ts
More file actions
436 lines (392 loc) · 21 KB
/
Copy pathanimation.ts
File metadata and controls
436 lines (392 loc) · 21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
import * as gifCodec from './codecs/gif.ts';
import * as png from './codecs/png.ts';
import * as webp from './codecs/webp.ts';
import * as jxl from './codecs/jxl.ts';
import { BinaryData, view } from './util/mem.ts';
import { Image, ResizeMode, ResizeOptions } from './image.ts';
import { Frame } from './frame.ts';
import { ImageType } from './image-type.ts';
import { optimize, OptimizeOptions, OptimizedFrame } from './util/optimize.ts';
import { Metadata, resolveMetadata, pngText, exifData, gifComments } from './metadata.ts';
import { isOptions, finite } from './util/options.ts';
import { MAX_PIXELS, decoding } from './util/limits.ts';
// loop counts: ImageScript (like GIF) counts repetitions (-1 forever, 0 play once), APNG and WebP count plays (0 forever)
const toPlays = (loopCount: number): number => loopCount < 0 ? 0 : loopCount + 1;
const fromPlays = (plays: number): number => plays === 0 ? -1 : plays - 1;
// frames decoded fully composited, as full canvas frames
function canvasFrames(width: number, height: number, frames: Array<{ pixels: Uint8Array | Uint8ClampedArray, duration: number }>): Frame[] {
return frames.map(({ pixels, duration }) => {
const frame = new Frame(width, height, duration, 0, 0, Frame.DISPOSAL_BACKGROUND);
// the composited frames are copies already, the frame can keep them
frame.bitmap = pixels;
return frame;
});
}
/** Options of {@link Animation} */
export interface AnimationOptions {
/** How many times to repeat the animation (-1 = forever, 0 = play once). Default: `-1` */
loopCount?: number;
}
/** Options of {@link Animation.decode} */
export interface AnimationDecodeOptions {
/** Whether to stop decoding after the first frame. Default: `false` */
onlyFirstFrame?: boolean;
/**
* The most pixels to decode, of all frames together (every frame is a full canvas): larger animations throw a
* RangeError before anything that size is allocated, so small files declaring huge animations (decompression
* bombs) can't exhaust the memory. Default: `268402689` (16383², like sharp), `Infinity` for no limit
*/
maxPixels?: number;
}
/** The options all animation encoders have */
export interface AnimationEncodeOptions {
/** Metadata to include, see {@link Metadata} */
metadata?: Metadata;
/**
* Whether to store each frame as the area that changed since the frame before and merge identical frames, which
* usually makes files a lot smaller. The file shows the same animation either way, but only decodes to the same
* frames without it, which also encodes faster when little changes. Default: `true`
*/
optimize?: boolean;
}
/** Options of {@link Animation.encodeWebP} */
export interface WebPAnimationEncodeOptions extends AnimationEncodeOptions {
/**
* Lossy frames (0-100, like libwebp's quality: 75 is its default), much smaller than lossless ones. Without it the
* frames are lossless
*/
quality?: number;
/** How much effort lossy frames take (0-6), see {@link WebPEncodeOptions.effort}. Default: `2` */
effort?: number;
}
/** Options of {@link Animation.encodeGIF} */
export interface GIFEncodeOptions extends AnimationEncodeOptions {
/** 1-100, scales the palette size from 8 colors (1) to 256 colors (90 and above). Default: `95` */
quality?: number;
}
/** Options of {@link Animation.encodeAPNG} */
export interface APNGEncodeOptions extends AnimationEncodeOptions {
/** The zlib compression level (0-9), higher is smaller but slower. Default: `1` */
compression?: number;
}
/**
* An animation: frames ({@link Animation.frames}) that can be decoded from and encoded to GIF, APNG and WebP.
*
* The animation itself is an array of its frames too (`animation[0]`, `animation.map(...)`), which is deprecated: in 4.0
* it won't extend `Array` anymore, use {@link Animation.frames} instead. Array methods like map and filter return
* plain arrays.
*/
export class Animation extends Array<Frame> {
/** How many times to repeat the animation (-1 = forever, 0 = play once) */
loopCount: number;
/**
* Creates a new animation
* @param frames The frames to create the animation from
* @param options See {@link AnimationOptions}
*/
constructor(frames: Frame[], options?: AnimationOptions);
/**
* Creates a new animation
* @param frames The frames to create the animation from
* @param loopCount How many times to repeat the animation (-1 = forever, 0 = play once)
*/
constructor(frames: Frame[], loopCount?: number);
constructor(frames: Frame[], loopCountOrOptions: number | AnimationOptions = -1) {
super();
const { loopCount = -1 } = isOptions(loopCountOrOptions) ? loopCountOrOptions : { loopCount: loopCountOrOptions };
// push instead of super(...frames), so a single frame isn't taken as a length
for (const frame of frames) this.push(frame);
for (let i = 0; i < this.length; i++)
if (!(this[i] instanceof Frame))
throw new TypeError(`Frame ${i} is not an instance of Frame`);
if (finite(loopCount, 'loop count') < -1)
throw new RangeError('Invalid loop count');
this.loopCount = loopCount;
}
/**
* @internal
* @ignore
*/
static override get [Symbol.species](): ArrayConstructor {
return Array;
}
/**
* The animation's frames: an array that changes the animation when it's changed (e.g. `push()`, `splice()`, or
* setting an index), and can be replaced as a whole
*/
get frames(): Frame[] {
return this;
}
/** Replaces the animation's frames */
set frames(frames: Frame[]) {
frames = Array.from(frames);
frames.forEach((frame, i) => {
if (!(frame instanceof Frame)) throw new TypeError(`Frame ${i} is not an instance of Frame`);
});
this.length = 0;
for (const frame of frames) this.push(frame);
}
/** The animation's width */
get width(): number {
let max = 0;
for (const frame of this) max = Math.max(max, frame.width + frame.xOffset);
return max;
}
/** The animation's height */
get height(): number {
let max = 0;
for (const frame of this) max = Math.max(max, frame.height + frame.yOffset);
return max;
}
/** The animation's duration (in ms) */
get duration(): number {
let duration = 0;
for (const frame of this) duration += frame.duration;
return duration;
}
/** A short description for debugging, e.g. `Animation<64x64x1000ms>` */
override toString(): string {
return `Animation<${this.width}x${this.height}x${this.duration}ms>`;
}
/**
* Returns a new animation of the same class containing a range of this one's frames
* (same semantics as Array.prototype.slice)
* @deprecated Use `new Animation(animation.frames.slice(start, end), animation.loopCount)`: in 4.0 animations
* aren't arrays anymore
*/
override slice(start?: number, end?: number): this {
const Class = this.constructor as new (frames: Frame[], loopCount: number) => this;
return new Class(Array.prototype.slice.call(this, start, end) as Frame[], this.loopCount);
}
/**
* Resizes the animation to the given dimensions, scaling every frame and its offsets proportionally.
* Use {@link Image.RESIZE_AUTO} as either width or height to automatically preserve the aspect ratio.
*/
resize(width: number, height: number, options?: ResizeOptions): this;
/**
* Resizes the animation to the given dimensions, scaling every frame and its offsets proportionally.
* Use {@link Image.RESIZE_AUTO} as either width or height to automatically preserve the aspect ratio.
*/
resize(width: number, height: number, mode?: ResizeMode): this;
resize(width: number, height: number, modeOrOptions?: ResizeMode | ResizeOptions): this {
const originalWidth = this.width, originalHeight = this.height;
if (width === Image.RESIZE_AUTO && height === Image.RESIZE_AUTO) throw new Error('RESIZE_AUTO can only be used for either width or height, not for both');
if (width === Image.RESIZE_AUTO) width = originalWidth / originalHeight * height;
if (height === Image.RESIZE_AUTO) height = originalHeight / originalWidth * width;
const sx = width / originalWidth, sy = height / originalHeight;
for (const frame of this) {
// scale both edges so adjacent frames stay aligned
const left = Math.round(frame.xOffset * sx), top = Math.round(frame.yOffset * sy);
const right = Math.round((frame.xOffset + frame.width) * sx), bottom = Math.round((frame.yOffset + frame.height) * sy);
Image.prototype.resize.call(frame, Math.max(1, right - left), Math.max(1, bottom - top), modeOrOptions as ResizeMode);
frame.xOffset = left;
frame.yOffset = top;
}
return this;
}
/**
* @internal
* @ignore
*/
protected __validate__(): void {
for (const frame of this)
if (!(frame instanceof Frame)) throw new Error('Animation contains invalid frames');
}
/**
* Encodes the animation as GIF. Like all encoders, it stores each frame as the area that changed since the frame
* before and merges identical frames (unless the `optimize` option is false), so the file decodes to what the
* animation shows, but not necessarily to the same frames. Semi-transparent pixels become opaque or transparent
* after the frames are composited
* @param options See {@link GIFEncodeOptions}
*/
encodeGIF(options?: GIFEncodeOptions): Promise<Uint8Array>;
/**
* Encodes the animation as GIF. Like all encoders, it stores each frame as the area that changed since the frame
* before and merges identical frames (unless the `optimize` option is false), so the file decodes to what the
* animation shows, but not necessarily to the same frames. Semi-transparent pixels become opaque or transparent
* after the frames are composited
* @param quality 1-100, scales the palette size from 8 colors (1) to 256 colors (90 and above)
*/
encodeGIF(quality?: number): Promise<Uint8Array>;
async encodeGIF(qualityOrOptions: number | GIFEncodeOptions = 95): Promise<Uint8Array> {
const { quality = 95, metadata = {}, optimize = true } = isOptions(qualityOrOptions) ? qualityOrOptions : { quality: qualityOrOptions };
this.__validate__();
const frames = optimize
? this.__optimized__({ replace: false, even: false, alpha: 'frame', palette: 256, cover: true }).map(f => ({ ...f, dispose: f.dispose ? 3 : 1 }))
: Array.from(this, frame => ({
x: frame.xOffset,
y: frame.yOffset,
width: frame.width,
height: frame.height,
duration: frame.duration,
dispose: frame.__disposal_id__,
pixels: new Uint8Array(frame.bitmap.buffer, frame.bitmap.byteOffset, frame.bitmap.byteLength)
}));
// gifs store centiseconds; rounding keeps e.g. 16 ms (60 fps) from becoming 10 ms
const gifFrames = frames.map(({ duration, ...f }) => ({ ...f, delay: Math.round(duration / 10) }));
return gifCodec.encode({ width: this.width, height: this.height, loopCount: this.loopCount, frames: gifFrames }, quality, gifComments(resolveMetadata(metadata)));
}
/**
* Encodes the animation as APNG (animated PNG, lossless and with full alpha). Viewers without APNG support show
* the first frame. Each frame is stored as the area that changed since the frame before, identical frames are
* merged (unless the `optimize` option is false): the file shows exactly the same animation, but doesn't
* necessarily decode to the same frames
* @param options See {@link APNGEncodeOptions}
*/
encodeAPNG(options?: APNGEncodeOptions): Promise<Uint8Array>;
/**
* Encodes the animation as APNG (animated PNG, lossless and with full alpha). Viewers without APNG support show
* the first frame. Each frame is stored as the area that changed since the frame before, identical frames are
* merged (unless the `optimize` option is false): the file shows exactly the same animation, but doesn't
* necessarily decode to the same frames
* @param compression The zlib compression level (0-9), higher is smaller but slower
*/
encodeAPNG(compression?: number): Promise<Uint8Array>;
async encodeAPNG(compressionOrOptions: number | APNGEncodeOptions = 1): Promise<Uint8Array> {
const { compression = 1, metadata = {}, optimize = true } = isOptions(compressionOrOptions) ? compressionOrOptions : { compression: compressionOrOptions };
this.__validate__();
if (!this.length) throw new Error('An animation needs at least one frame');
const frames = optimize
? this.__optimized__({ replace: true, even: false, alpha: frames => png.alphaCost(frames.map(f => f.pixels)) }).map(f => ({ ...f, dispose: f.dispose ? 1 : 0 }))
: this.__apngFrames__().map(({ blend: _, ...f }) => f); // blended, the encoder's default
return png.encodeAnimation(frames, this.width, this.height, toPlays(this.loopCount), compression, pngText(resolveMetadata(metadata)));
}
/**
* Encodes the animation as animated WebP, lossless or (with a quality) lossy. Each frame is stored as the area that
* changed since the frame before, identical frames are merged (unless the `optimize` option is false): the file
* shows the same animation, but doesn't necessarily decode to the same frames
* @param options See {@link WebPAnimationEncodeOptions}
*/
async encodeWebP({ metadata = {}, optimize = true, quality, effort = 2 }: WebPAnimationEncodeOptions = {}): Promise<Uint8Array> {
this.__validate__();
if (!this.length) throw new Error('An animation needs at least one frame');
if (quality !== undefined) {
const q = finite(quality, 'quality');
if (q < 0 || q > 100) throw new RangeError(`quality must be 0-100, not ${q}`);
if (!Number.isInteger(effort) || effort < 0 || effort > 6) throw new RangeError(`effort must be an integer 0-6, not ${effort}`);
}
const frames = optimize ? this.__optimized__({ replace: true, even: true, alpha: 'frame' }) : this.__webpFrames__();
return webp.encodeAnimation(frames, this.width, this.height, toPlays(this.loopCount), exifData(resolveMetadata(metadata)), quality, effort);
}
/**
* @internal the frames as they are, in the WebP codec's terms
* @ignore
*/
__webpFrames__(): webp.WebPEncodeFrame[] {
const width = this.width, height = this.height;
// WebP knows no "previous" disposal and stores offsets halved: such animations are stored as full frames
const fullFrames = this.some(f => f.disposalMode === 'previous' || (f.disposalMode === 'background' && (f.xOffset & 1 || f.yOffset & 1)));
if (fullFrames)
return png.compositeAnimation({ width, height, frames: this.__apngFrames__() })
.map(c => ({ x: 0, y: 0, width, height, duration: c.duration, dispose: true, pixels: c.pixels }));
return Array.from(this, frame => {
let { xOffset: x, yOffset: y, width: w, height: h } = frame;
let pixels = new Uint8Array(frame.bitmap.buffer, frame.bitmap.byteOffset, frame.bitmap.byteLength);
// odd offsets: one transparent pixel more on the left/top (blending makes it invisible)
if (x & 1 || y & 1) {
const dx = x & 1, dy = y & 1;
const padded = new Uint8Array((w + dx) * (h + dy) * 4);
for (let row = 0; row < h; row++) padded.set(pixels.subarray(row * w * 4, (row + 1) * w * 4), ((row + dy) * (w + dx) + dx) * 4);
pixels = padded;
x -= dx; y -= dy; w += dx; h += dy;
}
return { x, y, width: w, height: h, duration: frame.duration, dispose: frame.disposalMode === 'background', pixels };
});
}
/**
* The frames as the encoders store them: each frame only covers what changed since the one before (see
* util/optimize.ts)
* @internal
* @ignore
*/
__optimized__(options: OptimizeOptions): OptimizedFrame[] {
return optimize(this.__apngFrames__(), this.width, this.height, options);
}
/**
* @internal frames in the APNG codec's terms (alpha blended, disposal 0 none, 1 background, 2 previous)
* @ignore
*/
__apngFrames__(): png.APNGFrame[] {
const dispose = [0, 0, 2, 1];
return Array.from(this, frame => ({
x: frame.xOffset,
y: frame.yOffset,
width: frame.width,
height: frame.height,
duration: frame.duration,
dispose: dispose[frame.__disposal_id__],
blend: 1,
pixels: new Uint8Array(frame.bitmap.buffer, frame.bitmap.byteOffset, frame.bitmap.byteLength)
}));
}
/**
* Decodes an animated GIF, APNG, WebP or JPEG XL into fully composited frames (still images become a single frame)
* @param input The binary data to decode
* @param options See {@link AnimationDecodeOptions}
*/
static decode(input: BinaryData, options?: AnimationDecodeOptions): Promise<Animation>;
/**
* Decodes an animated GIF, APNG, WebP or JPEG XL into fully composited frames (still images become a single frame)
* @param input The binary data to decode
* @param onlyExtractFirstFrame Whether to stop decoding after the first frame
*/
static decode(input: BinaryData, onlyExtractFirstFrame?: boolean): Promise<Animation>;
static async decode(input: BinaryData, onlyFirstFrameOrOptions: boolean | AnimationDecodeOptions = false): Promise<Animation> {
return decoding(() => Animation.__decode__(input, onlyFirstFrameOrOptions));
}
/**
* @internal
* @ignore
*/
private static async __decode__(input: BinaryData, onlyFirstFrameOrOptions: boolean | AnimationDecodeOptions): Promise<Animation> {
const { onlyFirstFrame: onlyExtractFirstFrame = false, maxPixels = MAX_PIXELS } = isOptions(onlyFirstFrameOrOptions) ? onlyFirstFrameOrOptions : { onlyFirstFrame: onlyFirstFrameOrOptions };
const data = view(input);
const type = ImageType.getType(data);
if (type === 'gif') return Animation.__fromGIF__(data, onlyExtractFirstFrame, Animation, maxPixels);
if (type === 'webp') {
const decoded = webp.decodeAnimation(data, onlyExtractFirstFrame, maxPixels);
const frames = canvasFrames(decoded.width, decoded.height, webp.composite(decoded));
return new Animation(frames, fromPlays(decoded.loopCount));
}
// (still PNGs decode like images, keeping their orientation as it is like before)
if (type === 'png' && !png.isAnimated(data)) {
const image = await Image.decode(data, { maxPixels, applyOrientation: false });
return new Animation(canvasFrames(image.width, image.height, [{ pixels: image.bitmap, duration: 0 }]), -1);
}
if (type === 'png') {
const decoded = await png.decodeAnimation(data, onlyExtractFirstFrame, maxPixels);
const frames = canvasFrames(decoded.width, decoded.height, png.compositeAnimation(decoded));
return new Animation(frames, decoded.frames.length > 1 ? fromPlays(decoded.plays) : -1);
}
if (type === 'jxl') {
const frames: Frame[] = [];
let loopCount = -1;
for (const decoded of jxl.frames(data, maxPixels)) {
const frame = new Frame(decoded.width, decoded.height, decoded.duration, 0, 0, Frame.DISPOSAL_BACKGROUND);
frame.bitmap = decoded.pixels;
frames.push(frame.__orient__(decoded.orientation));
loopCount = fromPlays(decoded.loopCount);
if (onlyExtractFirstFrame) break;
}
return new Animation(frames, frames.length > 1 ? loopCount : -1);
}
if (type) return new Animation([Frame.from(await Image.decode(data, { maxPixels }), 0)]);
throw new Error('Unsupported image type');
}
/**
* @internal
* @ignore
*/
static __fromGIF__<T extends Animation>(data: Uint8Array, onlyExtractFirstFrame: boolean, Class: new (frames: Frame[], loopCount: number) => T, maxPixels = MAX_PIXELS): Promise<T> {
return decoding(() => Animation.__gifFrames__(data, onlyExtractFirstFrame, Class, maxPixels));
}
/**
* @internal
* @ignore
*/
private static __gifFrames__<T extends Animation>(data: Uint8Array, onlyExtractFirstFrame: boolean, Class: new (frames: Frame[], loopCount: number) => T, maxPixels: number): T {
const decoded = gifCodec.decode(data, onlyExtractFirstFrame, maxPixels);
const frames = gifCodec.composite(decoded).map(({ pixels, delay }) => ({ pixels, duration: 10 * delay }));
return new Class(canvasFrames(decoded.width, decoded.height, frames), decoded.loopCount);
}
}