imagescript 3.18.0 → 3.19.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/frame.js CHANGED
@@ -58,6 +58,13 @@ export class Frame extends Image {
58
58
  clone() {
59
59
  return Frame.from(this, { duration: this.duration, xOffset: this.xOffset, yOffset: this.yOffset, disposalMode: this.disposalMode });
60
60
  }
61
+ __room__() {
62
+ return { left: this.xOffset, top: this.yOffset };
63
+ }
64
+ __grown__(left, top) {
65
+ this.xOffset -= left;
66
+ this.yOffset -= top;
67
+ }
61
68
  trim(colorOrOptions, optionsArgument = {}) {
62
69
  const { color, ...options } = isOptions(colorOrOptions) ? colorOrOptions : { ...optionsArgument, color: colorOrOptions ?? optionsArgument.color };
63
70
  const bounds = this.__trim_bounds__(color, options);
package/dist/image.d.ts CHANGED
@@ -113,6 +113,63 @@ export interface SharpenOptions {
113
113
  /** The radius of the blur the image is compared with (larger values sharpen coarser details). Default: `1` */
114
114
  radius?: number;
115
115
  }
116
+ /** Options of {@link Image.dropShadow} */
117
+ export interface ShadowOptions {
118
+ /** How far to move the shadow to the right (negative values: to the left), in pixels. Default: `0` */
119
+ x?: number;
120
+ /** How far to move the shadow down (negative values: up), in pixels. Default: `0` */
121
+ y?: number;
122
+ /** How soft the shadow's edge is: the standard deviation of a gaussian blur in pixels, like {@link Image.blur}. Default: `0` */
123
+ blur?: number;
124
+ /** How far to grow the shadow's shape before blurring it, in pixels. Default: `0` */
125
+ spread?: number;
126
+ /** The shadow's color, its alpha is the shadow's opacity. Default: `0x00000080` (50% black) */
127
+ color?: Color;
128
+ /**
129
+ * Whether to make the image larger so the shadow isn't cut off. Without it, the image keeps its size and the part
130
+ * of the shadow outside is lost. Default: `true`
131
+ */
132
+ expand?: boolean;
133
+ }
134
+ /** Options of {@link Image.outline} */
135
+ export interface OutlineOptions {
136
+ /** The outline's width in pixels. Default: `1` */
137
+ width?: number;
138
+ /** The outline's color. Default: `0x000000ff` */
139
+ color?: Color;
140
+ /**
141
+ * Whether to make the image larger so the outline isn't cut off. Without it, the image keeps its size and the
142
+ * outline is cut where the image ends. Default: `true`
143
+ */
144
+ expand?: boolean;
145
+ }
146
+ /** The colors of a gradient, see {@link Image.linearGradient} */
147
+ export interface GradientOptions {
148
+ /**
149
+ * The colors along the gradient: their positions (0..1) and colors, like {@link Colors.gradient} takes them
150
+ * (`{ 0: 0xff0000ff, 0.5: 0xffff00ff, 1: 0x00ff00ff }`), or just the colors, evenly spaced
151
+ * (`[0xff0000ff, 0x00ff00ff]`)
152
+ */
153
+ stops: Record<number, Color> | Color[];
154
+ }
155
+ /** Options of {@link Image.linearGradient} */
156
+ export interface LinearGradientOptions extends GradientOptions {
157
+ /**
158
+ * The direction of the gradient in degrees, like CSS' `linear-gradient()`: `0` goes up, `90` to the right, `180`
159
+ * down, `270` to the left. The first stop is at the start of the line through the center that touches the
160
+ * corners. Default: `180`
161
+ */
162
+ angle?: number;
163
+ }
164
+ /** Options of {@link Image.radialGradient} */
165
+ export interface RadialGradientOptions extends GradientOptions {
166
+ /** The x position of the center in pixels. Default: the middle of the image */
167
+ x?: number;
168
+ /** The y position of the center in pixels. Default: the middle of the image */
169
+ y?: number;
170
+ /** The radius of the last stop in pixels. Default: the distance to the farthest corner */
171
+ radius?: number;
172
+ }
116
173
  /** Options of {@link Image.encode} */
117
174
  export interface PNGEncodeOptions {
118
175
  /** The compression level (0-9), higher is smaller but slower. Default: `4` */
@@ -558,6 +615,22 @@ export declare class Image {
558
615
  * @group Deprecated
559
616
  */
560
617
  static colorToRGB(color: Color): [r: number, g: number, b: number];
618
+ /**
619
+ * Creates an image with a linear gradient, like CSS' `linear-gradient()`
620
+ * @param width The image's width
621
+ * @param height The image's height
622
+ * @param options The colors and the direction, see {@link LinearGradientOptions}
623
+ * @returns The new image
624
+ */
625
+ static linearGradient(width: number, height: number, options: LinearGradientOptions): Image;
626
+ /**
627
+ * Creates an image with a radial (circular) gradient, like CSS' `radial-gradient(circle)`
628
+ * @param width The image's width
629
+ * @param height The image's height
630
+ * @param options The colors and the circle, see {@link RadialGradientOptions}
631
+ * @returns The new image
632
+ */
633
+ static radialGradient(width: number, height: number, options: RadialGradientOptions): Image;
561
634
  /**
562
635
  * Creates a multi-point gradient generator
563
636
  * @param colors The gradient points to use (e.g. `{0: 0xff0000ff, 1: 0x00ff00ff}`)
@@ -788,6 +861,13 @@ export declare class Image {
788
861
  * @param color The color to use, its alpha is ignored
789
862
  */
790
863
  recolor(color: Color): this;
864
+ /**
865
+ * Sets the RGB of every pixel to the RGB of the same pixel of another image, keeping each pixel's own alpha. With a
866
+ * gradient this fills a shape with it, e.g. gradient text:
867
+ * `text.recolor(Image.linearGradient(text.width, text.height, { stops: [0x22d3eeff, 0xfb923cff], angle: 90 }))`
868
+ * @param colors The image to take the colors from, it has to have the same size. Its alpha is ignored
869
+ */
870
+ recolor(colors: Image): this;
791
871
  /**
792
872
  * Scales the opacity of the image
793
873
  * @param opacity The factor to multiply the opacity with (0..1 lowers it, >1 raises it, results are clamped)
@@ -904,6 +984,21 @@ export declare class Image {
904
984
  * @param radius The blur radius: the gaussian's standard deviation in pixels, like CSS' blur()
905
985
  */
906
986
  blur(radius: number): this;
987
+ /**
988
+ * Adds a shadow behind the image, shaped by its alpha: a drop shadow with an offset, or a glow without one
989
+ * (`image.dropShadow({ blur: 8, color: 0xff2fb9ff })`, with `spread` to make it stronger). The image grows to fit
990
+ * the shadow, unless `expand` is `false`. Frames stay where they are (their offsets move with the growth), they only
991
+ * grow up and left as far as their offset allows
992
+ * @param options See {@link ShadowOptions}
993
+ */
994
+ dropShadow(options?: ShadowOptions): this;
995
+ /**
996
+ * Draws an outline around the image's shape (its alpha): the sticker effect for cut-out images, or an outline for
997
+ * text. The image grows to fit it, unless `expand` is `false`. Frames stay where they are (their offsets move with
998
+ * the growth), they only grow up and left as far as their offset allows
999
+ * @param options See {@link OutlineOptions}
1000
+ */
1001
+ outline(options?: OutlineOptions): this;
907
1002
  /**
908
1003
  * Sharpens the image with an unsharp mask: the difference to a blurred copy is amplified
909
1004
  * @param options See {@link SharpenOptions}
package/dist/image.js CHANGED
@@ -154,6 +154,34 @@ export class Image {
154
154
  static colorToRGB(color) {
155
155
  return Colors.toRGB(color);
156
156
  }
157
+ static linearGradient(width, height, options) {
158
+ const { stops, angle = 180 } = options;
159
+ const color = gradientFunction(stops);
160
+ finite(angle, 'angle');
161
+ const image = new Image(width, height);
162
+ const radians = angle * Math.PI / 180;
163
+ const dx = Math.sin(radians), dy = -Math.cos(radians);
164
+ const length = Math.abs(width * dx) + Math.abs(height * dy);
165
+ const cx = width / 2, cy = height / 2, bitmap = new DataView(image.bitmap.buffer, image.bitmap.byteOffset, image.bitmap.byteLength);
166
+ for (let y = 0, i = 0; y < height; y++)
167
+ for (let x = 0; x < width; x++, i += 4)
168
+ bitmap.setUint32(i, color(((x + 0.5 - cx) * dx + (y + 0.5 - cy) * dy) / length + 0.5), false);
169
+ return image;
170
+ }
171
+ static radialGradient(width, height, options) {
172
+ const { stops, x: cx = width / 2, y: cy = height / 2, radius = Math.hypot(Math.max(cx, width - cx), Math.max(cy, height - cy)) } = options;
173
+ const color = gradientFunction(stops);
174
+ finite(cx, 'x');
175
+ finite(cy, 'y');
176
+ if (!(finite(radius, 'radius') > 0))
177
+ throw new RangeError('Invalid radius');
178
+ const image = new Image(width, height);
179
+ const bitmap = new DataView(image.bitmap.buffer, image.bitmap.byteOffset, image.bitmap.byteLength);
180
+ for (let y = 0, i = 0; y < height; y++)
181
+ for (let x = 0; x < width; x++, i += 4)
182
+ bitmap.setUint32(i, color(Math.hypot(x + 0.5 - cx, y + 0.5 - cy) / radius), false);
183
+ return image;
184
+ }
157
185
  static gradient(colors) {
158
186
  return Colors.gradient(colors);
159
187
  }
@@ -508,10 +536,24 @@ export class Image {
508
536
  }
509
537
  return this;
510
538
  }
511
- recolor(color) {
539
+ recolor(colorOrImage) {
540
+ if (colorOrImage instanceof Image) {
541
+ const colors = colorOrImage;
542
+ if (colors.width !== this.width || colors.height !== this.height)
543
+ throw new RangeError(`The image to take the colors from has to be ${this.width}x${this.height}, not ${colors.width}x${colors.height}`);
544
+ if (this.__gain_map__)
545
+ return this.__recolor__(image => image.recolor(image.width === colors.width && image.height === colors.height ? colors : colors.clone().resize(image.width, image.height)));
546
+ const from = colors.bitmap, to = this.bitmap;
547
+ for (let i = 0; i < to.length; i += 4) {
548
+ to[i] = from[i];
549
+ to[i + 1] = from[i + 1];
550
+ to[i + 2] = from[i + 2];
551
+ }
552
+ return this;
553
+ }
554
+ const color = checkColor(colorOrImage);
512
555
  if (this.__gain_map__)
513
556
  return this.__recolor__(image => image.recolor(color));
514
- color = checkColor(color);
515
557
  const r = color >>> 24, g = color >> 16 & 0xff, b = color >> 8 & 0xff;
516
558
  for (let i = 0; i < this.bitmap.length; i += 4) {
517
559
  this.bitmap[i] = r;
@@ -793,6 +835,103 @@ export class Image {
793
835
  unpremultiply(pixels, this.bitmap);
794
836
  return this;
795
837
  }
838
+ dropShadow(options = {}) {
839
+ const { x = 0, y = 0, blur = 0, spread = 0, color = 0x00000080, expand = true } = options;
840
+ finite(x, 'x');
841
+ finite(y, 'y');
842
+ if (finite(blur, 'blur') < 0)
843
+ throw new RangeError('Invalid blur radius');
844
+ if (finite(spread, 'spread') < 0)
845
+ throw new RangeError('Invalid spread');
846
+ const [r, g, b, a] = Colors.toRGBA(checkColor(color));
847
+ const reach = spread + 3 * blur, margin = Math.ceil(reach) + 1;
848
+ const layer = this.__shape_layer__(margin);
849
+ const width = layer.width;
850
+ const coverage = new Uint8Array(width * layer.height);
851
+ for (let i = 0; i < coverage.length; i++)
852
+ coverage[i] = layer.bitmap[i * 4 + 3];
853
+ const grown = spread > 0 ? outline(coverage, width, layer.height, spread) : null;
854
+ const bitmap = layer.bitmap;
855
+ for (let i = 0; i < coverage.length; i++) {
856
+ bitmap[i * 4] = r;
857
+ bitmap[i * 4 + 1] = g;
858
+ bitmap[i * 4 + 2] = b;
859
+ bitmap[i * 4 + 3] = (grown ? grown[i] * 255 : coverage[i]) * a / 255;
860
+ }
861
+ if (blur > 0) {
862
+ layer.blur(blur);
863
+ for (let i = 0; i < coverage.length; i++) {
864
+ if (!bitmap[i * 4 + 3])
865
+ continue;
866
+ bitmap[i * 4] = r;
867
+ bitmap[i * 4 + 1] = g;
868
+ bitmap[i * 4 + 2] = b;
869
+ }
870
+ }
871
+ const room = this.__room__();
872
+ return this.__under__(layer, x - margin, y - margin, expand ? {
873
+ left: Math.min(room.left, Math.max(0, Math.ceil(reach - x))),
874
+ top: Math.min(room.top, Math.max(0, Math.ceil(reach - y))),
875
+ right: Math.max(0, Math.ceil(reach + x)),
876
+ bottom: Math.max(0, Math.ceil(reach + y))
877
+ } : { left: 0, top: 0, right: 0, bottom: 0 });
878
+ }
879
+ outline(options = {}) {
880
+ const { width = 1, color = 0x000000ff, expand = true } = options;
881
+ if (finite(width, 'outline width') < 0)
882
+ throw new RangeError('Invalid outline width');
883
+ const [r, g, b, a] = Colors.toRGBA(checkColor(color));
884
+ if (width === 0)
885
+ return this;
886
+ const margin = Math.ceil(width) + 1;
887
+ const layer = this.__shape_layer__(margin);
888
+ const coverage = new Uint8Array(layer.width * layer.height);
889
+ for (let i = 0; i < coverage.length; i++)
890
+ coverage[i] = layer.bitmap[i * 4 + 3];
891
+ const stroke = outline(coverage, layer.width, layer.height, width);
892
+ const bitmap = layer.bitmap;
893
+ for (let i = 0; i < coverage.length; i++) {
894
+ bitmap[i * 4] = r;
895
+ bitmap[i * 4 + 1] = g;
896
+ bitmap[i * 4 + 2] = b;
897
+ bitmap[i * 4 + 3] = stroke[i] * (1 - coverage[i] / 255) * a;
898
+ }
899
+ const room = this.__room__(), grow = Math.ceil(width);
900
+ return this.__under__(layer, -margin, -margin, expand
901
+ ? { left: Math.min(room.left, grow), top: Math.min(room.top, grow), right: grow, bottom: grow }
902
+ : { left: 0, top: 0, right: 0, bottom: 0 });
903
+ }
904
+ __shape_layer__(margin) {
905
+ const layer = new Image(this.width + 2 * margin, this.height + 2 * margin);
906
+ for (let y = 0; y < this.height; y++)
907
+ for (let x = 0; x < this.width; x++)
908
+ layer.bitmap[((y + margin) * layer.width + x + margin) * 4 + 3] = this.bitmap[(y * this.width + x) * 4 + 3];
909
+ return layer;
910
+ }
911
+ __under__(layer, layerX, layerY, grow) {
912
+ const { left, top, right, bottom } = grow;
913
+ const width = this.width + left + right, height = this.height + top + bottom;
914
+ const result = new Image(width, height);
915
+ result.composite(layer, { x: layerX + left, y: layerY + top });
916
+ const map = this.__gain_map__;
917
+ this.__gain_map__ = null;
918
+ try {
919
+ result.composite(this, { x: left, y: top });
920
+ }
921
+ finally {
922
+ this.__gain_map__ = map;
923
+ }
924
+ map?.__transform__(this.width, this.height, width, height, (X, Y) => [X - left, Y - top]);
925
+ this.__apply__(result);
926
+ if (left || top)
927
+ this.__grown__(left, top);
928
+ return this;
929
+ }
930
+ __room__() {
931
+ return { left: Infinity, top: Infinity };
932
+ }
933
+ __grown__(_left, _top) {
934
+ }
796
935
  sharpen(amountOrOptions = 1, radiusArgument = 1) {
797
936
  const { amount = 1, radius = 1 } = isOptions(amountOrOptions) ? amountOrOptions : { amount: amountOrOptions, radius: radiusArgument };
798
937
  if (!Number.isFinite(amount) || amount < 0)
@@ -1512,6 +1651,14 @@ const BLEND_MODES = {
1512
1651
  'exclusion': (b, s) => b + s - 2 * b * s,
1513
1652
  'add': (b, s) => Math.min(1, b + s)
1514
1653
  };
1654
+ function gradientFunction(stops) {
1655
+ if (!Array.isArray(stops))
1656
+ return Colors.gradient(stops);
1657
+ if (!stops.length)
1658
+ throw new RangeError('Invalid gradient point count');
1659
+ const colors = stops.length === 1 ? [stops[0], stops[0]] : stops;
1660
+ return Colors.gradient(Object.fromEntries(colors.map((color, i) => [i / (colors.length - 1), color])));
1661
+ }
1515
1662
  const OUTLINE_WINDOW = 1;
1516
1663
  function outline(coverage, width, height, radius) {
1517
1664
  const count = width * height;
package/dist/index.d.ts CHANGED
@@ -4,7 +4,7 @@ import { GIF } from './gif.js';
4
4
  import { Animation, AnimationDecodeOptions } from './animation.js';
5
5
  export { Image, ResizeModes, SVGScaleModes } from './image.js';
6
6
  export { Colors } from './colors.js';
7
- export type { Color, ColorFunction, ResizeMode, SVGScaleMode, PNGMetadata, TextStyle, DecodeOptions, CompositeOptions, BlendMode, CropCircleOptions, DominantColorOptions, RotateOptions, ColorToAlphaOptions, ResizeOptions, ResizeToOptions, AdjustOptions, SharpenOptions, PNGEncodeOptions, JPEGEncodeOptions, WebPEncodeOptions, AVIFEncodeOptions, JXLEncodeOptions, EncodeOptions, SVGOptions, TextOptions, RenderTextOptions, RoundCornersOptions, FisheyeOptions, TrimOptions } from './image.js';
7
+ export type { Color, ColorFunction, ResizeMode, SVGScaleMode, PNGMetadata, TextStyle, DecodeOptions, CompositeOptions, BlendMode, CropCircleOptions, DominantColorOptions, RotateOptions, ColorToAlphaOptions, ResizeOptions, ResizeToOptions, AdjustOptions, SharpenOptions, PNGEncodeOptions, JPEGEncodeOptions, WebPEncodeOptions, AVIFEncodeOptions, JXLEncodeOptions, EncodeOptions, SVGOptions, TextOptions, RenderTextOptions, ShadowOptions, OutlineOptions, GradientOptions, LinearGradientOptions, RadialGradientOptions, RoundCornersOptions, FisheyeOptions, TrimOptions } from './image.js';
8
8
  export { Frame, FrameDisposalModes } from './frame.js';
9
9
  export type { FrameDisposalModeName, FrameDisposalModeId, FrameOptions } from './frame.js';
10
10
  export { GainMap } from './gainmap.js';
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const VERSION = '3.18.0';
1
+ export const VERSION = '3.19.0';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "imagescript",
3
- "version": "3.18.0",
3
+ "version": "3.19.0",
4
4
  "description": "Pure TypeScript image processing that runs everywhere",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",