imagescript 3.20.3 → 4.0.0-rc.2
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/LICENSE +16 -12
- package/README.md +21 -12
- package/THIRD_PARTY_NOTICES.md +1 -1
- package/dist/animation-encoder.d.ts +1 -34
- package/dist/animation-encoder.js +35 -34
- package/dist/animation.d.ts +108 -122
- package/dist/animation.js +285 -121
- package/dist/codecs/av1/decode.js +1 -1
- package/dist/codecs/av1/encode.js +1 -1
- package/dist/codecs/gif.js +11 -9
- package/dist/codecs/hdr.d.ts +1 -1
- package/dist/codecs/jpeg.js +13 -13
- package/dist/codecs/jxl/headers.js +3 -3
- package/dist/codecs/jxl/patches-enc.js +1 -1
- package/dist/codecs/jxl.js +5 -5
- package/dist/codecs/netpbm.js +7 -7
- package/dist/codecs/png.js +15 -15
- package/dist/codecs/quantize.js +34 -8
- package/dist/codecs/tiff.js +1 -1
- package/dist/codecs/webp.js +3 -3
- package/dist/codecs/zlib.js +1 -1
- package/dist/colors.d.ts +8 -7
- package/dist/colors.js +23 -3
- package/dist/frame.d.ts +94 -100
- package/dist/frame.js +89 -65
- package/dist/gainmap.d.ts +88 -46
- package/dist/gainmap.js +75 -51
- package/dist/image-type.d.ts +6 -1
- package/dist/image-type.js +4 -1
- package/dist/image.d.ts +317 -738
- package/dist/image.js +308 -438
- package/dist/index.d.ts +19 -25
- package/dist/index.js +15 -14
- package/dist/metadata.d.ts +4 -3
- package/dist/metadata.js +9 -7
- package/dist/render/colr.js +1 -1
- package/dist/render/font.js +1 -1
- package/dist/render/layout.js +1 -1
- package/dist/render/segment.js +1 -1
- package/dist/render/shapers/hangul.js +1 -1
- package/dist/render/svg.js +68 -20
- package/dist/render/text.js +7 -9
- package/dist/text-layout.d.ts +5 -35
- package/dist/text-layout.js +11 -12
- package/dist/util/gain.js +12 -12
- package/dist/util/mem.d.ts +1 -1
- package/dist/util/optimize.js +3 -1
- package/dist/util/options.js +46 -7
- package/package.json +15 -10
- package/LICENSE.AGPL-3.0 +0 -619
- package/LICENSE.MIT +0 -19
- package/dist/gif.d.ts +0 -36
- package/dist/gif.js +0 -22
- package/dist/util/rotate.js +0 -43
- package/dist/version.js +0 -1
package/dist/animation.js
CHANGED
|
@@ -6,151 +6,298 @@ import * as jxl from "./codecs/jxl.js";
|
|
|
6
6
|
import { availableThreads } from "./util/threads.js";
|
|
7
7
|
import { view } from "./util/mem.js";
|
|
8
8
|
import { GainMap } from "./gainmap.js";
|
|
9
|
-
import { Image,
|
|
10
|
-
import { Frame
|
|
9
|
+
import { Image, resizeArguments, insideSize, alignedPixels } from "./image.js";
|
|
10
|
+
import { Frame } from "./frame.js";
|
|
11
11
|
import { ImageType } from "./image-type.js";
|
|
12
12
|
import { optimize } from "./util/optimize.js";
|
|
13
|
+
import { paletteOptions } from "./codecs/quantize.js";
|
|
13
14
|
import { resolveMetadata, pngText, exifData, gifComments } from "./metadata.js";
|
|
14
|
-
import {
|
|
15
|
+
import { checkOptions, finite, compressionLevel, loopCount, toPlays, fromPlays, fitLoops, fitDuration, maxDuration, color as checkColor } from "./util/options.js";
|
|
15
16
|
import { MAX_PIXELS, decoding } from "./util/limits.js";
|
|
16
17
|
import { AnimationEncoder } from "./animation-encoder.js";
|
|
17
|
-
const toPlays = (loopCount) => loopCount < 0 || loopCount === Infinity ? 0 : loopCount + 1;
|
|
18
|
-
const fromPlays = (plays) => plays === 0 ? -1 : plays - 1;
|
|
19
18
|
function canvasFrames(width, height, frames) {
|
|
20
19
|
return frames.map(({ pixels, duration }) => {
|
|
21
|
-
const
|
|
22
|
-
|
|
23
|
-
return
|
|
20
|
+
const image = new Image(width, height);
|
|
21
|
+
image.bitmap = alignedPixels(pixels);
|
|
22
|
+
return new Frame(image, { duration, disposalMode: 'background' });
|
|
24
23
|
});
|
|
25
24
|
}
|
|
26
|
-
export class Animation
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
if (
|
|
37
|
-
throw new
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
25
|
+
export class Animation {
|
|
26
|
+
__loopCount__ = Infinity;
|
|
27
|
+
__animationSignal__ = false;
|
|
28
|
+
__frames__ = [];
|
|
29
|
+
__framesView__ = null;
|
|
30
|
+
__canvas__ = null;
|
|
31
|
+
constructor(frames, options = {}) {
|
|
32
|
+
checkOptions(options, 'Animation(frames, { loopCount: 2 })');
|
|
33
|
+
const { loopCount: count = Infinity, width, height } = options;
|
|
34
|
+
const loops = loopCount(count);
|
|
35
|
+
if ((width === undefined) !== (height === undefined))
|
|
36
|
+
throw new TypeError('Animation() takes both a canvas width and height, or neither');
|
|
37
|
+
const canvas = width === undefined ? null : canvasSize(width, height);
|
|
38
|
+
this.frames = frames;
|
|
39
|
+
this.__loopCount__ = loops;
|
|
40
|
+
this.__canvas__ = canvas;
|
|
41
|
+
}
|
|
42
|
+
get loopCount() {
|
|
43
|
+
return this.__loopCount__;
|
|
44
|
+
}
|
|
45
|
+
set loopCount(value) {
|
|
46
|
+
this.__loopCount__ = loopCount(value);
|
|
42
47
|
}
|
|
43
48
|
get frames() {
|
|
44
|
-
return this;
|
|
49
|
+
return this.__framesView__ ??= Object.freeze([...this.__frames__]);
|
|
45
50
|
}
|
|
46
51
|
set frames(frames) {
|
|
47
|
-
|
|
48
|
-
|
|
52
|
+
const copy = [];
|
|
53
|
+
for (const frame of frames) {
|
|
49
54
|
if (!(frame instanceof Frame))
|
|
50
|
-
throw new TypeError(`Frame ${
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
+
throw new TypeError(`Frame ${copy.length} is not an instance of Frame`);
|
|
56
|
+
copy.push(frame);
|
|
57
|
+
}
|
|
58
|
+
this.__frames__ = copy;
|
|
59
|
+
this.__framesView__ = null;
|
|
60
|
+
}
|
|
61
|
+
get length() {
|
|
62
|
+
return this.__frames__.length;
|
|
63
|
+
}
|
|
64
|
+
[Symbol.iterator]() {
|
|
65
|
+
return this.__frames__[Symbol.iterator]();
|
|
55
66
|
}
|
|
56
67
|
get width() {
|
|
68
|
+
if (this.__canvas__)
|
|
69
|
+
return this.__canvas__[0];
|
|
57
70
|
let max = 0;
|
|
58
|
-
for (const frame of this)
|
|
71
|
+
for (const frame of this.frames)
|
|
59
72
|
max = Math.max(max, frame.width + frame.xOffset);
|
|
60
73
|
return max;
|
|
61
74
|
}
|
|
62
75
|
get height() {
|
|
76
|
+
if (this.__canvas__)
|
|
77
|
+
return this.__canvas__[1];
|
|
63
78
|
let max = 0;
|
|
64
|
-
for (const frame of this)
|
|
79
|
+
for (const frame of this.frames)
|
|
65
80
|
max = Math.max(max, frame.height + frame.yOffset);
|
|
66
81
|
return max;
|
|
67
82
|
}
|
|
83
|
+
clone() {
|
|
84
|
+
const copy = new Animation(this.frames.map(frame => frame.clone()), { loopCount: this.loopCount });
|
|
85
|
+
copy.__canvas__ = this.__canvas__ && [...this.__canvas__];
|
|
86
|
+
return copy;
|
|
87
|
+
}
|
|
68
88
|
get duration() {
|
|
69
89
|
let duration = 0;
|
|
70
|
-
for (const frame of this)
|
|
90
|
+
for (const frame of this.frames)
|
|
71
91
|
duration += frame.duration;
|
|
72
92
|
return duration;
|
|
73
93
|
}
|
|
74
94
|
toString() {
|
|
75
95
|
return `Animation<${this.width}x${this.height}x${this.duration}ms>`;
|
|
76
96
|
}
|
|
77
|
-
|
|
78
|
-
const
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
97
|
+
resize(widthOrOptions, heightArgument, optionsArgument) {
|
|
98
|
+
const [givenWidth, givenHeight, options, fit] = resizeArguments(widthOrOptions, heightArgument, optionsArgument);
|
|
99
|
+
if (fit === 'contain' || fit === 'cover') {
|
|
100
|
+
if (givenWidth === undefined || givenHeight === undefined)
|
|
101
|
+
throw new TypeError(`resize({ fit: '${fit}' }) needs both width and height`);
|
|
102
|
+
return fit === 'contain'
|
|
103
|
+
? this.__contain__(givenWidth, givenHeight, options)
|
|
104
|
+
: this.__cover__(givenWidth, givenHeight, options);
|
|
105
|
+
}
|
|
83
106
|
const originalWidth = this.width, originalHeight = this.height;
|
|
84
|
-
if (
|
|
85
|
-
throw new
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
if (
|
|
89
|
-
height = originalHeight
|
|
107
|
+
if (!originalWidth || !originalHeight)
|
|
108
|
+
throw new RangeError('An animation without frames or canvas can\'t be resized');
|
|
109
|
+
let width = givenWidth ?? originalWidth / originalHeight * givenHeight;
|
|
110
|
+
let height = givenHeight ?? originalHeight / originalWidth * width;
|
|
111
|
+
if (fit === 'inside' && givenWidth !== undefined && givenHeight !== undefined)
|
|
112
|
+
[width, height] = insideSize(originalWidth, originalHeight, givenWidth, givenHeight);
|
|
113
|
+
const canvas = canvasSize(Math.max(1, Math.round(width)), Math.max(1, Math.round(height)));
|
|
90
114
|
const sx = width / originalWidth, sy = height / originalHeight;
|
|
91
|
-
for (const frame of this) {
|
|
115
|
+
for (const frame of this.frames) {
|
|
92
116
|
const left = Math.round(frame.xOffset * sx), top = Math.round(frame.yOffset * sy);
|
|
93
117
|
const right = Math.round((frame.xOffset + frame.width) * sx), bottom = Math.round((frame.yOffset + frame.height) * sy);
|
|
94
|
-
|
|
95
|
-
.call(frame, Math.max(1, right - left), Math.max(1, bottom - top), modeOrOptions);
|
|
118
|
+
frame.image.resize(Math.max(1, right - left), Math.max(1, bottom - top), options);
|
|
96
119
|
frame.xOffset = left;
|
|
97
120
|
frame.yOffset = top;
|
|
98
121
|
}
|
|
122
|
+
this.__canvas__ = canvas;
|
|
99
123
|
return this;
|
|
100
124
|
}
|
|
101
|
-
|
|
102
|
-
|
|
125
|
+
crop(x, y, width, height) {
|
|
126
|
+
x = Math.max(0, Math.floor(finite(x, 'x')));
|
|
127
|
+
y = Math.max(0, Math.floor(finite(y, 'y')));
|
|
128
|
+
width = Math.min(Math.floor(finite(width, 'width')), this.width - x);
|
|
129
|
+
height = Math.min(Math.floor(finite(height, 'height')), this.height - y);
|
|
130
|
+
if (!(width >= 1 && height >= 1))
|
|
131
|
+
throw new RangeError('The crop region has to overlap the canvas');
|
|
132
|
+
for (const frame of this.frames) {
|
|
133
|
+
frame.xOffset -= x;
|
|
134
|
+
frame.yOffset -= y;
|
|
135
|
+
}
|
|
136
|
+
this.__canvas__ = [width, height];
|
|
137
|
+
this.frames = this.__clipped_frames__();
|
|
138
|
+
return this;
|
|
139
|
+
}
|
|
140
|
+
__contain__(width, height, options) {
|
|
141
|
+
checkOptions(options, 'resize({ width, height, fit: \'contain\', background: 0x000000ff })');
|
|
142
|
+
const { background = 0x00000000, ...resizeOpts } = options ?? {};
|
|
143
|
+
const backgroundColor = checkColor(background, 'background');
|
|
144
|
+
const [boxWidth, boxHeight] = canvasSize(Math.floor(finite(width, 'width')), Math.floor(finite(height, 'height')));
|
|
145
|
+
this.resize({ width: boxWidth, height: boxHeight, fit: 'inside', ...resizeOpts });
|
|
146
|
+
const x = (boxWidth - this.width) / 2 | 0, y = (boxHeight - this.height) / 2 | 0;
|
|
147
|
+
for (const frame of this.frames) {
|
|
148
|
+
frame.xOffset += x;
|
|
149
|
+
frame.yOffset += y;
|
|
150
|
+
}
|
|
151
|
+
this.__canvas__ = [boxWidth, boxHeight];
|
|
152
|
+
if (backgroundColor !== 0) {
|
|
153
|
+
this.frames = this.__flattened__().map(frame => {
|
|
154
|
+
frame.image = new Image(boxWidth, boxHeight).fill(backgroundColor).composite(frame.image);
|
|
155
|
+
return frame;
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
return this;
|
|
159
|
+
}
|
|
160
|
+
__cover__(width, height, options) {
|
|
161
|
+
checkOptions(options, 'resize({ width, height, fit: \'cover\' })');
|
|
162
|
+
const [boxWidth, boxHeight] = canvasSize(Math.floor(finite(width, 'width')), Math.floor(finite(height, 'height')));
|
|
163
|
+
const scale = Math.max(boxWidth / this.width, boxHeight / this.height);
|
|
164
|
+
this.resize(Math.max(boxWidth, Math.round(this.width * scale)), Math.max(boxHeight, Math.round(this.height * scale)), options);
|
|
165
|
+
return this.crop((this.width - boxWidth) / 2, (this.height - boxHeight) / 2, boxWidth, boxHeight);
|
|
166
|
+
}
|
|
167
|
+
scale(factor, options) {
|
|
168
|
+
if (!(finite(factor, 'factor') > 0))
|
|
169
|
+
throw new RangeError(`Invalid scale factor: ${factor}`);
|
|
170
|
+
return this.resize(this.width * factor, this.height * factor, options);
|
|
171
|
+
}
|
|
172
|
+
trim(options = {}) {
|
|
173
|
+
checkOptions(options, 'trim({ tolerance: 0.1 })');
|
|
174
|
+
const frames = this.__flattened__();
|
|
175
|
+
if (!frames.length)
|
|
176
|
+
return this;
|
|
177
|
+
const { color = frames[0].image.getPixelAt(0, 0), ...rest } = options;
|
|
178
|
+
let left = Infinity, top = Infinity, right = -Infinity, bottom = -Infinity;
|
|
179
|
+
for (const { image } of frames) {
|
|
180
|
+
const bounds = image.__trim_bounds__(color, rest);
|
|
181
|
+
if (!bounds)
|
|
182
|
+
continue;
|
|
183
|
+
left = Math.min(left, bounds[0]);
|
|
184
|
+
top = Math.min(top, bounds[1]);
|
|
185
|
+
right = Math.max(right, bounds[0] + bounds[2]);
|
|
186
|
+
bottom = Math.max(bottom, bounds[1] + bounds[3]);
|
|
187
|
+
}
|
|
188
|
+
if (left === Infinity)
|
|
189
|
+
return this;
|
|
190
|
+
return this.crop(left, top, right - left, bottom - top);
|
|
191
|
+
}
|
|
192
|
+
rotate(angle, options) {
|
|
193
|
+
return this.__each__(image => image.rotate(angle, options));
|
|
194
|
+
}
|
|
195
|
+
transform(matrix, options) {
|
|
196
|
+
return this.__each__(image => image.transform(matrix, options));
|
|
197
|
+
}
|
|
198
|
+
dropShadow(options) {
|
|
199
|
+
return this.__each__(image => image.dropShadow(options));
|
|
200
|
+
}
|
|
201
|
+
outline(options) {
|
|
202
|
+
return this.__each__(image => image.outline(options));
|
|
203
|
+
}
|
|
204
|
+
__flattened__() {
|
|
205
|
+
const { width, height } = this;
|
|
206
|
+
if (!this.length)
|
|
207
|
+
return [];
|
|
208
|
+
return png.compositeAnimation({ width, height, frames: this.__apngFrames__() }).map(({ pixels, duration }) => {
|
|
209
|
+
const image = new Image(width, height);
|
|
210
|
+
image.bitmap = alignedPixels(pixels);
|
|
211
|
+
return new Frame(image, { duration, disposalMode: 'background' });
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
__each__(change) {
|
|
215
|
+
const { width, height } = this;
|
|
216
|
+
if (!width || !height)
|
|
217
|
+
throw new RangeError('An animation without frames or canvas can\'t be changed');
|
|
218
|
+
const frames = this.__flattened__();
|
|
219
|
+
const probe = frames[0]?.image ?? new Image(width, height);
|
|
220
|
+
for (const frame of frames.length ? frames : [new Frame(probe)])
|
|
221
|
+
change(frame.image);
|
|
222
|
+
this.frames = frames;
|
|
223
|
+
this.__canvas__ = [probe.width, probe.height];
|
|
224
|
+
return this;
|
|
225
|
+
}
|
|
226
|
+
__clipped_frames__() {
|
|
227
|
+
const width = this.width, height = this.height;
|
|
228
|
+
return this.frames.map(frame => {
|
|
229
|
+
const { xOffset: x, yOffset: y } = frame;
|
|
230
|
+
if (x >= 0 && y >= 0 && x + frame.width <= width && y + frame.height <= height)
|
|
231
|
+
return frame;
|
|
232
|
+
const left = Math.max(0, x), top = Math.max(0, y);
|
|
233
|
+
const right = Math.min(width, x + frame.width), bottom = Math.min(height, y + frame.height);
|
|
234
|
+
const options = { duration: frame.duration, disposalMode: frame.disposalMode };
|
|
235
|
+
if (right <= left || bottom <= top)
|
|
236
|
+
return new Frame(new Image(1, 1), { ...options, disposalMode: 'keep' });
|
|
237
|
+
const clipped = frame.image.extract(left - x, top - y, right - left, bottom - top);
|
|
238
|
+
clipped.gainMap = null;
|
|
239
|
+
return new Frame(clipped, { ...options, xOffset: left, yOffset: top });
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
__validate__(format) {
|
|
243
|
+
if (!this.length)
|
|
244
|
+
throw new Error('An animation needs at least one frame');
|
|
245
|
+
for (const frame of this.frames) {
|
|
103
246
|
if (!(frame instanceof Frame))
|
|
104
247
|
throw new Error('Animation contains invalid frames');
|
|
248
|
+
fitDuration(frame.duration, format);
|
|
249
|
+
}
|
|
105
250
|
}
|
|
106
|
-
async encodeGIF(
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
251
|
+
async encodeGIF(options = {}) {
|
|
252
|
+
checkOptions(options, 'encodeGIF({ colors: 64 })');
|
|
253
|
+
const { colors = 256, dither = 'floyd-steinberg', ditherStrength = 0.85, metadata = {}, optimize = true } = options;
|
|
254
|
+
const palette = paletteOptions(colors, dither, ditherStrength);
|
|
255
|
+
const loops = fitLoops(this.loopCount, 'GIF');
|
|
256
|
+
this.__validate__('GIF');
|
|
110
257
|
const frames = optimize
|
|
111
|
-
? this.__optimized__({ replace: false, even: false, alpha: 'frame', palette: 256, cover: true }).map(f => ({ ...f, dispose: f.dispose ? 3 : 1 }))
|
|
112
|
-
: Array.from(this, frame => ({
|
|
258
|
+
? this.__optimized__({ replace: false, even: false, alpha: 'frame', palette: 256, cover: true, maxDuration: maxDuration('GIF') }).map(f => ({ ...f, dispose: f.dispose ? 3 : 1 }))
|
|
259
|
+
: Array.from(this.__clipped_frames__(), frame => ({
|
|
113
260
|
x: frame.xOffset,
|
|
114
261
|
y: frame.yOffset,
|
|
115
262
|
width: frame.width,
|
|
116
263
|
height: frame.height,
|
|
117
264
|
duration: frame.duration,
|
|
118
265
|
dispose: frame.__disposal_id__,
|
|
119
|
-
pixels: new Uint8Array(frame.bitmap.buffer, frame.bitmap.byteOffset, frame.bitmap.byteLength)
|
|
266
|
+
pixels: new Uint8Array(frame.image.bitmap.buffer, frame.image.bitmap.byteOffset, frame.image.bitmap.byteLength)
|
|
120
267
|
}));
|
|
121
268
|
const gifFrames = frames.map(({ duration, ...f }) => ({ ...f, delay: Math.round(duration / 10) }));
|
|
122
|
-
return gifCodec.encode({ width: this.width, height: this.height, loopCount:
|
|
269
|
+
return gifCodec.encode({ width: this.width, height: this.height, loopCount: loops, frames: gifFrames }, { colors: palette.colors, dither: palette.dither, strength: palette.strength }, gifComments(resolveMetadata(metadata)));
|
|
123
270
|
}
|
|
124
|
-
async encodeAPNG(
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
this.
|
|
128
|
-
|
|
129
|
-
throw new Error('An animation needs at least one frame');
|
|
271
|
+
async encodeAPNG(options = {}) {
|
|
272
|
+
checkOptions(options, 'encodeAPNG({ compression: 9 })');
|
|
273
|
+
const { compression = 4, metadata = {}, optimize = true } = options;
|
|
274
|
+
const loops = fitLoops(this.loopCount, 'APNG');
|
|
275
|
+
this.__validate__('APNG');
|
|
130
276
|
const frames = optimize
|
|
131
|
-
? this.__optimized__({ replace: true, even: false, alpha: frames => png.alphaCost(frames.map(f => f.pixels)) }).map(f => ({ ...f, dispose: f.dispose ? 1 : 0 }))
|
|
277
|
+
? 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 }))
|
|
132
278
|
: this.__apngFrames__().map(({ blend: _, ...f }) => f);
|
|
133
|
-
return png.encodeAnimation(frames, this.width, this.height, toPlays(
|
|
279
|
+
return png.encodeAnimation(frames, this.width, this.height, toPlays(loops), compressionLevel(compression), pngText(resolveMetadata(metadata)));
|
|
134
280
|
}
|
|
135
|
-
async encodeWebP(
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
281
|
+
async encodeWebP(options = {}) {
|
|
282
|
+
checkOptions(options, 'animation.encodeWebP({ quality: 80 })');
|
|
283
|
+
const { metadata = {}, optimize = true, quality, effort = 2, sharpYuv = false, alphaQuality = 100 } = options;
|
|
284
|
+
const loops = fitLoops(this.loopCount, 'WebP');
|
|
285
|
+
this.__validate__('WebP');
|
|
140
286
|
const lossy = webp.lossyOptions(quality, effort, sharpYuv, alphaQuality);
|
|
141
287
|
const tolerance = lossy ? webp.lossyTolerance(lossy.quality) : undefined;
|
|
142
|
-
const frames = optimize ? this.__optimized__({ replace: true, even: true, alpha: 'frame', tolerance }) : this.__webpFrames__();
|
|
143
|
-
return webp.encodeAnimation(frames, this.width, this.height, toPlays(
|
|
288
|
+
const frames = optimize ? this.__optimized__({ replace: true, even: true, alpha: 'frame', tolerance, maxDuration: maxDuration('WebP') }) : this.__webpFrames__();
|
|
289
|
+
return webp.encodeAnimation(frames, this.width, this.height, toPlays(loops), exifData(resolveMetadata(metadata)), lossy, effort);
|
|
144
290
|
}
|
|
145
291
|
__webpFrames__() {
|
|
146
292
|
const width = this.width, height = this.height;
|
|
147
|
-
const
|
|
293
|
+
const clipped = this.__clipped_frames__();
|
|
294
|
+
const fullFrames = clipped.some(f => f.disposalMode === 'previous' || (f.disposalMode === 'background' && (f.xOffset & 1 || f.yOffset & 1)));
|
|
148
295
|
if (fullFrames)
|
|
149
296
|
return png.compositeAnimation({ width, height, frames: this.__apngFrames__() })
|
|
150
297
|
.map(c => ({ x: 0, y: 0, width, height, duration: c.duration, dispose: true, pixels: c.pixels }));
|
|
151
|
-
return Array.from(
|
|
298
|
+
return Array.from(clipped, frame => {
|
|
152
299
|
let { xOffset: x, yOffset: y, width: w, height: h } = frame;
|
|
153
|
-
let pixels = new Uint8Array(frame.bitmap.buffer, frame.bitmap.byteOffset, frame.bitmap.byteLength);
|
|
300
|
+
let pixels = new Uint8Array(frame.image.bitmap.buffer, frame.image.bitmap.byteOffset, frame.image.bitmap.byteLength);
|
|
154
301
|
if (x & 1 || y & 1) {
|
|
155
302
|
const dx = x & 1, dy = y & 1;
|
|
156
303
|
const padded = new Uint8Array((w + dx) * (h + dy) * 4);
|
|
@@ -170,7 +317,7 @@ export class Animation extends Array {
|
|
|
170
317
|
}
|
|
171
318
|
__apngFrames__() {
|
|
172
319
|
const dispose = [0, 0, 2, 1];
|
|
173
|
-
return Array.from(this, frame => ({
|
|
320
|
+
return Array.from(this.__clipped_frames__(), frame => ({
|
|
174
321
|
x: frame.xOffset,
|
|
175
322
|
y: frame.yOffset,
|
|
176
323
|
width: frame.width,
|
|
@@ -178,109 +325,126 @@ export class Animation extends Array {
|
|
|
178
325
|
duration: frame.duration,
|
|
179
326
|
dispose: dispose[frame.__disposal_id__],
|
|
180
327
|
blend: 1,
|
|
181
|
-
pixels: new Uint8Array(frame.bitmap.buffer, frame.bitmap.byteOffset, frame.bitmap.byteLength)
|
|
328
|
+
pixels: new Uint8Array(frame.image.bitmap.buffer, frame.image.bitmap.byteOffset, frame.image.bitmap.byteLength)
|
|
182
329
|
}));
|
|
183
330
|
}
|
|
184
331
|
static webpEncoder(width, height, options = {}) {
|
|
332
|
+
checkOptions(options, 'Animation.webpEncoder(width, height, { quality: 80 })');
|
|
185
333
|
return AnimationEncoder.__webp__(width, height, options);
|
|
186
334
|
}
|
|
187
335
|
static gifEncoder(width, height, options = {}) {
|
|
336
|
+
checkOptions(options, 'Animation.gifEncoder(width, height, { colors: 64 })');
|
|
188
337
|
return AnimationEncoder.__gif__(width, height, options);
|
|
189
338
|
}
|
|
190
339
|
static apngEncoder(width, height, options = {}) {
|
|
340
|
+
checkOptions(options, 'Animation.apngEncoder(width, height, { compression: 9 })');
|
|
191
341
|
return AnimationEncoder.__apng__(width, height, options);
|
|
192
342
|
}
|
|
193
|
-
static async decode(input,
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
343
|
+
static async decode(input, options = {}) {
|
|
344
|
+
checkOptions(options, 'Animation.decode(data, { maxPixels: 1e8 })');
|
|
345
|
+
const { maxPixels = MAX_PIXELS, threads = 1 } = options;
|
|
346
|
+
if (!Number.isInteger(threads) || threads < 1)
|
|
347
|
+
throw new RangeError(`threads must be a positive integer, not ${threads}`);
|
|
348
|
+
if (!(Number(maxPixels) > 0))
|
|
349
|
+
throw new RangeError(`maxPixels must be a positive number, not ${maxPixels}`);
|
|
350
|
+
const { toneMapping = 'reinhard' } = options;
|
|
351
|
+
if (toneMapping !== 'reinhard' && toneMapping !== 'clamp')
|
|
352
|
+
throw new TypeError(`toneMapping must be 'reinhard' or 'clamp', not ${String(toneMapping)}`);
|
|
353
|
+
return decoding(() => Animation.__decode__(input, options));
|
|
354
|
+
}
|
|
355
|
+
static __is_animated__(animation) {
|
|
356
|
+
return animation.__animationSignal__;
|
|
205
357
|
}
|
|
206
|
-
static async __decode__(input,
|
|
207
|
-
const {
|
|
358
|
+
static async __decode__(input, options) {
|
|
359
|
+
const { maxPixels = MAX_PIXELS, applyColorProfile = true, threads = Math.max(1, Math.min(availableThreads(), 8)), applyOrientation = true, toneMapping = 'reinhard', gainMap = true } = options;
|
|
208
360
|
const orient = (frames, orientation) => {
|
|
209
361
|
if (applyOrientation && orientation > 1)
|
|
210
362
|
for (const frame of frames)
|
|
211
|
-
frame.__orient__(orientation);
|
|
363
|
+
frame.image.__orient__(orientation);
|
|
212
364
|
return frames;
|
|
213
365
|
};
|
|
214
366
|
const profile = (frames, icc, space) => {
|
|
215
367
|
const transform = !applyColorProfile ? null : icc ? profileTransform(icc) : space ? colorSpaceTransform(space.xy, space.curve) : null;
|
|
216
368
|
if (transform)
|
|
217
369
|
for (const frame of frames)
|
|
218
|
-
transform(frame.bitmap);
|
|
370
|
+
transform(frame.image.bitmap);
|
|
219
371
|
return frames;
|
|
220
372
|
};
|
|
221
373
|
const data = view(input);
|
|
222
374
|
const type = ImageType.getType(data);
|
|
223
375
|
if (type === 'gif')
|
|
224
|
-
return Animation.
|
|
376
|
+
return Animation.__gifFrames__(data, maxPixels);
|
|
225
377
|
if (type === 'webp') {
|
|
226
|
-
const
|
|
378
|
+
const animated = webp.isAnimated(data);
|
|
379
|
+
const decoded = webp.decodeAnimation(data, false, maxPixels);
|
|
227
380
|
const frames = orient(profile(canvasFrames(decoded.width, decoded.height, webp.composite(decoded)), decoded.icc), decoded.orientation);
|
|
228
|
-
|
|
381
|
+
const animation = new Animation(frames, { loopCount: fromPlays(decoded.loopCount), width: frames[0]?.width ?? decoded.width, height: frames[0]?.height ?? decoded.height });
|
|
382
|
+
animation.__animationSignal__ = animated;
|
|
383
|
+
return animation;
|
|
229
384
|
}
|
|
230
385
|
if (type === 'png' && !png.isAnimated(data)) {
|
|
231
386
|
const image = await Image.decode(data, { maxPixels, applyOrientation, applyColorProfile });
|
|
232
|
-
return new Animation(canvasFrames(image.width, image.height, [{ pixels: image.bitmap, duration: 0 }]),
|
|
387
|
+
return new Animation(canvasFrames(image.width, image.height, [{ pixels: image.bitmap, duration: 0 }]), { loopCount: Infinity, width: image.width, height: image.height });
|
|
233
388
|
}
|
|
234
389
|
if (type === 'png') {
|
|
235
|
-
const decoded = await png.decodeAnimation(data,
|
|
390
|
+
const decoded = await png.decodeAnimation(data, false, maxPixels);
|
|
236
391
|
const frames = orient(profile(canvasFrames(decoded.width, decoded.height, png.compositeAnimation(decoded)), decoded.icc), decoded.orientation);
|
|
237
|
-
|
|
392
|
+
const animation = new Animation(frames, { loopCount: fromPlays(decoded.plays), width: frames[0]?.width ?? decoded.width, height: frames[0]?.height ?? decoded.height });
|
|
393
|
+
animation.__animationSignal__ = true;
|
|
394
|
+
return animation;
|
|
238
395
|
}
|
|
239
396
|
if (type === 'jxl') {
|
|
240
397
|
const frames = [];
|
|
241
|
-
let
|
|
398
|
+
let loops = Infinity, animated = false, icc, space;
|
|
242
399
|
let orientation = 1;
|
|
243
400
|
for await (const decoded of jxl.frames(data, maxPixels, toneMapping, threads)) {
|
|
244
|
-
const
|
|
245
|
-
|
|
246
|
-
frames.push(
|
|
401
|
+
const image = new Image(decoded.width, decoded.height);
|
|
402
|
+
image.bitmap = alignedPixels(decoded.pixels);
|
|
403
|
+
frames.push(new Frame(image, { duration: decoded.duration, disposalMode: 'background' }));
|
|
247
404
|
orientation = decoded.orientation;
|
|
248
|
-
|
|
405
|
+
loops = fromPlays(decoded.loopCount);
|
|
406
|
+
animated ||= decoded.animated;
|
|
249
407
|
icc = decoded.icc;
|
|
250
408
|
space = decoded.colorSpace;
|
|
251
|
-
if (onlyExtractFirstFrame)
|
|
252
|
-
break;
|
|
253
409
|
}
|
|
254
410
|
const map = gainMap && data[0] !== 0xff ? await jxl.decodeGainMap(data, maxPixels).catch(() => undefined) : undefined;
|
|
255
411
|
if (map)
|
|
256
412
|
for (const frame of frames) {
|
|
257
413
|
const image = new Image(map.width, map.height);
|
|
258
414
|
image.bitmap = map.pixels.slice();
|
|
259
|
-
frame.gainMap = GainMap.__from_pixels__(image, map.params);
|
|
415
|
+
frame.image.gainMap = GainMap.__from_pixels__(image, map.params);
|
|
260
416
|
}
|
|
261
417
|
for (const frame of orient(profile(frames, icc, space), orientation)) {
|
|
262
|
-
const mapOfFrame = frame.gainMap;
|
|
418
|
+
const mapOfFrame = frame.image.gainMap;
|
|
263
419
|
if (!mapOfFrame || !applyOrientation || orientation <= 1)
|
|
264
420
|
continue;
|
|
265
421
|
try {
|
|
266
422
|
mapOfFrame.image = mapOfFrame.image.__orient__(orientation);
|
|
267
423
|
}
|
|
268
424
|
catch {
|
|
269
|
-
frame.gainMap = null;
|
|
425
|
+
frame.image.gainMap = null;
|
|
270
426
|
}
|
|
271
427
|
}
|
|
272
|
-
|
|
428
|
+
const animation = new Animation(frames, { loopCount: animated ? loops : Infinity, ...(frames.length ? { width: frames[0].width, height: frames[0].height } : {}) });
|
|
429
|
+
animation.__animationSignal__ = animated;
|
|
430
|
+
return animation;
|
|
431
|
+
}
|
|
432
|
+
if (type) {
|
|
433
|
+
const image = await Image.decode(data, { maxPixels, applyOrientation, applyColorProfile, toneMapping, gainMap, threads });
|
|
434
|
+
return new Animation([new Frame(image, { duration: 0 })], { width: image.width, height: image.height });
|
|
273
435
|
}
|
|
274
|
-
if (type)
|
|
275
|
-
return new Animation([Frame.from(await Image.decode(data, { maxPixels, applyOrientation, applyColorProfile, toneMapping, gainMap, threads }), 0)]);
|
|
276
436
|
throw new Error('Unsupported image type');
|
|
277
437
|
}
|
|
278
|
-
static
|
|
279
|
-
|
|
280
|
-
}
|
|
281
|
-
static __gifFrames__(data, onlyExtractFirstFrame, Class, maxPixels) {
|
|
282
|
-
const decoded = gifCodec.decode(data, onlyExtractFirstFrame, maxPixels);
|
|
438
|
+
static __gifFrames__(data, maxPixels) {
|
|
439
|
+
const decoded = gifCodec.decode(data, false, maxPixels);
|
|
283
440
|
const frames = gifCodec.composite(decoded).map(({ pixels, delay }) => ({ pixels, duration: 10 * delay }));
|
|
284
|
-
|
|
441
|
+
const animation = new Animation(canvasFrames(decoded.width, decoded.height, frames), { loopCount: decoded.loopCount, width: decoded.width, height: decoded.height });
|
|
442
|
+
animation.__animationSignal__ = decoded.hasLoopExtension;
|
|
443
|
+
return animation;
|
|
285
444
|
}
|
|
286
445
|
}
|
|
446
|
+
function canvasSize(width, height) {
|
|
447
|
+
if (!Number.isInteger(width) || !Number.isInteger(height) || width < 1 || height < 1)
|
|
448
|
+
throw new RangeError(`Invalid canvas size: ${width}x${height}`);
|
|
449
|
+
return [width, height];
|
|
450
|
+
}
|
|
@@ -1487,7 +1487,7 @@ export class TileDecoder {
|
|
|
1487
1487
|
const eobTok = m.symbol(cdf, eobCdf + ctx * 4, 2);
|
|
1488
1488
|
let tok = eobTok + 1;
|
|
1489
1489
|
let levelTok = tok * 0x41;
|
|
1490
|
-
let mag
|
|
1490
|
+
let mag;
|
|
1491
1491
|
let scanTable = null, stride, shift, shift2, mask, offsets = 0;
|
|
1492
1492
|
if (txClass === TX_CLASS_2D) {
|
|
1493
1493
|
const nonsquare = tx >= RTX_4X8 ? 1 : 0;
|
|
@@ -928,7 +928,7 @@ class TileEncoder {
|
|
|
928
928
|
const w = tdim(tx, W) * 4, h = tdim(tx, H) * 4, sw = Math.min(w, 32), sh = Math.min(h, 32);
|
|
929
929
|
const sx = plane ? this.ssx : 0, sy = plane ? this.ssy : 0;
|
|
930
930
|
const vw = Math.max(0, Math.min(w, (f.width + sx >> sx) - px)), vh = Math.max(0, Math.min(h, (f.height + sy >> sy) - py));
|
|
931
|
-
let eob
|
|
931
|
+
let eob;
|
|
932
932
|
if (!(this.w && b.skip)) {
|
|
933
933
|
const res = this.res, coefs = this.coefs, q = this.q;
|
|
934
934
|
let sse = 0;
|