imagescript 3.20.0 → 3.20.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.
@@ -6,7 +6,7 @@ import { Frame } from "./frame.js";
6
6
  import { Optimizer, Compositor, coverCanvas, fillUnchanged } from "./util/optimize.js";
7
7
  import { compressAsync, decompressAsync } from "./codecs/zlib.js";
8
8
  import { resolveMetadata, pngText, exifData, gifComments } from "./metadata.js";
9
- import { finite, compressionLevel } from "./util/options.js";
9
+ import { finite, compressionLevel, fitLoops } from "./util/options.js";
10
10
  const toPlays = (loopCount) => loopCount < 0 || loopCount === Infinity ? 0 : loopCount + 1;
11
11
  export class AnimationEncoder {
12
12
  format;
@@ -71,6 +71,7 @@ export class AnimationEncoder {
71
71
  }
72
72
  static __webp__(width, height, { loopCount = -1, metadata = {}, optimize = true, quality, effort = 2, sharpYuv = false, alphaQuality = 100 }) {
73
73
  canvas(width, height, loopCount);
74
+ fitLoops(loopCount, 'WebP');
74
75
  if (width > 1 << 24 || height > 1 << 24)
75
76
  throw new RangeError('WebP: canvas too large');
76
77
  const lossy = webp.lossyOptions(quality, effort, sharpYuv, alphaQuality);
@@ -100,6 +101,7 @@ export class AnimationEncoder {
100
101
  static __gif__(width, height, { loopCount = -1, metadata = {}, optimize = true, quality: qualityOption = 95 }) {
101
102
  const quality = finite(qualityOption, 'quality');
102
103
  canvas(width, height, loopCount);
104
+ fitLoops(loopCount, 'GIF');
103
105
  const comments = gifComments(resolveMetadata(metadata));
104
106
  let first = null, restores = false;
105
107
  const rest = [];
@@ -126,6 +128,7 @@ export class AnimationEncoder {
126
128
  static __apng__(width, height, { loopCount = -1, metadata = {}, optimize = true, compression: compressionOption = 4 }) {
127
129
  const compression = compressionLevel(compressionOption);
128
130
  canvas(width, height, loopCount);
131
+ fitLoops(loopCount, 'APNG');
129
132
  const text = pngText(resolveMetadata(metadata));
130
133
  const stored = [];
131
134
  const cost = new png.AlphaCost();
package/dist/animation.js CHANGED
@@ -11,7 +11,7 @@ import { Frame, FrameDisposalModes } from "./frame.js";
11
11
  import { ImageType } from "./image-type.js";
12
12
  import { optimize } from "./util/optimize.js";
13
13
  import { resolveMetadata, pngText, exifData, gifComments } from "./metadata.js";
14
- import { isOptions, finite, compressionLevel } from "./util/options.js";
14
+ import { isOptions, finite, compressionLevel, fitLoops } from "./util/options.js";
15
15
  import { MAX_PIXELS, decoding } from "./util/limits.js";
16
16
  import { AnimationEncoder } from "./animation-encoder.js";
17
17
  const toPlays = (loopCount) => loopCount < 0 || loopCount === Infinity ? 0 : loopCount + 1;
@@ -105,6 +105,7 @@ export class Animation extends Array {
105
105
  }
106
106
  async encodeGIF(qualityOrOptions = 95) {
107
107
  const { quality = 95, metadata = {}, optimize = true } = isOptions(qualityOrOptions) ? qualityOrOptions : { quality: qualityOrOptions };
108
+ fitLoops(this.loopCount, 'GIF');
108
109
  this.__validate__();
109
110
  const frames = optimize
110
111
  ? this.__optimized__({ replace: false, even: false, alpha: 'frame', palette: 256, cover: true }).map(f => ({ ...f, dispose: f.dispose ? 3 : 1 }))
@@ -122,6 +123,7 @@ export class Animation extends Array {
122
123
  }
123
124
  async encodeAPNG(compressionOrOptions = 4) {
124
125
  const { compression = 4, metadata = {}, optimize = true } = isOptions(compressionOrOptions) ? compressionOrOptions : { compression: compressionOrOptions };
126
+ fitLoops(this.loopCount, 'APNG');
125
127
  this.__validate__();
126
128
  if (!this.length)
127
129
  throw new Error('An animation needs at least one frame');
@@ -131,6 +133,7 @@ export class Animation extends Array {
131
133
  return png.encodeAnimation(frames, this.width, this.height, toPlays(this.loopCount), compressionLevel(compression), pngText(resolveMetadata(metadata)));
132
134
  }
133
135
  async encodeWebP({ metadata = {}, optimize = true, quality, effort = 2, sharpYuv = false, alphaQuality = 100 } = {}) {
136
+ fitLoops(this.loopCount, 'WebP');
134
137
  this.__validate__();
135
138
  if (!this.length)
136
139
  throw new Error('An animation needs at least one frame');
package/dist/image.js CHANGED
@@ -692,22 +692,42 @@ export class Image {
692
692
  const sourceOS = sourceMap?.offsetSDR ?? [0, 0, 0], sourceOH = sourceMap?.offsetHDR ?? [0, 0, 0];
693
693
  const oS = map.offsetSDR, oH = map.offsetHDR;
694
694
  const src = source.bitmap, Gs = [0, 0, 0];
695
+ let lastGain = NaN, lastPower = 1;
695
696
  gain.redraw(this, map, left, top, right, bottom, (px, py, S, boost, out) => {
696
697
  const s = 4 * ((py - y) * source.width + px - x), b = 4 * ((py - top) * beforeWidth + px - left);
697
698
  const as = src[s + 3] / 255 * opacity, ab = before[b + 3] / 255;
698
699
  const ao = as + ab * (1 - as);
699
- if (sample)
700
- sample(px - x, py - y, Gs);
700
+ if (blend && ab === 1) {
701
+ for (let c = 0; c < 3; c++)
702
+ out[c] = (S[c] + oS[c]) * boost[c] - oH[c];
703
+ return;
704
+ }
705
+ if (ao <= 0) {
706
+ out[0] = out[1] = out[2] = 0;
707
+ return;
708
+ }
709
+ let sampled = false;
701
710
  for (let c = 0; c < 3; c++) {
702
711
  const Sd = gain.LINEAR[before[b + c]], Ss = gain.LINEAR[src[s + c]];
703
712
  const Hd = (Sd + oS[c]) * boost[c] - oH[c];
704
- const Hs = sample ? (Ss + sourceOS[c]) * 2 ** Gs[c] - sourceOH[c] : Ss;
705
- if (blend && ab === 1) {
706
- out[c] = (S[c] + oS[c]) * boost[c] - oH[c];
713
+ const share = Math.abs(Ss - Sd) > 1e-4 ? Math.min(1, Math.max(0, (S[c] - Sd) / (Ss - Sd))) : as / ao;
714
+ if (share === 0) {
715
+ out[c] = Hd;
707
716
  continue;
708
717
  }
709
- const share = Math.abs(Ss - Sd) > 1e-4 ? Math.min(1, Math.max(0, (S[c] - Sd) / (Ss - Sd))) : ao > 0 ? as / ao : 0;
710
- out[c] = ao > 0 ? share * Hs + (1 - share) * Hd : 0;
718
+ let Hs = Ss;
719
+ if (sample) {
720
+ if (!sampled) {
721
+ sample(px - x, py - y, Gs);
722
+ sampled = true;
723
+ }
724
+ if (Gs[c] !== lastGain) {
725
+ lastGain = Gs[c];
726
+ lastPower = 2 ** lastGain;
727
+ }
728
+ Hs = (Ss + sourceOS[c]) * lastPower - sourceOH[c];
729
+ }
730
+ out[c] = share * Hs + (1 - share) * Hd;
711
731
  }
712
732
  }, map.channels === 1 && (!sourceMap || sourceMap.channels === 1), Math.max(0, ...(sourceMap?.gainMapMax ?? [0])));
713
733
  return this;
package/dist/util/gain.js CHANGED
@@ -2,13 +2,14 @@ import { Image } from "../image.js";
2
2
  export const LINEAR = new Float32Array(256).map((_, v) => (v /= 255) <= 0.04045 ? v / 12.92 : ((v + 0.055) / 1.055) ** 2.4);
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
+ const gainTables = (map) => [0, 1, 2].map(c => {
6
+ const min = map.gainMapMin[c], max = map.gainMapMax[c], gamma = map.gamma[c];
7
+ return new Float32Array(256).map((_, v) => min + (max - min) * (v / 255) ** (1 / gamma));
8
+ });
5
9
  export function decodeGains(map) {
6
10
  const { bitmap } = map.image;
7
11
  const gains = new Float32Array(bitmap.length / 4 * 3);
8
- const tables = [0, 1, 2].map(c => {
9
- const min = map.gainMapMin[c], max = map.gainMapMax[c], gamma = map.gamma[c];
10
- return new Float32Array(256).map((_, v) => min + (max - min) * (v / 255) ** (1 / gamma));
11
- });
12
+ const tables = gainTables(map);
12
13
  for (let i = 0, g = 0; i < bitmap.length; i += 4, g += 3) {
13
14
  gains[g] = tables[0][bitmap[i]];
14
15
  gains[g + 1] = tables[1][bitmap[i + 1]];
@@ -78,18 +79,22 @@ export function redraw(image, map, left, top, right, bottom, light, single = map
78
79
  return;
79
80
  const mapWidth = map.width, mapHeight = map.height;
80
81
  const sx = mapWidth / width, sy = mapHeight / height;
81
- const gains = decodeGains(map);
82
82
  const oS = map.offsetSDR, oH = map.offsetHDR;
83
83
  const lo = Math.min(0, ...map.gainMapMin), hi = Math.max(0, ...map.gainMapMax, 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));
87
+ if (i1 <= i0 || j1 <= j0)
88
+ return;
89
+ const tables = gainTables(map), stored = map.image.bitmap;
90
+ const regionWidth = i1 - i0;
91
+ const next = new Float32Array(regionWidth * (j1 - j0) * 3);
87
92
  for (let j = j0; j < j1; j++) {
88
93
  const y0 = Math.max(0, Math.floor(j / sy)), y1 = Math.min(height, Math.max(y0 + 1, Math.floor((j + 1) / sy)));
89
94
  for (let i = i0; i < i1; i++) {
90
95
  const x0 = Math.max(0, Math.floor(i / sx)), x1 = Math.min(width, Math.max(x0 + 1, Math.floor((i + 1) / sx)));
91
- const g = 3 * (j * mapWidth + i);
92
- const boost = [2 ** gains[g], 2 ** gains[g + 1], 2 ** gains[g + 2]];
96
+ const m = 4 * (j * mapWidth + i), g = 3 * ((j - j0) * regionWidth + (i - i0));
97
+ const boost = [2 ** tables[0][stored[m]], 2 ** tables[1][stored[m + 1]], 2 ** tables[2][stored[m + 2]]];
93
98
  sumS.fill(0);
94
99
  sumH.fill(0);
95
100
  for (let y = y0; y < y1; y++)
@@ -113,13 +118,35 @@ export function redraw(image, map, left, top, right, bottom, light, single = map
113
118
  s += LUMA[c] * (sumS[c] / n + oS[c]);
114
119
  h += LUMA[c] * (sumH[c] / n + oH[c]);
115
120
  }
116
- gains[g] = gains[g + 1] = gains[g + 2] = ratio(h, s, lo, hi);
121
+ next[g] = next[g + 1] = next[g + 2] = ratio(h, s, lo, hi);
117
122
  }
118
123
  else
119
124
  for (let c = 0; c < 3; c++)
120
- gains[g + c] = ratio(sumH[c] / n + oH[c], sumS[c] / n + oS[c], lo, hi);
125
+ next[g + c] = ratio(sumH[c] / n + oH[c], sumS[c] / n + oS[c], lo, hi);
121
126
  }
122
127
  }
128
+ const { gainMapMin: min, gainMapMax: max, gamma } = map;
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
+ for (let g = 0; fits && g < next.length; g += 3)
131
+ for (let c = 0; c < 3; c++)
132
+ if (!(next[g + c] >= min[c] && next[g + c] <= max[c]))
133
+ fits = false;
134
+ if (fits) {
135
+ for (let j = j0; j < j1; j++)
136
+ for (let i = i0, g = 3 * (j - j0) * regionWidth, m = 4 * (j * mapWidth + i0); i < i1; i++, g += 3, m += 4)
137
+ for (let c = 0; c < 3; c++) {
138
+ const t = max[c] > min[c] ? (next[g + c] - min[c]) / (max[c] - min[c]) : 0;
139
+ stored[m + c] = 255 * Math.min(1, Math.max(0, t)) ** gamma[c];
140
+ }
141
+ return;
142
+ }
143
+ const gains = decodeGains(map);
144
+ for (let j = j0; j < j1; j++)
145
+ for (let i = i0, g = 3 * (j - j0) * regionWidth, m = 3 * (j * mapWidth + i0); i < i1; i++, g += 3, m += 3) {
146
+ gains[m] = next[g];
147
+ gains[m + 1] = next[g + 1];
148
+ gains[m + 2] = next[g + 2];
149
+ }
123
150
  encodeGains(map, gains, mapWidth, mapHeight, single);
124
151
  }
125
152
  const ratio = (h, s, lo, hi) => {
@@ -20,3 +20,9 @@ export function effort(value, max) {
20
20
  }
21
21
  export const compressionLevel = (value) => Math.min(9, Math.max(0, ~~finite(value, 'compression level')));
22
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;
28
+ }
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const VERSION = '3.20.0';
1
+ export const VERSION = '3.20.2';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "imagescript",
3
- "version": "3.20.0",
3
+ "version": "3.20.2",
4
4
  "description": "Pure TypeScript image processing that runs everywhere",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",