imagescript 3.13.2 → 3.15.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,7 +30,7 @@ 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 { isOptions, finite, color as checkColor } from "./util/options.js";
33
+ import { isOptions, finite, color as checkColor, quality as checkQuality, effort as checkEffort, compressionLevel } from "./util/options.js";
34
34
  import { MAX_PIXELS, decoding } from "./util/limits.js";
35
35
  function featureList(features) {
36
36
  if (typeof features !== 'object' || features === null)
@@ -285,10 +285,16 @@ export class Image {
285
285
  width = Math.floor(width);
286
286
  height = Math.floor(height);
287
287
  const scale = Math.max(width / this.width, height / this.height);
288
- this.resize(Math.max(width, Math.round(this.width * scale)), Math.max(height, Math.round(this.height * scale)), options);
288
+ const resizedWidth = Math.max(width, Math.round(this.width * scale)), resizedHeight = Math.max(height, Math.round(this.height * scale));
289
+ if (options.mode !== Image.RESIZE_NEAREST_NEIGHBOR && width >= 1 && height >= 1) {
290
+ const region = { x: ~~((resizedWidth - width) / 2), y: ~~((resizedHeight - height) / 2), width, height };
291
+ this.__rescaled__?.(resizedWidth, resizedHeight);
292
+ return this.__apply__(this.__resize__(resizedWidth, resizedHeight, options.mode, options.fast, region));
293
+ }
294
+ this.resize(resizedWidth, resizedHeight, options);
289
295
  return this.crop((this.width - width) / 2, (this.height - height) / 2, width, height);
290
296
  }
291
- __resize__(width, height, mode = Image.RESIZE_BILINEAR, fast = true) {
297
+ __resize__(width, height, mode = Image.RESIZE_BILINEAR, fast = true, region) {
292
298
  if (width === Image.RESIZE_AUTO && height === Image.RESIZE_AUTO)
293
299
  throw new Error('RESIZE_AUTO can only be used for either width or height, not for both');
294
300
  else if (width === Image.RESIZE_AUTO)
@@ -306,8 +312,8 @@ export class Image {
306
312
  const filter = mode === Image.RESIZE_BILINEAR ? TRIANGLE : mode === Image.RESIZE_BICUBIC ? CUBIC : mode === Image.RESIZE_AREA ? BOX : null;
307
313
  if (!filter)
308
314
  throw new Error('Invalid resize mode');
309
- const image = new Image(width, height);
310
- resample(this.bitmap, this.width, this.height, width, height, filter, image.bitmap, fast);
315
+ const image = new Image(region?.width ?? width, region?.height ?? height);
316
+ resample(this.bitmap, this.width, this.height, width, height, filter, image.bitmap, fast, region);
311
317
  return image;
312
318
  }
313
319
  __resize_nearest_neighbor__(width, height) {
@@ -790,7 +796,7 @@ export class Image {
790
796
  dominantColor(ignoreBlackOrOptions = true, ignoreWhiteArgument = true, thresholdArgument = 0xf) {
791
797
  const { ignoreBlack = true, ignoreWhite = true, threshold: bwThreshold = 0xf } = typeof ignoreBlackOrOptions === 'object' && ignoreBlackOrOptions !== null
792
798
  ? ignoreBlackOrOptions : { ignoreBlack: ignoreBlackOrOptions, ignoreWhite: ignoreWhiteArgument, threshold: thresholdArgument };
793
- const threshold = Math.min(64, Math.max(0, bwThreshold)) * 4;
799
+ const threshold = Math.min(64, Math.max(0, finite(bwThreshold, 'threshold'))) * 4;
794
800
  const pixels = this.__bytes__();
795
801
  const color = quantize.dominantColor(pixels, (r, g, b) => !(ignoreBlack && Math.max(r, g, b) < threshold) && !(ignoreWhite && Math.min(r, g, b) > 255 - threshold))
796
802
  || quantize.dominantColor(pixels, () => true);
@@ -1021,8 +1027,7 @@ export class Image {
1021
1027
  ({ compression = 4, metadata = {} } = compressionOrOptions);
1022
1028
  else
1023
1029
  metadata = compressionOrOptions;
1024
- const level = Math.min(9, Math.max(0, ~~finite(compression, 'compression level')));
1025
- return png.encodeRGBA(this.__bytes__(), this.width, this.height, level, pngText(resolveMetadata(metadata)));
1030
+ return png.encodeRGBA(this.__bytes__(), this.width, this.height, compressionLevel(compression), pngText(resolveMetadata(metadata)));
1026
1031
  }
1027
1032
  encodePNG(compressionOrOptions, metadata) {
1028
1033
  return this.encode(compressionOrOptions, metadata);
@@ -1037,7 +1042,7 @@ export class Image {
1037
1042
  const exif = exifData(resolveMetadata(metadata));
1038
1043
  const lossy = webp.lossyOptions(quality, effort, sharpYuv, alphaQuality);
1039
1044
  if (!lossy)
1040
- return webp.encode(this.__bytes__(), this.width, this.height, exif, threads);
1045
+ return webp.encode(this.__bytes__(), this.width, this.height, exif, threads, effort);
1041
1046
  return webp.encodeLossy(this.__bytes__(), this.width, this.height, lossy, exif);
1042
1047
  }
1043
1048
  async encodeTGA({ metadata = {} } = {}) {
@@ -1051,19 +1056,19 @@ export class Image {
1051
1056
  return qoi.encode(this.__bytes__(), this.width, this.height);
1052
1057
  }
1053
1058
  async encodeAVIF({ quality = 60, alphaQuality = 100, subsampling = '4:4:4', effort = 1, threads = defaultThreads(), metadata = {} } = {}) {
1059
+ checkEffort(effort, 2);
1054
1060
  checkThreads(threads);
1061
+ if (subsampling !== '4:4:4' && subsampling !== '4:2:0')
1062
+ throw new TypeError(`subsampling must be '4:4:4' or '4:2:0', not ${String(subsampling)}`);
1055
1063
  const exif = exifData(resolveMetadata(metadata));
1056
- return avifEnc.encode(this.__bytes__(), this.width, this.height, { quality: finite(quality, 'quality'), alphaQuality: finite(alphaQuality, 'alphaQuality'), subsampling, effort, exif, threads });
1064
+ return avifEnc.encode(this.__bytes__(), this.width, this.height, { quality: checkQuality(quality), alphaQuality: checkQuality(alphaQuality, 'alphaQuality'), subsampling, effort, exif, threads });
1057
1065
  }
1058
1066
  async encodeJXL({ quality, effort = 1, threads = defaultThreads(), metadata = {} } = {}) {
1059
- if (!Number.isInteger(effort) || effort < 0 || effort > 2)
1060
- throw new RangeError(`effort must be an integer 0-2, not ${effort}`);
1067
+ checkEffort(effort, 2);
1061
1068
  checkThreads(threads);
1062
1069
  let distance = 0;
1063
1070
  if (quality !== undefined) {
1064
- const q = finite(quality, 'quality');
1065
- if (q < 0 || q > 100)
1066
- throw new RangeError(`quality must be 0-100, not ${q}`);
1071
+ const q = checkQuality(quality);
1067
1072
  distance = q >= 100 ? 0 : q >= 30 ? 0.1 + (100 - q) * 0.09 : 53 / 3000 * q * q - 23 / 20 * q + 25;
1068
1073
  }
1069
1074
  return jxl.encode(this.__bytes__(), this.width, this.height, exifData(resolveMetadata(metadata)), distance, effort, threads);
@@ -1074,6 +1079,8 @@ export class Image {
1074
1079
  static async decode(input, options = {}) {
1075
1080
  const { applyOrientation = true, applyColorProfile = true, maxPixels = MAX_PIXELS, toneMapping = 'reinhard', threads = defaultThreads() } = options;
1076
1081
  checkThreads(threads);
1082
+ if (!(Number(maxPixels) > 0))
1083
+ throw new RangeError(`maxPixels must be a positive number, not ${maxPixels}`);
1077
1084
  if (toneMapping !== 'reinhard' && toneMapping !== 'clamp')
1078
1085
  throw new TypeError(`toneMapping must be 'reinhard' or 'clamp', not ${String(toneMapping)}`);
1079
1086
  const data = view(input);
@@ -1112,6 +1119,8 @@ export class Image {
1112
1119
  return hdr.decode(data, maxPixels, toneMapping);
1113
1120
  if (ImageType.isTGA(dataView))
1114
1121
  return tga.decode(data, maxPixels);
1122
+ if (ImageType.isGIF(dataView))
1123
+ throw new Error('GIFs have to be decoded with Animation.decode() (or decode(), which returns an image or an animation)');
1115
1124
  throw new Error('Unsupported image type');
1116
1125
  });
1117
1126
  if (applyColorProfile) {
@@ -1181,6 +1190,7 @@ export class Image {
1181
1190
  const { size = 1, mode = Image.SVG_MODE_SCALE, fonts = [] } = isOptions(sizeOrOptions) ? sizeOrOptions : { size: sizeOrOptions, mode: modeArgument };
1182
1191
  if (![1, 2, 3].includes(mode))
1183
1192
  throw new Error('Invalid SVG scaling mode');
1193
+ finite(size, 'SVG size');
1184
1194
  if (mode === Image.SVG_MODE_SCALE && size <= 0)
1185
1195
  throw new RangeError('SVG scale must be > 0');
1186
1196
  if (mode !== Image.SVG_MODE_SCALE && size < 1)
@@ -1213,6 +1223,8 @@ export class Image {
1213
1223
  const { color = 0xffffffff, layout = new TextLayout(), strokeWidth = 0, strokeColor = 0x000000ff, hinting = true, features = {}, variations = {} } = options;
1214
1224
  const axes = textRenderer.axisValues(variations);
1215
1225
  size = finite(size, 'font size');
1226
+ if (size < 0)
1227
+ throw new RangeError(`Invalid font size: ${size}`);
1216
1228
  if (finite(strokeWidth, 'stroke width') < 0)
1217
1229
  throw new RangeError('Invalid stroke width');
1218
1230
  const fonts = Array.isArray(font) ? font : [font];
@@ -334,11 +334,15 @@ function apply(kind, linear, ts, region, subregion, input) {
334
334
  const pixmap = create(region.width, region.height);
335
335
  const sx = kind.scale * scaleX, sy = kind.scale * scaleY, w = src.width, h = src.height;
336
336
  const round = (v) => Math.sign(v) * Math.round(Math.abs(v));
337
+ const channel = (m, c) => {
338
+ const a = map.data[m + 3];
339
+ return c === 3 ? a / 255 : a ? map.data[m + c] / a : 0;
340
+ };
337
341
  for (let y = 0; y < Math.min(h, map.height); y++)
338
342
  for (let x = 0; x < Math.min(w, map.width); x++) {
339
343
  const m = (y * map.width + x) * 4;
340
- const ox = round(x + (map.data[m + kind.xChannel] / 255 - 0.5) * sx * kind.scale);
341
- const oy = round(y + (map.data[m + kind.yChannel] / 255 - 0.5) * sy * kind.scale);
344
+ const ox = round(x + (channel(m, kind.xChannel) - 0.5) * sx);
345
+ const oy = round(y + (channel(m, kind.yChannel) - 0.5) * sy);
342
346
  if (ox < 0 || ox >= w || oy < 0 || oy >= h)
343
347
  continue;
344
348
  pixmap.data.set(src.data.subarray((oy * w + ox) * 4, (oy * w + ox) * 4 + 4), (y * pixmap.width + x) * 4);
@@ -7,4 +7,16 @@ export function finite(value, name) {
7
7
  throw new RangeError(`Invalid ${name}: ${value}`);
8
8
  return number;
9
9
  }
10
+ export function quality(value, name = 'quality') {
11
+ const q = finite(value, name);
12
+ if (q < 0 || q > 100)
13
+ throw new RangeError(`${name} must be 0-100, not ${q}`);
14
+ return q;
15
+ }
16
+ export function effort(value, max) {
17
+ if (!Number.isInteger(value) || value < 0 || value > max)
18
+ throw new RangeError(`effort must be an integer 0-${max}, not ${value}`);
19
+ return value;
20
+ }
21
+ export const compressionLevel = (value) => Math.min(9, Math.max(0, ~~finite(value, 'compression level')));
10
22
  export const color = (value, name = 'color') => finite(value, name);
@@ -57,44 +57,66 @@ function coefficients(inSize, outSize, filter, bits, pad = false) {
57
57
  }
58
58
  return { start, weights, taps };
59
59
  }
60
+ function sliced(inSize, outSize, filter, bits, pad, from, count) {
61
+ const c = coefficients(inSize, outSize, filter, bits, pad);
62
+ if (from === 0 && count === outSize)
63
+ return c;
64
+ return { start: c.start.slice(from, from + count), weights: c.weights.slice(from * c.taps, (from + count) * c.taps), taps: c.taps };
65
+ }
66
+ function window(horizontal, region) {
67
+ if (!horizontal)
68
+ return { x0: region.x, x1: region.x + region.width };
69
+ const { start, taps } = horizontal, x0 = start[0], x1 = start[start.length - 1] + taps;
70
+ for (let i = 0; i < start.length; i++)
71
+ start[i] -= x0;
72
+ return { x0, x1 };
73
+ }
60
74
  const FAST_TAPS = 4;
61
- export function resample(src, width, height, outWidth, outHeight, filter, out, fast = false) {
75
+ export function resample(src, width, height, outWidth, outHeight, filter, out, fast = false, region = { x: 0, y: 0, width: outWidth, height: outHeight }) {
76
+ const partial = region.width !== outWidth || region.height !== outHeight;
77
+ const opaque = partial ? isOpaque(src) : undefined;
62
78
  if (littleEndian && filter.support <= 1) {
63
- if (fast && resampleFast(src, width, height, outWidth, outHeight, filter, out))
79
+ if (fast && resampleFast(src, width, height, outWidth, outHeight, filter, out, region, opaque))
64
80
  return;
65
- if (resampleOpaque(src, width, height, outWidth, outHeight, filter, out))
81
+ if (opaque !== false && resampleOpaque(src, width, height, outWidth, outHeight, filter, out, region))
66
82
  return;
67
83
  }
68
- resamplePremultiplied(src, width, height, outWidth, outHeight, filter, out);
84
+ else if (opaque !== false && resampleOpaqueSigned(src, width, height, outWidth, outHeight, filter, out, region))
85
+ return;
86
+ resamplePremultiplied(src, width, height, outWidth, outHeight, filter, out, region);
69
87
  }
70
- function resampleOpaque(src, width, height, outWidth, outHeight, filter, out) {
88
+ function resampleOpaque(src, width, height, outWidth, outHeight, filter, out, region) {
71
89
  const pixels = new Int32Array(src.buffer, src.byteOffset, width * height);
72
90
  let all = -1;
73
91
  for (let x = 0, last = (height - 1) * width; x < width; x++)
74
92
  all &= pixels[x] & pixels[last + x];
75
93
  if (all >>> 24 !== 255)
76
94
  return false;
77
- const lanes = new Int32Array(width * outHeight * 2);
95
+ const horizontal = outWidth === width ? null : sliced(width, outWidth, filter, BITS, false, region.x, region.width);
96
+ const { x0, x1 } = window(horizontal, region), cw = x1 - x0;
97
+ const rh = region.height;
98
+ const lanes = new Int32Array(cw * rh * 2);
78
99
  if (outHeight === height) {
79
- for (let i = 0, o = 0; i < pixels.length; i++, o += 2) {
80
- const p = pixels[i];
81
- all &= p;
82
- lanes[o] = (p & 0xff00ff) << 8;
83
- lanes[o + 1] = (p & 0xff00) | 0;
84
- }
100
+ for (let y = 0, o = 0; y < rh; y++)
101
+ for (let i = (region.y + y) * width + x0, end = i + cw; i < end; i++, o += 2) {
102
+ const p = pixels[i];
103
+ all &= p;
104
+ lanes[o] = (p & 0xff00ff) << 8;
105
+ lanes[o + 1] = (p & 0xff00) | 0;
106
+ }
85
107
  }
86
108
  else {
87
- let coefficientsV = coefficients(height, outHeight, filter, 8);
109
+ let coefficientsV = sliced(height, outHeight, filter, 8, false, region.y, rh);
88
110
  const swar = coefficientsV.taps <= 4;
89
111
  if (!swar)
90
- coefficientsV = coefficients(height, outHeight, filter, BITS);
112
+ coefficientsV = sliced(height, outHeight, filter, BITS, false, region.y, rh);
91
113
  const { start, weights, taps } = coefficientsV;
92
- for (let y = 0, o = 0; y < outHeight; y++) {
93
- const w = y * taps, r0 = start[y] * width;
114
+ for (let y = 0, o = 0; y < rh; y++) {
115
+ const w = y * taps, r0 = start[y] * width + x0;
94
116
  if (swar && taps === 4) {
95
117
  const w0 = weights[w], w1 = weights[w + 1], w2 = weights[w + 2], w3 = weights[w + 3];
96
118
  const r1 = r0 + width, r2 = r1 + width, r3 = r2 + width;
97
- for (let x = 0; x < width; x++, o += 2) {
119
+ for (let x = 0; x < cw; x++, o += 2) {
98
120
  const p0 = pixels[r0 + x], p1 = pixels[r1 + x], p2 = pixels[r2 + x], p3 = pixels[r3 + x];
99
121
  all &= p0 & p1 & p2 & p3;
100
122
  lanes[o] = Math.imul(p0 & 0xff00ff, w0) + Math.imul(p1 & 0xff00ff, w1) + Math.imul(p2 & 0xff00ff, w2) + Math.imul(p3 & 0xff00ff, w3);
@@ -103,7 +125,7 @@ function resampleOpaque(src, width, height, outWidth, outHeight, filter, out) {
103
125
  }
104
126
  else if (swar && taps === 3) {
105
127
  const w0 = weights[w], w1 = weights[w + 1], w2 = weights[w + 2], r1 = r0 + width, r2 = r1 + width;
106
- for (let x = 0; x < width; x++, o += 2) {
128
+ for (let x = 0; x < cw; x++, o += 2) {
107
129
  const p0 = pixels[r0 + x], p1 = pixels[r1 + x], p2 = pixels[r2 + x];
108
130
  all &= p0 & p1 & p2;
109
131
  lanes[o] = Math.imul(p0 & 0xff00ff, w0) + Math.imul(p1 & 0xff00ff, w1) + Math.imul(p2 & 0xff00ff, w2);
@@ -112,7 +134,7 @@ function resampleOpaque(src, width, height, outWidth, outHeight, filter, out) {
112
134
  }
113
135
  else if (swar && taps === 2) {
114
136
  const w0 = weights[w], w1 = weights[w + 1], r1 = r0 + width;
115
- for (let x = 0; x < width; x++, o += 2) {
137
+ for (let x = 0; x < cw; x++, o += 2) {
116
138
  const p0 = pixels[r0 + x], p1 = pixels[r1 + x];
117
139
  all &= p0 & p1;
118
140
  lanes[o] = Math.imul(p0 & 0xff00ff, w0) + Math.imul(p1 & 0xff00ff, w1);
@@ -120,7 +142,7 @@ function resampleOpaque(src, width, height, outWidth, outHeight, filter, out) {
120
142
  }
121
143
  }
122
144
  else if (swar) {
123
- for (let x = 0; x < width; x++, o += 2) {
145
+ for (let x = 0; x < cw; x++, o += 2) {
124
146
  let rb = 0, g = 0;
125
147
  for (let k = 0, s = r0 + x; k < taps; k++, s += width) {
126
148
  const p = pixels[s], wk = weights[w + k];
@@ -133,7 +155,7 @@ function resampleOpaque(src, width, height, outWidth, outHeight, filter, out) {
133
155
  }
134
156
  }
135
157
  else {
136
- for (let x = 0; x < width; x++, o += 2) {
158
+ for (let x = 0; x < cw; x++, o += 2) {
137
159
  let r = 0, g = 0, b = 0, k = 0, s = r0 + x;
138
160
  for (; k + 1 < taps; k += 2, s += width * 2) {
139
161
  const p = pixels[s], q = pixels[s + width], w0 = weights[w + k], w1 = weights[w + k + 1];
@@ -157,8 +179,8 @@ function resampleOpaque(src, width, height, outWidth, outHeight, filter, out) {
157
179
  }
158
180
  if (all >>> 24 !== 255)
159
181
  return false;
160
- const result = new Int32Array(out.buffer, out.byteOffset, outWidth * outHeight);
161
- if (outWidth === width) {
182
+ const result = new Int32Array(out.buffer, out.byteOffset, region.width * rh);
183
+ if (!horizontal) {
162
184
  for (let i = 0, o = 0; i < result.length; i++, o += 2) {
163
185
  const rb = lanes[o], g = lanes[o + 1];
164
186
  result[i] = ((rb & 0xffff) + 128 >> 8) | (g + 128 >> 8) << 8 | ((rb >>> 16) + 128 >> 8) << 16 | 0xff000000;
@@ -166,11 +188,11 @@ function resampleOpaque(src, width, height, outWidth, outHeight, filter, out) {
166
188
  return true;
167
189
  }
168
190
  const R = 1 << 7 + BITS, S = 8 + BITS;
169
- const { start, weights, taps } = coefficients(width, outWidth, filter, BITS);
170
- for (let y = 0, o = 0; y < outHeight; y++) {
171
- const row = y * width * 2;
191
+ const { start, weights, taps } = horizontal, rw = region.width;
192
+ for (let y = 0, o = 0; y < rh; y++) {
193
+ const row = y * cw * 2;
172
194
  if (taps === 4) {
173
- for (let x = 0; x < outWidth; x++, o++) {
195
+ for (let x = 0; x < rw; x++, o++) {
174
196
  const s = row + start[x] * 2, w = x * 4;
175
197
  const a0 = lanes[s], a1 = lanes[s + 2], a2 = lanes[s + 4], a3 = lanes[s + 6];
176
198
  const w0 = weights[w], w1 = weights[w + 1], w2 = weights[w + 2], w3 = weights[w + 3];
@@ -181,7 +203,7 @@ function resampleOpaque(src, width, height, outWidth, outHeight, filter, out) {
181
203
  }
182
204
  }
183
205
  else if (taps === 2) {
184
- for (let x = 0; x < outWidth; x++, o++) {
206
+ for (let x = 0; x < rw; x++, o++) {
185
207
  const s = row + start[x] * 2, w = x * 2;
186
208
  const a0 = lanes[s], a1 = lanes[s + 2], w0 = weights[w], w1 = weights[w + 1];
187
209
  const r = (a0 & 0xffff) * w0 + (a1 & 0xffff) * w1;
@@ -191,7 +213,7 @@ function resampleOpaque(src, width, height, outWidth, outHeight, filter, out) {
191
213
  }
192
214
  }
193
215
  else {
194
- for (let x = 0; x < outWidth; x++, o++) {
216
+ for (let x = 0; x < rw; x++, o++) {
195
217
  let r = 0, g = 0, b = 0, k = 0, s = row + start[x] * 2;
196
218
  const w = x * taps;
197
219
  for (; k + 1 < taps; k += 2, s += 4) {
@@ -221,14 +243,14 @@ function packedSum(pixels, s, step, weights, w, taps) {
221
243
  }
222
244
  return (rb >>> 8 & 0xff00ff) | (ga & 0xff00ff00);
223
245
  }
224
- function verticalFast(pixels, rows, width, outHeight, { start, weights, taps }) {
246
+ function verticalFast(pixels, rows, width, x0, cw, { start, weights, taps }) {
225
247
  let all = -1;
226
- for (let y = 0, o = 0; y < outHeight; y++) {
227
- const w = y * taps, r0 = start[y] * width;
248
+ for (let y = 0, o = 0; y < start.length; y++) {
249
+ const w = y * taps, r0 = start[y] * width + x0;
228
250
  if (taps === 4) {
229
251
  const w0 = weights[w], w1 = weights[w + 1], w2 = weights[w + 2], w3 = weights[w + 3];
230
252
  const r1 = r0 + width, r2 = r1 + width, r3 = r2 + width;
231
- for (let x = 0; x < width; x++, o++) {
253
+ for (let x = 0; x < cw; x++, o++) {
232
254
  const p0 = pixels[r0 + x], p1 = pixels[r1 + x], p2 = pixels[r2 + x], p3 = pixels[r3 + x];
233
255
  all &= p0 & p1 & p2 & p3;
234
256
  const rb = Math.imul(p0 & 0xff00ff, w0) + Math.imul(p1 & 0xff00ff, w1) + Math.imul(p2 & 0xff00ff, w2) + Math.imul(p3 & 0xff00ff, w3) + 0x800080;
@@ -238,7 +260,7 @@ function verticalFast(pixels, rows, width, outHeight, { start, weights, taps })
238
260
  }
239
261
  else if (taps === 2) {
240
262
  const w0 = weights[w], w1 = weights[w + 1], r1 = r0 + width;
241
- for (let x = 0; x < width; x++, o++) {
263
+ for (let x = 0; x < cw; x++, o++) {
242
264
  const p0 = pixels[r0 + x], p1 = pixels[r1 + x];
243
265
  all &= p0 & p1;
244
266
  const rb = Math.imul(p0 & 0xff00ff, w0) + Math.imul(p1 & 0xff00ff, w1) + 0x800080;
@@ -247,7 +269,7 @@ function verticalFast(pixels, rows, width, outHeight, { start, weights, taps })
247
269
  }
248
270
  }
249
271
  else {
250
- for (let x = 0; x < width; x++, o++) {
272
+ for (let x = 0; x < cw; x++, o++) {
251
273
  for (let k = 0, s = r0 + x; k < taps; k++, s += width)
252
274
  all &= pixels[s];
253
275
  rows[o] = packedSum(pixels, r0 + x, width, weights, w, taps);
@@ -284,14 +306,17 @@ function horizontalFast(rows, result, width, outWidth, outHeight, { start, weigh
284
306
  }
285
307
  }
286
308
  }
287
- function resampleFast(src, width, height, outWidth, outHeight, filter, out) {
288
- const vertical = coefficients(height, outHeight, filter, 8, true), horizontal = coefficients(width, outWidth, filter, 8, true);
289
- if (vertical.taps > FAST_TAPS || horizontal.taps > FAST_TAPS)
309
+ function resampleFast(src, width, height, outWidth, outHeight, filter, out, region, known) {
310
+ const { width: rw, height: rh } = region;
311
+ const vertical = sliced(height, outHeight, filter, 8, true, region.y, rh);
312
+ const horizontal = outWidth === width ? null : sliced(width, outWidth, filter, 8, true, region.x, rw);
313
+ if (vertical.taps > FAST_TAPS || horizontal && horizontal.taps > FAST_TAPS)
290
314
  return false;
315
+ const { x0, x1 } = window(horizontal, region), cw = x1 - x0;
291
316
  let pixels = new Int32Array(src.buffer, src.byteOffset, width * height), all = -1;
292
317
  for (let x = 0, last = (height - 1) * width; x < width; x++)
293
318
  all &= pixels[x] & pixels[last + x];
294
- let opaque = all >>> 24 === 255 && (outHeight !== height || isOpaque(src));
319
+ let opaque = known ?? (all >>> 24 === 255 && (outHeight !== height || isOpaque(src)));
295
320
  const premultiplied = () => {
296
321
  const bitmap = new Uint8ClampedArray(src.length);
297
322
  premultiply8(src, bitmap);
@@ -301,38 +326,155 @@ function resampleFast(src, width, height, outWidth, outHeight, filter, out) {
301
326
  pixels = premultiplied();
302
327
  let rows = pixels;
303
328
  if (outHeight !== height) {
304
- rows = new Int32Array(width * outHeight);
305
- if (verticalFast(pixels, rows, width, outHeight, vertical) >>> 24 !== 255 && opaque) {
329
+ rows = new Int32Array(cw * rh);
330
+ if (verticalFast(pixels, rows, width, x0, cw, vertical) >>> 24 !== 255 && opaque) {
306
331
  opaque = false;
307
- verticalFast(premultiplied(), rows, width, outHeight, vertical);
332
+ verticalFast(premultiplied(), rows, width, x0, cw, vertical);
308
333
  }
309
334
  }
310
- const result = new Int32Array(out.buffer, out.byteOffset, outWidth * outHeight);
311
- if (outWidth === width)
335
+ else if (cw !== width || rh !== height) {
336
+ rows = new Int32Array(cw * rh);
337
+ for (let y = 0; y < rh; y++)
338
+ rows.set(pixels.subarray((region.y + y) * width + x0, (region.y + y) * width + x1), y * cw);
339
+ }
340
+ const result = new Int32Array(out.buffer, out.byteOffset, rw * rh);
341
+ if (!horizontal)
312
342
  result.set(rows);
313
343
  else
314
- horizontalFast(rows, result, width, outWidth, outHeight, horizontal);
344
+ horizontalFast(rows, result, cw, rw, rh, horizontal);
315
345
  if (!opaque)
316
346
  unpremultiply8(out);
317
347
  return true;
318
348
  }
319
- function resamplePremultiplied(src, width, height, outWidth, outHeight, filter, out) {
320
- const plane = new Int32Array(width * outHeight * 4);
349
+ function resampleOpaqueSigned(src, width, height, outWidth, outHeight, filter, out, region) {
350
+ const pixels = new Int32Array(src.buffer, src.byteOffset, width * height);
351
+ let all = -1;
352
+ for (let x = 0, last = (height - 1) * width; x < width; x++)
353
+ all &= pixels[x] & pixels[last + x];
354
+ if (all >>> 24 !== 255)
355
+ return false;
356
+ const horizontal = outWidth === width ? null : sliced(width, outWidth, filter, BITS, false, region.x, region.width);
357
+ const { x0, x1 } = window(horizontal, region), cw = x1 - x0, rh = region.height;
358
+ const R = new Int32Array(cw * rh), G = new Int32Array(cw * rh), B = new Int32Array(cw * rh);
321
359
  if (outHeight === height) {
322
- for (let s = 0; s < src.length; s += 4) {
323
- const a = src[s + 3];
324
- plane[s] = src[s] * a;
325
- plane[s + 1] = src[s + 1] * a;
326
- plane[s + 2] = src[s + 2] * a;
327
- plane[s + 3] = a * 255;
360
+ for (let y = 0, o = 0; y < rh; y++)
361
+ for (let i = (region.y + y) * width + x0, end = i + cw; i < end; i++, o++) {
362
+ const p = pixels[i];
363
+ all &= p;
364
+ R[o] = (p & 255) * 255;
365
+ G[o] = (p >> 8 & 255) * 255;
366
+ B[o] = (p >> 16 & 255) * 255;
367
+ }
368
+ }
369
+ else {
370
+ const { start, weights, taps } = sliced(height, outHeight, filter, BITS, false, region.y, rh);
371
+ const H = ONE >> 1;
372
+ for (let y = 0, o = 0; y < rh; y++, o += cw) {
373
+ const w = y * taps;
374
+ for (let k = 0; k + 1 < taps; k += 2) {
375
+ const wa = weights[w + k], wb = weights[w + k + 1], ra = (start[y] + k) * width + x0, rb = ra + width;
376
+ if (k === 0) {
377
+ for (let x = 0; x < cw; x++) {
378
+ const p = pixels[ra + x], q = pixels[rb + x];
379
+ all &= p & q;
380
+ R[o + x] = (p & 255) * wa + (q & 255) * wb;
381
+ G[o + x] = (p >> 8 & 255) * wa + (q >> 8 & 255) * wb;
382
+ B[o + x] = (p >> 16 & 255) * wa + (q >> 16 & 255) * wb;
383
+ }
384
+ }
385
+ else {
386
+ for (let x = 0; x < cw; x++) {
387
+ const p = pixels[ra + x], q = pixels[rb + x];
388
+ all &= p & q;
389
+ R[o + x] += (p & 255) * wa + (q & 255) * wb;
390
+ G[o + x] += (p >> 8 & 255) * wa + (q >> 8 & 255) * wb;
391
+ B[o + x] += (p >> 16 & 255) * wa + (q >> 16 & 255) * wb;
392
+ }
393
+ }
394
+ }
395
+ if (taps & 1) {
396
+ const k = taps - 1, wk = weights[w + k], r = (start[y] + k) * width + x0;
397
+ for (let x = 0; x < cw; x++) {
398
+ const p = pixels[r + x];
399
+ all &= p;
400
+ if (k === 0) {
401
+ R[o + x] = (p & 255) * wk;
402
+ G[o + x] = (p >> 8 & 255) * wk;
403
+ B[o + x] = (p >> 16 & 255) * wk;
404
+ }
405
+ else {
406
+ R[o + x] += (p & 255) * wk;
407
+ G[o + x] += (p >> 8 & 255) * wk;
408
+ B[o + x] += (p >> 16 & 255) * wk;
409
+ }
410
+ }
411
+ }
412
+ for (let x = o, end = o + cw; x < end; x++) {
413
+ R[x] = R[x] * 255 + H >> BITS;
414
+ G[x] = G[x] * 255 + H >> BITS;
415
+ B[x] = B[x] * 255 + H >> BITS;
416
+ }
417
+ }
418
+ }
419
+ if (all >>> 24 !== 255)
420
+ return false;
421
+ const f = 255 / (65025 * ONE), rw = region.width;
422
+ const { start, weights, taps } = horizontal ?? { start: null, weights: null, taps: 0 };
423
+ for (let y = 0, o = 0; y < rh; y++) {
424
+ const row = y * cw;
425
+ if (!start || !weights) {
426
+ for (let x = 0; x < rw; x++, o += 4) {
427
+ const s = row + x;
428
+ out[o] = R[s] * ONE * f;
429
+ out[o + 1] = G[s] * ONE * f;
430
+ out[o + 2] = B[s] * ONE * f;
431
+ out[o + 3] = 255;
432
+ }
433
+ continue;
434
+ }
435
+ for (let x = 0; x < rw; x++, o += 4) {
436
+ let r = 0, g = 0, b = 0, k = 0, s = row + start[x];
437
+ const w = x * taps;
438
+ for (; k + 1 < taps; k += 2, s += 2) {
439
+ const w0 = weights[w + k], w1 = weights[w + k + 1];
440
+ r += R[s] * w0 + R[s + 1] * w1;
441
+ g += G[s] * w0 + G[s + 1] * w1;
442
+ b += B[s] * w0 + B[s + 1] * w1;
443
+ }
444
+ if (k < taps) {
445
+ const w0 = weights[w + k];
446
+ r += R[s] * w0;
447
+ g += G[s] * w0;
448
+ b += B[s] * w0;
449
+ }
450
+ out[o] = r * f;
451
+ out[o + 1] = g * f;
452
+ out[o + 2] = b * f;
453
+ out[o + 3] = 255;
328
454
  }
329
455
  }
456
+ return true;
457
+ }
458
+ function resamplePremultiplied(src, width, height, outWidth, outHeight, filter, out, region) {
459
+ const horizontal = outWidth === width ? null : sliced(width, outWidth, filter, BITS, false, region.x, region.width);
460
+ const { x0, x1 } = window(horizontal, region), cw = x1 - x0, rh = region.height;
461
+ const plane = new Int32Array(cw * rh * 4);
462
+ if (outHeight === height) {
463
+ for (let y = 0, o = 0; y < rh; y++)
464
+ for (let s = ((region.y + y) * width + x0) * 4, end = s + cw * 4; s < end; s += 4, o += 4) {
465
+ const a = src[s + 3];
466
+ plane[o] = src[s] * a;
467
+ plane[o + 1] = src[s + 1] * a;
468
+ plane[o + 2] = src[s + 2] * a;
469
+ plane[o + 3] = a * 255;
470
+ }
471
+ }
330
472
  else {
331
- const { start, weights, taps } = coefficients(height, outHeight, filter, BITS);
473
+ const { start, weights, taps } = sliced(height, outHeight, filter, BITS, false, region.y, rh);
332
474
  const stride = width * 4, H = ONE >> 1;
333
- for (let y = 0, o = 0; y < outHeight; y++) {
334
- const w = y * taps, r0 = start[y] * stride;
335
- for (let x = 0; x < stride; x += 4, o += 4) {
475
+ for (let y = 0, o = 0; y < rh; y++) {
476
+ const w = y * taps, r0 = start[y] * stride + x0 * 4;
477
+ for (let x = 0; x < cw * 4; x += 4, o += 4) {
336
478
  let r = 0, g = 0, b = 0, a = 0, k = 0, s = r0 + x;
337
479
  for (; k + 1 < taps; k += 2, s += stride * 2) {
338
480
  const t = s + stride, wa = weights[w + k] * src[s + 3], wb = weights[w + k + 1] * src[t + 3];
@@ -355,10 +497,10 @@ function resamplePremultiplied(src, width, height, outWidth, outHeight, filter,
355
497
  }
356
498
  }
357
499
  }
358
- const { start, weights, taps } = outWidth === width ? { start: null, weights: null, taps: 0 } : coefficients(width, outWidth, filter, BITS);
359
- for (let y = 0, o = 0; y < outHeight; y++) {
360
- const row = y * width * 4;
361
- for (let x = 0; x < outWidth; x++, o += 4) {
500
+ const { start, weights, taps } = horizontal ?? { start: null, weights: null, taps: 0 };
501
+ for (let y = 0, o = 0; y < rh; y++) {
502
+ const row = y * cw * 4;
503
+ for (let x = 0; x < region.width; x++, o += 4) {
362
504
  let r = 0, g = 0, b = 0, a = 0;
363
505
  if (!start || !weights) {
364
506
  const s = row + x * 4;
@@ -5,6 +5,7 @@ import { varblockChunks } from "../codecs/jxl/acs-enc.js";
5
5
  import { maskBlurChunks, maskingChunks } from "../codecs/jxl/aq-enc.js";
6
6
  import { coefficientChunks, hfTokenChunks, xybChunks } from "../codecs/jxl/vardct-enc.js";
7
7
  import { groupChunks, postChunks } from "../codecs/jxl/frame.js";
8
+ import { modularGroupChunks, modularWriteChunks } from "../codecs/jxl/modular-lossless.js";
8
9
  const chunkTasks = {
9
10
  'avif-convert': conversionChunks,
10
11
  'avif-tiles': tileChunks,
@@ -16,6 +17,8 @@ const chunkTasks = {
16
17
  'jxl-varblocks': varblockChunks,
17
18
  'jxl-coefficients': coefficientChunks,
18
19
  'jxl-hf-tokens': hfTokenChunks,
20
+ 'jxl-modular-groups': modularGroupChunks,
21
+ 'jxl-modular-write': modularWriteChunks,
19
22
  'jxl-groups': groupChunks,
20
23
  'jxl-post': postChunks,
21
24
  };
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const VERSION = '3.13.2';
1
+ export const VERSION = '3.15.0';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "imagescript",
3
- "version": "3.13.2",
3
+ "version": "3.15.0",
4
4
  "description": "Pure TypeScript image processing that runs everywhere",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",