imagescript 3.20.3 → 4.0.0-rc.2

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.
Files changed (55) hide show
  1. package/LICENSE +16 -12
  2. package/README.md +21 -12
  3. package/THIRD_PARTY_NOTICES.md +1 -1
  4. package/dist/animation-encoder.d.ts +1 -34
  5. package/dist/animation-encoder.js +35 -34
  6. package/dist/animation.d.ts +108 -122
  7. package/dist/animation.js +285 -121
  8. package/dist/codecs/av1/decode.js +1 -1
  9. package/dist/codecs/av1/encode.js +1 -1
  10. package/dist/codecs/gif.js +11 -9
  11. package/dist/codecs/hdr.d.ts +1 -1
  12. package/dist/codecs/jpeg.js +13 -13
  13. package/dist/codecs/jxl/headers.js +3 -3
  14. package/dist/codecs/jxl/patches-enc.js +1 -1
  15. package/dist/codecs/jxl.js +5 -5
  16. package/dist/codecs/netpbm.js +7 -7
  17. package/dist/codecs/png.js +15 -15
  18. package/dist/codecs/quantize.js +34 -8
  19. package/dist/codecs/tiff.js +1 -1
  20. package/dist/codecs/webp.js +3 -3
  21. package/dist/codecs/zlib.js +1 -1
  22. package/dist/colors.d.ts +8 -7
  23. package/dist/colors.js +23 -3
  24. package/dist/frame.d.ts +94 -100
  25. package/dist/frame.js +89 -65
  26. package/dist/gainmap.d.ts +88 -46
  27. package/dist/gainmap.js +75 -51
  28. package/dist/image-type.d.ts +6 -1
  29. package/dist/image-type.js +4 -1
  30. package/dist/image.d.ts +317 -738
  31. package/dist/image.js +308 -438
  32. package/dist/index.d.ts +19 -25
  33. package/dist/index.js +15 -14
  34. package/dist/metadata.d.ts +4 -3
  35. package/dist/metadata.js +9 -7
  36. package/dist/render/colr.js +1 -1
  37. package/dist/render/font.js +1 -1
  38. package/dist/render/layout.js +1 -1
  39. package/dist/render/segment.js +1 -1
  40. package/dist/render/shapers/hangul.js +1 -1
  41. package/dist/render/svg.js +68 -20
  42. package/dist/render/text.js +7 -9
  43. package/dist/text-layout.d.ts +5 -35
  44. package/dist/text-layout.js +11 -12
  45. package/dist/util/gain.js +12 -12
  46. package/dist/util/mem.d.ts +1 -1
  47. package/dist/util/optimize.js +3 -1
  48. package/dist/util/options.js +46 -7
  49. package/package.json +15 -10
  50. package/LICENSE.AGPL-3.0 +0 -619
  51. package/LICENSE.MIT +0 -19
  52. package/dist/gif.d.ts +0 -36
  53. package/dist/gif.js +0 -22
  54. package/dist/util/rotate.js +0 -43
  55. package/dist/version.js +0 -1
