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/image.js
CHANGED
|
@@ -17,7 +17,7 @@ import * as jxl from "./codecs/jxl.js";
|
|
|
17
17
|
import * as quantize from "./codecs/quantize.js";
|
|
18
18
|
import * as textRenderer from "./render/text.js";
|
|
19
19
|
import * as svgRenderer from "./render/svg.js";
|
|
20
|
-
import { view } from "./util/mem.js";
|
|
20
|
+
import { view, isBinaryData } from "./util/mem.js";
|
|
21
21
|
import { cicpTransform, colorSpaceTransform, profileTransform } from "./util/icc.js";
|
|
22
22
|
import { ImageType } from "./image-type.js";
|
|
23
23
|
import { textLayout } from "./text-layout.js";
|
|
@@ -26,14 +26,15 @@ import { encode as utf8 } from "./util/utf8.js";
|
|
|
26
26
|
import { resample, BOX, TRIANGLE, CUBIC } from "./util/resample.js";
|
|
27
27
|
import { gaussianBlur, boxBlurRGBA, premultiply, unpremultiply } from "./util/blur.js";
|
|
28
28
|
import { Colors, wrapHue, hue2rgb } from "./colors.js";
|
|
29
|
-
import { isOptions, checkOptions, finite, nonNegative, positive, unitInterval, oneOf, pixelLimit, checkThreads,
|
|
30
|
-
import { MAX_PIXELS, LimitError, decoding, checkGrowth } from "./util/limits.js";
|
|
29
|
+
import { allocate, scaleFactor, instance, shown, isOptions, checkOptions, finite, nonNegative, atLeast, positive, pixels, coordinate, cropBounds, unitInterval, oneOf, pixelLimit, checkThreads, gainMapEmbed, boolean, color as checkColor, quality as checkQuality, effort as checkEffort, compressionLevel } from "./util/options.js";
|
|
30
|
+
import { MAX_PIXELS, LimitError, decoding, checkGrowth, checkFormatSize } from "./util/limits.js";
|
|
31
31
|
import { GainMap } from "./gainmap.js";
|
|
32
32
|
import * as gain from "./util/gain.js";
|
|
33
33
|
import { multiply, invert } from "./util/matrix.js";
|
|
34
34
|
import { sampleAffine } from "./util/affine.js";
|
|
35
35
|
import { rotateBilinear } from "./util/rotate.js";
|
|
36
36
|
import * as gainMapCodec from "./codecs/gainmap.js";
|
|
37
|
+
import { describe } from "./util/inspect.js";
|
|
37
38
|
const TRANSFORM_MODES = ['nearest-neighbor', 'bilinear', 'bicubic'];
|
|
38
39
|
const RESIZE_FITS = ['fill', 'inside', 'contain', 'cover'];
|
|
39
40
|
const CIRCLE_FITS = ['contain', 'cover'];
|
|
@@ -42,14 +43,16 @@ export const TONE_MAPPINGS = ['reinhard', 'clamp'];
|
|
|
42
43
|
export const FLIP_DIRECTIONS = ['horizontal', 'vertical'];
|
|
43
44
|
function featureList(features) {
|
|
44
45
|
if (typeof features !== 'object' || features === null)
|
|
45
|
-
throw new TypeError(
|
|
46
|
+
throw new TypeError(`features must be an object, like { liga: false }, not ${shown(features)}`);
|
|
46
47
|
return Object.entries(features).map(([tag, value]) => {
|
|
47
48
|
if (!/^[\x20-\x7e]{4}$/.test(tag))
|
|
48
|
-
throw new RangeError(`
|
|
49
|
+
throw new RangeError(`An OpenType feature tag must be 4 printable ASCII characters, not ${JSON.stringify(tag)}`);
|
|
49
50
|
if (typeof value === 'boolean')
|
|
50
51
|
return [tag, value ? 1 : 0];
|
|
52
|
+
if (typeof value !== 'number')
|
|
53
|
+
throw new TypeError(`The value of the OpenType feature ${tag} must be a boolean or a number, not ${shown(value)}`);
|
|
51
54
|
if (!Number.isInteger(value) || value < 0 || value > 255)
|
|
52
|
-
throw new RangeError(`
|
|
55
|
+
throw new RangeError(`The value of the OpenType feature ${tag} must be a whole number from 0 to 255, not ${value}`);
|
|
53
56
|
return [tag, value];
|
|
54
57
|
});
|
|
55
58
|
}
|
|
@@ -61,36 +64,27 @@ export class Image {
|
|
|
61
64
|
__bitmap__;
|
|
62
65
|
__gain_map__ = null;
|
|
63
66
|
constructor(width, height) {
|
|
64
|
-
width =
|
|
65
|
-
height =
|
|
66
|
-
if (width < 1)
|
|
67
|
-
throw new RangeError('Image has to be at least 1 pixel wide');
|
|
68
|
-
if (height < 1)
|
|
69
|
-
throw new RangeError('Image has to be at least 1 pixel high');
|
|
70
|
-
if (width > 0x7fffffff)
|
|
71
|
-
throw new RangeError('Image width is too large');
|
|
72
|
-
if (height > 0x7fffffff)
|
|
73
|
-
throw new RangeError('Image height is too large');
|
|
67
|
+
width = pixels(width, 'width');
|
|
68
|
+
height = pixels(height, 'height');
|
|
74
69
|
this.__width__ = width;
|
|
75
70
|
this.__height__ = height;
|
|
76
|
-
this.__set_bitmap__(new Uint8ClampedArray(width * height * 4));
|
|
71
|
+
this.__set_bitmap__(allocate(width, height, () => new Uint8ClampedArray(width * height * 4)));
|
|
77
72
|
}
|
|
78
73
|
get gainMap() {
|
|
79
74
|
return this.__gain_map__;
|
|
80
75
|
}
|
|
81
76
|
set gainMap(gainMap) {
|
|
82
77
|
if (gainMap !== null && !(gainMap instanceof GainMap))
|
|
83
|
-
throw new TypeError(
|
|
78
|
+
throw new TypeError(`gainMap must be a GainMap or null, not ${shown(gainMap)}`);
|
|
84
79
|
this.__gain_map__ = gainMap?.clone() ?? null;
|
|
85
80
|
}
|
|
86
81
|
get bitmap() {
|
|
87
82
|
return this.__bitmap__;
|
|
88
83
|
}
|
|
89
84
|
set bitmap(bitmap) {
|
|
90
|
-
|
|
91
|
-
throw new TypeError('The bitmap has to be a Uint8ClampedArray or Uint8Array');
|
|
85
|
+
instance(bitmap, [Uint8ClampedArray, Uint8Array], 'bitmap');
|
|
92
86
|
if (bitmap.length !== this.width * this.height * 4)
|
|
93
|
-
throw new RangeError(`
|
|
87
|
+
throw new RangeError(`bitmap must have ${this.width * this.height * 4} bytes (width * height * 4), not ${bitmap.length}`);
|
|
94
88
|
if (bitmap.byteOffset % 4)
|
|
95
89
|
throw new TypeError(`The bitmap has to start at a byte offset divisible by 4 to be used without copying, not ${bitmap.byteOffset} (pass bitmap.slice() for a copy)`);
|
|
96
90
|
this.__set_bitmap__(bitmap instanceof Uint8ClampedArray ? bitmap : new Uint8ClampedArray(bitmap.buffer, bitmap.byteOffset, bitmap.length));
|
|
@@ -100,7 +94,7 @@ export class Image {
|
|
|
100
94
|
bitmap = bitmap.slice();
|
|
101
95
|
this.__bitmap__ = bitmap;
|
|
102
96
|
this.__view__ = new DataView(bitmap.buffer, bitmap.byteOffset, bitmap.byteLength);
|
|
103
|
-
this.__u32__ = new Uint32Array(bitmap.buffer, bitmap.byteOffset, bitmap.length
|
|
97
|
+
this.__u32__ = new Uint32Array(bitmap.buffer, bitmap.byteOffset, bitmap.length >>> 2);
|
|
104
98
|
}
|
|
105
99
|
toString() {
|
|
106
100
|
return `Image<${this.width}x${this.height}>`;
|
|
@@ -111,15 +105,20 @@ export class Image {
|
|
|
111
105
|
get height() {
|
|
112
106
|
return this.__height__;
|
|
113
107
|
}
|
|
114
|
-
|
|
108
|
+
pixels() {
|
|
115
109
|
const view = new DataView(this.bitmap.buffer, this.bitmap.byteOffset, this.bitmap.byteLength);
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
110
|
+
const width = this.width, height = this.height;
|
|
111
|
+
return (function* () {
|
|
112
|
+
let offset = 0;
|
|
113
|
+
for (let y = 0; y < height; y++)
|
|
114
|
+
for (let x = 0; x < width; x++, offset += 4)
|
|
115
|
+
yield [x, y, view.getUint32(offset, false)];
|
|
116
|
+
})();
|
|
120
117
|
}
|
|
121
118
|
static linearGradient(width, height, options) {
|
|
122
119
|
checkOptions(options, 'Image.linearGradient(100, 50, { stops, angle: 90 })');
|
|
120
|
+
if (options === undefined || options.stops === undefined)
|
|
121
|
+
throw new TypeError('Image.linearGradient(width, height, { stops }) needs the stops in the options');
|
|
123
122
|
const { stops, angle = 180 } = options;
|
|
124
123
|
const color = gradientFunction(stops);
|
|
125
124
|
finite(angle, 'angle');
|
|
@@ -136,14 +135,16 @@ export class Image {
|
|
|
136
135
|
}
|
|
137
136
|
static radialGradient(width, height, options) {
|
|
138
137
|
checkOptions(options, 'Image.radialGradient(100, 50, { stops })');
|
|
139
|
-
|
|
140
|
-
(
|
|
138
|
+
width = pixels(width, 'width');
|
|
139
|
+
height = pixels(height, 'height');
|
|
140
|
+
if (options === undefined || options.stops === undefined)
|
|
141
|
+
throw new TypeError('Image.radialGradient(width, height, { stops }) needs the stops in the options');
|
|
141
142
|
const { stops, x: cx = width / 2, y: cy = height / 2, radius = Math.hypot(Math.max(cx, width - cx), Math.max(cy, height - cy)) } = options;
|
|
142
143
|
const color = gradientFunction(stops);
|
|
143
144
|
finite(cx, 'x');
|
|
144
145
|
finite(cy, 'y');
|
|
145
|
-
|
|
146
|
-
|
|
146
|
+
positive(radius, 'radius');
|
|
147
|
+
const image = new Image(width, height);
|
|
147
148
|
const bitmap = new DataView(image.bitmap.buffer, image.bitmap.byteOffset, image.bitmap.byteLength);
|
|
148
149
|
for (let y = 0, i = 0; y < height; y++)
|
|
149
150
|
for (let x = 0; x < width; x++, i += 4)
|
|
@@ -167,21 +168,8 @@ export class Image {
|
|
|
167
168
|
this.__view__.setUint32((y * this.width + x) * 4, pixelColor, false);
|
|
168
169
|
}
|
|
169
170
|
__check_boundaries__(x, y) {
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
if (isNaN(y))
|
|
173
|
-
throw new TypeError(`Invalid pixel coordinates (y=${y})`);
|
|
174
|
-
if (x < 0)
|
|
175
|
-
throw new RangeError(`${Image.__out_of_bounds__} (x=${x})<0`);
|
|
176
|
-
if (x >= this.width)
|
|
177
|
-
throw new RangeError(`${Image.__out_of_bounds__} (x=${x})>=(width=${this.width})`);
|
|
178
|
-
if (y < 0)
|
|
179
|
-
throw new RangeError(`${Image.__out_of_bounds__} (y=${y})<0`);
|
|
180
|
-
if (y >= this.height)
|
|
181
|
-
throw new RangeError(`${Image.__out_of_bounds__} (y=${y})>=(height=${this.height})`);
|
|
182
|
-
}
|
|
183
|
-
static get __out_of_bounds__() {
|
|
184
|
-
return 'Tried referencing a pixel outside of the images boundaries:';
|
|
171
|
+
coordinate(x, this.width - 1, 'x');
|
|
172
|
+
coordinate(y, this.height - 1, 'y');
|
|
185
173
|
}
|
|
186
174
|
fill(color) {
|
|
187
175
|
const view = new DataView(this.bitmap.buffer, this.bitmap.byteOffset, this.bitmap.byteLength);
|
|
@@ -215,7 +203,7 @@ export class Image {
|
|
|
215
203
|
scale(factor, options) {
|
|
216
204
|
checkOptions(options, 'scale(factor, { mode: \'area\' })');
|
|
217
205
|
const { mode } = resizeOptions(options);
|
|
218
|
-
|
|
206
|
+
scaleFactor(factor, this.width, this.height);
|
|
219
207
|
const image = this.__scale__(factor, mode);
|
|
220
208
|
this.__gain_map__?.__rescale__(this.width, this.height, image.width, image.height);
|
|
221
209
|
this.__moved__?.(image.width / this.width, image.height / this.height, 0, 0);
|
|
@@ -246,12 +234,8 @@ export class Image {
|
|
|
246
234
|
const { background = 0x00000000, ...resizeOpts } = options ?? {};
|
|
247
235
|
const backgroundColor = checkColor(background, 'background');
|
|
248
236
|
resizeOptions(resizeOpts);
|
|
249
|
-
width =
|
|
250
|
-
height =
|
|
251
|
-
if (width < 1)
|
|
252
|
-
throw new RangeError('Image has to be at least 1 pixel wide');
|
|
253
|
-
if (height < 1)
|
|
254
|
-
throw new RangeError('Image has to be at least 1 pixel high');
|
|
237
|
+
width = pixels(width, 'width');
|
|
238
|
+
height = pixels(height, 'height');
|
|
255
239
|
const result = new Image(width, height);
|
|
256
240
|
if (backgroundColor !== 0)
|
|
257
241
|
result.fill(backgroundColor);
|
|
@@ -264,12 +248,8 @@ export class Image {
|
|
|
264
248
|
}
|
|
265
249
|
__cover__(width, height, options) {
|
|
266
250
|
const resizeOpts = resizeOptions(options);
|
|
267
|
-
width =
|
|
268
|
-
height =
|
|
269
|
-
if (!(width >= 1 && Number.isFinite(width)))
|
|
270
|
-
throw new RangeError('Image has to be at least 1 pixel wide');
|
|
271
|
-
if (!(height >= 1 && Number.isFinite(height)))
|
|
272
|
-
throw new RangeError('Image has to be at least 1 pixel high');
|
|
251
|
+
width = pixels(width, 'width');
|
|
252
|
+
height = pixels(height, 'height');
|
|
273
253
|
const scale = Math.max(width / this.width, height / this.height);
|
|
274
254
|
const resizedWidth = Math.max(width, Math.round(this.width * scale)), resizedHeight = Math.max(height, Math.round(this.height * scale));
|
|
275
255
|
if (resizeOpts.mode !== 'nearest-neighbor' && width >= 1 && height >= 1) {
|
|
@@ -285,7 +265,7 @@ export class Image {
|
|
|
285
265
|
}
|
|
286
266
|
__resized_size__(width, height, fit = 'fill') {
|
|
287
267
|
if (fit === 'inside' && width !== undefined && height !== undefined)
|
|
288
|
-
return insideSize(this.width, this.height, width, height);
|
|
268
|
+
return insideSize(this.width, this.height, pixels(width, 'width'), pixels(height, 'height'));
|
|
289
269
|
if (width === undefined)
|
|
290
270
|
return [Math.max(1, Math.round(this.width / this.height * Math.floor(height))), height];
|
|
291
271
|
if (height === undefined)
|
|
@@ -293,12 +273,8 @@ export class Image {
|
|
|
293
273
|
return [width, height];
|
|
294
274
|
}
|
|
295
275
|
__resize__(width, height, mode = 'bilinear', region) {
|
|
296
|
-
width =
|
|
297
|
-
height =
|
|
298
|
-
if (width < 1)
|
|
299
|
-
throw new RangeError('Image has to be at least 1 pixel wide');
|
|
300
|
-
if (height < 1)
|
|
301
|
-
throw new RangeError('Image has to be at least 1 pixel high');
|
|
276
|
+
width = pixels(width, 'width');
|
|
277
|
+
height = pixels(height, 'height');
|
|
302
278
|
oneOf(mode, RESIZE_MODES, 'resize mode');
|
|
303
279
|
if (mode === 'nearest-neighbor')
|
|
304
280
|
return this.__resize_nearest_neighbor__(width, height);
|
|
@@ -339,15 +315,7 @@ export class Image {
|
|
|
339
315
|
return image;
|
|
340
316
|
}
|
|
341
317
|
__crop_bounds__(x, y, width, height) {
|
|
342
|
-
|
|
343
|
-
y = Math.trunc(finite(y, 'y'));
|
|
344
|
-
width = Math.trunc(finite(width, 'width'));
|
|
345
|
-
height = Math.trunc(finite(height, 'height'));
|
|
346
|
-
const left = Math.max(0, x), top = Math.max(0, y);
|
|
347
|
-
const right = Math.min(this.width, x + width), bottom = Math.min(this.height, y + height);
|
|
348
|
-
if (right <= left || bottom <= top)
|
|
349
|
-
throw new RangeError('Crop region does not overlap the image');
|
|
350
|
-
return [left, top, right - left, bottom - top];
|
|
318
|
+
return cropBounds(this.width, this.height, x, y, width, height, 'image');
|
|
351
319
|
}
|
|
352
320
|
trim(options = {}) {
|
|
353
321
|
checkOptions(options, 'trim({ tolerance: 0.1 })');
|
|
@@ -360,9 +328,7 @@ export class Image {
|
|
|
360
328
|
return this.__apply__(this.__crop__(...bounds));
|
|
361
329
|
}
|
|
362
330
|
__trim_bounds__(color, { tolerance = 0 }) {
|
|
363
|
-
|
|
364
|
-
if (!(tolerance >= 0 && tolerance <= 1))
|
|
365
|
-
throw new RangeError('Invalid tolerance, it has to be between 0 and 1');
|
|
331
|
+
unitInterval(tolerance, 'tolerance');
|
|
366
332
|
const bitmap = this.bitmap;
|
|
367
333
|
const { width, height } = this;
|
|
368
334
|
if (color !== undefined)
|
|
@@ -463,36 +429,28 @@ export class Image {
|
|
|
463
429
|
return this;
|
|
464
430
|
}
|
|
465
431
|
opacity(factor) {
|
|
466
|
-
factor =
|
|
467
|
-
if (factor < 0)
|
|
468
|
-
throw new RangeError('Invalid opacity value');
|
|
432
|
+
factor = nonNegative(factor, 'opacity factor');
|
|
469
433
|
this.__set_channel_value__(factor, 3);
|
|
470
434
|
return this;
|
|
471
435
|
}
|
|
472
436
|
red(factor) {
|
|
473
437
|
if (this.__gain_map__)
|
|
474
438
|
return this.__recolor__(image => image.red(factor));
|
|
475
|
-
factor =
|
|
476
|
-
if (factor < 0)
|
|
477
|
-
throw new RangeError('Invalid red factor');
|
|
439
|
+
factor = nonNegative(factor, 'red factor');
|
|
478
440
|
this.__set_channel_value__(factor, 0);
|
|
479
441
|
return this;
|
|
480
442
|
}
|
|
481
443
|
green(factor) {
|
|
482
444
|
if (this.__gain_map__)
|
|
483
445
|
return this.__recolor__(image => image.green(factor));
|
|
484
|
-
factor =
|
|
485
|
-
if (factor < 0)
|
|
486
|
-
throw new RangeError('Invalid green factor');
|
|
446
|
+
factor = nonNegative(factor, 'green factor');
|
|
487
447
|
this.__set_channel_value__(factor, 1);
|
|
488
448
|
return this;
|
|
489
449
|
}
|
|
490
450
|
blue(factor) {
|
|
491
451
|
if (this.__gain_map__)
|
|
492
452
|
return this.__recolor__(image => image.blue(factor));
|
|
493
|
-
factor =
|
|
494
|
-
if (factor < 0)
|
|
495
|
-
throw new RangeError('Invalid blue factor');
|
|
453
|
+
factor = nonNegative(factor, 'blue factor');
|
|
496
454
|
this.__set_channel_value__(factor, 2);
|
|
497
455
|
return this;
|
|
498
456
|
}
|
|
@@ -503,17 +461,13 @@ export class Image {
|
|
|
503
461
|
lightness(factor) {
|
|
504
462
|
if (this.__gain_map__)
|
|
505
463
|
return this.__recolor__(image => image.lightness(factor));
|
|
506
|
-
factor =
|
|
507
|
-
if (factor < 0)
|
|
508
|
-
throw new RangeError('Invalid lightness factor');
|
|
464
|
+
factor = nonNegative(factor, 'lightness factor');
|
|
509
465
|
return this.__map_hsla__(c => { c.l = factor * c.l; });
|
|
510
466
|
}
|
|
511
467
|
saturation(factor) {
|
|
512
468
|
if (this.__gain_map__)
|
|
513
469
|
return this.__recolor__(image => image.saturation(factor), true);
|
|
514
|
-
factor =
|
|
515
|
-
if (factor < 0)
|
|
516
|
-
throw new RangeError('Invalid saturation factor');
|
|
470
|
+
factor = nonNegative(factor, 'saturation factor');
|
|
517
471
|
return this.__map_hsla__(c => { c.s = factor * c.s; });
|
|
518
472
|
}
|
|
519
473
|
composite(source, options = {}) {
|
|
@@ -532,7 +486,8 @@ export class Image {
|
|
|
532
486
|
return this.__blend__(source, x, y, opacity, blend);
|
|
533
487
|
const left = Math.max(0, x), top = Math.max(0, y);
|
|
534
488
|
const right = Math.min(this.width, x + source.width), bottom = Math.min(this.height, y + source.height);
|
|
535
|
-
const dst = this.bitmap
|
|
489
|
+
const dst = this.bitmap;
|
|
490
|
+
const src = source.bitmap.buffer === dst.buffer ? source.bitmap.slice() : source.bitmap;
|
|
536
491
|
for (let row = top; row < bottom; row++) {
|
|
537
492
|
for (let column = left, s = 4 * ((row - y) * source.width + left - x), d = 4 * (row * this.width + left); column < right; column++, s += 4, d += 4) {
|
|
538
493
|
const sa = src[s + 3];
|
|
@@ -622,7 +577,8 @@ export class Image {
|
|
|
622
577
|
__blend__(source, x, y, opacity, blend) {
|
|
623
578
|
const left = Math.max(0, x), top = Math.max(0, y);
|
|
624
579
|
const right = Math.min(this.width, x + source.width), bottom = Math.min(this.height, y + source.height);
|
|
625
|
-
const dst = this.bitmap
|
|
580
|
+
const dst = this.bitmap;
|
|
581
|
+
const src = source.bitmap.buffer === dst.buffer ? source.bitmap.slice() : source.bitmap;
|
|
626
582
|
for (let row = top; row < bottom; row++) {
|
|
627
583
|
for (let column = left; column < right; column++) {
|
|
628
584
|
const s = 4 * ((row - y) * source.width + column - x);
|
|
@@ -684,14 +640,14 @@ export class Image {
|
|
|
684
640
|
__map_hsla__(map) {
|
|
685
641
|
const bitmap = this.bitmap;
|
|
686
642
|
const hsla = { h: 0, s: 0, l: 0, a: 0 };
|
|
687
|
-
const pixels = new Uint32Array(bitmap.buffer, bitmap.byteOffset, bitmap.length
|
|
643
|
+
const pixels = new Uint32Array(bitmap.buffer, bitmap.byteOffset, bitmap.length >>> 2);
|
|
688
644
|
const keys = new Uint32Array(4096), values = new Uint32Array(4096);
|
|
689
645
|
keys[0] = 1;
|
|
690
646
|
for (let i = 0; i < bitmap.length; i += 4) {
|
|
691
|
-
const color = pixels[i
|
|
647
|
+
const color = pixels[i >>> 2];
|
|
692
648
|
const slot = Math.imul(color, 0x9e3779b1) >>> 20;
|
|
693
649
|
if (keys[slot] === color) {
|
|
694
|
-
pixels[i
|
|
650
|
+
pixels[i >>> 2] = values[slot];
|
|
695
651
|
continue;
|
|
696
652
|
}
|
|
697
653
|
const r = bitmap[i] / 255, g = bitmap[i + 1] / 255, b = bitmap[i + 2] / 255;
|
|
@@ -729,7 +685,7 @@ export class Image {
|
|
|
729
685
|
}
|
|
730
686
|
bitmap[i + 3] = Math.round(na * 255);
|
|
731
687
|
keys[slot] = color;
|
|
732
|
-
values[slot] = pixels[i
|
|
688
|
+
values[slot] = pixels[i >>> 2];
|
|
733
689
|
}
|
|
734
690
|
return this;
|
|
735
691
|
}
|
|
@@ -753,14 +709,12 @@ export class Image {
|
|
|
753
709
|
let fixed = null, count = 256;
|
|
754
710
|
if (palette !== undefined) {
|
|
755
711
|
if (!Array.isArray(palette))
|
|
756
|
-
throw new TypeError(
|
|
712
|
+
throw new TypeError(`palette must be an array of colors, not ${shown(palette)}`);
|
|
757
713
|
if (palette.length < 1 || palette.length > 256)
|
|
758
|
-
throw new RangeError(`palette
|
|
714
|
+
throw new RangeError(`palette must have 1-256 colors, not ${palette.length}`);
|
|
759
715
|
const packed = new Uint8Array(palette.length * 3);
|
|
760
716
|
palette.forEach((color, i) => {
|
|
761
717
|
const value = checkColor(color, 'palette color');
|
|
762
|
-
if (!Number.isInteger(value) || value < 0 || value > 0xffffffff)
|
|
763
|
-
throw new RangeError(`Invalid palette color: ${color}`);
|
|
764
718
|
packed[i * 3] = value >>> 24;
|
|
765
719
|
packed[i * 3 + 1] = value >>> 16 & 255;
|
|
766
720
|
packed[i * 3 + 2] = value >>> 8 & 255;
|
|
@@ -782,10 +736,12 @@ export class Image {
|
|
|
782
736
|
return this;
|
|
783
737
|
}
|
|
784
738
|
colorMatrix(matrix) {
|
|
785
|
-
if (matrix === null || typeof matrix !== 'object'
|
|
786
|
-
throw new
|
|
739
|
+
if (matrix === null || typeof matrix !== 'object')
|
|
740
|
+
throw new TypeError(`colorMatrix() takes an array of 9 or 20 values, not ${shown(matrix)}`);
|
|
741
|
+
if (matrix.length !== 9 && matrix.length !== 20)
|
|
742
|
+
throw new RangeError(`a color matrix must have 9 or 20 values, not ${matrix.length}`);
|
|
787
743
|
const m = new Float64Array(20);
|
|
788
|
-
const value = (v) => finite(
|
|
744
|
+
const value = (v) => finite(v, 'color matrix value');
|
|
789
745
|
if (matrix.length === 20)
|
|
790
746
|
for (let i = 0; i < 20; i++)
|
|
791
747
|
m[i] = value(matrix[i]);
|
|
@@ -809,11 +765,16 @@ export class Image {
|
|
|
809
765
|
}
|
|
810
766
|
transform(matrix, options = {}) {
|
|
811
767
|
checkOptions(options, 'image.transform([1, 0, 0, 1, 0, 0], { expand: false })');
|
|
812
|
-
if (matrix === null || typeof matrix !== 'object'
|
|
813
|
-
throw new
|
|
814
|
-
|
|
768
|
+
if (matrix === null || typeof matrix !== 'object')
|
|
769
|
+
throw new TypeError(`transform() takes an array of 6 values, like transform([1, 0, 0, 1, 0, 0]), not ${shown(matrix)}`);
|
|
770
|
+
if (matrix.length !== 6)
|
|
771
|
+
throw new RangeError(`a transform must have 6 values, [a, b, c, d, e, f], not ${matrix.length}`);
|
|
772
|
+
const values = Array.from({ length: 6 }, (_, i) => finite(matrix[i], 'transform value'));
|
|
815
773
|
const { origin = {}, expand = true, mode = 'bilinear' } = options;
|
|
774
|
+
if (typeof origin !== 'object' || origin === null)
|
|
775
|
+
throw new TypeError(`transform() takes an origin object like { x: 10, y: 20 }, not ${shown(origin)}`);
|
|
816
776
|
oneOf(mode, TRANSFORM_MODES, 'transform mode');
|
|
777
|
+
boolean(expand, 'expand');
|
|
817
778
|
const { x: pivotX = this.width / 2, y: pivotY = this.height / 2 } = origin;
|
|
818
779
|
finite(pivotX, 'origin x');
|
|
819
780
|
finite(pivotY, 'origin y');
|
|
@@ -834,8 +795,7 @@ export class Image {
|
|
|
834
795
|
outHeight = Math.max(1, Math.round(maxY - minY));
|
|
835
796
|
left = minX - (outWidth - (maxX - minX)) / 2;
|
|
836
797
|
top = minY - (outHeight - (maxY - minY)) / 2;
|
|
837
|
-
|
|
838
|
-
throw new RangeError(`The transformed image would have ${outWidth}x${outHeight} pixels, more than the ${MAX_PIXELS} allowed`);
|
|
798
|
+
checkGrowth(outWidth, outHeight, 'The transform');
|
|
839
799
|
}
|
|
840
800
|
const out = new Image(outWidth, outHeight);
|
|
841
801
|
sampleAffine(this.bitmap, width, height, out.bitmap, outWidth, outHeight, inverse, left, top, mode);
|
|
@@ -850,12 +810,11 @@ export class Image {
|
|
|
850
810
|
dropShadow(options = {}) {
|
|
851
811
|
checkOptions(options, 'image.dropShadow({ blur: 8 })');
|
|
852
812
|
const { x = 0, y = 0, blur = 0, spread = 0, color = 0x00000080, expand = true } = options;
|
|
813
|
+
boolean(expand, 'expand');
|
|
853
814
|
finite(x, 'x');
|
|
854
815
|
finite(y, 'y');
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
if (finite(spread, 'spread') < 0)
|
|
858
|
-
throw new RangeError('Invalid spread');
|
|
816
|
+
nonNegative(blur, 'blur');
|
|
817
|
+
nonNegative(spread, 'spread');
|
|
859
818
|
const [r, g, b, a] = Colors.toRGBA(checkColor(color));
|
|
860
819
|
const reach = spread + 3 * blur, margin = Math.ceil(reach) + 1;
|
|
861
820
|
const layer = this.__shape_layer__(margin);
|
|
@@ -891,8 +850,8 @@ export class Image {
|
|
|
891
850
|
outline(options = {}) {
|
|
892
851
|
checkOptions(options, 'image.outline({ width: 4 })');
|
|
893
852
|
const { width = 1, color = 0x000000ff, expand = true } = options;
|
|
894
|
-
|
|
895
|
-
|
|
853
|
+
boolean(expand, 'expand');
|
|
854
|
+
nonNegative(width, 'outline width');
|
|
896
855
|
const [r, g, b, a] = Colors.toRGBA(checkColor(color));
|
|
897
856
|
if (width === 0)
|
|
898
857
|
return this;
|
|
@@ -961,25 +920,19 @@ export class Image {
|
|
|
961
920
|
brightness(factor) {
|
|
962
921
|
if (this.__gain_map__)
|
|
963
922
|
return this.__recolor__(image => image.brightness(factor), false, v => v * factor);
|
|
964
|
-
factor =
|
|
965
|
-
if (factor < 0)
|
|
966
|
-
throw new RangeError('Invalid brightness factor');
|
|
923
|
+
factor = nonNegative(factor, 'brightness factor');
|
|
967
924
|
return this.__map_channels__(v => v * factor);
|
|
968
925
|
}
|
|
969
926
|
contrast(factor) {
|
|
970
927
|
if (this.__gain_map__)
|
|
971
928
|
return this.__recolor__(image => image.contrast(factor), false, v => (v - 0.5) * factor + 0.5);
|
|
972
|
-
factor =
|
|
973
|
-
if (factor < 0)
|
|
974
|
-
throw new RangeError('Invalid contrast factor');
|
|
929
|
+
factor = nonNegative(factor, 'contrast factor');
|
|
975
930
|
return this.__map_channels__(v => (v - 127.5) * factor + 127.5);
|
|
976
931
|
}
|
|
977
932
|
gamma(gamma) {
|
|
978
933
|
if (this.__gain_map__)
|
|
979
934
|
return this.__recolor__(image => image.gamma(gamma), false, v => Math.max(0, v) ** (1 / gamma));
|
|
980
|
-
gamma =
|
|
981
|
-
if (gamma <= 0)
|
|
982
|
-
throw new RangeError('Invalid gamma');
|
|
935
|
+
gamma = positive(gamma, 'gamma');
|
|
983
936
|
return this.__map_channels__(v => 255 * (v / 255) ** (1 / gamma));
|
|
984
937
|
}
|
|
985
938
|
grayscale() {
|
|
@@ -1009,7 +962,7 @@ export class Image {
|
|
|
1009
962
|
finite(transparencyThreshold, 'transparencyThreshold');
|
|
1010
963
|
finite(opacityThreshold, 'opacityThreshold');
|
|
1011
964
|
if (!(transparencyThreshold >= 0 && opacityThreshold <= 1 && transparencyThreshold < opacityThreshold))
|
|
1012
|
-
throw new RangeError(
|
|
965
|
+
throw new RangeError(`transparencyThreshold must be 0 or more and below opacityThreshold, which must be 1 or less, not ${transparencyThreshold} and ${opacityThreshold}`);
|
|
1013
966
|
const bitmap = this.bitmap;
|
|
1014
967
|
if (color !== undefined)
|
|
1015
968
|
color = checkColor(color);
|
|
@@ -1059,7 +1012,11 @@ export class Image {
|
|
|
1059
1012
|
dominantColor(options = {}) {
|
|
1060
1013
|
checkOptions(options, 'dominantColor({ ignoreBlack: false })');
|
|
1061
1014
|
const { ignoreBlack = true, ignoreWhite = true, threshold: bwThreshold = 60 } = options;
|
|
1062
|
-
|
|
1015
|
+
boolean(ignoreBlack, 'ignoreBlack');
|
|
1016
|
+
boolean(ignoreWhite, 'ignoreWhite');
|
|
1017
|
+
const threshold = finite(bwThreshold, 'threshold');
|
|
1018
|
+
if (threshold < 0 || threshold > 256)
|
|
1019
|
+
throw new RangeError(`threshold must be 0-256, not ${threshold}`);
|
|
1063
1020
|
const pixels = this.__bytes__();
|
|
1064
1021
|
const color = quantize.dominantColor(pixels, (r, g, b) => !(ignoreBlack && Math.max(r, g, b) < threshold) && !(ignoreWhite && Math.min(r, g, b) > 255 - threshold))
|
|
1065
1022
|
|| quantize.dominantColor(pixels, () => true);
|
|
@@ -1069,6 +1026,7 @@ export class Image {
|
|
|
1069
1026
|
checkOptions(options, 'rotate(angle, { expand: false })');
|
|
1070
1027
|
const { expand = true, mode = 'bilinear' } = options;
|
|
1071
1028
|
oneOf(mode, TRANSFORM_MODES, 'rotate mode');
|
|
1029
|
+
boolean(expand, 'expand');
|
|
1072
1030
|
angle = ((-finite(angle, 'angle') % 360) + 360) % 360;
|
|
1073
1031
|
if (angle === 0)
|
|
1074
1032
|
return this;
|
|
@@ -1245,24 +1203,85 @@ export class Image {
|
|
|
1245
1203
|
if (subsampling !== undefined)
|
|
1246
1204
|
oneOf(subsampling, SUBSAMPLINGS, 'subsampling');
|
|
1247
1205
|
const backgroundColor = checkColor(background, 'background');
|
|
1206
|
+
const embed = gainMapEmbed(gainMapOption);
|
|
1248
1207
|
const m = resolveMetadata(metadata);
|
|
1249
|
-
const
|
|
1250
|
-
|
|
1208
|
+
const exif = exifData(m), comment = m.comment ? utf8(m.comment) : undefined;
|
|
1209
|
+
if (comment && comment.length > 65533)
|
|
1210
|
+
throw new RangeError(`JPEG: the metadata's comment must be at most 65533 bytes in UTF-8, not ${comment.length}`);
|
|
1211
|
+
if (exif && exif.length > 65527)
|
|
1212
|
+
throw new RangeError(`JPEG: the metadata's description, author, copyright, software and creation time must be at most 65533 bytes together in Exif, not ${exif.length + 6}`);
|
|
1213
|
+
checkFormatSize('JPEG', this.width, this.height, 0xffff);
|
|
1214
|
+
if (!this.__gain_map__) {
|
|
1215
|
+
const flat = this.__flatRows__(backgroundColor);
|
|
1216
|
+
const base = { exif, comment, subsampling };
|
|
1217
|
+
if (!flat)
|
|
1218
|
+
return jpeg.encode(this.__bytes__(), this.width, this.height, quality, { ...base, gray: this.__is_gray__() });
|
|
1219
|
+
return jpeg.encode(flat.pixels, this.width, this.height, quality, { ...base, gray: flat.gray(), rows: flat.row });
|
|
1220
|
+
}
|
|
1221
|
+
const source = this.__flattened__(backgroundColor, embed !== null) ?? this;
|
|
1251
1222
|
const gainMap = source.__gain_map__;
|
|
1252
|
-
|
|
1223
|
+
const base = { exif, comment, subsampling, gray: source.__is_gray__() };
|
|
1224
|
+
if (!gainMap || !embed)
|
|
1253
1225
|
return jpeg.encode(source.__bytes__(), source.width, source.height, quality, base);
|
|
1254
|
-
const { quality: mapQuality, scale } =
|
|
1226
|
+
const { quality: mapQuality, scale } = embed;
|
|
1255
1227
|
let width, height;
|
|
1256
1228
|
if (scale !== undefined) {
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
height = Math.max(1, Math.round(this.height * s));
|
|
1229
|
+
width = Math.max(1, Math.round(this.width * scale));
|
|
1230
|
+
height = Math.max(1, Math.round(this.height * scale));
|
|
1260
1231
|
}
|
|
1261
1232
|
const params = gainMap.__params__();
|
|
1262
|
-
const map = gainMapCodec.mapWithMetadata(gainMap.__body__(mapQuality
|
|
1233
|
+
const map = gainMapCodec.mapWithMetadata(gainMap.__body__(mapQuality, width, height), params);
|
|
1263
1234
|
const primary = jpeg.encode(source.__bytes__(), source.width, source.height, quality, { ...base, segments: gainMapCodec.primarySegments(map.length) });
|
|
1264
1235
|
return gainMapCodec.finish(primary, map);
|
|
1265
1236
|
}
|
|
1237
|
+
__is_gray__() {
|
|
1238
|
+
const px = this.bitmap;
|
|
1239
|
+
for (let i = 0; i < px.length; i += 4)
|
|
1240
|
+
if (px[i] !== px[i + 1] || px[i] !== px[i + 2])
|
|
1241
|
+
return false;
|
|
1242
|
+
return true;
|
|
1243
|
+
}
|
|
1244
|
+
__flatRows__(background) {
|
|
1245
|
+
const src = this.bitmap;
|
|
1246
|
+
let opaque = true;
|
|
1247
|
+
for (let i = 3; i < src.length && opaque; i += 4)
|
|
1248
|
+
opaque = src[i] === 255;
|
|
1249
|
+
if (opaque)
|
|
1250
|
+
return null;
|
|
1251
|
+
const width = this.width, bgR = background >>> 24, bgG = background >>> 16 & 255, bgB = background >>> 8 & 255;
|
|
1252
|
+
const scratch = new Uint8ClampedArray(width * 4);
|
|
1253
|
+
const row = (y) => {
|
|
1254
|
+
for (let x = 0, s = 4 * y * width, d = 0; x < width; x++, s += 4, d += 4) {
|
|
1255
|
+
const sa = src[s + 3];
|
|
1256
|
+
if (sa === 0) {
|
|
1257
|
+
scratch[d] = bgR;
|
|
1258
|
+
scratch[d + 1] = bgG;
|
|
1259
|
+
scratch[d + 2] = bgB;
|
|
1260
|
+
continue;
|
|
1261
|
+
}
|
|
1262
|
+
if (sa === 255) {
|
|
1263
|
+
scratch[d] = src[s];
|
|
1264
|
+
scratch[d + 1] = src[s + 1];
|
|
1265
|
+
scratch[d + 2] = src[s + 2];
|
|
1266
|
+
continue;
|
|
1267
|
+
}
|
|
1268
|
+
const fa = sa / 255, ba = 1 - fa, a = fa + ba;
|
|
1269
|
+
scratch[d] = (src[s] * fa + bgR * ba) / a;
|
|
1270
|
+
scratch[d + 1] = (src[s + 1] * fa + bgG * ba) / a;
|
|
1271
|
+
scratch[d + 2] = (src[s + 2] * fa + bgB * ba) / a;
|
|
1272
|
+
}
|
|
1273
|
+
};
|
|
1274
|
+
const gray = () => {
|
|
1275
|
+
for (let y = 0; y < this.height; y++) {
|
|
1276
|
+
row(y);
|
|
1277
|
+
for (let d = 0; d < scratch.length; d += 4)
|
|
1278
|
+
if (scratch[d] !== scratch[d + 1] || scratch[d] !== scratch[d + 2])
|
|
1279
|
+
return false;
|
|
1280
|
+
}
|
|
1281
|
+
return true;
|
|
1282
|
+
};
|
|
1283
|
+
return { pixels: new Uint8Array(scratch.buffer), row, gray };
|
|
1284
|
+
}
|
|
1266
1285
|
__flattened__(background, withGainMap) {
|
|
1267
1286
|
const pixels = this.bitmap;
|
|
1268
1287
|
let opaque = true;
|
|
@@ -1283,6 +1302,7 @@ export class Image {
|
|
|
1283
1302
|
checkThreads(threads);
|
|
1284
1303
|
const exif = exifData(resolveMetadata(metadata));
|
|
1285
1304
|
const lossy = webp.lossyOptions(quality, effort, sharpYuv, alphaQuality);
|
|
1305
|
+
checkFormatSize('WebP', this.width, this.height, lossy ? 16383 : 16384);
|
|
1286
1306
|
if (!lossy)
|
|
1287
1307
|
return webp.encode(this.__bytes__(), this.width, this.height, exif, threads, effort);
|
|
1288
1308
|
return webp.encodeLossy(this.__bytes__(), this.width, this.height, lossy, exif);
|
|
@@ -1297,8 +1317,7 @@ export class Image {
|
|
|
1297
1317
|
checkOptions(options, 'image.encodeGIF({ colors: 64 })');
|
|
1298
1318
|
const { colors = 256, dither = 'floyd-steinberg', ditherStrength = 0.85, metadata = {} } = options;
|
|
1299
1319
|
const palette = quantize.paletteOptions(colors, dither, ditherStrength);
|
|
1300
|
-
|
|
1301
|
-
throw new RangeError(`GIF: images can be at most 65535 pixels wide and high, not ${this.width}x${this.height}`);
|
|
1320
|
+
checkFormatSize('GIF', this.width, this.height, 0xffff);
|
|
1302
1321
|
const frame = { x: 0, y: 0, width: this.width, height: this.height, delay: 0, dispose: 1, pixels: this.__bytes__() };
|
|
1303
1322
|
return gif.encode({ width: this.width, height: this.height, loopCount: 0, frames: [frame] }, { colors: palette.colors, dither: palette.dither, strength: palette.strength }, gifComments(resolveMetadata(metadata)));
|
|
1304
1323
|
}
|
|
@@ -1320,15 +1339,14 @@ export class Image {
|
|
|
1320
1339
|
});
|
|
1321
1340
|
}
|
|
1322
1341
|
__gain_map_pixels__(option) {
|
|
1342
|
+
const embed = gainMapEmbed(option);
|
|
1323
1343
|
const gainMap = this.__gain_map__;
|
|
1324
|
-
if (!gainMap ||
|
|
1344
|
+
if (!gainMap || !embed)
|
|
1325
1345
|
return undefined;
|
|
1326
|
-
const { quality = 90, scale } =
|
|
1327
|
-
const mapQuality = Math.max(1, checkQuality(quality, 'gain map quality'));
|
|
1346
|
+
const { quality: mapQuality = 90, scale } = embed;
|
|
1328
1347
|
let map = gainMap.image;
|
|
1329
1348
|
if (scale !== undefined) {
|
|
1330
|
-
const
|
|
1331
|
-
const width = Math.max(1, Math.round(this.width * s)), height = Math.max(1, Math.round(this.height * s));
|
|
1349
|
+
const width = Math.max(1, Math.round(this.width * scale)), height = Math.max(1, Math.round(this.height * scale));
|
|
1332
1350
|
if (width !== map.width || height !== map.height)
|
|
1333
1351
|
map = map.clone().resize(width, height);
|
|
1334
1352
|
}
|
|
@@ -1353,6 +1371,9 @@ export class Image {
|
|
|
1353
1371
|
checkThreads(threads);
|
|
1354
1372
|
pixelLimit(maxPixels);
|
|
1355
1373
|
oneOf(toneMapping, TONE_MAPPINGS, 'toneMapping');
|
|
1374
|
+
boolean(applyOrientation, 'applyOrientation');
|
|
1375
|
+
boolean(applyColorProfile, 'applyColorProfile');
|
|
1376
|
+
boolean(gainMap, 'gainMap');
|
|
1356
1377
|
const data = view(input);
|
|
1357
1378
|
const dataView = new DataView(data.buffer, data.byteOffset, data.byteLength);
|
|
1358
1379
|
const decoded = await decoding(() => {
|
|
@@ -1485,7 +1506,7 @@ export class Image {
|
|
|
1485
1506
|
return this.__apply__(out);
|
|
1486
1507
|
}
|
|
1487
1508
|
static async renderSVG(svg, options = {}) {
|
|
1488
|
-
checkOptions(options, 'renderSVG(svg, { width: 512 })');
|
|
1509
|
+
checkOptions(options, 'Image.renderSVG(svg, { width: 512 })');
|
|
1489
1510
|
const { width: givenWidth, height: givenHeight, scale, maxPixels = MAX_PIXELS, font: fonts = [], images: givenImages = {} } = options;
|
|
1490
1511
|
pixelLimit(maxPixels);
|
|
1491
1512
|
const { maxLayerPixels = 4 * maxPixels, maxImagePixels = maxPixels } = options;
|
|
@@ -1495,17 +1516,17 @@ export class Image {
|
|
|
1495
1516
|
if (sizes > 1)
|
|
1496
1517
|
throw new TypeError('renderSVG() takes one of width, height and scale');
|
|
1497
1518
|
const mode = givenWidth !== undefined ? 'width' : givenHeight !== undefined ? 'height' : 'scale';
|
|
1498
|
-
const size = finite(givenWidth
|
|
1499
|
-
if (mode === 'scale'
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1519
|
+
const size = finite(mode === 'width' ? givenWidth : mode === 'height' ? givenHeight : scale === undefined ? 1 : scale, `SVG ${mode}`);
|
|
1520
|
+
if (mode === 'scale')
|
|
1521
|
+
positive(size, 'SVG scale');
|
|
1522
|
+
else
|
|
1523
|
+
atLeast(size, 1, `SVG ${mode}`);
|
|
1503
1524
|
if (typeof givenImages !== 'object' || givenImages === null || Array.isArray(givenImages) || givenImages instanceof Map)
|
|
1504
1525
|
throw new TypeError('SVG images must be an object of hrefs to image data');
|
|
1505
1526
|
const given = new Map();
|
|
1506
1527
|
for (const href of Object.keys(givenImages)) {
|
|
1507
1528
|
const value = givenImages[href];
|
|
1508
|
-
if (!(value instanceof Image) && !(value
|
|
1529
|
+
if (!(value instanceof Image) && !isBinaryData(value))
|
|
1509
1530
|
throw new TypeError(`SVG image ${JSON.stringify(href)} must be BinaryData or an Image`);
|
|
1510
1531
|
given.set(href, value);
|
|
1511
1532
|
}
|
|
@@ -1543,28 +1564,31 @@ export class Image {
|
|
|
1543
1564
|
images.set(href, { error: err instanceof LimitError ? tooLarge(err) : new Error(`SVG: the image ${shown(href)} can't be decoded`, { cause: err }) });
|
|
1544
1565
|
}
|
|
1545
1566
|
}
|
|
1546
|
-
const { width, height, pixels } = svgRenderer.render(source, mode, size, images, (Array.isArray(fonts) ? fonts : [fonts]
|
|
1567
|
+
const { width, height, pixels } = svgRenderer.render(source, mode, size, images, Array.from(Array.isArray(fonts) ? fonts : [fonts], font => view(font)), maxPixels, maxLayerPixels);
|
|
1547
1568
|
const image = new Image(width, height);
|
|
1548
1569
|
image.bitmap.set(pixels);
|
|
1549
1570
|
return image;
|
|
1550
1571
|
}
|
|
1551
1572
|
static async renderText(text, options) {
|
|
1552
|
-
if (typeof text !== 'string'
|
|
1553
|
-
throw new TypeError(
|
|
1573
|
+
if (typeof text !== 'string')
|
|
1574
|
+
throw new TypeError(`Image.renderText() takes the text as a string, like Image.renderText('Hello', { font, size: 32 }), not ${shown(text)}`);
|
|
1575
|
+
if (!isOptions(options))
|
|
1576
|
+
throw new TypeError(`Image.renderText() takes an options object, like Image.renderText('Hello', { font, size: 32 }), not ${options === undefined ? 'undefined' : shown(options)}`);
|
|
1554
1577
|
if (options.font === undefined)
|
|
1555
|
-
throw new TypeError('renderText(text, options) needs the font in the options');
|
|
1578
|
+
throw new TypeError('Image.renderText(text, options) needs the font in the options');
|
|
1556
1579
|
const { font, color = 0xffffffff, strokeWidth = 0, strokeColor = 0x000000ff, hinting = true, features = {}, variations = {}, maxPixels = MAX_PIXELS } = options;
|
|
1557
1580
|
pixelLimit(maxPixels);
|
|
1581
|
+
boolean(hinting, 'hinting');
|
|
1558
1582
|
const { width: boxWidth, height: boxHeight, wrapStyle, horizontalAlign, verticalAlign } = options;
|
|
1559
1583
|
const layout = textLayout({ width: boxWidth, height: boxHeight, wrapStyle, horizontalAlign, verticalAlign });
|
|
1560
1584
|
const axes = textRenderer.axisValues(variations);
|
|
1561
1585
|
const size = positive(options.size, 'font size');
|
|
1562
|
-
|
|
1563
|
-
throw new RangeError('Invalid stroke width');
|
|
1586
|
+
nonNegative(strokeWidth, 'stroke width');
|
|
1564
1587
|
const fonts = Array.isArray(font) ? font : [font];
|
|
1565
1588
|
if (!fonts.length)
|
|
1566
|
-
throw new
|
|
1589
|
+
throw new TypeError('font must be a font or an array of one or more fonts, not an empty array');
|
|
1567
1590
|
const [r, g, b, a] = Colors.toRGBA(color);
|
|
1591
|
+
const [sr, sg, sb, sa] = Colors.toRGBA(strokeColor);
|
|
1568
1592
|
const pad = Math.ceil(strokeWidth);
|
|
1569
1593
|
if (layout.width !== undefined && layout.width <= 2 * pad)
|
|
1570
1594
|
throw new RangeError(`The width (${layout.width}) leaves no room for the text inside the ${strokeWidth} pixel outline`);
|
|
@@ -1574,7 +1598,7 @@ export class Image {
|
|
|
1574
1598
|
layout.width -= 2 * pad;
|
|
1575
1599
|
if (layout.height !== undefined)
|
|
1576
1600
|
layout.height -= 2 * pad;
|
|
1577
|
-
const rendered = await textRenderer.render(
|
|
1601
|
+
const rendered = await textRenderer.render(Array.from(fonts, view), size, text, layout, hinting, (r << 24 | g << 16 | b << 8 | 255) >>> 0, featureList(features), axes, maxPixels, pad);
|
|
1578
1602
|
const width = rendered.width + 2 * pad, height = rendered.height + 2 * pad;
|
|
1579
1603
|
const coverage = new Uint8Array(width * height);
|
|
1580
1604
|
for (let y = 0; y < rendered.height; y++)
|
|
@@ -1588,7 +1612,7 @@ export class Image {
|
|
|
1588
1612
|
const image = new Image(width, height);
|
|
1589
1613
|
const bitmap = image.bitmap;
|
|
1590
1614
|
if (!strokeWidth && !colored) {
|
|
1591
|
-
const pixels = new Uint32Array(bitmap.buffer, bitmap.byteOffset, bitmap.length
|
|
1615
|
+
const pixels = new Uint32Array(bitmap.buffer, bitmap.byteOffset, bitmap.length >>> 2), lut = new Uint32Array(256);
|
|
1592
1616
|
const bytes = new Uint8ClampedArray(lut.buffer);
|
|
1593
1617
|
for (let v = 1; v < 256; v++)
|
|
1594
1618
|
bytes.set([r, g, b, v * a / 255], v * 4);
|
|
@@ -1604,7 +1628,6 @@ export class Image {
|
|
|
1604
1628
|
shape[i] = Math.max(shape[i], colored[i * 4 + 3]);
|
|
1605
1629
|
}
|
|
1606
1630
|
const stroke = strokeWidth ? outline(shape, width, height, strokeWidth) : null;
|
|
1607
|
-
const [sr, sg, sb, sa] = Colors.toRGBA(strokeColor);
|
|
1608
1631
|
for (let i = 0; i < coverage.length; i++) {
|
|
1609
1632
|
let af = coverage[i] / 255 * a / 255, pr = r * af, pg = g * af, pb = b * af;
|
|
1610
1633
|
if (colored && colored[i * 4 + 3]) {
|
|
@@ -1653,6 +1676,8 @@ export function alignedPixels(pixels) {
|
|
|
1653
1676
|
}
|
|
1654
1677
|
export function resizeArguments(widthOrOptions, height, options) {
|
|
1655
1678
|
if (!isOptions(widthOrOptions)) {
|
|
1679
|
+
if (typeof widthOrOptions !== 'number')
|
|
1680
|
+
throw new TypeError(`resize() takes a width and a height, like resize(300, 200), or an options object, like resize({ width: 300 }), not ${shown(widthOrOptions)}`);
|
|
1656
1681
|
checkOptions(options, 'resize(width, height, { mode: \'area\' })');
|
|
1657
1682
|
return [finite(widthOrOptions, 'width'), finite(height, 'height'), options, 'fill'];
|
|
1658
1683
|
}
|
|
@@ -1797,3 +1822,4 @@ function distanceTransform(f, n, d, v, z, nearest) {
|
|
|
1797
1822
|
nearest[q] = v[j];
|
|
1798
1823
|
}
|
|
1799
1824
|
}
|
|
1825
|
+
describe(Image, image => ({ width: image.width, height: image.height }));
|