imagescript 3.9.0 → 3.11.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/README.md +16 -14
- package/THIRD_PARTY_NOTICES.md +1 -1
- package/dist/animation-encoder.js +3 -3
- package/dist/animation.d.ts +2 -0
- package/dist/animation.js +8 -4
- package/dist/codecs/av1/encode.js +149 -44
- package/dist/codecs/avif-enc.js +35 -9
- package/dist/codecs/avif.js +152 -81
- package/dist/codecs/jxl/acs-enc.js +18 -6
- package/dist/codecs/jxl/aq-enc.js +38 -37
- package/dist/codecs/jxl/color.js +14 -3
- package/dist/codecs/jxl/filters.js +44 -34
- package/dist/codecs/jxl/frame.js +218 -37
- package/dist/codecs/jxl/headers.js +208 -1
- package/dist/codecs/jxl/modular.js +40 -29
- package/dist/codecs/jxl/patches-enc.js +15 -8
- package/dist/codecs/jxl/vardct-enc.js +184 -134
- package/dist/codecs/jxl/vardct.js +53 -19
- package/dist/codecs/jxl.js +35 -24
- package/dist/codecs/vp8-enc.js +2 -2
- package/dist/codecs/vp8l.js +94 -62
- package/dist/codecs/webp.js +32 -11
- package/dist/image.d.ts +32 -3
- package/dist/image.js +19 -9
- package/dist/render/svg.js +99 -25
- package/dist/util/threads.js +102 -0
- package/dist/util/worker.js +52 -0
- package/dist/version.js +1 -1
- package/package.json +1 -1
|
@@ -184,13 +184,14 @@ export function imageHeader(r) {
|
|
|
184
184
|
}
|
|
185
185
|
return h;
|
|
186
186
|
}
|
|
187
|
-
export function
|
|
187
|
+
export function readICC(r) {
|
|
188
188
|
const size = r.u64();
|
|
189
189
|
if (size > 1 << 28)
|
|
190
190
|
throw new Error('JPEG XL: ICC profile too large');
|
|
191
191
|
const dec = new EntropyDecoder(r, 41);
|
|
192
192
|
dec.begin(r);
|
|
193
193
|
const kind = (b) => b >= 97 && b <= 122 || b >= 65 && b <= 90 ? 0 : b >= 48 && b <= 57 || b === 46 || b === 44 ? 1 : -1;
|
|
194
|
+
const out = new Uint8Array(size);
|
|
194
195
|
let b1 = 0, b2 = 0;
|
|
195
196
|
for (let i = 0; i < size; i++) {
|
|
196
197
|
let ctx = 0;
|
|
@@ -203,10 +204,216 @@ export function skipICC(r) {
|
|
|
203
204
|
const b = dec.read(r, ctx);
|
|
204
205
|
if (b > 255)
|
|
205
206
|
throw new Error('JPEG XL: invalid ICC profile');
|
|
207
|
+
out[i] = b;
|
|
206
208
|
b2 = b1;
|
|
207
209
|
b1 = b;
|
|
208
210
|
}
|
|
209
211
|
dec.finish();
|
|
212
|
+
return out;
|
|
213
|
+
}
|
|
214
|
+
const COMMON_TAGS = ['rTRC', 'rXYZ', 'cprt', 'wtpt', 'bkpt', 'rXYZ', 'gXYZ', 'bXYZ', 'kXYZ', 'rTRC', 'gTRC', 'bTRC', 'kTRC', 'chad', 'desc', 'chrm', 'dmnd', 'dmdd', 'lumi'];
|
|
215
|
+
const COMMON_DATA = ['XYZ ', 'desc', 'text', 'mluc', 'para', 'curv', 'sf32', 'gbd '];
|
|
216
|
+
export function decodeICC(stream) {
|
|
217
|
+
let at = 0;
|
|
218
|
+
const fail = () => {
|
|
219
|
+
throw new RangeError('invalid ICC stream');
|
|
220
|
+
};
|
|
221
|
+
const varint = (s, pos) => {
|
|
222
|
+
let value = 0;
|
|
223
|
+
for (let shift = 0; shift < 63; shift += 7) {
|
|
224
|
+
if (pos.at >= s.length)
|
|
225
|
+
fail();
|
|
226
|
+
const b = s[pos.at++];
|
|
227
|
+
value += (b & 127) * 2 ** shift;
|
|
228
|
+
if (!(b & 128))
|
|
229
|
+
break;
|
|
230
|
+
}
|
|
231
|
+
return value;
|
|
232
|
+
};
|
|
233
|
+
try {
|
|
234
|
+
const head = { at: 0 };
|
|
235
|
+
const outputSize = varint(stream, head), commandsSize = varint(stream, head);
|
|
236
|
+
at = head.at;
|
|
237
|
+
if (at + commandsSize > stream.length || outputSize > 1 << 28)
|
|
238
|
+
fail();
|
|
239
|
+
const commands = stream.subarray(at, at + commandsSize), cmd = { at: 0 };
|
|
240
|
+
let data = stream.subarray(at + commandsSize);
|
|
241
|
+
const headerSize = Math.min(outputSize, 128);
|
|
242
|
+
if (data.length < headerSize)
|
|
243
|
+
fail();
|
|
244
|
+
const out = new Uint8Array(outputSize);
|
|
245
|
+
let o = 0;
|
|
246
|
+
const push = (...bytes) => pushBytes(bytes);
|
|
247
|
+
const pushBytes = (bytes) => {
|
|
248
|
+
if (o + bytes.length > outputSize)
|
|
249
|
+
fail();
|
|
250
|
+
out.set(bytes, o);
|
|
251
|
+
o += bytes.length;
|
|
252
|
+
};
|
|
253
|
+
const pushText = (t) => pushBytes([...t].map(c => c.charCodeAt(0)));
|
|
254
|
+
const u32 = (v) => [v >>> 24 & 255, v >>> 16 & 255, v >>> 8 & 255, v & 255];
|
|
255
|
+
const take = (n) => {
|
|
256
|
+
if (n > data.length)
|
|
257
|
+
fail();
|
|
258
|
+
const bytes = data.subarray(0, n);
|
|
259
|
+
data = data.subarray(n);
|
|
260
|
+
return bytes;
|
|
261
|
+
};
|
|
262
|
+
const header = take(headerSize);
|
|
263
|
+
const predict = (i) => {
|
|
264
|
+
const h40 = String.fromCharCode(header[40]), h41 = String.fromCharCode(header[41]);
|
|
265
|
+
if (i <= 3)
|
|
266
|
+
return u32(outputSize)[i];
|
|
267
|
+
if (i === 8)
|
|
268
|
+
return 4;
|
|
269
|
+
if (i >= 12 && i <= 23)
|
|
270
|
+
return 'mntrRGB XYZ '.charCodeAt(i - 12);
|
|
271
|
+
if (i >= 36 && i <= 39)
|
|
272
|
+
return 'acsp'.charCodeAt(i - 36);
|
|
273
|
+
if (h40 === 'A' && i >= 41 && i <= 43)
|
|
274
|
+
return 'PPL'.charCodeAt(i - 41);
|
|
275
|
+
if (h40 === 'M' && i >= 41 && i <= 43)
|
|
276
|
+
return 'SFT'.charCodeAt(i - 41);
|
|
277
|
+
if (h40 === 'S' && h41 === 'G' && (i === 42 || i === 43))
|
|
278
|
+
return 'I '.charCodeAt(i - 42);
|
|
279
|
+
if (h40 === 'S' && h41 === 'U' && (i === 42 || i === 43))
|
|
280
|
+
return 'NW'.charCodeAt(i - 42);
|
|
281
|
+
if (i === 70)
|
|
282
|
+
return 246;
|
|
283
|
+
if (i === 71)
|
|
284
|
+
return 214;
|
|
285
|
+
if (i === 73)
|
|
286
|
+
return 1;
|
|
287
|
+
if (i === 78)
|
|
288
|
+
return 211;
|
|
289
|
+
if (i === 79)
|
|
290
|
+
return 45;
|
|
291
|
+
if (i >= 80 && i <= 83)
|
|
292
|
+
return header[4 + i - 80];
|
|
293
|
+
return 0;
|
|
294
|
+
};
|
|
295
|
+
for (let i = 0; i < headerSize; i++)
|
|
296
|
+
push(predict(i) + header[i] & 255);
|
|
297
|
+
if (outputSize <= 128)
|
|
298
|
+
return out;
|
|
299
|
+
const v = varint(commands, cmd);
|
|
300
|
+
if (v > 0) {
|
|
301
|
+
const numTags = v - 1;
|
|
302
|
+
if ((outputSize - 128) / 12 < numTags)
|
|
303
|
+
fail();
|
|
304
|
+
push(...u32(numTags));
|
|
305
|
+
let prevStart = numTags * 12 + 128, prevSize = 0;
|
|
306
|
+
for (;;) {
|
|
307
|
+
if (cmd.at >= commands.length)
|
|
308
|
+
return out.subarray(0, o);
|
|
309
|
+
const command = commands[cmd.at++], code = command & 63;
|
|
310
|
+
if (code === 0)
|
|
311
|
+
break;
|
|
312
|
+
let tag;
|
|
313
|
+
if (code === 1)
|
|
314
|
+
tag = String.fromCharCode(...take(4));
|
|
315
|
+
else if (code <= 20)
|
|
316
|
+
tag = COMMON_TAGS[code - 2];
|
|
317
|
+
else
|
|
318
|
+
return fail();
|
|
319
|
+
const start = command & 64 ? varint(commands, cmd) : prevStart + prevSize;
|
|
320
|
+
const size = command & 128 ? varint(commands, cmd) : ['rXYZ', 'gXYZ', 'bXYZ', 'kXYZ', 'wtpt', 'bkpt', 'lumi'].includes(tag) ? 20 : prevSize;
|
|
321
|
+
if (start + size > outputSize)
|
|
322
|
+
fail();
|
|
323
|
+
prevStart = start;
|
|
324
|
+
prevSize = size;
|
|
325
|
+
pushText(tag);
|
|
326
|
+
push(...u32(start), ...u32(size));
|
|
327
|
+
if (code === 2) {
|
|
328
|
+
pushText('gTRC');
|
|
329
|
+
push(...u32(start), ...u32(size));
|
|
330
|
+
pushText('bTRC');
|
|
331
|
+
push(...u32(start), ...u32(size));
|
|
332
|
+
}
|
|
333
|
+
else if (code === 3) {
|
|
334
|
+
pushText('gXYZ');
|
|
335
|
+
push(...u32(start + size), ...u32(size));
|
|
336
|
+
pushText('bXYZ');
|
|
337
|
+
push(...u32(start + 2 * size), ...u32(size));
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
const shuffle = (bytes, width) => {
|
|
342
|
+
const n = bytes.length, shuffled = new Uint8Array(n);
|
|
343
|
+
if (width === 2) {
|
|
344
|
+
const half = n >> 1, odd = n & 1;
|
|
345
|
+
let k = 0;
|
|
346
|
+
for (let i = 0; i < half; i++) {
|
|
347
|
+
shuffled[k++] = bytes[i];
|
|
348
|
+
shuffled[k++] = bytes[i + half + odd];
|
|
349
|
+
}
|
|
350
|
+
if (odd)
|
|
351
|
+
shuffled[k] = bytes[half];
|
|
352
|
+
}
|
|
353
|
+
else {
|
|
354
|
+
const step = n >> 2, wide = n & 3;
|
|
355
|
+
let k = 0;
|
|
356
|
+
for (let i = 0; i < step; i++) {
|
|
357
|
+
let base = i;
|
|
358
|
+
for (let j = 0; j < wide; j++, base += step + 1)
|
|
359
|
+
shuffled[k++] = bytes[base];
|
|
360
|
+
for (let j = wide; j < 4; j++, base += step)
|
|
361
|
+
shuffled[k++] = bytes[base];
|
|
362
|
+
}
|
|
363
|
+
for (let i = 1; i <= wide; i++)
|
|
364
|
+
shuffled[k++] = bytes[(step + 1) * i - 1];
|
|
365
|
+
}
|
|
366
|
+
return shuffled;
|
|
367
|
+
};
|
|
368
|
+
while (cmd.at < commands.length) {
|
|
369
|
+
const command = commands[cmd.at++];
|
|
370
|
+
if (command === 1)
|
|
371
|
+
pushBytes(take(varint(commands, cmd)));
|
|
372
|
+
else if (command === 2 || command === 3)
|
|
373
|
+
pushBytes(shuffle(take(varint(commands, cmd)), command === 2 ? 2 : 4));
|
|
374
|
+
else if (command === 4) {
|
|
375
|
+
if (cmd.at >= commands.length)
|
|
376
|
+
fail();
|
|
377
|
+
const flags = commands[cmd.at++], width = (flags & 3) + 1, order = flags >> 2 & 3;
|
|
378
|
+
if (width === 3 || order === 3)
|
|
379
|
+
fail();
|
|
380
|
+
const stride = flags & 16 ? varint(commands, cmd) : width;
|
|
381
|
+
if (stride < width || stride * 4 >= o)
|
|
382
|
+
fail();
|
|
383
|
+
const num = varint(commands, cmd), raw = take(num), bytes = width === 1 ? raw : shuffle(raw, width);
|
|
384
|
+
for (let i = 0; i < num; i += width) {
|
|
385
|
+
const prev = [0, 0, 0];
|
|
386
|
+
for (let j = 0; j <= order; j++) {
|
|
387
|
+
const from = o - stride * (j + 1);
|
|
388
|
+
let value = 0;
|
|
389
|
+
for (let k = 0; k < width; k++)
|
|
390
|
+
value = (value << 8 | out[from + k]) >>> 0;
|
|
391
|
+
prev[j] = value;
|
|
392
|
+
}
|
|
393
|
+
const p = order === 0 ? prev[0] : order === 1 ? 2 * prev[0] - prev[1] >>> 0 : 3 * (prev[0] - prev[1]) + prev[2] >>> 0;
|
|
394
|
+
for (let j = 0; j < Math.min(width, num - i); j++)
|
|
395
|
+
push(bytes[i + j] + (p >>> 8 * (width - 1 - j)) & 255);
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
else if (command === 10) {
|
|
399
|
+
pushText('XYZ ');
|
|
400
|
+
push(0, 0, 0, 0);
|
|
401
|
+
pushBytes(take(12));
|
|
402
|
+
}
|
|
403
|
+
else if (command >= 16 && command <= 23) {
|
|
404
|
+
pushText(COMMON_DATA[command - 16]);
|
|
405
|
+
push(0, 0, 0, 0);
|
|
406
|
+
}
|
|
407
|
+
else
|
|
408
|
+
fail();
|
|
409
|
+
}
|
|
410
|
+
return o === outputSize ? out : null;
|
|
411
|
+
}
|
|
412
|
+
catch (e) {
|
|
413
|
+
if (e instanceof RangeError)
|
|
414
|
+
return null;
|
|
415
|
+
throw e;
|
|
416
|
+
}
|
|
210
417
|
}
|
|
211
418
|
const UP2 = [
|
|
212
419
|
-0.01716200, -0.03452303, -0.04022174, -0.02921014, -0.00624645, 0.14111091, 0.28896755, 0.00278718, -0.01610267,
|
|
@@ -263,6 +263,34 @@ function transform(r) {
|
|
|
263
263
|
}
|
|
264
264
|
return t;
|
|
265
265
|
}
|
|
266
|
+
export function rctOrder(rctType) {
|
|
267
|
+
const perm = Math.floor(rctType / 7);
|
|
268
|
+
return [perm % 3, (perm + 1 + Math.floor(perm / 3)) % 3, (perm + 2 - Math.floor(perm / 3)) % 3];
|
|
269
|
+
}
|
|
270
|
+
export function rctSamples([a, b, c], rctType, i0, i1) {
|
|
271
|
+
const type = rctType % 7;
|
|
272
|
+
if (type === 6) {
|
|
273
|
+
for (let i = i0; i < i1; i++) {
|
|
274
|
+
const Y = a[i], Co = b[i], Cg = c[i];
|
|
275
|
+
const tmp = (Y - (Cg >> 1)) | 0, G = (Cg + tmp) | 0, B = (tmp - (Co >> 1)) | 0;
|
|
276
|
+
a[i] = (B + Co) | 0;
|
|
277
|
+
b[i] = G;
|
|
278
|
+
c[i] = B;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
else if (type !== 0) {
|
|
282
|
+
const second = type >> 1, third = type & 1;
|
|
283
|
+
for (let i = i0; i < i1; i++) {
|
|
284
|
+
const first = a[i];
|
|
285
|
+
if (third)
|
|
286
|
+
c[i] = (c[i] + first) | 0;
|
|
287
|
+
if (second === 1)
|
|
288
|
+
b[i] = (b[i] + first) | 0;
|
|
289
|
+
else if (second === 2)
|
|
290
|
+
b[i] = (b[i] + (((first + c[i]) | 0) >> 1)) | 0;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
}
|
|
266
294
|
export class ModularImage {
|
|
267
295
|
channels;
|
|
268
296
|
bitDepth;
|
|
@@ -363,8 +391,8 @@ export class ModularImage {
|
|
|
363
391
|
}
|
|
364
392
|
return out;
|
|
365
393
|
}
|
|
366
|
-
undo() {
|
|
367
|
-
while (this.transforms.length) {
|
|
394
|
+
undo(keep = 0) {
|
|
395
|
+
while (this.transforms.length > keep) {
|
|
368
396
|
const t = this.transforms.pop();
|
|
369
397
|
if (t.id === TR_RCT)
|
|
370
398
|
this.inverseRCT(t);
|
|
@@ -376,34 +404,17 @@ export class ModularImage {
|
|
|
376
404
|
}
|
|
377
405
|
}
|
|
378
406
|
inverseRCT(t) {
|
|
407
|
+
const c = this.channels[t.beginC];
|
|
408
|
+
rctSamples(this.rctChannels(t), t.rctType, 0, c.w * c.h);
|
|
409
|
+
this.permuteRCT(t);
|
|
410
|
+
}
|
|
411
|
+
rctChannels(t) {
|
|
379
412
|
this.checkEqual(t.beginC, t.beginC + 2);
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
if (type === 6) {
|
|
386
|
-
for (let i = 0; i < n; i++) {
|
|
387
|
-
const Y = a[i], Co = b[i], Cg = c[i];
|
|
388
|
-
const tmp = (Y - (Cg >> 1)) | 0, G = (Cg + tmp) | 0, B = (tmp - (Co >> 1)) | 0;
|
|
389
|
-
a[i] = (B + Co) | 0;
|
|
390
|
-
b[i] = G;
|
|
391
|
-
c[i] = B;
|
|
392
|
-
}
|
|
393
|
-
}
|
|
394
|
-
else {
|
|
395
|
-
const second = type >> 1, third = type & 1;
|
|
396
|
-
for (let i = 0; i < n; i++) {
|
|
397
|
-
const first = a[i];
|
|
398
|
-
if (third)
|
|
399
|
-
c[i] = (c[i] + first) | 0;
|
|
400
|
-
if (second === 1)
|
|
401
|
-
b[i] = (b[i] + first) | 0;
|
|
402
|
-
else if (second === 2)
|
|
403
|
-
b[i] = (b[i] + (((first + c[i]) | 0) >> 1)) | 0;
|
|
404
|
-
}
|
|
405
|
-
}
|
|
406
|
-
}
|
|
413
|
+
return [0, 1, 2].map(i => this.channels[t.beginC + i].data);
|
|
414
|
+
}
|
|
415
|
+
permuteRCT(t) {
|
|
416
|
+
const m = t.beginC, ch = this.channels, [c0, c1, c2] = ch.slice(m, m + 3);
|
|
417
|
+
const [o0, o1, o2] = rctOrder(t.rctType).map(o => m + o);
|
|
407
418
|
ch[o0] = c0;
|
|
408
419
|
ch[o1] = c1;
|
|
409
420
|
ch[o2] = c2;
|
|
@@ -110,17 +110,18 @@ function candidates(planes, stride, width, height) {
|
|
|
110
110
|
}
|
|
111
111
|
}
|
|
112
112
|
const visited = new Uint8Array(width * height), found = [];
|
|
113
|
+
let stack = new Int32Array(1024);
|
|
113
114
|
for (let y = 0; y < height; y++) {
|
|
114
115
|
for (let x = 0; x < width; x++) {
|
|
115
116
|
if (isBackground[y * width + x] || visited[y * width + x])
|
|
116
117
|
continue;
|
|
117
|
-
|
|
118
|
-
let minX = x, maxX = x, minY = y, maxY = y, reference = -1, allSimilar = true;
|
|
119
|
-
while (
|
|
120
|
-
const
|
|
121
|
-
if (visited[
|
|
118
|
+
stack[0] = y * width + x;
|
|
119
|
+
let top = 1, minX = x, maxX = x, minY = y, maxY = y, reference = -1, allSimilar = true;
|
|
120
|
+
while (top) {
|
|
121
|
+
const pixel = stack[--top], cy = pixel / width | 0, cx = pixel - cy * width;
|
|
122
|
+
if (visited[pixel])
|
|
122
123
|
continue;
|
|
123
|
-
visited[
|
|
124
|
+
visited[pixel] = 1;
|
|
124
125
|
minX = Math.min(minX, cx);
|
|
125
126
|
maxX = Math.max(maxX, cx);
|
|
126
127
|
minY = Math.min(minY, cy);
|
|
@@ -134,8 +135,14 @@ function candidates(planes, stride, width, height) {
|
|
|
134
135
|
continue;
|
|
135
136
|
const i = ny * width + nx;
|
|
136
137
|
if (!isBackground[i]) {
|
|
137
|
-
if (
|
|
138
|
-
|
|
138
|
+
if (visited[i])
|
|
139
|
+
continue;
|
|
140
|
+
if (top === stack.length) {
|
|
141
|
+
const grown = new Int32Array(2 * top);
|
|
142
|
+
grown.set(stack);
|
|
143
|
+
stack = grown;
|
|
144
|
+
}
|
|
145
|
+
stack[top++] = i;
|
|
139
146
|
}
|
|
140
147
|
else if (reference < 0)
|
|
141
148
|
reference = i;
|