imagescript 3.20.2 → 4.0.0-rc.1
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 +21 -12
- package/dist/animation-encoder.d.ts +1 -34
- package/dist/animation-encoder.js +24 -26
- package/dist/animation.d.ts +108 -122
- package/dist/animation.js +274 -110
- 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 +2 -2
- 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/colors.d.ts +8 -7
- package/dist/colors.js +13 -3
- package/dist/frame.d.ts +93 -102
- package/dist/frame.js +89 -62
- package/dist/gainmap.d.ts +88 -46
- package/dist/gainmap.js +73 -49
- package/dist/image-type.d.ts +6 -1
- package/dist/image-type.js +4 -1
- package/dist/image.d.ts +287 -732
- package/dist/image.js +255 -404
- package/dist/index.d.ts +15 -25
- package/dist/index.js +15 -14
- package/dist/render/svg.js +7 -2
- package/dist/text-layout.d.ts +5 -35
- package/dist/text-layout.js +9 -9
- package/dist/util/gain.js +12 -12
- package/dist/util/mem.d.ts +1 -1
- package/dist/util/options.js +20 -6
- package/dist/version.js +1 -1
- package/package.json +1 -1
- package/dist/gif.d.ts +0 -36
- package/dist/gif.js +0 -22
- package/dist/util/rotate.js +0 -43
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 } 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, 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 = 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;
|
|
123
|
+
return this;
|
|
124
|
+
}
|
|
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 = 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];
|
|
99
224
|
return this;
|
|
100
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
|
+
}
|
|
101
242
|
__validate__() {
|
|
102
|
-
for (const frame of this)
|
|
243
|
+
for (const frame of this.frames)
|
|
103
244
|
if (!(frame instanceof Frame))
|
|
104
245
|
throw new Error('Animation contains invalid frames');
|
|
105
246
|
}
|
|
106
|
-
async encodeGIF(
|
|
107
|
-
|
|
108
|
-
|
|
247
|
+
async encodeGIF(options = {}) {
|
|
248
|
+
checkOptions(options, 'encodeGIF({ colors: 64 })');
|
|
249
|
+
const { colors = 256, dither = 'floyd-steinberg', ditherStrength = 0.85, metadata = {}, optimize = true } = options;
|
|
250
|
+
const palette = paletteOptions(colors, dither, ditherStrength);
|
|
251
|
+
const loops = fitLoops(this.loopCount, 'GIF');
|
|
109
252
|
this.__validate__();
|
|
110
253
|
const frames = optimize
|
|
111
254
|
? 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 => ({
|
|
255
|
+
: Array.from(this.__clipped_frames__(), frame => ({
|
|
113
256
|
x: frame.xOffset,
|
|
114
257
|
y: frame.yOffset,
|
|
115
258
|
width: frame.width,
|
|
116
259
|
height: frame.height,
|
|
117
260
|
duration: frame.duration,
|
|
118
261
|
dispose: frame.__disposal_id__,
|
|
119
|
-
pixels: new Uint8Array(frame.bitmap.buffer, frame.bitmap.byteOffset, frame.bitmap.byteLength)
|
|
262
|
+
pixels: new Uint8Array(frame.image.bitmap.buffer, frame.image.bitmap.byteOffset, frame.image.bitmap.byteLength)
|
|
120
263
|
}));
|
|
121
264
|
const gifFrames = frames.map(({ duration, ...f }) => ({ ...f, delay: Math.round(duration / 10) }));
|
|
122
|
-
return gifCodec.encode({ width: this.width, height: this.height, loopCount:
|
|
265
|
+
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
266
|
}
|
|
124
|
-
async encodeAPNG(
|
|
125
|
-
|
|
126
|
-
|
|
267
|
+
async encodeAPNG(options = {}) {
|
|
268
|
+
checkOptions(options, 'encodeAPNG({ compression: 9 })');
|
|
269
|
+
const { compression = 4, metadata = {}, optimize = true } = options;
|
|
270
|
+
const loops = fitLoops(this.loopCount, 'APNG');
|
|
127
271
|
this.__validate__();
|
|
128
272
|
if (!this.length)
|
|
129
273
|
throw new Error('An animation needs at least one frame');
|
|
130
274
|
const frames = optimize
|
|
131
275
|
? this.__optimized__({ replace: true, even: false, alpha: frames => png.alphaCost(frames.map(f => f.pixels)) }).map(f => ({ ...f, dispose: f.dispose ? 1 : 0 }))
|
|
132
276
|
: this.__apngFrames__().map(({ blend: _, ...f }) => f);
|
|
133
|
-
return png.encodeAnimation(frames, this.width, this.height, toPlays(
|
|
277
|
+
return png.encodeAnimation(frames, this.width, this.height, toPlays(loops), compressionLevel(compression), pngText(resolveMetadata(metadata)));
|
|
134
278
|
}
|
|
135
|
-
async encodeWebP(
|
|
136
|
-
|
|
279
|
+
async encodeWebP(options = {}) {
|
|
280
|
+
checkOptions(options, 'animation.encodeWebP({ quality: 80 })');
|
|
281
|
+
const { metadata = {}, optimize = true, quality, effort = 2, sharpYuv = false, alphaQuality = 100 } = options;
|
|
282
|
+
const loops = fitLoops(this.loopCount, 'WebP');
|
|
137
283
|
this.__validate__();
|
|
138
284
|
if (!this.length)
|
|
139
285
|
throw new Error('An animation needs at least one frame');
|
|
140
286
|
const lossy = webp.lossyOptions(quality, effort, sharpYuv, alphaQuality);
|
|
141
287
|
const tolerance = lossy ? webp.lossyTolerance(lossy.quality) : undefined;
|
|
142
288
|
const frames = optimize ? this.__optimized__({ replace: true, even: true, alpha: 'frame', tolerance }) : this.__webpFrames__();
|
|
143
|
-
return webp.encodeAnimation(frames, this.width, this.height, toPlays(
|
|
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
|
-
return decoding(() => Animation.__decode__(input, onlyFirstFrameOrOptions));
|
|
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));
|
|
205
354
|
}
|
|
206
|
-
static
|
|
207
|
-
|
|
355
|
+
static __is_animated__(animation) {
|
|
356
|
+
return animation.__animationSignal__;
|
|
357
|
+
}
|
|
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 = 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
|
+
}
|
package/dist/codecs/gif.js
CHANGED
|
@@ -5,11 +5,11 @@ const DISPOSAL_TO_GIF = [0, 1, 3, 2];
|
|
|
5
5
|
const DISPOSAL_FROM_GIF = [0, 1, 3, 2, 0, 0, 0, 0];
|
|
6
6
|
const MAX_CODE = 4095;
|
|
7
7
|
const UNEXPECTED_END = new Error('GIF: unexpected end of data');
|
|
8
|
-
export function encode({ width, height, loopCount =
|
|
8
|
+
export function encode({ width, height, loopCount = Infinity, frames }, palette, comments = []) {
|
|
9
9
|
const restores = frames.some(frame => frame.dispose === 3);
|
|
10
10
|
const parts = [header(width, height, loopCount, comments)];
|
|
11
11
|
for (const [i, frame] of frames.entries())
|
|
12
|
-
parts.push(encodeFrame(frame,
|
|
12
|
+
parts.push(encodeFrame(frame, palette, frame.dispose === 3 || (i === 0 && restores)));
|
|
13
13
|
parts.push(new Uint8Array([0x3b]));
|
|
14
14
|
const out = new Uint8Array(parts.reduce((n, p) => n + p.length, 0));
|
|
15
15
|
let offset = 0;
|
|
@@ -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 ===
|
|
32
|
+
out.u16(loopCount === Infinity ? 0 : loopCount);
|
|
33
33
|
out.bytes(0x00);
|
|
34
34
|
}
|
|
35
35
|
for (const comment of comments) {
|
|
@@ -38,9 +38,9 @@ export function header(width, height, loopCount, comments) {
|
|
|
38
38
|
}
|
|
39
39
|
return out.finish();
|
|
40
40
|
}
|
|
41
|
-
export function encodeFrame(frame,
|
|
41
|
+
export function encodeFrame(frame, colors, transparent) {
|
|
42
42
|
const out = new GIFWriter();
|
|
43
|
-
const { palette, indices, transparentIndex } = quantize(frame.pixels, frame.width, {
|
|
43
|
+
const { palette, indices, transparentIndex } = quantize(frame.pixels, frame.width, { maxColors: colors.colors, dither: colors.dither, ditherStrength: colors.strength, transparent });
|
|
44
44
|
const entries = palette.length / 3;
|
|
45
45
|
const bits = Math.max(1, Math.ceil(Math.log2(entries)));
|
|
46
46
|
out.bytes(0x21, 0xf9, 0x04);
|
|
@@ -125,7 +125,7 @@ function writeSubBlocks(out, data) {
|
|
|
125
125
|
}
|
|
126
126
|
out.byte(0x00);
|
|
127
127
|
}
|
|
128
|
-
export function decode(data,
|
|
128
|
+
export function decode(data, firstFrameOnly = false, maxPixels = MAX_PIXELS) {
|
|
129
129
|
const reader = new ByteReader(data);
|
|
130
130
|
const signature = reader.string(6);
|
|
131
131
|
if (signature !== 'GIF87a' && signature !== 'GIF89a')
|
|
@@ -137,6 +137,7 @@ export function decode(data, onlyFirstFrame = false, maxPixels = MAX_PIXELS) {
|
|
|
137
137
|
const globalTable = flags & 0x80 ? reader.slice(3 << (flags & 7) + 1) : null;
|
|
138
138
|
const frames = [];
|
|
139
139
|
let loopCount = 0;
|
|
140
|
+
let hasLoopExtension = false;
|
|
140
141
|
let control = null;
|
|
141
142
|
const budget = new PixelBudget(maxPixels);
|
|
142
143
|
try {
|
|
@@ -147,7 +148,7 @@ export function decode(data, onlyFirstFrame = false, maxPixels = MAX_PIXELS) {
|
|
|
147
148
|
throw err;
|
|
148
149
|
}
|
|
149
150
|
checkPixels(width, height, maxPixels, Math.max(1, frames.length));
|
|
150
|
-
return { width, height, loopCount, frames };
|
|
151
|
+
return { width, height, loopCount, hasLoopExtension, frames };
|
|
151
152
|
function parse() {
|
|
152
153
|
while (reader.offset < data.length) {
|
|
153
154
|
const block = reader.u8();
|
|
@@ -170,11 +171,12 @@ export function decode(data, onlyFirstFrame = false, maxPixels = MAX_PIXELS) {
|
|
|
170
171
|
const size = reader.u8();
|
|
171
172
|
const id = reader.string(size);
|
|
172
173
|
if (id === 'NETSCAPE2.0' || id === 'ANIMEXTS1.0') {
|
|
174
|
+
hasLoopExtension = true;
|
|
173
175
|
for (let length; (length = reader.u8());) {
|
|
174
176
|
const sub = reader.slice(length);
|
|
175
177
|
if (sub[0] === 1 && length >= 3) {
|
|
176
178
|
const count = sub[1] | sub[2] << 8;
|
|
177
|
-
loopCount = count === 0 ?
|
|
179
|
+
loopCount = count === 0 ? Infinity : count;
|
|
178
180
|
}
|
|
179
181
|
}
|
|
180
182
|
}
|
|
@@ -220,7 +222,7 @@ export function decode(data, onlyFirstFrame = false, maxPixels = MAX_PIXELS) {
|
|
|
220
222
|
ordered.set(indices.subarray(row * fw, (row + 1) * fw), rows[row] * fw);
|
|
221
223
|
}
|
|
222
224
|
frames.push({ x, y, width: fw, height: fh, delay, dispose, indices: ordered, colors, transparentIndex });
|
|
223
|
-
if (
|
|
225
|
+
if (firstFrameOnly)
|
|
224
226
|
break;
|
|
225
227
|
}
|
|
226
228
|
}
|
package/dist/codecs/hdr.d.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
export type ToneMapping = 'reinhard' | 'clamp';
|
|
3
3
|
/**
|
|
4
4
|
* Linear light RGB (3 values per pixel) as sRGB RGBA. Reinhard's extended operator ("Photographic Tone Reproduction
|
|
5
|
-
* for Digital Images", 2002, equation 4) without its automatic exposure, which makes dark scenes like nights
|
|
5
|
+
* for Digital Images", 2002, equation 4) without its automatic exposure, which makes dark scenes like nights gray:
|
|
6
6
|
* the luminance L becomes L (1 + L / Lw²) / (1 + L), the brightest pixel's (at least 1) Lw white, and the channels are
|
|
7
7
|
* scaled by that, keeping their hue; images no brighter than white stay as they are
|
|
8
8
|
*/
|