package/dist/util/gain.js CHANGED
@@ -3,7 +3,7 @@ export const LINEAR = new Float32Array(256).map((_, v) => (v /= 255) <= 0.04045
3
3
  export const encodeSRGB = (v) => 255 * (v <= 0.0031308 ? 12.92 * Math.max(0, v) : 1.055 * Math.min(1, v) ** (1 / 2.4) - 0.055);
4
4
  const LUMA = [0.2126, 0.7152, 0.0722];
5
5
  const gainTables = (map) => [0, 1, 2].map(c => {
6
- const min = map.gainMapMin[c], max = map.gainMapMax[c], gamma = map.gamma[c];
6
+ const min = map.__min__[c], max = map.__max__[c], gamma = map.__gamma__[c];
7
7
  return new Float32Array(256).map((_, v) => min + (max - min) * (v / 255) ** (1 / gamma));
8
8
  });
9
9
  export function decodeGains(map) {
@@ -18,7 +18,7 @@ export function decodeGains(map) {
18
18
  return gains;
19
19
  }
20
20
  export function encodeGains(map, gains, width, height, single) {
21
- const min = map.gainMapMin.slice(), max = map.gainMapMax.slice();
21
+ const min = map.__min__.slice(), max = map.__max__.slice();
22
22
  for (let g = 0; g < gains.length; g += 3)
23
23
  for (let c = 0; c < 3; c++) {
24
24
  const v = gains[g + c];
@@ -31,17 +31,17 @@ export function encodeGains(map, gains, width, height, single) {
31
31
  const lo = Math.min(...min), hi = Math.max(...max);
32
32
  min.fill(lo);
33
33
  max.fill(hi);
34
- const gamma = map.gamma[0];
35
- map.gamma = [gamma, gamma, gamma];
34
+ const gamma = map.__gamma__[0];
35
+ map.__gamma__ = [gamma, gamma, gamma];
36
36
  }
37
- map.gainMapMin = min;
38
- map.gainMapMax = max;
37
+ map.__min__ = min;
38
+ map.__max__ = max;
39
39
  const image = map.image.width === width && map.image.height === height ? map.image : new Image(width, height);
40
40
  const out = image.bitmap;
41
41
  for (let i = 0, g = 0; i < out.length; i += 4, g += 3) {
42
42
  for (let c = 0; c < 3; c++) {
43
43
  const t = max[c] > min[c] ? (gains[g + c] - min[c]) / (max[c] - min[c]) : 0;
44
- out[i + c] = 255 * Math.min(1, Math.max(0, t)) ** map.gamma[c];
44
+ out[i + c] = 255 * Math.min(1, Math.max(0, t)) ** map.__gamma__[c];
45
45
  }
46
46
  out[i + 3] = 255;
47
47
  }
@@ -61,8 +61,8 @@ export function neutralMap(like, likeWidth, width, height) {
61
61
  bitmap[i + 3] = 255;
62
62
  }
63
63
  map.image = image;
64
- map.gainMapMin = map.gainMapMin.map(v => Math.min(v, 0));
65
- map.gainMapMax = map.gainMapMax.map(v => Math.max(v, 0));
64
+ map.__min__ = map.__min__.map(v => Math.min(v, 0));
65
+ map.__max__ = map.__max__.map(v => Math.max(v, 0));
66
66
  return map;
67
67
  }
68
68
  const sample = (gains, width, height, x, y, out) => {
@@ -80,7 +80,7 @@ export function redraw(image, map, left, top, right, bottom, light, single = map
80
80
  const mapWidth = map.width, mapHeight = map.height;
81
81
  const sx = mapWidth / width, sy = mapHeight / height;
82
82
  const oS = map.offsetSDR, oH = map.offsetHDR;
83
- const lo = Math.min(0, ...map.gainMapMin), hi = Math.max(0, ...map.gainMapMax, extraMax);
83
+ const lo = Math.min(0, ...map.__min__), hi = Math.max(0, ...map.__max__, extraMax);
84
84
  const S = [0, 0, 0], H = [0, 0, 0], sumS = [0, 0, 0], sumH = [0, 0, 0];
85
85
  const i0 = Math.max(0, Math.floor(left * sx)), i1 = Math.min(mapWidth, Math.ceil(right * sx));
86
86
  const j0 = Math.max(0, Math.floor(top * sy)), j1 = Math.min(mapHeight, Math.ceil(bottom * sy));
@@ -125,7 +125,7 @@ export function redraw(image, map, left, top, right, bottom, light, single = map
125
125
  next[g + c] = ratio(sumH[c] / n + oH[c], sumS[c] / n + oS[c], lo, hi);
126
126
  }
127
127
  }
128
- const { gainMapMin: min, gainMapMax: max, gamma } = map;
128
+ const { __min__: min, __max__: max, gamma } = map;
129
129
  let fits = !single || (min[0] === min[1] && min[1] === min[2] && max[0] === max[1] && max[1] === max[2] && gamma[0] === gamma[1] && gamma[1] === gamma[2]);
130
130
  for (let g = 0; fits && g < next.length; g += 3)
131
131
  for (let c = 0; c < 3; c++)
@@ -217,7 +217,7 @@ export function recolor(map, before, apply, exact) {
217
217
  let changedPeak = 1;
218
218
  for (let g = 0; g < changed.length; g++)
219
219
  changedPeak = Math.max(changedPeak, changed[g]);
220
- const lo = Math.min(0, ...map.gainMapMin), hi = Math.max(Math.log2(Math.max(peak, changedPeak)), ...map.gainMapMax);
220
+ const lo = Math.min(0, ...map.__min__), hi = Math.max(Math.log2(Math.max(peak, changedPeak)), ...map.__max__);
221
221
  const single = map.channels === 1;
222
222
  for (let g = 0; g < gains.length; g += 3) {
223
223
  if (single) {
@@ -7,4 +7,4 @@ export declare function view(buffer: BinaryData): Uint8Array;
7
7
  /**
8
8
  * Concatenates byte arrays
9
9
  */
10
- export declare function concat(parts: ArrayLike<number>[]): Uint8Array;
10
+ export declare function concat(parts: ArrayLike<number>[]): Uint8Array<ArrayBuffer>;
@@ -62,8 +62,10 @@ export class Optimizer {
62
62
  const { replace, even, alpha, palette, tolerance } = this.options;
63
63
  let changed = bounds(state, { x: 0, y: 0, width, height });
64
64
  if (!changed) {
65
- if (this.last)
65
+ if (this.last && this.last.duration + duration <= (this.options.maxDuration ?? Infinity))
66
66
  this.last.duration += duration;
67
+ else if (this.last)
68
+ this.output(empty(duration));
67
69
  else {
68
70
  this.output(empty(duration));
69
71
  this.opaque = false;
@@ -1,12 +1,48 @@
1
1
  export function isOptions(value) {
2
2
  return typeof value === 'object' && value !== null && !ArrayBuffer.isView(value);
3
3
  }
4
+ export function checkOptions(value, usage) {
5
+ if (value !== undefined && !isOptions(value))
6
+ throw new TypeError(`${usage.split('(')[0]}() takes an options object, like ${usage}, not ${value === null ? 'null' : typeof value}`);
7
+ }
8
+ export function oneOf(value, allowed, name) {
9
+ if (typeof value !== 'string' || !allowed.includes(value)) {
10
+ const list = allowed.map(a => `'${a}'`);
11
+ const expected = list.length > 1 ? `${list.slice(0, -1).join(', ')} or ${list[list.length - 1]}` : list[0];
12
+ throw new TypeError(`${name} must be ${expected}, not ${typeof value === 'string' ? `'${value}'` : String(value)}`);
13
+ }
14
+ return value;
15
+ }
4
16
  export function finite(value, name) {
5
17
  const number = Number(value);
6
18
  if (!Number.isFinite(number))
7
19
  throw new RangeError(`Invalid ${name}: ${value}`);
8
20
  return number;
9
21
  }
22
+ export function loopCount(value, name = 'loop count') {
23
+ if (typeof value !== 'number')
24
+ throw new TypeError(`${name} must be a number (Infinity or an integer from 0), not ${value === null ? 'null' : typeof value}`);
25
+ if (value !== Infinity && (!Number.isInteger(value) || value < 0))
26
+ throw new RangeError(`${name} must be Infinity or an integer from 0, not ${value}`);
27
+ return value;
28
+ }
29
+ export const toPlays = (count) => count === Infinity ? 0 : count + 1;
30
+ export const fromPlays = (plays) => plays === 0 ? Infinity : plays - 1;
31
+ const LOOP_LIMITS = { GIF: 65535, WebP: 65534, APNG: 2 ** 31 - 2 };
32
+ export function fitLoops(count, format) {
33
+ const max = LOOP_LIMITS[format];
34
+ if (count !== Infinity && count > max)
35
+ throw new RangeError(`refuse loop counts that don't fit the format: ${format} loop counts go up to ${max}, not ${count}`);
36
+ return count;
37
+ }
38
+ const DURATION_LIMITS = { GIF: 655350, APNG: 655350, WebP: 0xffffff };
39
+ export const maxDuration = (format) => DURATION_LIMITS[format];
40
+ export function fitDuration(duration, format) {
41
+ const max = DURATION_LIMITS[format];
42
+ if (duration > max)
43
+ throw new RangeError(`refuse frame durations that don't fit the format: ${format} frames last up to ${max} ms, not ${duration}`);
44
+ return duration;
45
+ }
10
46
  export function quality(value, name = 'quality') {
11
47
  const q = finite(value, name);
12
48
  if (q < 0 || q > 100)
@@ -18,11 +54,14 @@ export function effort(value, max) {
18
54
  throw new RangeError(`effort must be an integer 0-${max}, not ${value}`);
19
55
  return value;
20
56
  }
21
- export const compressionLevel = (value) => Math.min(9, Math.max(0, ~~finite(value, 'compression level')));
22
- export const color = (value, name = 'color') => finite(value, name);
23
- const LOOP_LIMITS = { GIF: 65535, WebP: 65534, APNG: 2 ** 31 - 2 };
24
- export function fitLoops(loopCount, format) {
25
- if (loopCount !== Infinity && loopCount > LOOP_LIMITS[format])
26
- throw new RangeError(`${format} stores loop counts up to ${LOOP_LIMITS[format]} (or Infinity to loop forever), not ${loopCount}`);
27
- return loopCount;
57
+ export function compressionLevel(value) {
58
+ if (!Number.isInteger(value) || value < 0 || value > 9)
59
+ throw new RangeError(`compression level must be an integer 0-9, not ${value}`);
60
+ return value;
61
+ }
62
+ export function color(value, name = 'color') {
63
+ const number = finite(value, name);
64
+ if (!Number.isInteger(number) || number < -0x80000000 || number > 0xffffffff)
65
+ throw new RangeError(`Invalid ${name}: ${value} (a color is an integer like 0xrrggbbaa)`);
66
+ return number >>> 0;
28
67
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "imagescript",
3
- "version": "3.20.3",
3
+ "version": "4.0.0-rc.2",
4
4
  "description": "Pure TypeScript image processing that runs everywhere",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -14,8 +14,6 @@
14
14
  "files": [
15
15
  "dist",
16
16
  "LICENSE",
17
- "LICENSE.MIT",
18
- "LICENSE.AGPL-3.0",
19
17
  "THIRD_PARTY_NOTICES.md",
20
18
  "licenses",
21
19
  "README.md"
@@ -29,7 +27,7 @@
29
27
  "test": "npm run build && node ./tests/run.js",
30
28
  "coverage": "node scripts/build.js --source-maps && c8 node ./tests/run.js && node scripts/coverage-report.js",
31
29
  "prepack": "npm run build",
32
- "version": "node scripts/version.js && git add src/version.ts jsr.json"
30
+ "version": "node scripts/version.js && git add jsr.json"
33
31
  },
34
32
  "repository": {
35
33
  "type": "git",
@@ -86,7 +84,7 @@
86
84
  "font"
87
85
  ],
88
86
  "author": "Mathis Mensing <mathis@matmen.dev>",
89
- "license": "(AGPL-3.0-or-later OR MIT)",
87
+ "license": "MIT",
90
88
  "bugs": {
91
89
  "url": "https://github.com/imagescript/ImageScript/issues"
92
90
  },
@@ -95,12 +93,19 @@
95
93
  "node": ">=22.13.0"
96
94
  },
97
95
  "devDependencies": {
98
- "@eslint/js": "9.39.5",
96
+ "@eslint/js": "10.0.1",
99
97
  "c8": "12.0.0",
100
- "eslint": "9.39.5",
98
+ "eslint": "10.14.0",
101
99
  "eslint-plugin-sonarjs": "4.2.2",
102
- "typedoc": "0.27.9",
103
- "typescript": "5.7.3",
104
- "typescript-eslint": "8.72.0"
100
+ "typedoc": "0.28.20",
101
+ "typescript": "6.0.3",
102
+ "typescript-eslint": "8.72.2"
103
+ },
104
+ "overrides": {
105
+ "minimatch": "11.0.8",
106
+ "glob": "14.0.5",
107
+ "typedoc": {
108
+ "minimatch": "9.0.9"
109
+ }
105
110
  }
106
111
  }