imagescript 3.6.0 → 3.8.0
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 +46 -97
- package/THIRD_PARTY_NOTICES.md +104 -3
- package/dist/animation-encoder.d.ts +62 -0
- package/dist/animation-encoder.js +204 -0
- package/dist/animation.d.ts +32 -2
- package/dist/animation.js +14 -3
- package/dist/codecs/av1/encode.js +47 -23
- package/dist/codecs/gif.js +37 -24
- package/dist/codecs/jxl/acs-enc.js +294 -0
- package/dist/codecs/jxl/aq-enc.js +176 -0
- package/dist/codecs/jxl/bit-writer.js +46 -0
- package/dist/codecs/jxl/color.js +61 -1
- package/dist/codecs/jxl/dct.js +7 -7
- package/dist/codecs/jxl/entropy-enc.js +589 -0
- package/dist/codecs/jxl/entropy.js +1 -1
- package/dist/codecs/jxl/fast-lossless.js +2 -107
- package/dist/codecs/jxl/fdct.js +1114 -0
- package/dist/codecs/jxl/filters.js +49 -2
- package/dist/codecs/jxl/frame.js +4 -1
- package/dist/codecs/jxl/headers-enc.js +60 -0
- package/dist/codecs/jxl/idct.js +804 -2
- package/dist/codecs/jxl/modular-enc.js +100 -0
- package/dist/codecs/jxl/modular.js +3 -2
- package/dist/codecs/jxl/quant.js +1 -0
- package/dist/codecs/jxl/transforms-enc.js +188 -0
- package/dist/codecs/jxl/vardct-enc.js +397 -0
- package/dist/codecs/jxl/vardct.js +6 -6
- package/dist/codecs/jxl.js +3 -2
- package/dist/codecs/png.js +77 -37
- package/dist/codecs/sharpyuv.js +150 -0
- package/dist/codecs/vp8-enc-dsp.js +9 -1
- package/dist/codecs/vp8l.js +357 -59
- package/dist/codecs/webp.js +36 -26
- package/dist/codecs/zlib.js +20 -0
- package/dist/image.d.ts +50 -10
- package/dist/image.js +65 -17
- package/dist/index.d.ts +3 -1
- package/dist/index.js +1 -0
- package/dist/render/filter.js +939 -0
- package/dist/render/shape.js +12 -6
- package/dist/render/svg.js +1140 -36
- package/dist/render/text.js +9 -9
- package/dist/util/optimize.js +208 -62
- package/dist/version.js +1 -1
- package/package.json +1 -1
package/dist/codecs/webp.js
CHANGED
|
@@ -268,8 +268,8 @@ export function encode(rgba, width, height, exif) {
|
|
|
268
268
|
set24(vp8x, 7, height - 1);
|
|
269
269
|
return riff([riffChunk('VP8X', vp8x), bitstream, riffChunk('EXIF', exif)]);
|
|
270
270
|
}
|
|
271
|
-
export function encodeLossy(rgba, width, height, quality, exif, method = 4) {
|
|
272
|
-
const { alpha, chunks } = lossyChunks(rgba, width, height, quality, method);
|
|
271
|
+
export function encodeLossy(rgba, width, height, quality, exif, method = 4, sharp = false) {
|
|
272
|
+
const { alpha, chunks } = lossyChunks(rgba, width, height, quality, method, sharp);
|
|
273
273
|
if (!alpha && !exif)
|
|
274
274
|
return riff(chunks);
|
|
275
275
|
const vp8x = new Uint8Array(10);
|
|
@@ -278,10 +278,10 @@ export function encodeLossy(rgba, width, height, quality, exif, method = 4) {
|
|
|
278
278
|
set24(vp8x, 7, height - 1);
|
|
279
279
|
return riff([riffChunk('VP8X', vp8x), ...chunks, ...exif ? [riffChunk('EXIF', exif)] : []]);
|
|
280
280
|
}
|
|
281
|
-
function lossyChunks(rgba, width, height, quality, method) {
|
|
281
|
+
function lossyChunks(rgba, width, height, quality, method, sharp = false) {
|
|
282
282
|
if (width > 16383 || height > 16383)
|
|
283
283
|
throw new RangeError('WebP: lossy images can be at most 16383 pixels wide and high');
|
|
284
|
-
const picture = rgbaToYUV(rgba, width, height);
|
|
284
|
+
const picture = rgbaToYUV(rgba, width, height, sharp);
|
|
285
285
|
const frame = encodeVP8(picture, quality, method);
|
|
286
286
|
const padded = new Uint8Array(frame.length + (frame.length & 1));
|
|
287
287
|
padded.set(frame);
|
|
@@ -290,14 +290,41 @@ function lossyChunks(rgba, width, height, quality, method) {
|
|
|
290
290
|
chunks.unshift(riffChunk('ALPH', encodeAlpha(picture.a, width, height, method)));
|
|
291
291
|
return { alpha: !!picture.a, chunks };
|
|
292
292
|
}
|
|
293
|
-
export function
|
|
293
|
+
export function lossyTolerance(quality) {
|
|
294
|
+
const value = Math.sqrt(quality / 100);
|
|
295
|
+
return Math.max(1, Math.round(31 * (1 - value) + value));
|
|
296
|
+
}
|
|
297
|
+
export function encodeAnimation(frames, width, height, plays, exif, quality, method = 4, sharp = false) {
|
|
294
298
|
if (!frames.length)
|
|
295
299
|
throw new Error('WebP: an animation needs at least one frame');
|
|
296
300
|
if (width > 1 << 24 || height > 1 << 24)
|
|
297
301
|
throw new RangeError('WebP: canvas too large');
|
|
298
|
-
|
|
299
|
-
const
|
|
300
|
-
|
|
302
|
+
let alpha = false;
|
|
303
|
+
for (const [i, f] of frames.entries())
|
|
304
|
+
alpha ||= needsAlpha(f, i === 0, width, height);
|
|
305
|
+
return animationFile(frames.map(f => frameChunk(f, quality, method, sharp)), width, height, plays, alpha, exif);
|
|
306
|
+
}
|
|
307
|
+
export function needsAlpha(f, first, width, height) {
|
|
308
|
+
return (first && (f.x > 0 || f.y > 0 || f.width < width || f.height < height)) || f.dispose || hasAlpha(f.pixels);
|
|
309
|
+
}
|
|
310
|
+
export function frameChunk(f, quality, method = 4, sharp = false) {
|
|
311
|
+
if (f.x & 1 || f.y & 1)
|
|
312
|
+
throw new RangeError('WebP: frame offsets have to be even');
|
|
313
|
+
const bitstream = quality === undefined ? riffChunk('VP8L', vp8l.encode(toARGB(f.pixels, f.width * f.height), f.width, f.height))
|
|
314
|
+
: concat(lossyChunks(f.pixels, f.width, f.height, quality, method, sharp).chunks);
|
|
315
|
+
const header = new Uint8Array(16);
|
|
316
|
+
set24(header, 0, f.x >> 1);
|
|
317
|
+
set24(header, 3, f.y >> 1);
|
|
318
|
+
set24(header, 6, f.width - 1);
|
|
319
|
+
set24(header, 9, f.height - 1);
|
|
320
|
+
set24(header, 12, Math.min(0xffffff, Math.max(0, Math.round(f.duration))));
|
|
321
|
+
header[15] = (f.blend === false ? 2 : 0) | (f.dispose ? 1 : 0);
|
|
322
|
+
const body = new Uint8Array(16 + bitstream.length);
|
|
323
|
+
body.set(header);
|
|
324
|
+
body.set(bitstream, 16);
|
|
325
|
+
return riffChunk('ANMF', body);
|
|
326
|
+
}
|
|
327
|
+
export function animationFile(frames, width, height, plays, alpha, exif) {
|
|
301
328
|
const vp8x = new Uint8Array(10);
|
|
302
329
|
vp8x[0] = 2 | (alpha ? 16 : 0) | (exif ? 8 : 0);
|
|
303
330
|
set24(vp8x, 4, width - 1);
|
|
@@ -305,24 +332,7 @@ export function encodeAnimation(frames, width, height, plays, exif, quality, met
|
|
|
305
332
|
const anim = new Uint8Array(6);
|
|
306
333
|
anim[4] = plays & 255;
|
|
307
334
|
anim[5] = plays >> 8 & 255;
|
|
308
|
-
const chunks = [riffChunk('VP8X', vp8x), riffChunk('ANIM', anim)];
|
|
309
|
-
for (const f of frames) {
|
|
310
|
-
if (f.x & 1 || f.y & 1)
|
|
311
|
-
throw new RangeError('WebP: frame offsets have to be even');
|
|
312
|
-
const bitstream = quality === undefined ? riffChunk('VP8L', vp8l.encode(toARGB(f.pixels, f.width * f.height), f.width, f.height))
|
|
313
|
-
: concat(lossyChunks(f.pixels, f.width, f.height, quality, method).chunks);
|
|
314
|
-
const header = new Uint8Array(16);
|
|
315
|
-
set24(header, 0, f.x >> 1);
|
|
316
|
-
set24(header, 3, f.y >> 1);
|
|
317
|
-
set24(header, 6, f.width - 1);
|
|
318
|
-
set24(header, 9, f.height - 1);
|
|
319
|
-
set24(header, 12, Math.min(0xffffff, Math.max(0, Math.round(f.duration))));
|
|
320
|
-
header[15] = (f.blend === false ? 2 : 0) | (f.dispose ? 1 : 0);
|
|
321
|
-
const body = new Uint8Array(16 + bitstream.length);
|
|
322
|
-
body.set(header);
|
|
323
|
-
body.set(bitstream, 16);
|
|
324
|
-
chunks.push(riffChunk('ANMF', body));
|
|
325
|
-
}
|
|
335
|
+
const chunks = [riffChunk('VP8X', vp8x), riffChunk('ANIM', anim), ...frames];
|
|
326
336
|
if (exif)
|
|
327
337
|
chunks.push(riffChunk('EXIF', exif));
|
|
328
338
|
return riff(chunks);
|
package/dist/codecs/zlib.js
CHANGED
|
@@ -553,6 +553,26 @@ export function decompress(data, limit) {
|
|
|
553
553
|
throw new Error('zlib: preset dictionaries are not supported');
|
|
554
554
|
return inflate(data.subarray(2), new u8(limit));
|
|
555
555
|
}
|
|
556
|
+
export function gunzip(data, limit) {
|
|
557
|
+
if (data.length < 18 || data[0] !== 0x1f || data[1] !== 0x8b || data[2] !== 8)
|
|
558
|
+
throw new Error('gzip: invalid header');
|
|
559
|
+
const flags = data[3];
|
|
560
|
+
let o = 10;
|
|
561
|
+
if (flags & 4)
|
|
562
|
+
o += 2 + (data[o] | data[o + 1] << 8);
|
|
563
|
+
if (flags & 8)
|
|
564
|
+
while (o < data.length && data[o++])
|
|
565
|
+
;
|
|
566
|
+
if (flags & 16)
|
|
567
|
+
while (o < data.length && data[o++])
|
|
568
|
+
;
|
|
569
|
+
if (flags & 2)
|
|
570
|
+
o += 2;
|
|
571
|
+
const end = data.length - 4, size = (data[end] | data[end + 1] << 8 | data[end + 2] << 16 | data[end + 3] << 24) >>> 0;
|
|
572
|
+
if (o > end - 4 || size > limit)
|
|
573
|
+
throw new Error('gzip: invalid or too large');
|
|
574
|
+
return inflate(data.subarray(o, end - 4), new u8(size));
|
|
575
|
+
}
|
|
556
576
|
const { process: runtime, CompressionStream: NativeCompression, DecompressionStream: NativeDecompression, Blob: NativeBlob } = globalThis;
|
|
557
577
|
export const nodeZlib = (() => {
|
|
558
578
|
try {
|
package/dist/image.d.ts
CHANGED
|
@@ -79,10 +79,17 @@ export interface WebPEncodeOptions {
|
|
|
79
79
|
* quantized values. Default: `2`: about a quarter of the time of 4 (cwebp's default) for files about 5% larger
|
|
80
80
|
*/
|
|
81
81
|
effort?: number;
|
|
82
|
+
/**
|
|
83
|
+
* For lossy compression: convert to YUV like libwebp's sharp conversion (cwebp's `-sharp_yuv`, the same file),
|
|
84
|
+
* which picks the color planes so that colored edges (red text on blue, thin colored lines) stay sharp instead of
|
|
85
|
+
* bleeding. It takes about twice the time at the default effort; photos get about as large, images with many
|
|
86
|
+
* hard color edges a lot larger (1200x800 red and blue squares: 2.7 times). Default: `false`
|
|
87
|
+
*/
|
|
88
|
+
sharpYuv?: boolean;
|
|
82
89
|
/** Metadata to include, see {@link Metadata} */
|
|
83
90
|
metadata?: Metadata;
|
|
84
91
|
}
|
|
85
|
-
/** Options of {@link Image.encodeTGA}
|
|
92
|
+
/** Options of {@link Image.encodeTGA} */
|
|
86
93
|
export interface EncodeOptions {
|
|
87
94
|
/** Metadata to include, see {@link Metadata} */
|
|
88
95
|
metadata?: Metadata;
|
|
@@ -132,6 +139,21 @@ export interface DecodeOptions {
|
|
|
132
139
|
*/
|
|
133
140
|
toneMapping?: ToneMapping;
|
|
134
141
|
}
|
|
142
|
+
/** Options of {@link Image.encodeJXL} */
|
|
143
|
+
export interface JXLEncodeOptions {
|
|
144
|
+
/**
|
|
145
|
+
* Lossy compression (0-100, like cjxl's `-q`: 90 is visually lossless, 100 lossless): higher is better and larger.
|
|
146
|
+
* Without it the image is encoded losslessly
|
|
147
|
+
*/
|
|
148
|
+
quality?: number;
|
|
149
|
+
/**
|
|
150
|
+
* How hard lossy compression searches for the best transforms: 0-2, faster to smaller. 0 uses 8x8 DCTs only (like
|
|
151
|
+
* `cjxl -e 3`), 1 and 2 search their sizes like `cjxl -e 5` (2 tries more of the small ones). Default: `1`
|
|
152
|
+
*/
|
|
153
|
+
effort?: number;
|
|
154
|
+
/** Metadata to include, see {@link Metadata} */
|
|
155
|
+
metadata?: Metadata;
|
|
156
|
+
}
|
|
135
157
|
/** Options of {@link Image.encodeAVIF} */
|
|
136
158
|
export interface AVIFEncodeOptions {
|
|
137
159
|
/** 0-100, like avifenc's: higher is better and larger, 100 is lossless. Default: `60` */
|
|
@@ -157,6 +179,12 @@ export interface TextStyle {
|
|
|
157
179
|
* pixels. Fonts without instructions (and CFF/OpenType outlines) aren't affected. Default: `true`
|
|
158
180
|
*/
|
|
159
181
|
hinting?: boolean;
|
|
182
|
+
/**
|
|
183
|
+
* OpenType features by their tags, for the whole text: `true` turns one on (like `tnum` for tabular numbers,
|
|
184
|
+
* `smcp` for small caps, `ss01` for a stylistic set), `false` off (also the ones on by default, like `liga` or
|
|
185
|
+
* `kern`), a number picks an alternate (`salt: 2`). Fonts without a feature ignore it. Default: `{}`
|
|
186
|
+
*/
|
|
187
|
+
features?: Record<string, boolean | number>;
|
|
160
188
|
}
|
|
161
189
|
/** Options of {@link Image.cropCircle} */
|
|
162
190
|
export interface CropCircleOptions {
|
|
@@ -448,6 +476,16 @@ export declare class Image {
|
|
|
448
476
|
* @param height The new images height
|
|
449
477
|
*/
|
|
450
478
|
crop(x: number, y: number, width: number, height: number): this;
|
|
479
|
+
/**
|
|
480
|
+
* Copies a region of the image into a new image, this one stays unchanged (like {@link Image.crop} on a clone,
|
|
481
|
+
* without copying the whole image). The region is clipped to the image, so the result can be smaller than the
|
|
482
|
+
* requested size
|
|
483
|
+
* @param x The x offset (0-based)
|
|
484
|
+
* @param y The y offset (0-based)
|
|
485
|
+
* @param width The region's width
|
|
486
|
+
* @param height The region's height
|
|
487
|
+
*/
|
|
488
|
+
region(x: number, y: number, width: number, height: number): Image;
|
|
451
489
|
/**
|
|
452
490
|
* Removes borders of one color, e.g. the empty space around rendered text or around a logo on a solid background.
|
|
453
491
|
* Frames keep their position: their offsets move by the trimmed amount. An image that only has the color stays
|
|
@@ -752,7 +790,7 @@ export declare class Image {
|
|
|
752
790
|
* quality
|
|
753
791
|
* @param options See {@link WebPEncodeOptions}
|
|
754
792
|
*/
|
|
755
|
-
encodeWebP({ quality, effort, metadata }?: WebPEncodeOptions): Promise<Uint8Array>;
|
|
793
|
+
encodeWebP({ quality, effort, sharpYuv, metadata }?: WebPEncodeOptions): Promise<Uint8Array>;
|
|
756
794
|
/**
|
|
757
795
|
* Encodes the image as run length encoded TGA (32 bit, or 24 bit if the image is opaque)
|
|
758
796
|
* @param options See {@link EncodeOptions}
|
|
@@ -773,13 +811,14 @@ export declare class Image {
|
|
|
773
811
|
*/
|
|
774
812
|
encodeAVIF({ quality, alphaQuality, subsampling, effort, metadata }?: AVIFEncodeOptions): Promise<Uint8Array>;
|
|
775
813
|
/**
|
|
776
|
-
* Encodes the image as
|
|
777
|
-
* or NEON (its fast lossless mode: the gradient predictor, a palette if the image has at most 511
|
|
778
|
-
* black, else YCoCg
|
|
779
|
-
*
|
|
780
|
-
*
|
|
814
|
+
* Encodes the image as JPEG XL: losslessly (the default), the codestream byte for byte like libjxl's `cjxl -e 1` on
|
|
815
|
+
* CPUs with AVX2 or NEON (its fast lossless mode: the gradient predictor, a palette if the image has at most 511
|
|
816
|
+
* colors besides black, else YCoCg; greyscale if the image is), or lossy with a quality (like `cjxl -q`: XYB,
|
|
817
|
+
* varblocks of libjxl's search, chroma from luma, the edge preserving filter). Alpha (if the image has
|
|
818
|
+
* transparency) is lossless either way. The codestream is in the container, with the metadata as Exif
|
|
819
|
+
* @param options See {@link JXLEncodeOptions}
|
|
781
820
|
*/
|
|
782
|
-
encodeJXL({ metadata }?:
|
|
821
|
+
encodeJXL({ quality, effort, metadata }?: JXLEncodeOptions): Promise<Uint8Array>;
|
|
783
822
|
/**
|
|
784
823
|
* Encodes the image as the smallest fitting Netpbm format: PBM for black and white images, PGM for greyscale ones,
|
|
785
824
|
* PPM for others, PAM if the image has transparency
|
|
@@ -810,8 +849,9 @@ export declare class Image {
|
|
|
810
849
|
static get SVG_MODE_HEIGHT(): 3;
|
|
811
850
|
/**
|
|
812
851
|
* Creates a new image from the given SVG
|
|
813
|
-
* Supports shapes, paths, transforms, use/symbol, CSS styles, gradients, strokes, clip paths,
|
|
814
|
-
*
|
|
852
|
+
* Supports shapes, paths, transforms, use/symbol, CSS styles, gradients, patterns, strokes, markers, clip paths,
|
|
853
|
+
* masks, opacity, filters (all primitives and the filter functions) and images embedded as data URIs (PNG, JPEG,
|
|
854
|
+
* GIF, WebP, SVG); text is not rendered
|
|
815
855
|
* @param svg The SVG content
|
|
816
856
|
* @param options See {@link SVGOptions}
|
|
817
857
|
* @return The rendered SVG graphic
|
package/dist/image.js
CHANGED
|
@@ -2,6 +2,7 @@ import * as png from "./codecs/png.js";
|
|
|
2
2
|
import * as jpeg from "./codecs/jpeg.js";
|
|
3
3
|
import * as tiff from "./codecs/tiff.js";
|
|
4
4
|
import * as webp from "./codecs/webp.js";
|
|
5
|
+
import * as gif from "./codecs/gif.js";
|
|
5
6
|
import * as tga from "./codecs/tga.js";
|
|
6
7
|
import * as bmp from "./codecs/bmp.js";
|
|
7
8
|
import * as ico from "./codecs/ico.js";
|
|
@@ -24,6 +25,19 @@ import { rotateBilinear } from "./util/rotate.js";
|
|
|
24
25
|
import { gaussianBlur, boxBlurRGBA, premultiply, unpremultiply } from "./util/blur.js";
|
|
25
26
|
import { isOptions, finite, color as checkColor } from "./util/options.js";
|
|
26
27
|
import { MAX_PIXELS, decoding } from "./util/limits.js";
|
|
28
|
+
function featureList(features) {
|
|
29
|
+
if (typeof features !== 'object' || features === null)
|
|
30
|
+
throw new TypeError('Features must be an object');
|
|
31
|
+
return Object.entries(features).map(([tag, value]) => {
|
|
32
|
+
if (!/^[\x20-\x7e]{4}$/.test(tag))
|
|
33
|
+
throw new RangeError(`Invalid OpenType feature tag: ${JSON.stringify(tag)}`);
|
|
34
|
+
if (typeof value === 'boolean')
|
|
35
|
+
return [tag, value ? 1 : 0];
|
|
36
|
+
if (!Number.isInteger(value) || value < 0 || value > 255)
|
|
37
|
+
throw new RangeError(`Invalid value of the OpenType feature ${tag}: ${value}`);
|
|
38
|
+
return [tag, value];
|
|
39
|
+
});
|
|
40
|
+
}
|
|
27
41
|
export class Image {
|
|
28
42
|
__width__;
|
|
29
43
|
__height__;
|
|
@@ -301,6 +315,12 @@ export class Image {
|
|
|
301
315
|
return image;
|
|
302
316
|
}
|
|
303
317
|
crop(x, y, width, height) {
|
|
318
|
+
return this.__apply__(this.__crop__(...this.__crop_bounds__(x, y, width, height)));
|
|
319
|
+
}
|
|
320
|
+
region(x, y, width, height) {
|
|
321
|
+
return this.__crop__(...this.__crop_bounds__(x, y, width, height));
|
|
322
|
+
}
|
|
323
|
+
__crop_bounds__(x, y, width, height) {
|
|
304
324
|
x = ~~finite(x, 'x');
|
|
305
325
|
y = ~~finite(y, 'y');
|
|
306
326
|
width = ~~finite(width, 'width');
|
|
@@ -309,7 +329,7 @@ export class Image {
|
|
|
309
329
|
const right = Math.min(this.width, x + width), bottom = Math.min(this.height, y + height);
|
|
310
330
|
if (right <= left || bottom <= top)
|
|
311
331
|
throw new RangeError('Crop region does not overlap the image');
|
|
312
|
-
return
|
|
332
|
+
return [left, top, right - left, bottom - top];
|
|
313
333
|
}
|
|
314
334
|
trim(color, options = {}) {
|
|
315
335
|
const bounds = this.__trim_bounds__(color, options);
|
|
@@ -510,26 +530,27 @@ export class Image {
|
|
|
510
530
|
const blend = BLEND_MODES[mode];
|
|
511
531
|
if (!blend || !Object.prototype.hasOwnProperty.call(BLEND_MODES, mode))
|
|
512
532
|
throw new RangeError(`Invalid blend mode: ${mode}`);
|
|
513
|
-
if (
|
|
533
|
+
if (mode !== 'normal')
|
|
514
534
|
return this.__blend__(source, x, y, opacity, blend);
|
|
535
|
+
if (opacity === 0)
|
|
536
|
+
return this;
|
|
515
537
|
const left = Math.max(0, x), top = Math.max(0, y);
|
|
516
538
|
const right = Math.min(this.width, x + source.width), bottom = Math.min(this.height, y + source.height);
|
|
517
539
|
const dst = this.bitmap, src = source.bitmap;
|
|
518
|
-
const dst32 = new Uint32Array(dst.buffer, dst.byteOffset, dst.length >> 2);
|
|
519
|
-
const src32 = new Uint32Array(src.buffer, src.byteOffset, src.length >> 2);
|
|
520
540
|
for (let row = top; row < bottom; row++) {
|
|
521
|
-
for (let column = left; column < right; column
|
|
522
|
-
const sp = (row - y) * source.width + column - x, s = 4 * sp;
|
|
541
|
+
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) {
|
|
523
542
|
const sa = src[s + 3];
|
|
524
543
|
if (sa === 0)
|
|
525
544
|
continue;
|
|
526
|
-
const dp = row * this.width + column, d = 4 * dp;
|
|
527
545
|
const da = dst[d + 3];
|
|
528
|
-
if (sa === 255 || da === 0) {
|
|
529
|
-
|
|
546
|
+
if ((sa === 255 && opacity === 1) || da === 0) {
|
|
547
|
+
dst[d] = src[s];
|
|
548
|
+
dst[d + 1] = src[s + 1];
|
|
549
|
+
dst[d + 2] = src[s + 2];
|
|
550
|
+
dst[d + 3] = sa * opacity;
|
|
530
551
|
continue;
|
|
531
552
|
}
|
|
532
|
-
const fa = sa / 255, ba = da / 255 * (1 - fa);
|
|
553
|
+
const fa = sa / 255 * opacity, ba = da / 255 * (1 - fa);
|
|
533
554
|
const a = fa + ba;
|
|
534
555
|
dst[d] = (src[s] * fa + dst[d] * ba) / a;
|
|
535
556
|
dst[d + 1] = (src[s + 1] * fa + dst[d + 1] * ba) / a;
|
|
@@ -1004,7 +1025,7 @@ export class Image {
|
|
|
1004
1025
|
const m = resolveMetadata(metadata);
|
|
1005
1026
|
return jpeg.encode(this.__bytes__(), this.width, this.height, finite(quality, 'quality'), { exif: exifData(m), comment: m.comment ? utf8(m.comment) : undefined });
|
|
1006
1027
|
}
|
|
1007
|
-
async encodeWebP({ quality, effort = 2, metadata = {} } = {}) {
|
|
1028
|
+
async encodeWebP({ quality, effort = 2, sharpYuv = false, metadata = {} } = {}) {
|
|
1008
1029
|
const exif = exifData(resolveMetadata(metadata));
|
|
1009
1030
|
if (quality === undefined)
|
|
1010
1031
|
return webp.encode(this.__bytes__(), this.width, this.height, exif);
|
|
@@ -1013,7 +1034,7 @@ export class Image {
|
|
|
1013
1034
|
throw new RangeError(`quality must be 0-100, not ${q}`);
|
|
1014
1035
|
if (!Number.isInteger(effort) || effort < 0 || effort > 6)
|
|
1015
1036
|
throw new RangeError(`effort must be an integer 0-6, not ${effort}`);
|
|
1016
|
-
return webp.encodeLossy(this.__bytes__(), this.width, this.height, q, exif, effort);
|
|
1037
|
+
return webp.encodeLossy(this.__bytes__(), this.width, this.height, q, exif, effort, sharpYuv);
|
|
1017
1038
|
}
|
|
1018
1039
|
async encodeTGA({ metadata = {} } = {}) {
|
|
1019
1040
|
const { author, comment, creationTime: time, software } = resolveMetadata(metadata);
|
|
@@ -1029,8 +1050,17 @@ export class Image {
|
|
|
1029
1050
|
const exif = exifData(resolveMetadata(metadata));
|
|
1030
1051
|
return avifEnc.encode(this.__bytes__(), this.width, this.height, { quality: finite(quality, 'quality'), alphaQuality: finite(alphaQuality, 'alphaQuality'), subsampling, effort, exif });
|
|
1031
1052
|
}
|
|
1032
|
-
async encodeJXL({ metadata = {} } = {}) {
|
|
1033
|
-
|
|
1053
|
+
async encodeJXL({ quality, effort = 1, metadata = {} } = {}) {
|
|
1054
|
+
if (!Number.isInteger(effort) || effort < 0 || effort > 2)
|
|
1055
|
+
throw new RangeError(`effort must be an integer 0-2, not ${effort}`);
|
|
1056
|
+
let distance = 0;
|
|
1057
|
+
if (quality !== undefined) {
|
|
1058
|
+
const q = finite(quality, 'quality');
|
|
1059
|
+
if (q < 0 || q > 100)
|
|
1060
|
+
throw new RangeError(`quality must be 0-100, not ${q}`);
|
|
1061
|
+
distance = q >= 100 ? 0 : q >= 30 ? 0.1 + (100 - q) * 0.09 : 53 / 3000 * q * q - 23 / 20 * q + 25;
|
|
1062
|
+
}
|
|
1063
|
+
return jxl.encode(this.__bytes__(), this.width, this.height, exifData(resolveMetadata(metadata)), distance, effort);
|
|
1034
1064
|
}
|
|
1035
1065
|
async encodePNM() {
|
|
1036
1066
|
return netpbm.encode(this.__bytes__(), this.width, this.height);
|
|
@@ -1144,14 +1174,32 @@ export class Image {
|
|
|
1144
1174
|
throw new RangeError('SVG scale must be > 0');
|
|
1145
1175
|
if (mode !== Image.SVG_MODE_SCALE && size < 1)
|
|
1146
1176
|
throw new RangeError('SVG size must be >= 1');
|
|
1147
|
-
const
|
|
1177
|
+
const source = typeof svg === 'string' ? svg : view(svg);
|
|
1178
|
+
const images = new Map();
|
|
1179
|
+
for (const { href, data } of svgRenderer.embeddedImages(source)) {
|
|
1180
|
+
if (images.has(href))
|
|
1181
|
+
continue;
|
|
1182
|
+
try {
|
|
1183
|
+
if (ImageType.getType(data) === 'gif') {
|
|
1184
|
+
const decoded = gif.decode(data, true);
|
|
1185
|
+
images.set(href, { width: decoded.width, height: decoded.height, pixels: gif.composite(decoded)[0].pixels });
|
|
1186
|
+
}
|
|
1187
|
+
else {
|
|
1188
|
+
const image = await Image.decode(data);
|
|
1189
|
+
images.set(href, { width: image.width, height: image.height, pixels: image.bitmap });
|
|
1190
|
+
}
|
|
1191
|
+
}
|
|
1192
|
+
catch {
|
|
1193
|
+
}
|
|
1194
|
+
}
|
|
1195
|
+
const { width, height, pixels } = svgRenderer.render(source, mode, size, images);
|
|
1148
1196
|
const image = new Image(width, height);
|
|
1149
1197
|
image.bitmap.set(pixels);
|
|
1150
1198
|
return image;
|
|
1151
1199
|
}
|
|
1152
1200
|
static async renderText(font, size, text, colorOrOptions = 0xffffffff, layoutArgument, styleArgument = {}) {
|
|
1153
1201
|
const options = isOptions(colorOrOptions) ? colorOrOptions : { ...styleArgument, color: colorOrOptions, layout: layoutArgument };
|
|
1154
|
-
const { color = 0xffffffff, layout = new TextLayout(), strokeWidth = 0, strokeColor = 0x000000ff, hinting = true } = options;
|
|
1202
|
+
const { color = 0xffffffff, layout = new TextLayout(), strokeWidth = 0, strokeColor = 0x000000ff, hinting = true, features = {} } = options;
|
|
1155
1203
|
size = finite(size, 'font size');
|
|
1156
1204
|
if (finite(strokeWidth, 'stroke width') < 0)
|
|
1157
1205
|
throw new RangeError('Invalid stroke width');
|
|
@@ -1159,7 +1207,7 @@ export class Image {
|
|
|
1159
1207
|
if (!fonts.length)
|
|
1160
1208
|
throw new RangeError('No font given');
|
|
1161
1209
|
const [r, g, b, a] = Image.colorToRGBA(color);
|
|
1162
|
-
const rendered = await textRenderer.render(fonts.map(view), size, text, layout, hinting, (r << 24 | g << 16 | b << 8 | 255) >>> 0);
|
|
1210
|
+
const rendered = await textRenderer.render(fonts.map(view), size, text, layout, hinting, (r << 24 | g << 16 | b << 8 | 255) >>> 0, featureList(features));
|
|
1163
1211
|
const pad = Math.ceil(strokeWidth);
|
|
1164
1212
|
const width = rendered.width + 2 * pad, height = rendered.height + 2 * pad;
|
|
1165
1213
|
const coverage = new Uint8Array(width * height);
|
package/dist/index.d.ts
CHANGED
|
@@ -3,12 +3,14 @@ import { Image, DecodeOptions } from './image.js';
|
|
|
3
3
|
import { GIF } from './gif.js';
|
|
4
4
|
import { Animation, AnimationDecodeOptions } from './animation.js';
|
|
5
5
|
export { Image } from './image.js';
|
|
6
|
-
export type { Color, ColorFunction, ResizeMode, SVGScaleMode, PNGMetadata, TextStyle, DecodeOptions, CompositeOptions, BlendMode, CropCircleOptions, DominantColorOptions, RotateOptions, ColorToAlphaOptions, ResizeOptions, AdjustOptions, SharpenOptions, PNGEncodeOptions, JPEGEncodeOptions, WebPEncodeOptions, EncodeOptions, SVGOptions, TextOptions, RoundCornersOptions, FisheyeOptions, TrimOptions } from './image.js';
|
|
6
|
+
export type { Color, ColorFunction, ResizeMode, SVGScaleMode, PNGMetadata, TextStyle, DecodeOptions, CompositeOptions, BlendMode, CropCircleOptions, DominantColorOptions, RotateOptions, ColorToAlphaOptions, ResizeOptions, AdjustOptions, SharpenOptions, PNGEncodeOptions, JPEGEncodeOptions, WebPEncodeOptions, AVIFEncodeOptions, JXLEncodeOptions, EncodeOptions, SVGOptions, TextOptions, RoundCornersOptions, FisheyeOptions, TrimOptions } from './image.js';
|
|
7
7
|
export { Frame } from './frame.js';
|
|
8
8
|
export type { FrameDisposalModeName, FrameDisposalModeId, FrameOptions } from './frame.js';
|
|
9
9
|
export { GIF } from './gif.js';
|
|
10
10
|
export { Animation } from './animation.js';
|
|
11
11
|
export type { AnimationOptions, AnimationDecodeOptions, AnimationEncodeOptions, WebPAnimationEncodeOptions, GIFEncodeOptions, APNGEncodeOptions } from './animation.js';
|
|
12
|
+
export { AnimationEncoder } from './animation-encoder.js';
|
|
13
|
+
export type { AnimationEncoderOptions, WebPAnimationEncoderOptions, GIFAnimationEncoderOptions, APNGAnimationEncoderOptions } from './animation-encoder.js';
|
|
12
14
|
export { TextLayout } from './text-layout.js';
|
|
13
15
|
export type { TextLayoutOptions, WrapStyle, HorizontalAlign, VerticalAlign } from './text-layout.js';
|
|
14
16
|
export { ImageType } from './image-type.js';
|
package/dist/index.js
CHANGED
|
@@ -9,6 +9,7 @@ export { Image } from "./image.js";
|
|
|
9
9
|
export { Frame } from "./frame.js";
|
|
10
10
|
export { GIF } from "./gif.js";
|
|
11
11
|
export { Animation } from "./animation.js";
|
|
12
|
+
export { AnimationEncoder } from "./animation-encoder.js";
|
|
12
13
|
export { TextLayout } from "./text-layout.js";
|
|
13
14
|
export { ImageType } from "./image-type.js";
|
|
14
15
|
export function decode(data, onlyFirstFrameOrOptions = false) {
|