imagescript 3.10.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.
@@ -16,7 +16,7 @@ following projects, under their own licenses. Each derived file says so in its h
16
16
  | [dav1d](https://code.videolan.org/videolan/dav1d) by VideoLAN, the dav1d authors and Two Orioles | `src/codecs/av1/*` (the AV1 decoder, and the encoder's syntax, contexts and reconstruction mirrored from it: bitstream parsing, symbol decoding, prediction, inverse transforms, loop filters, film grain; the tables in `src/codecs/av1/data.ts`, generated from dav1d by `scripts/av1-tables.c`) | BSD-2-Clause |
17
17
  | [libaom](https://aomedia.googlesource.com/aom/) by the Alliance for Open Media | `src/codecs/av1/msac-enc.ts` (the symbol encoder), `src/codecs/av1/fdct.ts` (the forward Walsh-Hadamard transform), `src/codecs/av1/encode.ts` (the loop filter level from the quantizer), `src/codecs/avif-enc.ts` (the quantizer to qindex table) | BSD-2-Clause |
18
18
  | [libavif](https://github.com/AOMediaCodec/libavif) by Joe Drago | `src/codecs/avif.ts` (YUV to RGB conversion with bilinear chroma upsampling, alpha range expansion and unpremultiplication) | BSD-2-Clause |
19
- | [jxl-oxide](https://github.com/tirr-c/jxl-oxide) by Wonwoo Choi | `src/codecs/jxl/*` (the JPEG XL decoder: bit reader, header and frame header fields, entropy decoding, VarDCT, dequantization matrices, transforms, restoration filters, upsampling, the parsing of patches, splines and noise) | MIT (or Apache-2.0) |
19
+ | [jxl-oxide](https://github.com/tirr-c/jxl-oxide) by Wonwoo Choi | `src/codecs/jxl/*` (the JPEG XL decoder: bit reader, header and frame header fields, entropy decoding, VarDCT, dequantization matrices, transforms, restoration filters, upsampling, the parsing of patches, splines and noise, the ICC profile stream) | MIT (or Apache-2.0) |
20
20
  | [libjxl](https://github.com/libjxl/libjxl) by the JPEG XL Project Authors | `src/codecs/jxl/modular.ts` (Modular decoding: MA trees, predictors, the self-correcting predictor, transforms), `src/codecs/jxl/frame.ts` (the sections of frames, the order of the rendering stages, reference frames), `src/codecs/jxl/blend.ts`, `src/codecs/jxl/color.ts` (XYB to RGB), `src/codecs/jxl/features.ts` (rendering patches, splines and noise), `src/codecs/jxl/fast-lossless.ts` (the lossless encoder: palette detection, YCoCg, the gradient predictor, prefix codes, runs as LZ77, the headers), `src/codecs/jxl/patches-enc.ts` (finding text-like patches, packing them into a reference frame, quantizing it like Modular XYB, the patch dictionary) | BSD-3-Clause |
21
21
  | [JPEG XL conformance](https://github.com/libjxl/conformance) by the JPEG XL Project Authors | `tests/jxl/alpha-*.jxl`, `animation-spline.jxl`, `blendmodes.jxl`, `mul-no-extra-channels.jxl` (test files) | BSD-3-Clause |
22
22
  | [Various smartphones - Android, Blackberry, Windows Phone, and iOS.jpg](https://commons.wikimedia.org/wiki/File:Various_smartphones_-_Android,_Blackberry,_Windows_Phone,_and_iOS.jpg) by Carlos Varela | `tests/targets/external.png` and `external.jpg` (a square of it, scaled to 638x638), the test files made from them (`tests/avif`, `tests/jxl`, `tests/ico`, `tests/hdr`, `tests/qoi`, `tests/tiffs`) and the test targets showing them; `bench/inputs/photo.jpg` (unmodified) | [CC BY 2.0](https://creativecommons.org/licenses/by/2.0/) |
package/dist/animation.js CHANGED
@@ -218,16 +218,17 @@ export class Animation extends Array {
218
218
  }
219
219
  if (type === 'jxl') {
220
220
  const frames = [];
221
- let loopCount = -1;
221
+ let loopCount = -1, icc;
222
222
  for await (const decoded of jxl.frames(data, maxPixels, 'reinhard', threads)) {
223
223
  const frame = new Frame(decoded.width, decoded.height, decoded.duration, 0, 0, Frame.DISPOSAL_BACKGROUND);
224
224
  frame.bitmap = decoded.pixels;
225
225
  frames.push(frame.__orient__(decoded.orientation));
226
226
  loopCount = fromPlays(decoded.loopCount);
227
+ icc = decoded.icc;
227
228
  if (onlyExtractFirstFrame)
228
229
  break;
229
230
  }
230
- return new Animation(frames, frames.length > 1 ? loopCount : -1);
231
+ return new Animation(profile(frames, icc), frames.length > 1 ? loopCount : -1);
231
232
  }
232
233
  if (type)
233
234
  return new Animation([Frame.from(await Image.decode(data, { maxPixels, applyColorProfile }), 0)]);
@@ -184,13 +184,14 @@ export function imageHeader(r) {
184
184
  }
185
185
  return h;
186
186
  }
187
- export function skipICC(r) {
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,
@@ -1,7 +1,7 @@
1
1
  import { startThreads } from "../util/threads.js";
2
2
  import { MAX_PIXELS, checkPixels } from "../util/limits.js";
3
3
  import { BitReader } from "./jxl/bits.js";
4
- import { imageHeader, skipICC } from "./jxl/headers.js";
4
+ import { decodeICC, imageHeader, readICC } from "./jxl/headers.js";
5
5
  import { frameHeader, toc } from "./jxl/frame-header.js";
6
6
  import { decodeFrame } from "./jxl/frame.js";
7
7
  import { ColorOutput } from "./jxl/color.js";
@@ -64,8 +64,8 @@ export async function* frames(data, maxPixels = MAX_PIXELS, toneMapping = 'reinh
64
64
  const r = new BitReader(d);
65
65
  const image = imageHeader(r);
66
66
  checkPixels(image.width, image.height, maxPixels);
67
- if (image.colour.wantICC)
68
- skipICC(r);
67
+ const stream = image.colour.wantICC ? readICC(r) : null;
68
+ const icc = stream && !image.xybEncoded ? decodeICC(stream) ?? undefined : undefined;
69
69
  r.zeroPadToByte();
70
70
  let at = r.bytePosition;
71
71
  if (image.preview) {
@@ -94,7 +94,7 @@ export async function* frames(data, maxPixels = MAX_PIXELS, toneMapping = 'reinh
94
94
  shown++;
95
95
  yield {
96
96
  width: image.width, height: image.height, pixels: state.output.toRGBA(frame.canvas), duration: frame.header.duration * tick,
97
- orientation: image.orientation, loopCount: a ? a.numLoops : 1
97
+ orientation: image.orientation, loopCount: a ? a.numLoops : 1, icc
98
98
  };
99
99
  }
100
100
  if (frame.header.isLast)
@@ -62,7 +62,8 @@ export function startThreads(n) {
62
62
  }
63
63
  else {
64
64
  const { Worker } = builtin('node:worker_threads');
65
- const w = new Worker(new URL(import.meta.url.endsWith('.ts') ? './worker.ts' : './worker.js', import.meta.url));
65
+ const url = import.meta.url;
66
+ const w = new Worker(new URL(url.endsWith('.ts') ? './worker.ts' : './worker.js', url));
66
67
  w.unref();
67
68
  threads.push(thread(m => w.postMessage(m), (message, error) => {
68
69
  w.on('message', m => message(m));
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const VERSION = '3.10.0';
1
+ export const VERSION = '3.11.0';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "imagescript",
3
- "version": "3.10.0",
3
+ "version": "3.11.0",
4
4
  "description": "zero-dependency javascript image manipulation",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",