imagescript 3.6.0 → 3.7.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,50 +1,5 @@
1
- /*!
2
- * Lossless JPEG XL encoding, ported from libjxl's fast lossless encoder (lib/jxl/enc_fast_lossless.cc, what cjxl -e 1
3
- * uses) for 8-bit samples: Modular with the gradient predictor, YCoCg or a palette, prefix codes and runs of zero
4
- * residuals as LZ77 copies; with libjxl's image header (lib/jxl/headers.cc, fields.cc). Copyright (c) the JPEG XL
5
- * Project Authors, BSD-3-Clause License (see THIRD_PARTY_NOTICES.md)
6
- */
7
- class BitWriter {
8
- data = new Uint8Array(256);
9
- length = 0;
10
- buf = 0;
11
- bits = 0;
12
- write(count, v) {
13
- if (count > 24) {
14
- this.write(16, v & 0xffff);
15
- this.write(count - 16, Math.floor(v / 65536));
16
- return;
17
- }
18
- if (this.length + 4 > this.data.length) {
19
- const data = new Uint8Array(this.data.length * 2);
20
- data.set(this.data);
21
- this.data = data;
22
- }
23
- this.buf |= v << this.bits;
24
- this.bits += count;
25
- while (this.bits >= 8) {
26
- this.data[this.length++] = this.buf & 0xff;
27
- this.buf >>>= 8;
28
- this.bits -= 8;
29
- }
30
- }
31
- zeroPadToByte() {
32
- if (this.bits)
33
- this.write(8 - this.bits, 0);
34
- }
35
- get size() {
36
- return this.length * 8 + this.bits;
37
- }
38
- append(w) {
39
- for (let i = 0; i < w.length; i++)
40
- this.write(8, w.data[i]);
41
- if (w.bits)
42
- this.write(w.bits, w.buf);
43
- }
44
- bytes() {
45
- return this.data.subarray(0, this.length);
46
- }
47
- }
1
+ import { BitWriter } from "./bit-writer.js";
2
+ import { imageHeader } from "./headers-enc.js";
48
3
  const NUM_RAW_SYMBOLS = 19, NUM_LZ77 = 33, LZ77_CACHE_SIZE = 32, LZ77_MIN_LENGTH = 7;
49
4
  const MAX_NUM_SYMBOLS = NUM_LZ77;
50
5
  const MIN_RAW_LENGTH = new Uint8Array(12), MAX_RAW_LENGTH = new Uint8Array([7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 10]);
@@ -718,66 +673,6 @@ export function encode(px, width, height, numChans, chunkSize = 16) {
718
673
  }
719
674
  return w.bytes().slice();
720
675
  }
721
- function u32(w, value, dists) {
722
- for (let s = 0; s < dists.length; s++) {
723
- const [bits, offset] = dists[s];
724
- if (value >= offset && value - offset < 2 ** bits) {
725
- w.write(2, s);
726
- w.write(bits, value - offset);
727
- return;
728
- }
729
- }
730
- throw new RangeError('JPEG XL: dimensions out of range');
731
- }
732
- const RATIOS = [[1, 1], [12, 10], [4, 3], [3, 2], [16, 9], [5, 4], [2, 1]];
733
- function imageHeader(w, width, height, numChans) {
734
- w.write(16, 0x0aff);
735
- let ratio = 0;
736
- for (let r = 1; r < 8 && !ratio; r++)
737
- if (width === Math.floor(height * RATIOS[r - 1][0] / RATIOS[r - 1][1]))
738
- ratio = r;
739
- const small = height <= 256 && height % 8 === 0 && (ratio !== 0 || (width <= 256 && width % 8 === 0));
740
- const dists = [[9, 1], [13, 1], [18, 1], [30, 1]];
741
- w.write(1, +small);
742
- if (small)
743
- w.write(5, height / 8 - 1);
744
- else
745
- u32(w, height, dists);
746
- w.write(3, ratio);
747
- if (!ratio) {
748
- if (small)
749
- w.write(5, width / 8 - 1);
750
- else
751
- u32(w, width, dists);
752
- }
753
- w.write(1, 0);
754
- w.write(1, 0);
755
- w.write(1, 0);
756
- w.write(2, 0b00);
757
- w.write(1, 1);
758
- if (numChans === 2 || numChans === 4) {
759
- w.write(2, 0b01);
760
- w.write(1, 1);
761
- }
762
- else
763
- w.write(2, 0b00);
764
- w.write(1, 0);
765
- if (numChans > 2)
766
- w.write(1, 1);
767
- else {
768
- w.write(1, 0);
769
- w.write(1, 0);
770
- w.write(2, 1);
771
- w.write(2, 1);
772
- w.write(1, 0);
773
- w.write(2, 0b10);
774
- w.write(4, 11);
775
- w.write(2, 1);
776
- }
777
- w.write(2, 0b00);
778
- w.write(1, 1);
779
- w.zeroPadToByte();
780
- }
781
676
  function frameHeader(w, alpha) {
782
677
  w.write(1, 0);
783
678
  w.write(2, 0b00);