imagescript 4.0.0-rc.4 → 4.0.0-rc.6
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 +9 -5
- package/dist/animation-encoder.d.ts +2 -1
- package/dist/animation-encoder.js +23 -19
- package/dist/animation.d.ts +26 -20
- package/dist/animation.js +58 -55
- package/dist/codecs/avif-enc.js +3 -3
- package/dist/codecs/avif.js +7 -2
- package/dist/codecs/bmp.js +1 -1
- package/dist/codecs/gif.js +2 -2
- package/dist/codecs/jpeg.js +55 -14
- package/dist/codecs/jxl/vardct-enc.js +2 -1
- package/dist/codecs/png.js +7 -3
- package/dist/codecs/tga.js +4 -3
- package/dist/codecs/tiff.js +1 -1
- package/dist/codecs/vp8.js +1 -1
- package/dist/codecs/vp8l.js +4 -3
- package/dist/codecs/webp.js +11 -9
- package/dist/colors.d.ts +10 -8
- package/dist/colors.js +28 -31
- package/dist/frame.d.ts +4 -3
- package/dist/frame.js +6 -8
- package/dist/gainmap.d.ts +6 -6
- package/dist/gainmap.js +32 -24
- package/dist/image.d.ts +75 -37
- package/dist/image.js +189 -163
- package/dist/metadata.d.ts +12 -1
- package/dist/metadata.js +30 -5
- package/dist/render/color.js +16 -3
- package/dist/render/colr.js +3 -3
- package/dist/render/filter.js +27 -3
- package/dist/render/font.js +54 -18
- package/dist/render/layout.js +2 -2
- package/dist/render/linebreak.js +9 -12
- package/dist/render/svg-text.js +112 -28
- package/dist/render/svg.js +183 -34
- package/dist/render/text.js +93 -23
- package/dist/text-layout.d.ts +2 -1
- package/dist/text-layout.js +3 -8
- package/dist/util/alpha.js +1 -1
- package/dist/util/inspect.js +16 -0
- package/dist/util/limits.d.ts +16 -3
- package/dist/util/limits.js +20 -5
- package/dist/util/mem.d.ts +2 -0
- package/dist/util/mem.js +24 -4
- package/dist/util/optimize.js +4 -3
- package/dist/util/options.js +104 -7
- package/package.json +1 -1
package/dist/render/text.js
CHANGED
|
@@ -4,8 +4,9 @@
|
|
|
4
4
|
*/
|
|
5
5
|
import { Font } from "./font.js";
|
|
6
6
|
import { Hinter } from "./hinting.js";
|
|
7
|
-
import { parseColorLayers, parseBitmaps } from "./color.js";
|
|
7
|
+
import { parseColorLayers, parseBitmaps, bitmapPixels, MAX_BITMAP_GLYPH_PIXELS } from "./color.js";
|
|
8
8
|
import { shapeFont, shapeRun, isDefaultIgnorableCodepoint } from "./shape.js";
|
|
9
|
+
import { shown } from "../util/options.js";
|
|
9
10
|
import "./shapers/index.js";
|
|
10
11
|
import { classOf, resolve, lineLevels } from "./bidi.js";
|
|
11
12
|
import { lineBreaks } from "./linebreak.js";
|
|
@@ -39,7 +40,9 @@ export function axisValues(variations) {
|
|
|
39
40
|
if (!tag)
|
|
40
41
|
throw new RangeError(`Unknown font variation ${key}: use weight, width, slant, italic, opticalSize or an axis tag`);
|
|
41
42
|
const v = typeof value === 'boolean' ? +value : value;
|
|
42
|
-
if (typeof v !== 'number'
|
|
43
|
+
if (typeof v !== 'number')
|
|
44
|
+
throw new TypeError(`The font variation ${key} must be a number or a boolean, not ${shown(v)}`);
|
|
45
|
+
if (!Number.isFinite(v))
|
|
43
46
|
throw new RangeError(`Invalid font variation ${key}: ${String(value)}`);
|
|
44
47
|
values[tag] = v;
|
|
45
48
|
}
|
|
@@ -65,8 +68,10 @@ function sizedFont(data, size, hinting, variations) {
|
|
|
65
68
|
let shared = colorTables.get(font);
|
|
66
69
|
if (!shared)
|
|
67
70
|
colorTables.set(font, shared = { layers: parseColorLayers(font), paints: parsePaintGlyphs(font) });
|
|
68
|
-
if (bySize.size >= SIZES)
|
|
71
|
+
if (bySize.size >= SIZES) {
|
|
69
72
|
bySize.clear();
|
|
73
|
+
keptBytes.delete(font);
|
|
74
|
+
}
|
|
70
75
|
bySize.set(key, sized = { font, scale: size / font.unitsPerEm, hinter, layers: shared.layers, bitmaps: parseBitmaps(font, size), paints: shared.paints, shaping: shapeFont(font), glyphs: new Map() });
|
|
71
76
|
return sized;
|
|
72
77
|
}
|
|
@@ -124,7 +129,8 @@ async function layOut(fonts, size, text, layout, hinting, foreground, features,
|
|
|
124
129
|
const paragraphs = text.split(/\r\n|[\r\n\u0085\u2028\u2029]/);
|
|
125
130
|
const lines = [];
|
|
126
131
|
for (const paragraph of paragraphs)
|
|
127
|
-
|
|
132
|
+
for (const line of layOutParagraph(analyze(paragraph, fonts.length, font), layout, font, size, features))
|
|
133
|
+
lines.push(line);
|
|
128
134
|
let visible = lines;
|
|
129
135
|
if (layout.height !== undefined) {
|
|
130
136
|
const count = Math.max(1, Math.floor(layout.height / lineHeight));
|
|
@@ -134,7 +140,10 @@ async function layOut(fonts, size, text, layout, hinting, foreground, features,
|
|
|
134
140
|
visible = lines.slice(start, start + count);
|
|
135
141
|
}
|
|
136
142
|
}
|
|
137
|
-
|
|
143
|
+
let extent = 0;
|
|
144
|
+
for (const line of visible)
|
|
145
|
+
extent = Math.max(extent, line.extent);
|
|
146
|
+
const width = layout.width ?? Math.max(1, Math.ceil(extent));
|
|
138
147
|
const height = layout.height ?? visible.length * lineHeight;
|
|
139
148
|
const free = height - visible.length * lineHeight;
|
|
140
149
|
const top = layout.verticalAlign === 'bottom' ? free : layout.verticalAlign === 'middle' ? Math.floor(free / 2) : 0;
|
|
@@ -142,9 +151,27 @@ async function layOut(fonts, size, text, layout, hinting, foreground, features,
|
|
|
142
151
|
throw new LimitError(`Text too large to render: ${width + 2 * pad}x${height + 2 * pad} pixels exceed the limit of ${maxPixels} (the render options' maxPixels)`);
|
|
143
152
|
const coverage = new Uint8Array(width * height);
|
|
144
153
|
let color = null;
|
|
154
|
+
const budget = { left: 2 ** 28 + 64 * width * height, image: width * height };
|
|
155
|
+
const scaled = new Map();
|
|
156
|
+
let scaledPixels = 0;
|
|
157
|
+
const local = new Map();
|
|
158
|
+
let localBytes = 0;
|
|
159
|
+
const keep = (g, key, raster, withFont) => {
|
|
160
|
+
const bytes = raster ? raster.data.length : 0;
|
|
161
|
+
if (keptWithFont(g, bytes))
|
|
162
|
+
withFont();
|
|
163
|
+
else if (localBytes + bytes <= 2 ** 26) {
|
|
164
|
+
localBytes += bytes;
|
|
165
|
+
let rasters = local.get(g);
|
|
166
|
+
if (!rasters)
|
|
167
|
+
local.set(g, rasters = new Map());
|
|
168
|
+
rasters.set(key, raster);
|
|
169
|
+
}
|
|
170
|
+
return raster;
|
|
171
|
+
};
|
|
145
172
|
for (const [n, line] of visible.entries()) {
|
|
146
|
-
|
|
147
|
-
|
|
173
|
+
const align = layout.horizontalAlign ?? (line.rtl ? 'right' : 'left');
|
|
174
|
+
let offset = align === 'right' ? width - line.extent : align === 'center' ? (width - line.extent - line.start) / 2 : -line.start;
|
|
148
175
|
if (primary.hinter)
|
|
149
176
|
offset = Math.round(offset);
|
|
150
177
|
const baseline = top + ascent + n * lineHeight;
|
|
@@ -160,24 +187,53 @@ async function layOut(fonts, size, text, layout, hinting, foreground, features,
|
|
|
160
187
|
subpixel = 0;
|
|
161
188
|
const key = 'bitmap' in g.color ? '0' : `${subpixel}:${foreground}`;
|
|
162
189
|
let bitmap = g.colors.get(key);
|
|
163
|
-
if (bitmap
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
190
|
+
if ('bitmap' in g.color) {
|
|
191
|
+
bitmap = scaled.get(g);
|
|
192
|
+
if (bitmap === undefined) {
|
|
193
|
+
bitmap = await scaledBitmap(g.color.bitmap, size / g.color.bitmap.ppem, budget);
|
|
194
|
+
const pixels = bitmap ? bitmap.width * bitmap.height : 0;
|
|
195
|
+
if (scaledPixels + pixels <= 2 ** 22) {
|
|
196
|
+
scaledPixels += pixels;
|
|
197
|
+
scaled.set(g, bitmap);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
else if (bitmap === undefined) {
|
|
202
|
+
bitmap = local.get(g)?.get(key);
|
|
203
|
+
if (bitmap === undefined) {
|
|
204
|
+
const made = 'layers' in g.color ? layeredGlyph(g, g.color.layers, subpixel / SUBPIXELS, foreground) : paintedGlyph(g, subpixel / SUBPIXELS, foreground);
|
|
205
|
+
spend(budget, made ? made.width * made.height : 0);
|
|
206
|
+
bitmap = g.colors.size < 8 ? keep(g, key, made, () => g.colors.set(key, made)) : keep(g, key, made, () => { });
|
|
207
|
+
}
|
|
208
|
+
}
|
|
168
209
|
if (bitmap)
|
|
169
210
|
blitColor(color ??= new Uint8ClampedArray(width * height * 4), width, height, bitmap, pixel + bitmap.left, y - bitmap.top);
|
|
170
211
|
continue;
|
|
171
212
|
}
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
213
|
+
let bitmap = g.bitmaps[subpixel];
|
|
214
|
+
if (bitmap === undefined)
|
|
215
|
+
bitmap = local.get(g)?.get(`${subpixel}`);
|
|
216
|
+
if (bitmap === undefined) {
|
|
217
|
+
const made = rasterizeGlyph(g.path, g.scale, subpixel / SUBPIXELS);
|
|
218
|
+
spend(budget, made ? made.width * made.height : 0);
|
|
219
|
+
bitmap = keep(g, `${subpixel}`, made, () => g.bitmaps[subpixel] = made);
|
|
220
|
+
}
|
|
175
221
|
if (bitmap)
|
|
176
222
|
blit(coverage, width, height, bitmap, pixel + bitmap.left, y - bitmap.top);
|
|
177
223
|
}
|
|
178
224
|
}
|
|
179
225
|
return { width, height, coverage, color };
|
|
180
226
|
}
|
|
227
|
+
const keptBytes = new WeakMap();
|
|
228
|
+
function keptWithFont(g, bytes) {
|
|
229
|
+
if (bytes > 2 ** 18)
|
|
230
|
+
return false;
|
|
231
|
+
const kept = (keptBytes.get(g.font.font) ?? 0) + bytes;
|
|
232
|
+
if (kept > 2 ** 22)
|
|
233
|
+
return false;
|
|
234
|
+
keptBytes.set(g.font.font, kept);
|
|
235
|
+
return true;
|
|
236
|
+
}
|
|
181
237
|
function analyze(text, count, font) {
|
|
182
238
|
const codepoints = Array.from(text, char => char.codePointAt(0));
|
|
183
239
|
const n = codepoints.length;
|
|
@@ -201,7 +257,7 @@ function analyze(text, count, font) {
|
|
|
201
257
|
}
|
|
202
258
|
return first;
|
|
203
259
|
};
|
|
204
|
-
const normalized = (grapheme, form) => Array.from(String.fromCodePoint(
|
|
260
|
+
const normalized = (grapheme, form) => Array.from(grapheme.map(c => String.fromCodePoint(c)).join('').normalize(form), char => char.codePointAt(0));
|
|
205
261
|
const graphemes = graphemeBoundaries(codepoints), fontOf = new Int32Array(n);
|
|
206
262
|
for (let start = 0; start < n;) {
|
|
207
263
|
let end = start + 1;
|
|
@@ -287,8 +343,9 @@ function shapeRange(p, start, end, levels, font, size, features) {
|
|
|
287
343
|
const isSpace = (codepoint) => codepoint === 0x20 || codepoint === 0x09 || codepoint === 0x3000 || codepoint >= 0x2000 && codepoint <= 0x200a;
|
|
288
344
|
function layOutParagraph(p, layout, font, size, features) {
|
|
289
345
|
const n = p.codepoints.length;
|
|
346
|
+
const rtl = (p.bidi.level & 1) === 1;
|
|
290
347
|
if (!n)
|
|
291
|
-
return [{ glyphs: [], extent: 0 }];
|
|
348
|
+
return [{ glyphs: [], extent: 0, start: 0, rtl }];
|
|
292
349
|
const whole = shapeRange(p, 0, n, p.bidi.levels, font, size, features);
|
|
293
350
|
const width = new Float64Array(n), starts = new Uint8Array(n + 1);
|
|
294
351
|
starts[n] = 1;
|
|
@@ -331,27 +388,40 @@ function layOutParagraph(p, layout, font, size, features) {
|
|
|
331
388
|
ranges.push([lineStart, fit]);
|
|
332
389
|
lineStart = fit;
|
|
333
390
|
}
|
|
391
|
+
const shifted = new Int8Array(n);
|
|
334
392
|
return ranges.map(([start, end]) => {
|
|
335
393
|
const levels = lineLevels(p.bidi, start, end);
|
|
336
|
-
const shifted = new Int8Array(n);
|
|
337
394
|
shifted.set(levels, start);
|
|
338
395
|
const { glyphs } = ranges.length === 1 ? whole : shapeRange(p, start, end, shifted, font, size, features);
|
|
339
|
-
let extent = 0;
|
|
396
|
+
let extent = 0, left = Infinity;
|
|
340
397
|
for (const { x, dx, glyph } of glyphs)
|
|
341
|
-
if (glyph.ink)
|
|
398
|
+
if (glyph.ink) {
|
|
342
399
|
extent = Math.max(extent, x + dx + glyph.ink.right);
|
|
343
|
-
|
|
400
|
+
left = Math.min(left, x + dx + glyph.ink.left);
|
|
401
|
+
}
|
|
402
|
+
return { glyphs, extent, start: rtl && left < Infinity ? Math.max(0, left) : 0, rtl };
|
|
344
403
|
});
|
|
345
404
|
}
|
|
346
|
-
|
|
405
|
+
function spend(budget, pixels) {
|
|
406
|
+
if ((budget.left -= pixels) < 0)
|
|
407
|
+
throw new LimitError('Text too complex: its glyphs take more work to decode, scale and rasterize than an image of this size allows (2^28 pixels and 64 per pixel of the image)');
|
|
408
|
+
}
|
|
409
|
+
async function scaledBitmap(glyph, scale, budget) {
|
|
410
|
+
const declared = bitmapPixels(glyph.png);
|
|
411
|
+
if (!(declared <= MAX_BITMAP_GLYPH_PIXELS))
|
|
412
|
+
return null;
|
|
413
|
+
spend(budget, 4 * Math.max(declared, 1));
|
|
347
414
|
let decoded;
|
|
348
415
|
try {
|
|
349
|
-
decoded = await decodePNG(glyph.png);
|
|
416
|
+
decoded = await decodePNG(glyph.png, MAX_BITMAP_GLYPH_PIXELS);
|
|
350
417
|
}
|
|
351
418
|
catch {
|
|
352
419
|
return null;
|
|
353
420
|
}
|
|
354
421
|
const width = Math.max(1, Math.round(decoded.width * scale)), height = Math.max(1, Math.round(decoded.height * scale));
|
|
422
|
+
if (width * height > Math.max(MAX_BITMAP_GLYPH_PIXELS, 4 * budget.image))
|
|
423
|
+
return null;
|
|
424
|
+
spend(budget, width * height);
|
|
355
425
|
const data = new Uint8ClampedArray(width * height * 4);
|
|
356
426
|
resample(new Uint8ClampedArray(decoded.pixels.buffer, decoded.pixels.byteOffset, decoded.pixels.length), decoded.width, decoded.height, width, height, TRIANGLE, data, true);
|
|
357
427
|
premultiply(data);
|
package/dist/text-layout.d.ts
CHANGED
|
@@ -25,7 +25,8 @@ export interface TextLayoutOptions {
|
|
|
25
25
|
/**
|
|
26
26
|
* Where the lines are placed horizontally: `'left'`, `'center'` or `'right'` in the `width`, or (without one)
|
|
27
27
|
* relative to the widest line
|
|
28
|
-
* @defaultValue `'left'`
|
|
28
|
+
* @defaultValue The paragraph's direction: `'left'` for left-to-right text, `'right'` for right-to-left text
|
|
29
|
+
* (Arabic, Hebrew, ...), like the `start` of CSS' `text-align`
|
|
29
30
|
*/
|
|
30
31
|
horizontalAlign?: HorizontalAlign;
|
|
31
32
|
/**
|
package/dist/text-layout.js
CHANGED
|
@@ -1,13 +1,8 @@
|
|
|
1
|
-
import { oneOf } from "./util/options.js";
|
|
1
|
+
import { oneOf, pixels } from "./util/options.js";
|
|
2
2
|
const HORIZONTAL = ['left', 'center', 'right'];
|
|
3
3
|
const VERTICAL = ['top', 'middle', 'bottom'];
|
|
4
4
|
function boxSide(value, name) {
|
|
5
|
-
|
|
6
|
-
return undefined;
|
|
7
|
-
const side = Math.floor(Number(value));
|
|
8
|
-
if (typeof value !== 'number' || !(side >= 1) || !Number.isFinite(side))
|
|
9
|
-
throw new RangeError(`${name} must be a finite number from 1, not ${String(value)}`);
|
|
10
|
-
return side;
|
|
5
|
+
return value === undefined ? undefined : pixels(value, name);
|
|
11
6
|
}
|
|
12
7
|
export function textLayout(options) {
|
|
13
8
|
const { width, height, wrapStyle, verticalAlign, horizontalAlign } = options;
|
|
@@ -15,7 +10,7 @@ export function textLayout(options) {
|
|
|
15
10
|
width: boxSide(width, 'width'),
|
|
16
11
|
height: boxSide(height, 'height'),
|
|
17
12
|
wrapStyle: wrapStyle === undefined ? 'word' : oneOf(wrapStyle, ['word', 'char'], 'wrapStyle'),
|
|
18
|
-
horizontalAlign: horizontalAlign === undefined ?
|
|
13
|
+
horizontalAlign: horizontalAlign === undefined ? undefined : oneOf(horizontalAlign, HORIZONTAL, 'horizontalAlign'),
|
|
19
14
|
verticalAlign: verticalAlign === undefined ? 'top' : oneOf(verticalAlign, VERTICAL, 'verticalAlign')
|
|
20
15
|
};
|
|
21
16
|
}
|
package/dist/util/alpha.js
CHANGED
|
@@ -2,7 +2,7 @@ export let littleEndian = new Uint8Array(new Uint32Array([1]).buffer)[0] === 1;
|
|
|
2
2
|
export function useLittleEndian(value) {
|
|
3
3
|
littleEndian = value;
|
|
4
4
|
}
|
|
5
|
-
const words = (bitmap) => new Int32Array(bitmap.buffer, bitmap.byteOffset, bitmap.length
|
|
5
|
+
const words = (bitmap) => new Int32Array(bitmap.buffer, bitmap.byteOffset, bitmap.length >>> 2);
|
|
6
6
|
export function isOpaque(bitmap) {
|
|
7
7
|
const pixels = words(bitmap);
|
|
8
8
|
let all = -1;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export function describe(type, json) {
|
|
2
|
+
Object.defineProperty(type.prototype, Symbol.for('nodejs.util.inspect.custom'), {
|
|
3
|
+
value() {
|
|
4
|
+
return this.toString();
|
|
5
|
+
},
|
|
6
|
+
writable: true,
|
|
7
|
+
configurable: true
|
|
8
|
+
});
|
|
9
|
+
Object.defineProperty(type.prototype, 'toJSON', {
|
|
10
|
+
value() {
|
|
11
|
+
return json(this);
|
|
12
|
+
},
|
|
13
|
+
writable: true,
|
|
14
|
+
configurable: true
|
|
15
|
+
});
|
|
16
|
+
}
|
package/dist/util/limits.d.ts
CHANGED
|
@@ -8,11 +8,21 @@ export declare class LimitError extends RangeError {
|
|
|
8
8
|
}
|
|
9
9
|
/** The default of the decode options' maxPixels: 16383 squared (like sharp's limitInputPixels) */
|
|
10
10
|
export declare const MAX_PIXELS: number;
|
|
11
|
+
/**
|
|
12
|
+
* What a frame of an animation counts at least: a frame takes memory however small it is (200000 frames of 1x1, a
|
|
13
|
+
* 4.6 MB GIF, took 720 MB), so the limit bounds the memory of tiny frames too
|
|
14
|
+
*/
|
|
15
|
+
export declare const FRAME_PIXELS = 1024;
|
|
11
16
|
/**
|
|
12
17
|
* Throws if decoding an image of the given size would exceed the limit, before anything that size is allocated
|
|
13
|
-
* @param count the number of such images (frames)
|
|
18
|
+
* @param count the number of such images (frames of an animation, each counting at least {@link FRAME_PIXELS})
|
|
14
19
|
*/
|
|
15
20
|
export declare function checkPixels(width: number, height: number, limit: number, count?: number): void;
|
|
21
|
+
/**
|
|
22
|
+
* Throws a RangeError for an image that a format can't store: one message for all encoders, naming the format, what it
|
|
23
|
+
* allows and the size it got
|
|
24
|
+
*/
|
|
25
|
+
export declare function checkFormatSize(format: string, width: number, height: number, max: number): void;
|
|
16
26
|
/**
|
|
17
27
|
* Throws a {@link LimitError} naming the operation if an image it makes would have more pixels than an image can
|
|
18
28
|
* (the default `maxPixels` of the decoders): a shadow or outline of a huge size or offset would only fail allocating
|
|
@@ -25,11 +35,14 @@ export declare const MAX_BYTES_PER_PIXEL = 8;
|
|
|
25
35
|
* samples per pixel
|
|
26
36
|
*/
|
|
27
37
|
export declare function checkBytes(bytes: number, limit: number): void;
|
|
28
|
-
/** Counts the pixels of decoded frames against a limit */
|
|
38
|
+
/** Counts the pixels of decoded frames against a limit, each frame at least `floor` ({@link FRAME_PIXELS} for animations) */
|
|
29
39
|
export declare class PixelBudget {
|
|
40
|
+
private readonly floor;
|
|
30
41
|
private left;
|
|
31
42
|
private limit;
|
|
32
|
-
|
|
43
|
+
private count;
|
|
44
|
+
private first;
|
|
45
|
+
constructor(limit: number, floor?: number);
|
|
33
46
|
/** Throws if another frame of the given size would exceed the limit */
|
|
34
47
|
take(width: number, height: number): void;
|
|
35
48
|
}
|
package/dist/util/limits.js
CHANGED
|
@@ -2,9 +2,15 @@ export class LimitError extends RangeError {
|
|
|
2
2
|
name = 'LimitError';
|
|
3
3
|
}
|
|
4
4
|
export const MAX_PIXELS = 0x3fff * 0x3fff;
|
|
5
|
+
export const FRAME_PIXELS = 1024;
|
|
5
6
|
export function checkPixels(width, height, limit, count = 1) {
|
|
6
|
-
|
|
7
|
-
|
|
7
|
+
const floor = count > 1 && width * height < FRAME_PIXELS;
|
|
8
|
+
if (!((floor ? FRAME_PIXELS : width * height) * count <= limit))
|
|
9
|
+
throw new LimitError(`Image too large: ${count > 1 ? `${count} frames of ` : ''}${width}x${height} pixels exceed the limit of ${limit}${floor ? ` (each frame counts at least ${FRAME_PIXELS})` : ''} (the decode options' maxPixels)`);
|
|
10
|
+
}
|
|
11
|
+
export function checkFormatSize(format, width, height, max) {
|
|
12
|
+
if (!(width <= max && height <= max))
|
|
13
|
+
throw new RangeError(`${format}: images can be at most ${max} pixels wide and high, not ${width}x${height}`);
|
|
8
14
|
}
|
|
9
15
|
export function checkGrowth(width, height, what) {
|
|
10
16
|
if (!(width * height <= MAX_PIXELS))
|
|
@@ -16,15 +22,24 @@ export function checkBytes(bytes, limit) {
|
|
|
16
22
|
throw new LimitError(`Image too large: ${bytes} bytes of samples exceed what the limit of ${limit} pixels allows (the decode options' maxPixels)`);
|
|
17
23
|
}
|
|
18
24
|
export class PixelBudget {
|
|
25
|
+
floor;
|
|
19
26
|
left;
|
|
20
27
|
limit;
|
|
21
|
-
|
|
28
|
+
count = 0;
|
|
29
|
+
first = 0;
|
|
30
|
+
constructor(limit, floor = 0) {
|
|
31
|
+
this.floor = floor;
|
|
22
32
|
this.limit = this.left = limit;
|
|
23
33
|
}
|
|
24
34
|
take(width, height) {
|
|
25
|
-
|
|
35
|
+
const pixels = width * height;
|
|
36
|
+
if (++this.count === 1)
|
|
37
|
+
this.first = pixels;
|
|
38
|
+
else if (this.count === 2)
|
|
39
|
+
this.left -= Math.max(0, this.floor - this.first);
|
|
40
|
+
this.left -= this.count === 1 ? pixels : Math.max(pixels, this.floor);
|
|
26
41
|
if (!(this.left >= 0))
|
|
27
|
-
throw new LimitError(`Image too large:
|
|
42
|
+
throw new LimitError(`Image too large: ${this.count} frames exceed the limit of ${this.limit} pixels${this.floor ? `, at least ${this.floor} each` : ''} (the decode options' maxPixels)`);
|
|
28
43
|
}
|
|
29
44
|
}
|
|
30
45
|
export async function decoding(decode) {
|
package/dist/util/mem.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
/** Data accepted wherever binary input is expected */
|
|
2
2
|
export type BinaryData = ArrayBuffer | ArrayBufferView;
|
|
3
|
+
/** Whether a value is binary data (an ArrayBuffer, a SharedArrayBuffer or a view), also one of another realm */
|
|
4
|
+
export declare function isBinaryData(value: unknown): value is BinaryData;
|
|
3
5
|
/**
|
|
4
6
|
* Returns a Uint8Array view of the given buffer without copying
|
|
5
7
|
*/
|
package/dist/util/mem.js
CHANGED
|
@@ -1,13 +1,33 @@
|
|
|
1
|
+
import { shown } from "./options.js";
|
|
2
|
+
const byteLengthOf = Object.getOwnPropertyDescriptor(ArrayBuffer.prototype, 'byteLength').get;
|
|
3
|
+
const sharedByteLengthOf = typeof SharedArrayBuffer === 'undefined' ? null : Object.getOwnPropertyDescriptor(SharedArrayBuffer.prototype, 'byteLength').get;
|
|
4
|
+
function isBuffer(value) {
|
|
5
|
+
if (typeof value !== 'object' || value === null)
|
|
6
|
+
return false;
|
|
7
|
+
for (const getter of [byteLengthOf, sharedByteLengthOf]) {
|
|
8
|
+
try {
|
|
9
|
+
getter?.call(value);
|
|
10
|
+
if (getter)
|
|
11
|
+
return true;
|
|
12
|
+
}
|
|
13
|
+
catch { }
|
|
14
|
+
}
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
export function isBinaryData(value) {
|
|
18
|
+
return ArrayBuffer.isView(value) || isBuffer(value);
|
|
19
|
+
}
|
|
1
20
|
export function view(buffer) {
|
|
21
|
+
const data = ArrayBuffer.isView(buffer) ? buffer.buffer : buffer;
|
|
22
|
+
if (isBuffer(data) && data.detached === true)
|
|
23
|
+
throw new TypeError('Binary data has to be readable, not of a detached ArrayBuffer (one transferred, to a worker for one)');
|
|
2
24
|
if (buffer instanceof Uint8Array && buffer.constructor === Uint8Array)
|
|
3
25
|
return buffer;
|
|
4
|
-
if (buffer instanceof ArrayBuffer)
|
|
5
|
-
return new Uint8Array(buffer);
|
|
6
26
|
if (ArrayBuffer.isView(buffer))
|
|
7
27
|
return new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength);
|
|
8
|
-
if (
|
|
28
|
+
if (isBuffer(buffer))
|
|
9
29
|
return new Uint8Array(buffer);
|
|
10
|
-
throw new TypeError(
|
|
30
|
+
throw new TypeError(`Binary data has to be an ArrayBuffer or a typed array like Uint8Array, not ${shown(buffer)}`);
|
|
11
31
|
}
|
|
12
32
|
export function concat(parts) {
|
|
13
33
|
let length = 0;
|
package/dist/util/optimize.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { allocate } from "./options.js";
|
|
1
2
|
const ALPHA = new Int32Array(new Uint8Array([0, 0, 0, 255]).buffer)[0];
|
|
2
3
|
const ALPHA_COST = 4 / 3;
|
|
3
4
|
export class Optimizer {
|
|
@@ -19,8 +20,8 @@ export class Optimizer {
|
|
|
19
20
|
this.height = height;
|
|
20
21
|
this.options = options;
|
|
21
22
|
const size = width * height * 4;
|
|
22
|
-
this.canvas = new Uint8Array(size);
|
|
23
|
-
this.shown = new Uint8Array(size);
|
|
23
|
+
this.canvas = allocate(width, height, () => new Uint8Array(size));
|
|
24
|
+
this.shown = allocate(width, height, () => new Uint8Array(size));
|
|
24
25
|
this.state = { canvas32: new Int32Array(this.canvas.buffer), shown32: new Int32Array(this.shown.buffer), width, replace: options.replace, tolerance: options.tolerance };
|
|
25
26
|
this.cleared = { x: 0, y: 0, width, height };
|
|
26
27
|
}
|
|
@@ -130,7 +131,7 @@ export class Compositor {
|
|
|
130
131
|
constructor(width, height) {
|
|
131
132
|
this.width = width;
|
|
132
133
|
this.height = height;
|
|
133
|
-
this.canvas = new Uint8Array(width * height * 4);
|
|
134
|
+
this.canvas = allocate(width, height, () => new Uint8Array(width * height * 4));
|
|
134
135
|
}
|
|
135
136
|
push(f) {
|
|
136
137
|
const { canvas, width, height } = this;
|
package/dist/util/options.js
CHANGED
|
@@ -1,31 +1,74 @@
|
|
|
1
|
+
export function shown(value) {
|
|
2
|
+
if (value === null)
|
|
3
|
+
return 'null';
|
|
4
|
+
if (Array.isArray(value))
|
|
5
|
+
return 'an array';
|
|
6
|
+
if (typeof value === 'string') {
|
|
7
|
+
const characters = Array.from(value.length > 80 ? value.slice(0, 82) : value);
|
|
8
|
+
return characters.length > 40 ? `'${characters.slice(0, 40).join('')}...'` : `'${value}'`;
|
|
9
|
+
}
|
|
10
|
+
if (typeof value === 'number')
|
|
11
|
+
return String(value);
|
|
12
|
+
if (typeof value === 'object')
|
|
13
|
+
return 'an object';
|
|
14
|
+
return typeof value;
|
|
15
|
+
}
|
|
1
16
|
export function isOptions(value) {
|
|
2
|
-
return typeof value === 'object' && value !== null && !ArrayBuffer.isView(value);
|
|
17
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value) && !ArrayBuffer.isView(value);
|
|
3
18
|
}
|
|
4
19
|
export function checkOptions(value, usage) {
|
|
5
20
|
if (value !== undefined && !isOptions(value))
|
|
6
|
-
throw new TypeError(`${usage.split('(')[0]}() takes an options object, like ${usage}, not ${value
|
|
21
|
+
throw new TypeError(`${usage.split('(')[0].replace(/^new /, '').replace(/^[a-z]\w*\./, '')}() takes an options object, like ${usage}, not ${shown(value)}`);
|
|
7
22
|
}
|
|
8
23
|
export function oneOf(value, allowed, name) {
|
|
9
24
|
if (typeof value !== 'string' || !allowed.includes(value)) {
|
|
10
25
|
const list = allowed.map(a => `'${a}'`);
|
|
11
26
|
const expected = list.length > 1 ? `${list.slice(0, -1).join(', ')} or ${list[list.length - 1]}` : list[0];
|
|
12
|
-
throw new TypeError(`${name} must be ${expected}, not ${
|
|
27
|
+
throw new TypeError(`${name} must be ${expected}, not ${shown(value)}`);
|
|
13
28
|
}
|
|
14
29
|
return value;
|
|
15
30
|
}
|
|
31
|
+
export function boolean(value, name) {
|
|
32
|
+
if (typeof value !== 'boolean')
|
|
33
|
+
throw new TypeError(`${name} must be true or false, not ${shown(value)}`);
|
|
34
|
+
return value;
|
|
35
|
+
}
|
|
16
36
|
export function checkNumber(value, name) {
|
|
17
37
|
if (typeof value !== 'number')
|
|
18
|
-
throw new TypeError(`${name} must be a number, not ${value
|
|
38
|
+
throw new TypeError(`${name} must be a number, not ${shown(value)}`);
|
|
19
39
|
}
|
|
20
40
|
export function finite(value, name) {
|
|
21
41
|
checkNumber(value, name);
|
|
22
42
|
if (!Number.isFinite(value))
|
|
23
|
-
throw new RangeError(
|
|
43
|
+
throw new RangeError(`${name} must be finite, not ${value}`);
|
|
44
|
+
return value;
|
|
45
|
+
}
|
|
46
|
+
export function notNaN(value, name) {
|
|
47
|
+
checkNumber(value, name);
|
|
48
|
+
if (Number.isNaN(value))
|
|
49
|
+
throw new RangeError(`${name} must be a number, not NaN`);
|
|
50
|
+
return value;
|
|
51
|
+
}
|
|
52
|
+
export function atLeast(value, min, name) {
|
|
53
|
+
finite(value, name);
|
|
54
|
+
if (value < min)
|
|
55
|
+
throw new RangeError(`${name} must be ${min} or more, not ${value}`);
|
|
24
56
|
return value;
|
|
25
57
|
}
|
|
58
|
+
export function canvasSize(width, height) {
|
|
59
|
+
return [pixels(width, 'width'), pixels(height, 'height')];
|
|
60
|
+
}
|
|
61
|
+
export function allocate(width, height, make) {
|
|
62
|
+
try {
|
|
63
|
+
return make();
|
|
64
|
+
}
|
|
65
|
+
catch (err) {
|
|
66
|
+
throw new RangeError(`An image of ${width}x${height} pixels (${BigInt(width) * BigInt(height) * 4n} bytes) can't be allocated`, { cause: err });
|
|
67
|
+
}
|
|
68
|
+
}
|
|
26
69
|
export function loopCount(value, name = 'loop count') {
|
|
27
70
|
if (typeof value !== 'number')
|
|
28
|
-
throw new TypeError(`${name} must be a number (Infinity or an integer from 0), not ${value
|
|
71
|
+
throw new TypeError(`${name} must be a number (Infinity or an integer from 0), not ${shown(value)}`);
|
|
29
72
|
if (value !== Infinity && (!Number.isInteger(value) || value < 0))
|
|
30
73
|
throw new RangeError(`${name} must be Infinity or an integer from 0, not ${value}`);
|
|
31
74
|
return value;
|
|
@@ -47,12 +90,51 @@ export function fitDuration(duration, format) {
|
|
|
47
90
|
throw new RangeError(`refuse frame durations that don't fit the format: ${format} frames last up to ${max} ms, not ${duration}`);
|
|
48
91
|
return duration;
|
|
49
92
|
}
|
|
93
|
+
export function instance(value, types, name) {
|
|
94
|
+
if (!types.some(type => value instanceof type))
|
|
95
|
+
throw new TypeError(`${name} must be ${types.map(type => `${/^[AEIOU]/.test(type.name) ? 'an' : 'a'} ${type.name}`).join(' or ')}, not ${shown(value)}`);
|
|
96
|
+
}
|
|
97
|
+
export function scaleFactor(factor, width, height) {
|
|
98
|
+
positive(factor, 'scale factor');
|
|
99
|
+
const largest = Math.floor(0x7fffffff / Math.max(width, height));
|
|
100
|
+
if (Math.max(width, height) * factor >= 0x7fffffff + 0.5)
|
|
101
|
+
throw new RangeError(`scale factor must be at most ${largest} for an image of ${width}x${height}, not ${factor}`);
|
|
102
|
+
return factor;
|
|
103
|
+
}
|
|
50
104
|
export function nonNegative(value, name) {
|
|
51
105
|
finite(value, name);
|
|
52
106
|
if (value < 0)
|
|
53
107
|
throw new RangeError(`${name} must be 0 or more, not ${value}`);
|
|
54
108
|
return value;
|
|
55
109
|
}
|
|
110
|
+
export function byte(value, name) {
|
|
111
|
+
finite(value, name);
|
|
112
|
+
const rounded = Math.round(value);
|
|
113
|
+
if (rounded < 0 || rounded > 255)
|
|
114
|
+
throw new RangeError(`${name} must be 0-255, not ${value}`);
|
|
115
|
+
return rounded;
|
|
116
|
+
}
|
|
117
|
+
export function pixels(value, name) {
|
|
118
|
+
finite(value, name);
|
|
119
|
+
const whole = Math.trunc(value);
|
|
120
|
+
if (whole < 1 || whole > 0x7fffffff)
|
|
121
|
+
throw new RangeError(`${name} must be 1-2147483647, not ${value}`);
|
|
122
|
+
return whole;
|
|
123
|
+
}
|
|
124
|
+
export function coordinate(value, last, name) {
|
|
125
|
+
if (!(value >= 0 && value <= last))
|
|
126
|
+
throw new RangeError(`${name} must be 0-${last}, not ${value}`);
|
|
127
|
+
return value;
|
|
128
|
+
}
|
|
129
|
+
export function cropBounds(width, height, x, y, regionWidth, regionHeight, what) {
|
|
130
|
+
const rx = Math.trunc(finite(x, 'x')), ry = Math.trunc(finite(y, 'y'));
|
|
131
|
+
const rw = Math.trunc(finite(regionWidth, 'width')), rh = Math.trunc(finite(regionHeight, 'height'));
|
|
132
|
+
const left = Math.max(0, rx), top = Math.max(0, ry);
|
|
133
|
+
const right = Math.min(width, rx + rw), bottom = Math.min(height, ry + rh);
|
|
134
|
+
if (right <= left || bottom <= top)
|
|
135
|
+
throw new RangeError(`crop region must be partly inside the ${width}x${height} ${what}, not ${regionWidth}x${regionHeight} at ${x},${y}`);
|
|
136
|
+
return [left, top, right - left, bottom - top];
|
|
137
|
+
}
|
|
56
138
|
export function unitInterval(value, name) {
|
|
57
139
|
finite(value, name);
|
|
58
140
|
if (value < 0 || value > 1)
|
|
@@ -77,6 +159,21 @@ export function pixelLimit(value, name = 'maxPixels') {
|
|
|
77
159
|
throw new RangeError(`${name} must be a positive number, not ${value}`);
|
|
78
160
|
return value;
|
|
79
161
|
}
|
|
162
|
+
export function gainMapEmbed(value) {
|
|
163
|
+
if (value === false)
|
|
164
|
+
return null;
|
|
165
|
+
if (value === true)
|
|
166
|
+
return {};
|
|
167
|
+
if (typeof value !== 'object' || value === null)
|
|
168
|
+
throw new TypeError(`gainMap must be true, false or an options object like { quality: 90, scale: 0.5 }, not ${shown(value)}`);
|
|
169
|
+
const { quality: q, scale } = value;
|
|
170
|
+
const options = {};
|
|
171
|
+
if (q !== undefined)
|
|
172
|
+
options.quality = quality(q, 'gain map quality');
|
|
173
|
+
if (scale !== undefined)
|
|
174
|
+
options.scale = gainMapScale(scale);
|
|
175
|
+
return options;
|
|
176
|
+
}
|
|
80
177
|
export function gainMapScale(value) {
|
|
81
178
|
const scale = finite(value, 'gain map scale');
|
|
82
179
|
if (!(scale > 0 && scale <= 1))
|
|
@@ -103,6 +200,6 @@ export function compressionLevel(value) {
|
|
|
103
200
|
export function color(value, name = 'color') {
|
|
104
201
|
const number = finite(value, name);
|
|
105
202
|
if (!Number.isInteger(number) || number < -0x80000000 || number > 0xffffffff)
|
|
106
|
-
throw new RangeError(
|
|
203
|
+
throw new RangeError(`${name} must be an integer from -2147483648 to 4294967295, like 0xrrggbbaa, not ${value}`);
|
|
107
204
|
return number >>> 0;
|
|
108
205
|
}
|