imagescript 4.0.0-rc.5 → 4.0.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/dist/image.d.ts CHANGED
@@ -286,7 +286,7 @@ export interface EncodeOptions {
286
286
  */
287
287
  export interface GainMapEmbedOptions {
288
288
  /**
289
- * The map's quality (1-100, like the format's `quality`; for JPEG XL 100 is lossless)
289
+ * The map's quality (0-100, like the format's `quality`; for JPEG XL 100 is lossless)
290
290
  * @defaultValue `90`
291
291
  */
292
292
  quality?: number;
@@ -344,8 +344,8 @@ export interface JPEGEncodeOptions extends EncodeOptions {
344
344
  /** Options of {@link Image.encodeWebP} */
345
345
  export interface WebPEncodeOptions extends EncodeOptions {
346
346
  /**
347
- * Lossy compression (0-100): higher values usually preserve more detail but make larger files. Without it the image
348
- * is encoded losslessly, keeping every pixel exact
347
+ * Lossy compression (0-100): higher values usually preserve more detail but make larger files. `100`, like no
348
+ * quality, encodes losslessly and keeps every pixel exact (`99` is the highest lossy quality)
349
349
  */
350
350
  quality?: number;
351
351
  /**
@@ -398,9 +398,10 @@ export interface FisheyeOptions {
398
398
  /** The limits, fonts and images of {@link Image.renderSVG}, part of {@link RenderSVGOptions} */
399
399
  export interface SVGResourceOptions {
400
400
  /**
401
- * Maximum total pixels in the rendered output. Output over the limit throws a RangeError before anything is
401
+ * Maximum total pixels in the rendered output. Output over the limit throws a {@link LimitError} (a RangeError) before anything is
402
402
  * rendered. It's also the default of {@link SVGResourceOptions.maxLayerPixels} and
403
- * {@link SVGResourceOptions.maxImagePixels}
403
+ * {@link SVGResourceOptions.maxImagePixels}. The work limits of rendering grow with it: see
404
+ * {@link SVGResourceOptions.maxLayerPixels}
404
405
  * @defaultValue `268402689` (16383²); `Infinity` disables the limit
405
406
  */
406
407
  maxPixels?: number;
@@ -408,16 +409,25 @@ export interface SVGResourceOptions {
408
409
  * Maximum pixels of the layers rendering uses at once, the output's included. Groups with opacity, masks, clip
409
410
  * paths of nested viewports, filters and blend modes are each rendered into a layer of their own (up to the
410
411
  * output's size, 16 bytes per pixel) while their content is drawn, so deeply nested ones multiply the memory a
411
- * small SVG takes. Exceeding it throws a RangeError. With the defaults, rendering can take about 90 bytes per pixel
412
- * of `maxPixels` (64 for the layers, up to 20 for the images, 4 for the output): lower `maxPixels` to bound it for
413
- * untrusted SVGs
412
+ * small SVG takes. Exceeding it throws a {@link LimitError} (a RangeError). With the defaults, rendering can take
413
+ * about 90 bytes per pixel of `maxPixels` (64 for the layers, up to 20 for the images, 4 for the output): lower
414
+ * `maxPixels` to bound it for untrusted SVGs.
415
+ *
416
+ * Three limits on the work of a render don't depend on these options, and grow with the size of the output (so
417
+ * lowering `maxPixels` is what bounds them too). Each throws a {@link LimitError} when exceeded:
418
+ * - Painting: shapes and layers may paint 2²⁸ pixels plus 64 per pixel of the output
419
+ * - Paths: all paths together may be made of 2²⁵ points plus 4 per pixel of the output (curves flattened, each
420
+ * copy drawn through `<use>` counted, a command of a glyph outline 16), one path of 2²¹ (texts excepted)
421
+ * - Filters: all the filters' primitives together may work on 2²⁵ pixels plus 16 per pixel of the output (a
422
+ * primitive counts its region's pixels once per pass: an `feConvolveMatrix` per 9 taps of its kernel, an
423
+ * `feTurbulence` per octave, an `feMerge` per node)
414
424
  * @defaultValue `4 * maxPixels`; `Infinity` disables the limit
415
425
  */
416
426
  maxLayerPixels?: number;
417
427
  /**
418
428
  * Maximum total pixels of the raster images the SVG references (data URIs and {@link SVGResourceOptions.images}),
419
429
  * decoded together; already-decoded {@link Image} values are used as supplied and don't count. Drawing an image
420
- * beyond the limit throws a RangeError
430
+ * beyond the limit throws a {@link LimitError} (a RangeError)
421
431
  * @defaultValue `maxPixels`; `Infinity` disables the limit
422
432
  */
423
433
  maxImagePixels?: number;
@@ -428,8 +438,10 @@ export interface SVGResourceOptions {
428
438
  * installed fonts). Generic families (`serif`, `sans-serif`, `monospace`, ...) are the first font that fits them,
429
439
  * other families and text without one get the first font's family; characters a face doesn't have come from the
430
440
  * other fonts (TrueType, OpenType or WOFF). Fonts the SVG embeds with `@font-face` (in data: URIs) are used too,
431
- * before these for their family names. Without any fonts text isn't rendered. One font can be given without an
432
- * array
441
+ * before these for their family names. Without any fonts text isn't rendered (an empty array is the same as no
442
+ * option here; {@link Image.renderText} needs a font and throws for it). One font can be given without an
443
+ * array. A font here that can't be read throws an Error, like in {@link Image.renderText}; a damaged font that the
444
+ * SVG embeds is left out
433
445
  * @defaultValue `[]`
434
446
  */
435
447
  font?: BinaryData | BinaryData[];
@@ -498,7 +510,7 @@ export interface RenderTextOptions extends TextStyleOptions, TextLayoutOptions {
498
510
  */
499
511
  color?: Color;
500
512
  /**
501
- * Maximum total pixels of the rendered image (the outline included): larger text throws a RangeError before its
513
+ * Maximum total pixels of the rendered image (the outline included): larger text throws a {@link LimitError} (a RangeError) before its
502
514
  * pixels are allocated
503
515
  * @defaultValue `268402689` (16383²); `Infinity` disables the limit
504
516
  */
@@ -508,7 +520,8 @@ export interface RenderTextOptions extends TextStyleOptions, TextLayoutOptions {
508
520
  export interface DecodeOptions {
509
521
  /**
510
522
  * Whether to rotate or mirror the image according to its saved orientation information, so photos taken with a
511
- * rotated camera display upright. This applies to every animation frame too
523
+ * rotated camera display upright. This applies to every animation frame too. AVIF's rotation and mirroring
524
+ * (`irot`, `imir`) are part of the image in that format and always applied, like other AVIF decoders do
512
525
  * @defaultValue `true`
513
526
  */
514
527
  applyOrientation?: boolean;
@@ -522,9 +535,10 @@ export interface DecodeOptions {
522
535
  */
523
536
  applyColorProfile?: boolean;
524
537
  /**
525
- * The maximum total pixels to decode. An image declaring more pixels throws a RangeError before its pixel buffer
538
+ * The maximum total pixels to decode. An image declaring more pixels throws a {@link LimitError} (a RangeError) before its pixel buffer
526
539
  * is allocated, helping prevent a small or untrusted file from requesting an enormous amount of memory.
527
- * {@link Animation.decode} counts all frames together because each frame uses a full canvas. The decoded image
540
+ * {@link Animation.decode} counts all frames together because each frame uses a full canvas (and at least 1024
541
+ * pixels, for the memory a frame takes however small it is). The decoded image
528
542
  * takes 4 bytes per pixel (about 1 GiB at the default), and decoding needs more while it runs: for 8-bit images,
529
543
  * about 4-7 bytes per pixel for PNG, JPEG, WebP, GIF and the simpler formats, about 14 for AVIF and about 60 for
530
544
  * lossy JPEG XL on one thread, about 110 with worker threads (more with high bit depths). Lower it to bound the
@@ -746,13 +760,16 @@ export interface ColorToAlphaOptions {
746
760
  * and return it, so calls can be chained
747
761
  *
748
762
  * @remarks Subclassing is not supported: the methods that make an image ({@link Image.clone}, {@link Image.extract},
749
- * {@link Image.decode}, ...) return plain `Image`s, without calling a subclass' constructor
763
+ * {@link Image.decode}, ...) return plain `Image`s, without calling a subclass' constructor.
764
+ *
765
+ * The `maxPixels` limits guard what comes from outside: decoding and rendering. `new Image()`, `resize()` and
766
+ * `scale()` make the size they are asked for, so check sizes that come from users before you pass them on
750
767
  */
751
768
  export declare class Image {
752
769
  /**
753
770
  * Creates a new image with the given dimensions, all pixels transparent black
754
- * @param width The width in pixels
755
- * @param height The height in pixels
771
+ * @param width The width in pixels (a fraction is truncated)
772
+ * @param height The height in pixels (a fraction is truncated)
756
773
  * @throws `TypeError` if the width or height is not a number
757
774
  * @throws `RangeError` if the width or height is less than 1, or the image is too large to allocate
758
775
  */
@@ -760,7 +777,8 @@ export declare class Image {
760
777
  /**
761
778
  * Extra image data that lets high-dynamic-range (HDR) screens show brighter highlights. Standard screens show the
762
779
  * image without the extra brightness. {@link Image.encodeJPEG}, {@link Image.encodeAVIF} and
763
- * {@link Image.encodeJXL} can write it. `null` means there is no gain map
780
+ * {@link Image.encodeJXL} can write it. `null` means there is no gain map. This is the image's own map, not a
781
+ * copy: changing it (`image.gainMap.max = 3`) changes the image's
764
782
  */
765
783
  get gainMap(): GainMap | null;
766
784
  /**
@@ -796,7 +814,7 @@ export declare class Image {
796
814
  get height(): number;
797
815
  /**
798
816
  * Yields an [x, y, color] array for every pixel in the image (0-based), row by row. Resizing the image meanwhile
799
- * doesn't affect the iteration (it goes on over the pixels it started with), but other changes to the pixels, like
817
+ * doesn't affect the iteration (it goes on over the pixels the image had when `pixels()` was called), but other changes to the pixels, like
800
818
  * `setPixelAt()`, are seen by the pixels not yet yielded
801
819
  * @yields The coordinates and color of the pixel ([x, y, color])
802
820
  */
@@ -819,8 +837,8 @@ export declare class Image {
819
837
  static radialGradient(width: number, height: number, options: RadialGradientOptions): Image;
820
838
  /**
821
839
  * Gets the pixel color at the specified position (0-based). {@link Colors.toRGBA} splits it into its channels
822
- * @param x The x coordinate
823
- * @param y The y coordinate
840
+ * @param x The x coordinate (a fraction is dropped: 1.9 is the pixel 1)
841
+ * @param y The y coordinate (likewise)
824
842
  * @returns The color value (`0xRRGGBBAA`)
825
843
  * @throws `RangeError` if the position isn't inside the image
826
844
  * @see {@link Image.pixels} to go over every pixel
@@ -835,7 +853,8 @@ export declare class Image {
835
853
  */
836
854
  setPixelAt(x: number, y: number, color: Color): this;
837
855
  /**
838
- * Fills the image data with the supplied color
856
+ * Fills the image data with the supplied color. The image's gain map, if it has one, is made neutral (no boost
857
+ * anywhere): the new pixels have no HDR light beyond their own
839
858
  * @param color The color, or a function returning the color of each pixel (checked like any color: a result that
840
859
  * isn't a number throws a TypeError, a number that isn't a color a RangeError)
841
860
  */
@@ -845,7 +864,8 @@ export declare class Image {
845
864
  */
846
865
  clone(): Image;
847
866
  /**
848
- * Resizes the image by the given factor
867
+ * Resizes the image by the given factor: a side of 7 pixels scaled by 0.5 is 3 (fractions are dropped, a side
868
+ * doesn't go below 1 pixel)
849
869
  * @param factor The factor to resize the image with
850
870
  * @param options See {@link ResizeOptions}
851
871
  */
@@ -1132,7 +1152,8 @@ export declare class Image {
1132
1152
  fisheye(options?: FisheyeOptions): this;
1133
1153
  /**
1134
1154
  * Encodes the image into a PNG, with the smallest lossless color type (a palette for up to 256 colors,
1135
- * grayscale or RGB, alpha only if needed). Uses the runtime's native zlib when available (the files differ between runtimes, whose zlibs compress differently: about a quarter larger on Deno than on Node and Bun)
1155
+ * grayscale or RGB, alpha only if needed). Uses the runtime's native zlib when available, so the compressed
1156
+ * bytes can differ between runtimes and browsers; the pixels are the same
1136
1157
  * @param options See {@link PNGEncodeOptions}
1137
1158
  * @return The encoded data
1138
1159
  */
@@ -1142,23 +1163,27 @@ export declare class Image {
1142
1163
  * composited onto the `background` color (black by default) first, the image itself stays unchanged. An image whose
1143
1164
  * pixels are all gray is written as a grayscale JPEG (one component)
1144
1165
  * @param options See {@link JPEGEncodeOptions}
1166
+ * @throws `RangeError` if the image is wider or higher than 65535 pixels, or the comment or the Exif fields of the metadata are longer than 65533 bytes
1145
1167
  */
1146
1168
  encodeJPEG(options?: JPEGEncodeOptions): Promise<Uint8Array<ArrayBuffer>>;
1147
1169
  /**
1148
1170
  * Encodes the image as WebP: lossless (usually 20-30% smaller than PNG, but slower to encode), or lossy with a
1149
- * quality
1171
+ * quality below 100
1150
1172
  * @param options See {@link WebPEncodeOptions}
1173
+ * @throws `RangeError` if the image is wider or higher than 16383 pixels (lossy) or 16384 (lossless): the limits of the two codecs
1151
1174
  */
1152
1175
  encodeWebP(options?: WebPEncodeOptions): Promise<Uint8Array<ArrayBuffer>>;
1153
1176
  /**
1154
1177
  * Encodes the image as run length encoded TGA (32 bit, or 24 bit if the image is opaque)
1155
1178
  * @param options See {@link TGAEncodeOptions}
1179
+ * @throws `RangeError` if the image is wider or higher than 65535 pixels
1156
1180
  */
1157
1181
  encodeTGA(options?: TGAEncodeOptions): Promise<Uint8Array<ArrayBuffer>>;
1158
1182
  /**
1159
1183
  * Encodes the image as a GIF (one frame): at most 256 colors, transparency only on or off (pixels with an alpha below
1160
1184
  * 128 become transparent). For animations, see {@link Animation.encodeGIF}
1161
1185
  * @param options The palette size, the dithering and the metadata, see {@link GIFEncodeOptions}
1186
+ * @throws `RangeError` if the image is wider or higher than 65535 pixels
1162
1187
  */
1163
1188
  encodeGIF(options?: GIFEncodeOptions): Promise<Uint8Array<ArrayBuffer>>;
1164
1189
  /**
@@ -1173,6 +1198,7 @@ export declare class Image {
1173
1198
  /**
1174
1199
  * Encodes the image as AVIF (AV1, with an alpha image if the image has transparency)
1175
1200
  * @param options See {@link AVIFEncodeOptions}
1201
+ * @throws `RangeError` if the image is wider or higher than 65536 pixels
1176
1202
  */
1177
1203
  encodeAVIF(options?: AVIFEncodeOptions): Promise<Uint8Array<ArrayBuffer>>;
1178
1204
  /**
@@ -1234,11 +1260,13 @@ export declare class Image {
1234
1260
  * @param svg The SVG content
1235
1261
  * @param options See {@link RenderSVGOptions}
1236
1262
  * @return The rendered SVG graphic
1237
- * @throws {@link LimitError} if the output, the layers or the images exceed the limits of the options
1263
+ * @throws {@link LimitError} if the output, the layers or the images exceed the limits of the options, or the SVG
1264
+ * exceeds a fixed limit of the work it may take: more than 100000 elements drawn through `<use>` references and
1265
+ * markers, more than 10000 markers, masks, clip paths, patterns, markers and filter images nested so deeply that
1266
+ * they would multiply, entities that expand past the size limit, or filters past their budget
1238
1267
  * @throws `RangeError` if the requested size is below 1 pixel (or a scale isn't above 0)
1239
- * @throws `Error` if the SVG can't be parsed (it has no `<svg>` element), an image in it can't be drawn (it isn't given
1240
- * or can't be decoded), or masks, clip paths, patterns, markers and filter images are nested so deeply that they
1241
- * would multiply
1268
+ * @throws `Error` if the SVG can't be parsed (it has no `<svg>` element) or an image in it can't be drawn (it isn't
1269
+ * given or can't be decoded)
1242
1270
  */
1243
1271
  static renderSVG(svg: string | BinaryData, options?: RenderSVGOptions): Promise<Image>;
1244
1272
  /**