imagescript 4.0.0 → 4.1.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.
@@ -1,6 +1,7 @@
1
1
  import { decompressAsync } from "./zlib.js";
2
2
  import * as jpeg from "./jpeg.js";
3
3
  import { MAX_PIXELS, checkPixels, checkBytes } from "../util/limits.js";
4
+ import { littleEndian } from "../util/alpha.js";
4
5
  const TYPE_SIZES = { 1: 1, 2: 1, 3: 2, 4: 4, 5: 8, 6: 1, 7: 1, 8: 2, 9: 4, 10: 8, 11: 4, 12: 8, 13: 4, 16: 8, 17: 8, 18: 8 };
5
6
  const TAGS = {
6
7
  ImageWidth: 256,
@@ -238,6 +239,10 @@ export async function decode(data, maxPixels = MAX_PIXELS) {
238
239
  }
239
240
  }
240
241
  const sampleLittle = predictor === 3 ? true : little;
242
+ if (!planar && (bps === 8 || bps === 16) && sampleFormat !== 2 && sampleFormat !== 3 && photometric <= 2) {
243
+ rowsToRGBA(raw, width, height, rowBytes, samplesPerPixel, bps === 16 ? (sampleLittle ? 1 : 0) : -1, photometric, alphaChannel, alphaType === 1, pixels);
244
+ return { width, height, pixels, orientation, icc };
245
+ }
241
246
  const readSample = sampleReader(raw, bps, sampleFormat, sampleLittle, sampleFormat === 3 ? floatRange(raw, bps, sampleLittle) : null);
242
247
  const sampleAt = planar
243
248
  ? (x, y, s) => readSample(s * planeBytes + y * rowBytes, x)
@@ -285,6 +290,68 @@ export async function decode(data, maxPixels = MAX_PIXELS) {
285
290
  }
286
291
  return { width, height, pixels, orientation, icc };
287
292
  }
