imagescript 3.16.1 → 3.17.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.js CHANGED
@@ -30,11 +30,26 @@ import { encode as utf8 } from "./util/utf8.js";
30
30
  import { resample, BOX, TRIANGLE, CUBIC } from "./util/resample.js";
31
31
  import { rotateBilinear } from "./util/rotate.js";
32
32
  import { gaussianBlur, boxBlurRGBA, premultiply, unpremultiply } from "./util/blur.js";
33
+ import { Colors, wrapHue, hue2rgb, rgbaToHSLA } from "./colors.js";
33
34
  import { isOptions, finite, color as checkColor, quality as checkQuality, effort as checkEffort, compressionLevel } from "./util/options.js";
34
35
  import { MAX_PIXELS, decoding } from "./util/limits.js";
35
36
  import { GainMap } from "./gainmap.js";
36
37
  import * as gain from "./util/gain.js";
37
38
  import * as gainMapCodec from "./codecs/gainmap.js";
39
+ export class ResizeModes {
40
+ static NEAREST_NEIGHBOR = 'RESIZE_NEAREST_NEIGHBOR';
41
+ static BILINEAR = 'RESIZE_BILINEAR';
42
+ static BICUBIC = 'RESIZE_BICUBIC';
43
+ static AREA = 'RESIZE_AREA';
44
+ static AUTO = -1;
45
+ constructor() { }
46
+ }
47
+ export class SVGScaleModes {
48
+ static SCALE = 1;
49
+ static WIDTH = 2;
50
+ static HEIGHT = 3;
51
+ constructor() { }
52
+ }
38
53
  function featureList(features) {
39
54
  if (typeof features !== 'object' || features === null)
40
55
  throw new TypeError('Features must be an object');
@@ -113,75 +128,28 @@ export class Image {
113
128
  yield [x, y, view.getUint32(offset, false)];
114
129
  }
115
130
  static rgbaToColor(r, g, b, a) {
116
- r = finite(r, 'red');
117
- g = finite(g, 'green');
118
- b = finite(b, 'blue');
119
- a = finite(a, 'alpha');
120
- return (channel(r) << 24 | channel(g) << 16 | channel(b) << 8 | channel(a)) >>> 0;
131
+ return Colors.fromRGBA(r, g, b, a);
121
132
  }
122
133
  static rgbToColor(r, g, b) {
123
- return Image.rgbaToColor(r, g, b, 0xff);
134
+ return Colors.fromRGB(r, g, b);
124
135
  }
125
136
  static hslaToColor(h, s, l, a) {
126
- h = wrapHue(finite(h, 'hue'));
127
- s = finite(s, 'saturation');
128
- l = finite(l, 'lightness');
129
- a = finite(a, 'alpha');
130
- s = Math.min(1, Math.max(0, s));
131
- l = Math.min(1, Math.max(0, l));
132
- a = Math.min(1, Math.max(0, a));
133
- let r, g, b;
134
- if (s === 0) {
135
- r = g = b = l;
136
- }
137
- else {
138
- const q = l < 0.5 ? l * (1 + s) : l + s - l * s;
139
- const p = 2 * l - q;
140
- r = hue2rgb(p, q, h + 1 / 3);
141
- g = hue2rgb(p, q, h);
142
- b = hue2rgb(p, q, h - 1 / 3);
143
- }
144
- return Image.rgbaToColor(Math.round(r * 255), Math.round(g * 255), Math.round(b * 255), Math.round(a * 255));
137
+ return Colors.fromHSLA(h, s, l, a);
145
138
  }
146
139
  static hslToColor(h, s, l) {
147
- return Image.hslaToColor(h, s, l, 1);
140
+ return Colors.fromHSL(h, s, l);
148
141
  }
149
142
  static rgbaToHSLA(r, g, b, a) {
150
- r = finite(r, 'red') / 255;
151
- g = finite(g, 'green') / 255;
152
- b = finite(b, 'blue') / 255;
153
- a = finite(a, 'alpha');
154
- const max = Math.max(r, g, b), min = Math.min(r, g, b);
155
- let h = 0, s;
156
- const l = (max + min) / 2;
157
- if (max === min) {
158
- h = s = 0;
159
- }
160
- else {
161
- const d = max - min;
162
- s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
163
- switch (max) {
164
- case r:
165
- h = (g - b) / d + (g < b ? 6 : 0);
166
- break;
167
- case g:
168
- h = (b - r) / d + 2;
169
- break;
170
- case b:
171
- h = (r - g) / d + 4;
172
- break;
173
- }
174
- h /= 6;
175
- }
176
- return [h, s, l, a / 255];
143
+ return rgbaToHSLA(r, g, b, a);
177
144
  }
178
145
  static colorToRGBA(color) {
179
- color = checkColor(color);
180
- return [(color >> 24) & 0xff, (color >> 16) & 0xff, (color >> 8) & 0xff, color & 0xff];
146
+ return Colors.toRGBA(color);
181
147
  }
182
148
  static colorToRGB(color) {
183
- const [r, g, b] = Image.colorToRGBA(color);
184
- return [r, g, b];
149
+ return Colors.toRGB(color);
150
+ }
151
+ static gradient(colors) {
152
+ return Colors.gradient(colors);
185
153
  }
186
154
  getPixelAt(x, y) {
187
155
  x = ~~finite(x, 'x');
@@ -252,28 +220,18 @@ export class Image {
252
220
  image.__gain_map__ = this.__gain_map__ ? this.__gain_map__.clone() : null;
253
221
  return image;
254
222
  }
255
- static get RESIZE_NEAREST_NEIGHBOR() {
256
- return 'RESIZE_NEAREST_NEIGHBOR';
257
- }
258
- static get RESIZE_BILINEAR() {
259
- return 'RESIZE_BILINEAR';
260
- }
261
- static get RESIZE_BICUBIC() {
262
- return 'RESIZE_BICUBIC';
263
- }
264
- static get RESIZE_AREA() {
265
- return 'RESIZE_AREA';
266
- }
267
- static get RESIZE_AUTO() {
268
- return -1;
269
- }
223
+ static RESIZE_NEAREST_NEIGHBOR = ResizeModes.NEAREST_NEIGHBOR;
224
+ static RESIZE_BILINEAR = ResizeModes.BILINEAR;
225
+ static RESIZE_BICUBIC = ResizeModes.BICUBIC;
226
+ static RESIZE_AREA = ResizeModes.AREA;
227
+ static RESIZE_AUTO = ResizeModes.AUTO;
270
228
  scale(factor, modeOrOptions) {
271
229
  const { mode, fast } = resizeOptions(modeOrOptions);
272
230
  const image = this.__scale__(factor, mode, fast);
273
231
  this.__gain_map__?.__rescale__(this.width, this.height, image.width, image.height);
274
232
  return this.__apply__(image);
275
233
  }
276
- __scale__(factor, mode = Image.RESIZE_BILINEAR, fast = true) {
234
+ __scale__(factor, mode = ResizeModes.BILINEAR, fast = true) {
277
235
  if (factor === 1)
278
236
  return this;
279
237
  return this.__resize__(this.width * factor, this.height * factor, mode, fast);
@@ -310,7 +268,7 @@ export class Image {
310
268
  throw new RangeError('Image has to be at least 1 pixel high');
311
269
  const scale = Math.max(width / this.width, height / this.height);
312
270
  const resizedWidth = Math.max(width, Math.round(this.width * scale)), resizedHeight = Math.max(height, Math.round(this.height * scale));
313
- if (options.mode !== Image.RESIZE_NEAREST_NEIGHBOR && width >= 1 && height >= 1) {
271
+ if (options.mode !== ResizeModes.NEAREST_NEIGHBOR && width >= 1 && height >= 1) {
314
272
  const region = { x: ~~((resizedWidth - width) / 2), y: ~~((resizedHeight - height) / 2), width, height };
315
273
  this.__rescaled__?.(resizedWidth, resizedHeight);
316
274
  const fx = resizedWidth / this.width, fy = resizedHeight / this.height;
@@ -320,12 +278,12 @@ export class Image {
320
278
  this.resize(resizedWidth, resizedHeight, options);
321
279
  return this.crop((this.width - width) / 2, (this.height - height) / 2, width, height);
322
280
  }
323
- __resize__(width, height, mode = Image.RESIZE_BILINEAR, fast = true, region) {
324
- if (width === Image.RESIZE_AUTO && height === Image.RESIZE_AUTO)
281
+ __resize__(width, height, mode = ResizeModes.BILINEAR, fast = true, region) {
282
+ if (width === ResizeModes.AUTO && height === ResizeModes.AUTO)
325
283
  throw new Error('RESIZE_AUTO can only be used for either width or height, not for both');
326
- else if (width === Image.RESIZE_AUTO)
284
+ else if (width === ResizeModes.AUTO)
327
285
  width = Math.max(1, Math.round(this.width / this.height * Math.floor(height)));
328
- else if (height === Image.RESIZE_AUTO)
286
+ else if (height === ResizeModes.AUTO)
329
287
  height = Math.max(1, Math.round(this.height / this.width * Math.floor(width)));
330
288
  width = Math.floor(width);
331
289
  height = Math.floor(height);
@@ -333,9 +291,9 @@ export class Image {
333
291
  throw new RangeError('Image has to be at least 1 pixel wide');
334
292
  if (height < 1)
335
293
  throw new RangeError('Image has to be at least 1 pixel high');
336
- if (mode === Image.RESIZE_NEAREST_NEIGHBOR)
294
+ if (mode === ResizeModes.NEAREST_NEIGHBOR)
337
295
  return this.__resize_nearest_neighbor__(width, height);
338
- const filter = mode === Image.RESIZE_BILINEAR ? TRIANGLE : mode === Image.RESIZE_BICUBIC ? CUBIC : mode === Image.RESIZE_AREA ? BOX : null;
296
+ const filter = mode === ResizeModes.BILINEAR ? TRIANGLE : mode === ResizeModes.BICUBIC ? CUBIC : mode === ResizeModes.AREA ? BOX : null;
339
297
  if (!filter)
340
298
  throw new Error('Invalid resize mode');
341
299
  const image = new Image(region?.width ?? width, region?.height ?? height);
@@ -540,6 +498,18 @@ export class Image {
540
498
  }
541
499
  return this;
542
500
  }
501
+ recolor(color) {
502
+ if (this.__gain_map__)
503
+ return this.__recolor__(image => image.recolor(color));
504
+ color = checkColor(color);
505
+ const r = color >>> 24, g = color >> 16 & 0xff, b = color >> 8 & 0xff;
506
+ for (let i = 0; i < this.bitmap.length; i += 4) {
507
+ this.bitmap[i] = r;
508
+ this.bitmap[i + 1] = g;
509
+ this.bitmap[i + 2] = b;
510
+ }
511
+ return this;
512
+ }
543
513
  opacity(opacity, absoluteOrOptions = false) {
544
514
  const absolute = isOptions(absoluteOrOptions) ? absoluteOrOptions.absolute ?? false : absoluteOrOptions;
545
515
  opacity = finite(opacity, 'opacity value');
@@ -924,8 +894,8 @@ export class Image {
924
894
  weight += a;
925
895
  }
926
896
  if (!weight)
927
- return Image.rgbaToColor(0, 0, 0, 0xff);
928
- return Image.rgbaToColor(Math.round(r / weight), Math.round(g / weight), Math.round(b / weight), 0xff);
897
+ return Colors.fromRGBA(0, 0, 0, 0xff);
898
+ return Colors.fromRGBA(Math.round(r / weight), Math.round(g / weight), Math.round(b / weight), 0xff);
929
899
  }
930
900
  dominantColor(ignoreBlackOrOptions = true, ignoreWhiteArgument = true, thresholdArgument = 0xf) {
931
901
  const { ignoreBlack = true, ignoreWhite = true, threshold: bwThreshold = 0xf } = typeof ignoreBlackOrOptions === 'object' && ignoreBlackOrOptions !== null
@@ -934,7 +904,7 @@ export class Image {
934
904
  const pixels = this.__bytes__();
935
905
  const color = quantize.dominantColor(pixels, (r, g, b) => !(ignoreBlack && Math.max(r, g, b) < threshold) && !(ignoreWhite && Math.min(r, g, b) > 255 - threshold))
936
906
  || quantize.dominantColor(pixels, () => true);
937
- return color ? Image.rgbToColor(...color) : Image.rgbaToColor(0, 0, 0, 0xff);
907
+ return color ? Colors.fromRGB(...color) : Colors.fromRGBA(0, 0, 0, 0xff);
938
908
  }
939
909
  rotate(angle, resizeOrOptions = true) {
940
910
  const { resize = true, fast = true } = typeof resizeOrOptions === 'object' && resizeOrOptions !== null ? resizeOrOptions : { resize: resizeOrOptions };
@@ -1086,51 +1056,6 @@ export class Image {
1086
1056
  this.__u32__ = image.__u32__;
1087
1057
  return this;
1088
1058
  }
1089
- static gradient(colors) {
1090
- const entries = Object.entries(colors).sort((a, b) => Number(a[0]) - Number(b[0]));
1091
- const positions = entries.map(e => finite(parseFloat(e[0]), 'gradient position'));
1092
- const values = entries.map(e => checkColor(e[1]));
1093
- if (positions.length === 0)
1094
- throw new RangeError('Invalid gradient point count');
1095
- else if (positions.length === 1) {
1096
- return () => values[0];
1097
- }
1098
- else if (positions.length === 2) {
1099
- const gradient = this.__gradient__(values[0], values[1]);
1100
- return position => {
1101
- if (position <= positions[0])
1102
- return values[0];
1103
- if (position >= positions[1])
1104
- return values[1];
1105
- return gradient((position - positions[0]) / (positions[1] - positions[0]));
1106
- };
1107
- }
1108
- const minDef = Math.min(...positions);
1109
- const maxDef = Math.max(...positions);
1110
- const gradients = [];
1111
- for (let i = 0; i < positions.length; i++) {
1112
- const minPos = positions[i - 1];
1113
- if (minPos === undefined)
1114
- continue;
1115
- const maxPos = positions[i];
1116
- let minVal = values[i - 1];
1117
- if (minVal === undefined)
1118
- minVal = values[i];
1119
- const maxVal = values[i];
1120
- const gradient = this.__gradient__(minVal, maxVal);
1121
- gradients.push({ min: minPos, max: maxPos, gradient });
1122
- }
1123
- return position => {
1124
- if (position <= minDef)
1125
- return gradients[0].gradient(0);
1126
- if (position >= maxDef)
1127
- return gradients[gradients.length - 1].gradient(1);
1128
- for (const gradient of gradients)
1129
- if (position >= gradient.min && position <= gradient.max)
1130
- return gradient.gradient((position - gradient.min) / (gradient.max - gradient.min));
1131
- throw new RangeError(`Invalid gradient position: ${position}`);
1132
- };
1133
- }
1134
1059
  roundCorners(radiusOrOptions) {
1135
1060
  let radius = (isOptions(radiusOrOptions) ? radiusOrOptions.radius : radiusOrOptions) ?? Math.min(this.width, this.height) / 4;
1136
1061
  radius = finite(radius, 'radius');
@@ -1148,23 +1073,6 @@ export class Image {
1148
1073
  }
1149
1074
  return this;
1150
1075
  }
1151
- static __gradient__(startColor, endColor) {
1152
- const sr = startColor >>> 24;
1153
- const sg = startColor >> 16 & 0xff;
1154
- const sb = startColor >> 8 & 0xff;
1155
- const sa = startColor & 0xff;
1156
- const er = (endColor >>> 24) - sr;
1157
- const eg = (endColor >> 16 & 0xff) - sg;
1158
- const eb = (endColor >> 8 & 0xff) - sb;
1159
- const ea = (endColor & 0xff) - sa;
1160
- return position => {
1161
- const r = sr + position * er;
1162
- const g = sg + position * eg;
1163
- const b = sb + position * eb;
1164
- const a = sa + position * ea;
1165
- return (((r & 0xff) << 24) | ((g & 0xff) << 16) | ((b & 0xff) << 8) | (a & 0xff)) >>> 0;
1166
- };
1167
- }
1168
1076
  fisheye(strengthOrOptions) {
1169
1077
  const strength = finite((isOptions(strengthOrOptions) ? strengthOrOptions.strength : strengthOrOptions) ?? 2, 'strength');
1170
1078
  const r = new Image(this.width, this.height);
@@ -1418,23 +1326,17 @@ export class Image {
1418
1326
  }
1419
1327
  return this.__apply__(out);
1420
1328
  }
1421
- static get SVG_MODE_SCALE() {
1422
- return 1;
1423
- }
1424
- static get SVG_MODE_WIDTH() {
1425
- return 2;
1426
- }
1427
- static get SVG_MODE_HEIGHT() {
1428
- return 3;
1429
- }
1430
- static async renderSVG(svg, sizeOrOptions = 1, modeArgument = Image.SVG_MODE_SCALE) {
1431
- const { size = 1, mode = Image.SVG_MODE_SCALE, fonts = [] } = isOptions(sizeOrOptions) ? sizeOrOptions : { size: sizeOrOptions, mode: modeArgument };
1329
+ static SVG_MODE_SCALE = SVGScaleModes.SCALE;
1330
+ static SVG_MODE_WIDTH = SVGScaleModes.WIDTH;
1331
+ static SVG_MODE_HEIGHT = SVGScaleModes.HEIGHT;
1332
+ static async renderSVG(svg, sizeOrOptions = 1, modeArgument = SVGScaleModes.SCALE) {
1333
+ const { size = 1, mode = SVGScaleModes.SCALE, fonts = [] } = isOptions(sizeOrOptions) ? sizeOrOptions : { size: sizeOrOptions, mode: modeArgument };
1432
1334
  if (![1, 2, 3].includes(mode))
1433
1335
  throw new Error('Invalid SVG scaling mode');
1434
1336
  finite(size, 'SVG size');
1435
- if (mode === Image.SVG_MODE_SCALE && size <= 0)
1337
+ if (mode === SVGScaleModes.SCALE && size <= 0)
1436
1338
  throw new RangeError('SVG scale must be > 0');
1437
- if (mode !== Image.SVG_MODE_SCALE && size < 1)
1339
+ if (mode !== SVGScaleModes.SCALE && size < 1)
1438
1340
  throw new RangeError('SVG size must be >= 1');
1439
1341
  const source = typeof svg === 'string' ? svg : view(svg);
1440
1342
  const images = new Map();
@@ -1471,7 +1373,7 @@ export class Image {
1471
1373
  const fonts = Array.isArray(font) ? font : [font];
1472
1374
  if (!fonts.length)
1473
1375
  throw new RangeError('No font given');
1474
- const [r, g, b, a] = Image.colorToRGBA(color);
1376
+ const [r, g, b, a] = Colors.toRGBA(color);
1475
1377
  const rendered = await textRenderer.render(fonts.map(view), size, text, layout, hinting, (r << 24 | g << 16 | b << 8 | 255) >>> 0, featureList(features), axes);
1476
1378
  const pad = Math.ceil(strokeWidth);
1477
1379
  const width = rendered.width + 2 * pad, height = rendered.height + 2 * pad;
@@ -1503,7 +1405,7 @@ export class Image {
1503
1405
  shape[i] = Math.max(shape[i], colored[i * 4 + 3]);
1504
1406
  }
1505
1407
  const stroke = strokeWidth ? outline(shape, width, height, strokeWidth) : null;
1506
- const [sr, sg, sb, sa] = Image.colorToRGBA(strokeColor);
1408
+ const [sr, sg, sb, sa] = Colors.toRGBA(strokeColor);
1507
1409
  for (let i = 0; i < coverage.length; i++) {
1508
1410
  let af = coverage[i] / 255 * a / 255, pr = r * af, pg = g * af, pb = b * af;
1509
1411
  if (colored && colored[i * 4 + 3]) {
@@ -1547,29 +1449,9 @@ function circleCoverage(px, py, cx, cy, radius) {
1547
1449
  return 0;
1548
1450
  return pixelCoverage(px, py, (x, y) => (x - cx) ** 2 + (y - cy) ** 2 <= radius * radius);
1549
1451
  }
1550
- function channel(value) {
1551
- return value > 255 ? 255 : value > 0 ? Math.round(value) : 0;
1552
- }
1553
- function wrapHue(h) {
1554
- const t = h - Math.trunc(h) + 1;
1555
- return t < 1 ? t : t < 2 ? t - 1 : 0;
1556
- }
1557
- function hue2rgb(p, q, t) {
1558
- if (t < 0)
1559
- t += 1;
1560
- if (t > 1)
1561
- t -= 1;
1562
- if (t < 1 / 6)
1563
- return p + (q - p) * 6 * t;
1564
- if (t < 1 / 2)
1565
- return q;
1566
- if (t < 2 / 3)
1567
- return p + (q - p) * (2 / 3 - t) * 6;
1568
- return p;
1569
- }
1570
1452
  function resizeOptions(modeOrOptions) {
1571
1453
  const options = isOptions(modeOrOptions) ? modeOrOptions : { mode: modeOrOptions };
1572
- return { mode: options.mode ?? Image.RESIZE_BILINEAR, fast: options.fast ?? true };
1454
+ return { mode: options.mode ?? ResizeModes.BILINEAR, fast: options.fast ?? true };
1573
1455
  }
1574
1456
  const hardLight = (b, s) => s <= 0.5 ? b * 2 * s : 1 - (1 - b) * (1 - (2 * s - 1));
1575
1457
  const BLEND_MODES = {
package/dist/index.d.ts CHANGED
@@ -2,9 +2,10 @@ import { BinaryData } from './util/mem.js';
2
2
  import { Image, DecodeOptions } from './image.js';
3
3
  import { GIF } from './gif.js';
4
4
  import { Animation, AnimationDecodeOptions } from './animation.js';
5
- export { Image } from './image.js';
5
+ export { Image, ResizeModes, SVGScaleModes } from './image.js';
6
+ export { Colors } from './colors.js';
6
7
  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
- export { Frame } from './frame.js';
8
+ export { Frame, FrameDisposalModes } from './frame.js';
8
9
  export type { FrameDisposalModeName, FrameDisposalModeId, FrameOptions } from './frame.js';
9
10
  export { GainMap } from './gainmap.js';
10
11
  export type { GainMapOptions } from './gainmap.js';
package/dist/index.js CHANGED
@@ -5,8 +5,9 @@ import { isOptions } from "./util/options.js";
5
5
  import * as webp from "./codecs/webp.js";
6
6
  import { view } from "./util/mem.js";
7
7
  import { ImageType } from "./image-type.js";
8
- export { Image } from "./image.js";
9
- export { Frame } from "./frame.js";
8
+ export { Image, ResizeModes, SVGScaleModes } from "./image.js";
9
+ export { Colors } from "./colors.js";
10
+ export { Frame, FrameDisposalModes } from "./frame.js";
10
11
  export { GainMap } from "./gainmap.js";
11
12
  export { GIF } from "./gif.js";
12
13
  export { Animation } from "./animation.js";
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const VERSION = '3.16.1';
1
+ export const VERSION = '3.17.0';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "imagescript",
3
- "version": "3.16.1",
3
+ "version": "3.17.0",
4
4
  "description": "Pure TypeScript image processing that runs everywhere",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",