293
+ let UNPREMULTIPLY;
294
+ function unpremultiplyTable() {
295
+ if (!UNPREMULTIPLY) {
296
+ UNPREMULTIPLY = new Uint8Array(65536);
297
+ for (let alpha = 1; alpha < 256; alpha++)
298
+ for (let v = 0; v < 256; v++)
299
+ UNPREMULTIPLY[alpha << 8 | v] = Math.min(255, Math.round(v * 255 / alpha));
300
+ }
301
+ return UNPREMULTIPLY;
302
+ }
303
+ function rowsToRGBA(raw, width, height, rowBytes, samples, high, photometric, alphaChannel, associated, pixels) {
304
+ const size = high < 0 ? 1 : 2, offset = high < 0 ? 0 : high;
305
+ const step = samples * size;
306
+ const rgb = photometric === 2, invert = photometric === 0 ? 255 : 0;
307
+ const g = rgb ? size + offset : offset, b = rgb ? 2 * size + offset : offset, a = alphaChannel * size + offset;
308
+ const table = associated ? unpremultiplyTable() : null;
309
+ const words = littleEndian && pixels.byteOffset % 4 === 0 ? new Uint32Array(pixels.buffer, pixels.byteOffset, width * height) : null;
310
+ for (let y = 0, p = 0; y < height; y++) {
311
+ let i = y * rowBytes + offset;
312
+ if (words && !table) {
313
+ if (alphaChannel < 0) {
314
+ if (rgb)
315
+ for (let x = 0; x < width; x++, i += step)
316
+ words[p++] = (raw[i] | raw[i - offset + g] << 8 | raw[i - offset + b] << 16 | 0xff000000) >>> 0;
317
+ else
318
+ for (let x = 0; x < width; x++, i += step) {
319
+ const v = raw[i] ^ invert;
320
+ words[p++] = (v | v << 8 | v << 16 | 0xff000000) >>> 0;
321
+ }
322
+ }
323
+ else {
324
+ if (rgb)
325
+ for (let x = 0; x < width; x++, i += step)
326
+ words[p++] = (raw[i] | raw[i - offset + g] << 8 | raw[i - offset + b] << 16 | raw[i - offset + a] << 24) >>> 0;
327
+ else
328
+ for (let x = 0; x < width; x++, i += step) {
329
+ const v = raw[i] ^ invert;
330
+ words[p++] = (v | v << 8 | v << 16 | raw[i - offset + a] << 24) >>> 0;
331
+ }
332
+ }
333
+ continue;
334
+ }
335
+ for (let x = 0, o = p * 4; x < width; x++, i += step, o += 4) {
336
+ const r = raw[i] ^ invert;
337
+ pixels[o] = r;
338
+ pixels[o + 1] = rgb ? raw[i - offset + g] : r;
339
+ pixels[o + 2] = rgb ? raw[i - offset + b] : r;
340
+ if (alphaChannel < 0)
341
+ pixels[o + 3] = 0xff;
342
+ else {
343
+ const alpha = pixels[o + 3] = raw[i - offset + a];
344
+ if (table) {
345
+ const row = alpha << 8;
346
+ pixels[o] = table[row | pixels[o]];
347
+ pixels[o + 1] = table[row | pixels[o + 1]];
348
+ pixels[o + 2] = table[row | pixels[o + 2]];
349
+ }
350
+ }
351
+ }
352
+ p += width;
353
+ }
354
+ }
288
355
  function sampleReader(raw, bps, sampleFormat, little, range) {
289
356
  const view = new DataView(raw.buffer, raw.byteOffset, raw.byteLength);
290
357
  const result = { raw: 0, value: 0 };
@@ -375,73 +442,88 @@ async function decompressSegment(bytes, compression, expected) {
375
442
  function lzw(input, expected) {
376
443
  const out = new Uint8Array(expected);
377
444
  const compat = input[0] === 0 && (input[1] & 1) === 1;
378
- const prefix = new Uint16Array(4096);
379
- const suffix = new Uint8Array(4096);
380
- const first = new Uint8Array(4096);
445
+ const early = compat ? 0 : 1;
446
+ const starts = new Int32Array(4096);
381
447
  const lengths = new Uint16Array(4096);
382
- for (let i = 0; i < 256; i++) {
383
- suffix[i] = first[i] = i;
384
- lengths[i] = 1;
385
- }
386
- let codeSize = 9, next = 258, previous = -1;
448
+ let codeSize = 9, mask = 511, limit = 512 - early, next = 258, previous = -1, previousStart = 0;
387
449
  let buffer = 0, bits = 0, position = 0, written = 0;
450
+ const end = input.length;
388
451
  while (written < expected) {
389
- while (bits < codeSize) {
390
- if (position >= input.length)
391
- return out.subarray(0, written);
392
- buffer = compat ? buffer | input[position++] << bits : (buffer << 8 | input[position++]) & 0xffffff;
393
- bits += 8;
452
+ if (bits < codeSize) {
453
+ if (position >= end)
454
+ break;
455
+ if (compat) {
456
+ buffer |= input[position++] << bits;
457
+ bits += 8;
458
+ if (bits < codeSize) {
459
+ if (position >= end)
460
+ break;
461
+ buffer |= input[position++] << bits;
462
+ bits += 8;
463
+ }
464
+ }
465
+ else {
466
+ buffer = (buffer << 8 | input[position++]) & 0xffffff;
467
+ bits += 8;
468
+ if (bits < codeSize) {
469
+ if (position >= end)
470
+ break;
471
+ buffer = (buffer << 8 | input[position++]) & 0xffffff;
472
+ bits += 8;
473
+ }
474
+ }
394
475
  }
395
476
  let code;
396
477
  if (compat) {
397
- code = buffer & (1 << codeSize) - 1;
478
+ code = buffer & mask;
398
479
  buffer >>>= codeSize;
399
480
  }
400
481
  else
401
- code = buffer >> bits - codeSize & (1 << codeSize) - 1;
482
+ code = buffer >> bits - codeSize & mask;
402
483
  bits -= codeSize;
403
- if (code === 256) {
484
+ const start = written;
485
+ if (code < 256)
486
+ out[written++] = code;
487
+ else if (code === 256) {
404
488
  codeSize = 9;
489
+ mask = 511;
490
+ limit = 512 - early;
405
491
  next = 258;
406
492
  previous = -1;
407
493
  continue;
408
494
  }
409
- if (code === 257)
495
+ else if (code === 257 || previous === -1 || code > next)
410
496
  break;
411
- let entry;
412
- if (previous === -1) {
413
- if (code > 255)
414
- break;
415
- entry = code;
416
- }
417
- else if (code < next)
418
- entry = code;
419
- else if (code === next)
420
- entry = previous;
421
- else
422
- break;
423
- const length = lengths[entry];
424
- let c = entry;
425
- for (let i = written + length - 1; i >= written; i--) {
426
- if (i < expected)
427
- out[i] = suffix[c];
428
- c = prefix[c];
497
+ else {
498
+ let from, length;
499
+ if (code === next) {
500
+ from = previousStart;
501
+ length = start - previousStart + 1;
502
+ }
503
+ else {
504
+ from = starts[code];
505
+ length = lengths[code];
506
+ }
507
+ const stop = start + length > expected ? expected : start + length;
508
+ if (length > 16 && from + length <= start)
509
+ out.copyWithin(start, from, from + stop - start);
510
+ else
511
+ for (let i = from; written < stop;)
512
+ out[written++] = out[i++];
513
+ written = start + length;
429
514
  }
430
- written += length;
431
- if (code === next && written < expected)
432
- out[written] = first[previous];
433
- if (code === next)
434
- written++;
435
515
  if (previous !== -1 && next < 4096) {
436
- prefix[next] = previous;
437
- suffix[next] = first[code === next ? previous : code];
438
- first[next] = first[previous];
439
- lengths[next] = lengths[previous] + 1;
516
+ starts[next] = previousStart;
517
+ lengths[next] = start - previousStart + 1;
440
518
  next++;
441
519
  }
442
- if (next + (compat ? 0 : 1) >= 1 << codeSize && codeSize < 12)
520
+ if (next >= limit && codeSize < 12) {
443
521
  codeSize++;
522
+ mask = (1 << codeSize) - 1;
523
+ limit = (1 << codeSize) - early;
524
+ }
444
525
  previous = code;
526
+ previousStart = start;
445
527
  }
446
528
  return out.subarray(0, Math.min(written, expected));
447
529
  }
@@ -302,43 +302,364 @@ export function decode(data, maxPixels = MAX_PIXELS) {
302
302
  return { width, height, pixels: toRGBA(Y, U, V, ys, cs, width, height) };
303
303
  }
304
304
  function readCoefficients(d, probs, type, ctx, first, dcFactor, acFactor, out, offset) {
305
+ const data = d.data, end = d.end;
306
+ let pos = d.pos, value = d.value, range = d.range, bitCount = d.bitCount;
307
+ let bit;
305
308
  let i = first;
306
309
  let p = ((type * 8 + BANDS[i]) * 3 + ctx) * 11;
307
310
  let previousZero = false;
308
311
  for (; i < 16; i++) {
309
- if (!previousZero && !d.read(probs[p]))
310
- return i;
311
- if (!d.read(probs[p + 1])) {
312
+ if (!previousZero) {
313
+ {
314
+ const split = 1 + ((range - 1) * (probs[p]) >> 8), bigSplit = split << 8;
315
+ if (value >= bigSplit) {
316
+ bit = 1;
317
+ range -= split;
318
+ value -= bigSplit;
319
+ }
320
+ else {
321
+ bit = 0;
322
+ range = split;
323
+ }
324
+ if (range < 128) {
325
+ const shift = Math.clz32(range) - 24;
326
+ range <<= shift;
327
+ value <<= shift;
328
+ bitCount += shift;
329
+ if (bitCount >= 8) {
330
+ bitCount -= 8;
331
+ value |= (pos < end ? data[pos] : 0) << bitCount;
332
+ pos++;
333
+ }
334
+ }
335
+ }
336
+ if (!bit)
337
+ break;
338
+ }
339
+ {
340
+ const split = 1 + ((range - 1) * (probs[p + 1]) >> 8), bigSplit = split << 8;
341
+ if (value >= bigSplit) {
342
+ bit = 1;
343
+ range -= split;
344
+ value -= bigSplit;
345
+ }
346
+ else {
347
+ bit = 0;
348
+ range = split;
349
+ }
350
+ if (range < 128) {
351
+ const shift = Math.clz32(range) - 24;
352
+ range <<= shift;
353
+ value <<= shift;
354
+ bitCount += shift;
355
+ if (bitCount >= 8) {
356
+ bitCount -= 8;
357
+ value |= (pos < end ? data[pos] : 0) << bitCount;
358
+ pos++;
359
+ }
360
+ }
361
+ }
362
+ if (!bit) {
312
363
  previousZero = true;
313
364
  p = ((type * 8 + BANDS[i + 1]) * 3) * 11;
314
365
  continue;
315
366
  }
316
367
  let v;
317
- if (!d.read(probs[p + 2]))
368
+ {
369
+ const split = 1 + ((range - 1) * (probs[p + 2]) >> 8), bigSplit = split << 8;
370
+ if (value >= bigSplit) {
371
+ bit = 1;
372
+ range -= split;
373
+ value -= bigSplit;
374
+ }
375
+ else {
376
+ bit = 0;
377
+ range = split;
378
+ }
379
+ if (range < 128) {
380
+ const shift = Math.clz32(range) - 24;
381
+ range <<= shift;
382
+ value <<= shift;
383
+ bitCount += shift;
384
+ if (bitCount >= 8) {
385
+ bitCount -= 8;
386
+ value |= (pos < end ? data[pos] : 0) << bitCount;
387
+ pos++;
388
+ }
389
+ }
390
+ }
391
+ if (!bit)
318
392
  v = 1;
319
- else if (!d.read(probs[p + 3]))
320
- v = d.read(probs[p + 4]) ? 3 + d.read(probs[p + 5]) : 2;
321
393
  else {
322
- let category;
323
- if (!d.read(probs[p + 6]))
324
- category = d.read(probs[p + 7]);
325
- else if (!d.read(probs[p + 8]))
326
- category = 2 + d.read(probs[p + 9]);
327
- else
328
- category = 4 + d.read(probs[p + 10]);
329
- const extra = PCAT[category];
330
- let e = 0;
331
- for (let k = 0; k < extra.length; k++)
332
- e = e << 1 | d.read(extra[k]);
333
- v = CATEGORY_BASE[category] + e;
394
+ {
395
+ const split = 1 + ((range - 1) * (probs[p + 3]) >> 8), bigSplit = split << 8;
396
+ if (value >= bigSplit) {
397
+ bit = 1;
398
+ range -= split;
399
+ value -= bigSplit;
400
+ }
401
+ else {
402
+ bit = 0;
403
+ range = split;
404
+ }
405
+ if (range < 128) {
406
+ const shift = Math.clz32(range) - 24;
407
+ range <<= shift;
408
+ value <<= shift;
409
+ bitCount += shift;
410
+ if (bitCount >= 8) {
411
+ bitCount -= 8;
412
+ value |= (pos < end ? data[pos] : 0) << bitCount;
413
+ pos++;
414
+ }
415
+ }
416
+ }
417
+ if (!bit) {
418
+ {
419
+ const split = 1 + ((range - 1) * (probs[p + 4]) >> 8), bigSplit = split << 8;
420
+ if (value >= bigSplit) {
421
+ bit = 1;
422
+ range -= split;
423
+ value -= bigSplit;
424
+ }
425
+ else {
426
+ bit = 0;
427
+ range = split;
428
+ }
429
+ if (range < 128) {
430
+ const shift = Math.clz32(range) - 24;
431
+ range <<= shift;
432
+ value <<= shift;
433
+ bitCount += shift;
434
+ if (bitCount >= 8) {
435
+ bitCount -= 8;
436
+ value |= (pos < end ? data[pos] : 0) << bitCount;
437
+ pos++;
438
+ }
439
+ }
440
+ }
441
+ if (bit) {
442
+ {
443
+ const split = 1 + ((range - 1) * (probs[p + 5]) >> 8), bigSplit = split << 8;
444
+ if (value >= bigSplit) {
445
+ bit = 1;
446
+ range -= split;
447
+ value -= bigSplit;
448
+ }
449
+ else {
450
+ bit = 0;
451
+ range = split;
452
+ }
453
+ if (range < 128) {
454
+ const shift = Math.clz32(range) - 24;
455
+ range <<= shift;
456
+ value <<= shift;
457
+ bitCount += shift;
458
+ if (bitCount >= 8) {
459
+ bitCount -= 8;
460
+ value |= (pos < end ? data[pos] : 0) << bitCount;
461
+ pos++;
462
+ }
463
+ }
464
+ }
465
+ v = 3 + bit;
466
+ }
467
+ else
468
+ v = 2;
469
+ }
470
+ else {
471
+ let category;
472
+ {
473
+ const split = 1 + ((range - 1) * (probs[p + 6]) >> 8), bigSplit = split << 8;
474
+ if (value >= bigSplit) {
475
+ bit = 1;
476
+ range -= split;
477
+ value -= bigSplit;
478
+ }
479
+ else {
480
+ bit = 0;
481
+ range = split;
482
+ }
483
+ if (range < 128) {
484
+ const shift = Math.clz32(range) - 24;
485
+ range <<= shift;
486
+ value <<= shift;
487
+ bitCount += shift;
488
+ if (bitCount >= 8) {
489
+ bitCount -= 8;
490
+ value |= (pos < end ? data[pos] : 0) << bitCount;
491
+ pos++;
492
+ }
493
+ }
494
+ }
495
+ if (!bit) {
496
+ {
497
+ const split = 1 + ((range - 1) * (probs[p + 7]) >> 8), bigSplit = split << 8;
498
+ if (value >= bigSplit) {
499
+ bit = 1;
500
+ range -= split;
501
+ value -= bigSplit;
502
+ }
503
+ else {
504
+ bit = 0;
505
+ range = split;
506
+ }
507
+ if (range < 128) {
508
+ const shift = Math.clz32(range) - 24;
509
+ range <<= shift;
510
+ value <<= shift;
511
+ bitCount += shift;
512
+ if (bitCount >= 8) {
513
+ bitCount -= 8;
514
+ value |= (pos < end ? data[pos] : 0) << bitCount;
515
+ pos++;
516
+ }
517
+ }
518
+ }
519
+ category = bit;
520
+ }
521
+ else {
522
+ {
523
+ const split = 1 + ((range - 1) * (probs[p + 8]) >> 8), bigSplit = split << 8;
524
+ if (value >= bigSplit) {
525
+ bit = 1;
526
+ range -= split;
527
+ value -= bigSplit;
528
+ }
529
+ else {
530
+ bit = 0;
531
+ range = split;
532
+ }
533
+ if (range < 128) {
534
+ const shift = Math.clz32(range) - 24;
535
+ range <<= shift;
536
+ value <<= shift;
537
+ bitCount += shift;
538
+ if (bitCount >= 8) {
539
+ bitCount -= 8;
540
+ value |= (pos < end ? data[pos] : 0) << bitCount;
541
+ pos++;
542
+ }
543
+ }
544
+ }
545
+ if (!bit) {
546
+ {
547
+ const split = 1 + ((range - 1) * (probs[p + 9]) >> 8), bigSplit = split << 8;
548
+ if (value >= bigSplit) {
549
+ bit = 1;
550
+ range -= split;
551
+ value -= bigSplit;
552
+ }
553
+ else {
554
+ bit = 0;
555
+ range = split;
556
+ }
557
+ if (range < 128) {
558
+ const shift = Math.clz32(range) - 24;
559
+ range <<= shift;
560
+ value <<= shift;
561
+ bitCount += shift;
562
+ if (bitCount >= 8) {
563
+ bitCount -= 8;
564
+ value |= (pos < end ? data[pos] : 0) << bitCount;
565
+ pos++;
566
+ }
567
+ }
568
+ }
569
+ category = 2 + bit;
570
+ }
571
+ else {
572
+ {
573
+ const split = 1 + ((range - 1) * (probs[p + 10]) >> 8), bigSplit = split << 8;
574
+ if (value >= bigSplit) {
575
+ bit = 1;
576
+ range -= split;
577
+ value -= bigSplit;
578
+ }
579
+ else {
580
+ bit = 0;
581
+ range = split;
582
+ }
583
+ if (range < 128) {
584
+ const shift = Math.clz32(range) - 24;
585
+ range <<= shift;
586
+ value <<= shift;
587
+ bitCount += shift;
588
+ if (bitCount >= 8) {
589
+ bitCount -= 8;
590
+ value |= (pos < end ? data[pos] : 0) << bitCount;
591
+ pos++;
592
+ }
593
+ }
594
+ }
595
+ category = 4 + bit;
596
+ }
597
+ }
598
+ const extra = PCAT[category];
599
+ let e = 0;
600
+ for (let k = 0; k < extra.length; k++) {
601
+ {
602
+ const split = 1 + ((range - 1) * (extra[k]) >> 8), bigSplit = split << 8;
603
+ if (value >= bigSplit) {
604
+ bit = 1;
605
+ range -= split;
606
+ value -= bigSplit;
607
+ }
608
+ else {
609
+ bit = 0;
610
+ range = split;
611
+ }
612
+ if (range < 128) {
613
+ const shift = Math.clz32(range) - 24;
614
+ range <<= shift;
615
+ value <<= shift;
616
+ bitCount += shift;
617
+ if (bitCount >= 8) {
618
+ bitCount -= 8;
619
+ value |= (pos < end ? data[pos] : 0) << bitCount;
620
+ pos++;
621
+ }
622
+ }
623
+ }
624
+ e = e << 1 | bit;
625
+ }
626
+ v = CATEGORY_BASE[category] + e;
627
+ }
628
+ }
629
+ {
630
+ const split = 1 + ((range - 1) * (128) >> 8), bigSplit = split << 8;
631
+ if (value >= bigSplit) {
632
+ bit = 1;
633
+ range -= split;
634
+ value -= bigSplit;
635
+ }
636
+ else {
637
+ bit = 0;
638
+ range = split;
639
+ }
640
+ if (range < 128) {
641
+ const shift = Math.clz32(range) - 24;
642
+ range <<= shift;
643
+ value <<= shift;
644
+ bitCount += shift;
645
+ if (bitCount >= 8) {
646
+ bitCount -= 8;
647
+ value |= (pos < end ? data[pos] : 0) << bitCount;
648
+ pos++;
649
+ }
650
+ }
334
651
  }
335
- if (d.read(128))
652
+ if (bit)
336
653
  v = -v;
337
654
  out[offset + ZIGZAG[i]] = v * (i > 0 ? acFactor : dcFactor);
338
655
  previousZero = false;
339
656
  p = ((type * 8 + BANDS[i + 1]) * 3 + (v === 1 || v === -1 ? 1 : 2)) * 11;
340
657
  }
341
- return 16;
658
+ d.pos = pos;
659
+ d.value = value;
660
+ d.range = range;
661
+ d.bitCount = bitCount;
662
+ return i;
342
663
  }
343
664
  function inverseWHT(c, offset, count) {
344
665
  const out = new Int32Array(16);