@chr33s/pdf-upng 5.0.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/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2017 Photopea
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,112 @@
1
+ # @chr33s/pdf-upng
2
+
3
+ > Small, fast, and modernized PNG/APNG encoder and decoder for the `@chr33s/pdf` ecosystem.
4
+
5
+ This package wraps the excellent [`UPNG.js`](https://github.com/photopea/UPNG.js) library and keeps it maintained and
6
+ published for `@chr33s/pdf` and related tools. Key differences from upstream distributions include:
7
+
8
+ - native ES module output targeting NodeNext (Node.js 18+ or a modern bundler required),
9
+ - TypeScript source with generated declaration files bundled in the package, and
10
+ - continuous maintenance inside the [`chr33s/pdf`](https://github.com/chr33s/pdf) monorepo alongside other PDF
11
+ dependencies.
12
+
13
+ ## Example of `UPNG.toRGBA8`
14
+ ```ts
15
+ import UPNG from "@chr33s/pdf-upng";
16
+
17
+ const pngImage = UPNG.decode(/* Uint8Array containing bytes of PNG image */);
18
+
19
+ // `pixels` is a 1D array (in RGBA order) of decoded pixel data
20
+ const pixels = UPNG.toRGBA8(pngImage)[0];
21
+ ```
22
+
23
+ ## Installation
24
+
25
+ ```bash
26
+ npm install @chr33s/pdf-upng
27
+ ```
28
+
29
+ Package consumers should run on Node.js 18+ or enable NodeNext-compatible module resolution in their bundler configuration.
30
+
31
+
32
+ ## Encoder
33
+
34
+ UPNG.js supports APNG and the interface expects "frames". Regular PNG is just a single-frame animation (single-item array).
35
+
36
+ #### `UPNG.encode(imgs, w, h, cnum, [dels])`
37
+ * `imgs`: array of frames. A frame is an ArrayBuffer containing the pixel data (RGBA, 8 bits per channel)
38
+ * `w`, `h` : width and height of the image
39
+ * `cnum`: number of colors in the result; 0: all colors (lossless PNG)
40
+ * `dels`: array of millisecond delays for each frame (only when 2 or more frames)
41
+ * returns an ArrayBuffer with binary data of a PNG file
42
+
43
+ UPNG.js can do a lossy minification of PNG files, similar to [TinyPNG](https://tinypng.com/) and other tools. It performed quantization with [k-means algorithm](https://en.wikipedia.org/wiki/K-means_clustering) in the past, but now we use [K-d trees](https://en.wikipedia.org/wiki/K-d_tree).
44
+
45
+ Lossy compression is allowed by the last parameter `cnum`. Set it to zero for a lossless compression, or write the number of allowed colors in the image. Smaller values produce smaller files. **Or just use 0 for lossless / 256 for lossy.**
46
+
47
+ // Read RGBA from canvas and encode with UPNG
48
+ let dta = ctx.getImageData(0,0,200,300).data; // ctx is Context2D of a Canvas
49
+ // dta = new Uint8Array(200 * 300 * 4); // or generate pixels manually
50
+ let png = UPNG.encode([dta.buffer], 200, 300, 0); console.log(new Uint8Array(png));
51
+
52
+ #### `UPNG.encodeLL(imgs, w, h, cc, ac, depth, [dels])` - low-level encode
53
+ * `imgs`: array of frames. A frame is an ArrayBuffer containing the pixel data (corresponding to following parameters)
54
+ * `w`, `h` : width and height of the image
55
+ * `cc`, `ac`: number of color channels (1 or 3) and alpha channels (0 or 1)
56
+ * `depth`: bit depth of pixel data (1, 2, 4, 8, 16)
57
+ * `dels`: array of millisecond delays for each frame (only when 2 or more frames)
58
+ * returns an ArrayBuffer with binary data of a PNG file
59
+
60
+ This function does not do any optimizations, it just stores what you give it. There are two cases when it is useful:
61
+ * saving 16-bit colors (note, that PNG is big-endian, unlike Uint16Array in JS)
62
+ * your image is too large, and "expanding" to 8-bit RGBA would use too much memory (e.g. 4-bit grayscale 50,000 x 50,000 = 1.25 GB, 8-bit RGBA would be 10 GB)
63
+
64
+ ## Decoder
65
+
66
+ Supports all color types (including Grayscale and Palettes), all channel depths (1, 2, 4, 8, 16), interlaced images etc. Opens PNGs which other libraries can not open (tested with [PngSuite](http://www.schaik.com/pngsuite/)).
67
+
68
+ #### `UPNG.decode(buffer)`
69
+ * `buffer`: ArrayBuffer containing the PNG file
70
+ * returns an image object with following properties:
71
+ * * `width`: the width of the image
72
+ * * `height`: the height of the image
73
+ * * `depth`: number of bits per channel
74
+ * * `ctype`: color type of the file (Truecolor, Grayscale, Palette ...)
75
+ * * `frames`: additional info about frames (frame delays etc.)
76
+ * * `tabs`: additional chunks of the PNG file
77
+ * * `data`: pixel data of the image
78
+
79
+ PNG files may have a various number of channels and a various color depth. The interpretation of `data` depends on the current color type and color depth (see the [PNG specification](https://www.w3.org/TR/PNG/)).
80
+
81
+ #### `UPNG.toRGBA8(img)`
82
+ * `img`: PNG image object (returned by UPNG.decode())
83
+ * returns an array of frames. A frame is ArrayBuffer of the image in RGBA format, 8 bits per channel.
84
+
85
+ ### Example
86
+ let img = UPNG.decode(buff); // put ArrayBuffer of the PNG file into UPNG.decode
87
+ let rgba = UPNG.toRGBA8(img)[0]; // UPNG.toRGBA8 returns array of frames, size: width * height * 4 bytes.
88
+
89
+ PNG format uses the Inflate algorithm. Right now, UPNG.js calls [@chr33s/pdf-common](https://github.com/chr33s/pdf/tree/main/packages/common) for the Inflate and Deflate method.
90
+
91
+ ## Quantizer
92
+
93
+ UPNG.js contains a very good Quantizer of 4-component 8-bit vectors (i.e. pixels). It can be used to generate nice color palettes (e.g. Photopea uses UPNG.js to make palettes for GIF images).
94
+
95
+ Quantization consists of two important steps: Finding a nice palette and Finding the closest color in the palette for each sample (non-trivial for large palettes). UPNG perfroms both steps.
96
+
97
+ let res = UPNG.quantize(data, psize);
98
+
99
+ * `data`: ArrayBuffer of samples (byte length is a multiple of four)
100
+ * `psize` : Palette size (how many colors you want to have)
101
+
102
+ The result object "res" has following properties:
103
+
104
+ * `abuf`: ArrayBuffer corresponding to `data`, where colors are remapped by a palette
105
+ * `inds`: Uint8Array : the index of a color for each sample (only when `psize`<=256)
106
+ * `plte`: Array : the Palette - a list of colors, `plte[i].est.q` and `plte[i].est.rgba` is the color value
107
+
108
+ ### FAQ
109
+
110
+ - To get one common palette for multiple images (e.g. frames of the animation), concatenate them into one array `data`.
111
+ - When working with less than four components, set the remaining components to a constant value (e.g. to zero)
112
+ - When working with transparency, premultiply color components by transparency (otherwise, rgba(1,1,1,0) would be closer to rgba(1,1,1,1) than to rgba(0,0,0,0) - transparent mapped to white instead of transparent)
package/dist/bin.d.ts ADDED
@@ -0,0 +1,12 @@
1
+ export declare class Bin {
2
+ static nextZero(data: Uint8Array, p: number): number;
3
+ static readUshort(buff: Uint8Array, p: number): number;
4
+ static writeUshort(buff: Uint8Array, p: number, n: number): void;
5
+ static readUint(buff: Uint8Array, p: number): number;
6
+ static writeUint(buff: Uint8Array, p: number, n: number): void;
7
+ static readASCII(buff: Uint8Array, p: number, l: number): string;
8
+ static writeASCII(data: Uint8Array, p: number, s: string): void;
9
+ static readBytes(buff: Uint8Array, p: number, l: number): number[];
10
+ static pad(n: string): string;
11
+ static readUTF8(buff: Uint8Array, p: number, l: number): string;
12
+ }
package/dist/bin.js ADDED
@@ -0,0 +1,56 @@
1
+ export class Bin {
2
+ static nextZero(data, p) {
3
+ while (data[p] != 0)
4
+ p++;
5
+ return p;
6
+ }
7
+ static readUshort(buff, p) {
8
+ return (buff[p] << 8) | buff[p + 1];
9
+ }
10
+ static writeUshort(buff, p, n) {
11
+ buff[p] = (n >> 8) & 255;
12
+ buff[p + 1] = n & 255;
13
+ }
14
+ static readUint(buff, p) {
15
+ return buff[p] * (256 * 256 * 256) + ((buff[p + 1] << 16) | (buff[p + 2] << 8) | buff[p + 3]);
16
+ }
17
+ static writeUint(buff, p, n) {
18
+ buff[p] = (n >> 24) & 255;
19
+ buff[p + 1] = (n >> 16) & 255;
20
+ buff[p + 2] = (n >> 8) & 255;
21
+ buff[p + 3] = n & 255;
22
+ }
23
+ static readASCII(buff, p, l) {
24
+ let s = "";
25
+ for (let i = 0; i < l; i++)
26
+ s += String.fromCharCode(buff[p + i]);
27
+ return s;
28
+ }
29
+ static writeASCII(data, p, s) {
30
+ for (let i = 0; i < s.length; i++)
31
+ data[p + i] = s.charCodeAt(i);
32
+ }
33
+ static readBytes(buff, p, l) {
34
+ const arr = [];
35
+ for (let i = 0; i < l; i++)
36
+ arr.push(buff[p + i]);
37
+ return arr;
38
+ }
39
+ static pad(n) {
40
+ return n.length < 2 ? "0" + n : n;
41
+ }
42
+ static readUTF8(buff, p, l) {
43
+ let s = "";
44
+ let ns;
45
+ for (let i = 0; i < l; i++)
46
+ s += "%" + Bin.pad(buff[p + i].toString(16));
47
+ try {
48
+ ns = decodeURIComponent(s);
49
+ }
50
+ catch {
51
+ return Bin.readASCII(buff, p, l);
52
+ }
53
+ return ns;
54
+ }
55
+ }
56
+ //# sourceMappingURL=bin.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bin.js","sourceRoot":"","sources":["../src/bin.ts"],"names":[],"mappings":"AAAA,MAAM,OAAO,GAAG;IACd,MAAM,CAAC,QAAQ,CAAC,IAAgB,EAAE,CAAS;QACzC,OAAO,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC;YAAE,CAAC,EAAE,CAAC;QACzB,O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AAC,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,CAAC;QACzE,IAAI,CAAC;YACH,EAAE,GAAG,kBAAkB,CAAC,CAAC,CAAC,CAAC;QAC7B,CAAC;QAAC,MAAM,CAAC;YACP,OAAO,GAAG,CAAC,SAAS,CAAC,IAAI,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC;QACnC,CAAC;QACD,OAAO,EAAE,CAAC;IACZ,CAAC;CACF"}
package/dist/crc.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ export declare class CRC {
2
+ static table: Uint32Array;
3
+ static update(c: number, buf: Uint8Array, off: number, len: number): number;
4
+ static crc(b: Uint8Array, o: number, l: number): number;
5
+ }
package/dist/crc.js ADDED
@@ -0,0 +1,25 @@
1
+ export class CRC {
2
+ static table = (function () {
3
+ const tab = new Uint32Array(256);
4
+ for (let n = 0; n < 256; n++) {
5
+ let c = n;
6
+ for (let k = 0; k < 8; k++) {
7
+ if (c & 1)
8
+ c = 0xedb88320 ^ (c >>> 1);
9
+ else
10
+ c = c >>> 1;
11
+ }
12
+ tab[n] = c;
13
+ }
14
+ return tab;
15
+ })();
16
+ static update(c, buf, off, len) {
17
+ for (let i = 0; i < len; i++)
18
+ c = CRC.table[(c ^ buf[off + i]) & 0xff] ^ (c >>> 8);
19
+ return c;
20
+ }
21
+ static crc(b, o, l) {
22
+ return CRC.update(0xffffffff, b, o, l) ^ 0xffffffff;
23
+ }
24
+ }
25
+ //# sourceMappingURL=crc.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"crc.js","sourceRoot":"","sources":["../src/crc.ts"],"names":[],"mappings":"AAAA,MAAM,OAAO,GAAG;IACd,MAAM,CAAC,KAAK,GAAgB,CAAC;QAC3B,MAAM,GAAG,GAAG,IAAI,WAAW,CAAC,GAAG,CAAC,CAAC;QACjC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,GAAG,EAAE,CAAC,EAAE,EAAE,CAAC;YAC7B,IAAI,CAAC,GAAG,CAAC,CAAC;YACV,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;gBAC3B,IAAI,CAAC,GAAG,CAAC;oBAAE,CAAC,GAAG,UAAU,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC;;oBACjC,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;YACnB,CAAC;YACD,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;QACb,CAAC;QACD,OAAO,GAAG,CAAC;IACb,CAAC,CAAC,EAAE,CAAC;IAEL,MAAM,CAAC,MAAM,CAAC,CAAS,EAAE,GAAe,EAAE,GAAW,EAAE,GAAW;QAChE,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,GAAG,EAAE,CAAC,EAAE;YAAE,CAAC,GAAG,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC;QACnF,OAAO,CAAC,CAAC;IACX,CAAC;IAED,MAAM,CAAC,GAAG,CAAC,CAAa,EAAE,CAAS,EAAE,CAAS;QAC5C,OAAO,GAAG,CAAC,MAAM,CAAC,UAAU,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,GAAG,UAAU,CAAC;IACtD,CAAC"}
@@ -0,0 +1,3 @@
1
+ import { UPNG } from "./upng.js";
2
+ export default UPNG;
3
+ export * from "./upng.js";
package/dist/index.js ADDED
@@ -0,0 +1,4 @@
1
+ import { UPNG } from "./upng.js";
2
+ export default UPNG;
3
+ export * from "./upng.js";
4
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,eAAe,IAAI,CAAC;AACpB,cAAc,WAAW,CAAC"}
@@ -0,0 +1,39 @@
1
+ export declare class Inflator {
2
+ static readonly D: InflatorTables;
3
+ static inflateRaw(o: Uint8Array, j?: Uint8Array): Uint8Array<ArrayBufferLike>;
4
+ static C_inner(o: number[], j: number): void;
5
+ static t_inner(o: number[], j: number, I: Uint16Array): void;
6
+ static H(o: Uint8Array, j: number): Uint8Array<ArrayBufferLike>;
7
+ static B(o: Uint16Array, j: number, I: number, A: Uint8Array, r: number, i: number[]): number;
8
+ static d(o: number[], j: number, I: number, A: number[]): number;
9
+ }
10
+ interface InflatorTables {
11
+ m: Uint16Array;
12
+ v: Uint16Array;
13
+ d: number[];
14
+ o: number[];
15
+ z: number[];
16
+ B: Uint16Array;
17
+ p: number[];
18
+ w: number[];
19
+ h: Uint32Array;
20
+ g: Uint16Array;
21
+ s: number[];
22
+ A: Uint16Array;
23
+ t: number[];
24
+ k: Uint16Array;
25
+ c: number[];
26
+ a: number[];
27
+ n: Uint16Array;
28
+ e: number[];
29
+ C: Uint16Array;
30
+ b: number[];
31
+ i: Uint16Array;
32
+ r: Uint32Array;
33
+ f: Uint32Array;
34
+ l: Uint32Array;
35
+ u: Uint32Array;
36
+ q: Uint16Array;
37
+ j: Uint16Array;
38
+ }
39
+ export {};
@@ -0,0 +1,368 @@
1
+ const createInflatorTables = () => {
2
+ const Uint16Ctor = Uint16Array;
3
+ const Uint32Ctor = Uint32Array;
4
+ const tables = {
5
+ m: new Uint16Ctor(16),
6
+ v: new Uint16Ctor(16),
7
+ d: [16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15],
8
+ o: [
9
+ 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, 67, 83, 99, 115, 131,
10
+ 163, 195, 227, 258, 999, 999, 999,
11
+ ],
12
+ z: [
13
+ 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 0, 0,
14
+ 0,
15
+ ],
16
+ B: new Uint16Ctor(32),
17
+ p: [
18
+ 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, 769, 1025, 1537,
19
+ 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577, 65535, 65535,
20
+ ],
21
+ w: [
22
+ 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13,
23
+ 13, 0, 0,
24
+ ],
25
+ h: new Uint32Ctor(32),
26
+ g: new Uint16Ctor(512),
27
+ s: [],
28
+ A: new Uint16Ctor(32),
29
+ t: [],
30
+ k: new Uint16Ctor(32768),
31
+ c: [],
32
+ a: [],
33
+ n: new Uint16Ctor(32768),
34
+ e: [],
35
+ C: new Uint16Ctor(512),
36
+ b: [],
37
+ i: new Uint16Ctor(1 << 15),
38
+ r: new Uint32Ctor(286),
39
+ f: new Uint32Ctor(30),
40
+ l: new Uint32Ctor(19),
41
+ u: new Uint32Ctor(15e3),
42
+ q: new Uint16Ctor(1 << 16),
43
+ j: new Uint16Ctor(1 << 15),
44
+ };
45
+ const assignCanonicalCodes = (values, maxBits) => {
46
+ const len = values.length;
47
+ const counts = tables.v;
48
+ let acc = 0;
49
+ for (let idx = 0; idx <= maxBits; idx++)
50
+ counts[idx] = 0;
51
+ for (let idx = 1; idx < len; idx += 2)
52
+ counts[values[idx]]++;
53
+ const starts = tables.m;
54
+ counts[0] = 0;
55
+ for (let bit = 1; bit <= maxBits; bit++) {
56
+ acc = (acc + counts[bit - 1]) << 1;
57
+ starts[bit] = acc;
58
+ }
59
+ for (let idx = 0; idx < len; idx += 2) {
60
+ const bitLength = values[idx + 1];
61
+ if (bitLength != 0) {
62
+ values[idx] = starts[bitLength];
63
+ starts[bitLength]++;
64
+ }
65
+ }
66
+ };
67
+ const buildLookupTable = (values, bits, target) => {
68
+ const len = values.length;
69
+ const bitReverse = tables.i;
70
+ for (let idx = 0; idx < len; idx += 2) {
71
+ const bitLength = values[idx + 1];
72
+ if (bitLength != 0) {
73
+ const symbol = idx >> 1;
74
+ const packed = (symbol << 4) | bitLength;
75
+ const remaining = bits - bitLength;
76
+ const start = values[idx] << remaining;
77
+ const end = start + (1 << remaining);
78
+ for (let code = start; code < end; code++) {
79
+ const mapped = bitReverse[code] >>> (15 - bits);
80
+ target[mapped] = packed;
81
+ }
82
+ }
83
+ }
84
+ };
85
+ const remapCodes = (values, bits) => {
86
+ const bitReverse = tables.i;
87
+ const shift = 15 - bits;
88
+ for (let idx = 0; idx < values.length; idx += 2) {
89
+ const mapped = values[idx] << (bits - values[idx + 1]);
90
+ values[idx] = bitReverse[mapped] >>> shift;
91
+ }
92
+ };
93
+ const appendPairs = (target, count, bitLength) => {
94
+ for (let idx = 0; idx < count; idx++) {
95
+ target.push(0, bitLength);
96
+ }
97
+ };
98
+ const tableSize = 1 << 15;
99
+ for (let idx = 0; idx < tableSize; idx++) {
100
+ let value = idx;
101
+ value = ((value & 2863311530) >>> 1) | ((value & 1431655765) << 1);
102
+ value = ((value & 3435973836) >>> 2) | ((value & 858993459) << 2);
103
+ value = ((value & 4042322160) >>> 4) | ((value & 252645135) << 4);
104
+ value = ((value & 4278255360) >>> 8) | ((value & 16711935) << 8);
105
+ tables.i[idx] = ((value >>> 16) | (value << 16)) >>> 17;
106
+ }
107
+ for (let idx = 0; idx < 32; idx++) {
108
+ tables.B[idx] = (tables.o[idx] << 3) | tables.z[idx];
109
+ tables.h[idx] = (tables.p[idx] << 4) | tables.w[idx];
110
+ }
111
+ appendPairs(tables.s, 144, 8);
112
+ appendPairs(tables.s, 255 - 143, 9);
113
+ appendPairs(tables.s, 279 - 255, 7);
114
+ appendPairs(tables.s, 287 - 279, 8);
115
+ assignCanonicalCodes(tables.s, 9);
116
+ buildLookupTable(tables.s, 9, tables.g);
117
+ remapCodes(tables.s, 9);
118
+ appendPairs(tables.t, 32, 5);
119
+ assignCanonicalCodes(tables.t, 5);
120
+ buildLookupTable(tables.t, 5, tables.A);
121
+ remapCodes(tables.t, 5);
122
+ appendPairs(tables.b, 19, 0);
123
+ appendPairs(tables.c, 286, 0);
124
+ appendPairs(tables.e, 30, 0);
125
+ appendPairs(tables.a, 320, 0);
126
+ return tables;
127
+ };
128
+ export class Inflator {
129
+ static D = createInflatorTables();
130
+ static inflateRaw(o, j) {
131
+ const D = Inflator.D;
132
+ function F(o, j, I) {
133
+ return ((o[j >>> 3] | (o[(j >>> 3) + 1] << 8)) >>> (j & 7)) & ((1 << I) - 1);
134
+ }
135
+ function s(o, j, I) {
136
+ return (((o[j >>> 3] | (o[(j >>> 3) + 1] << 8) | (o[(j >>> 3) + 2] << 16)) >>> (j & 7)) &
137
+ ((1 << I) - 1));
138
+ }
139
+ function w(o, j) {
140
+ return (o[j >>> 3] | (o[(j >>> 3) + 1] << 8) | (o[(j >>> 3) + 2] << 16)) >>> (j & 7);
141
+ }
142
+ const I = Uint8Array;
143
+ let r = 0;
144
+ let i = 0;
145
+ let y = 0;
146
+ let G = 0;
147
+ let f = 0;
148
+ let a = 0;
149
+ let k = 0;
150
+ let N = 0;
151
+ let x = 0;
152
+ let P;
153
+ let J;
154
+ if (o[0] == 3 && o[1] == 0)
155
+ return j ? j : new I(0);
156
+ const A = j == null;
157
+ if (A)
158
+ j = new I((o.length >>> 2) << 3);
159
+ let j_arr = j;
160
+ while (r == 0) {
161
+ r = s(o, x, 1);
162
+ i = s(o, x + 1, 2);
163
+ x += 3;
164
+ if (i == 0) {
165
+ if ((x & 7) != 0)
166
+ x += 8 - (x & 7);
167
+ const K = (x >>> 3) + 4;
168
+ const m = o[K - 4] | (o[K - 3] << 8);
169
+ if (A)
170
+ j_arr = Inflator.H(j_arr, N + m);
171
+ j_arr.set(new I(o.buffer, o.byteOffset + K, m), N);
172
+ x = (K + m) << 3;
173
+ N += m;
174
+ continue;
175
+ }
176
+ if (A)
177
+ j_arr = Inflator.H(j_arr, N + (1 << 17));
178
+ if (i == 1) {
179
+ P = D.g;
180
+ J = D.A;
181
+ a = (1 << 9) - 1;
182
+ k = (1 << 5) - 1;
183
+ }
184
+ if (i == 2) {
185
+ y = F(o, x, 5) + 257;
186
+ G = F(o, x + 5, 5) + 1;
187
+ f = F(o, x + 10, 4) + 4;
188
+ x += 14;
189
+ let Q = 1;
190
+ for (let p = 0; p < 38; p += 2) {
191
+ D.b[p] = 0;
192
+ D.b[p + 1] = 0;
193
+ }
194
+ for (let p = 0; p < f; p++) {
195
+ const l = F(o, x + p * 3, 3);
196
+ D.b[(D.d[p] << 1) + 1] = l;
197
+ if (l > Q)
198
+ Q = l;
199
+ }
200
+ x += 3 * f;
201
+ Inflator.C_inner(D.b, Q);
202
+ Inflator.t_inner(D.b, Q, D.C);
203
+ P = D.k;
204
+ J = D.n;
205
+ x = Inflator.B(D.C, (1 << Q) - 1, y + G, o, x, D.a);
206
+ const u = Inflator.d(D.a, 0, y, D.c);
207
+ a = (1 << u) - 1;
208
+ const n = Inflator.d(D.a, y, G, D.e);
209
+ k = (1 << n) - 1;
210
+ Inflator.C_inner(D.c, u);
211
+ Inflator.t_inner(D.c, u, P);
212
+ Inflator.C_inner(D.e, n);
213
+ Inflator.t_inner(D.e, n, J);
214
+ }
215
+ for (;;) {
216
+ const h = P[w(o, x) & a];
217
+ x += h & 15;
218
+ const L = h >>> 4;
219
+ if (L >>> 8 == 0) {
220
+ j_arr[N++] = L;
221
+ }
222
+ else if (L == 256) {
223
+ break;
224
+ }
225
+ else {
226
+ let M = N + L - 254;
227
+ if (L > 264) {
228
+ const z = D.B[L - 257];
229
+ M = N + (z >>> 3) + F(o, x, z & 7);
230
+ x += z & 7;
231
+ }
232
+ const e = J[w(o, x) & k];
233
+ x += e & 15;
234
+ const E = e >>> 4;
235
+ const c = D.h[E];
236
+ const q = (c >>> 4) + s(o, x, c & 15);
237
+ x += c & 15;
238
+ if (A)
239
+ j_arr = Inflator.H(j_arr, N + (1 << 17));
240
+ while (N < M) {
241
+ j_arr[N] = j_arr[N++ - q];
242
+ j_arr[N] = j_arr[N++ - q];
243
+ j_arr[N] = j_arr[N++ - q];
244
+ j_arr[N] = j_arr[N++ - q];
245
+ }
246
+ N = M;
247
+ }
248
+ }
249
+ }
250
+ return j_arr.length == N ? j_arr : j_arr.slice(0, N);
251
+ }
252
+ static C_inner(o, j) {
253
+ const D = Inflator.D;
254
+ const I = o.length;
255
+ const f = D.v;
256
+ let A;
257
+ let r;
258
+ let i;
259
+ let y;
260
+ let G;
261
+ for (y = 0; y <= j; y++)
262
+ f[y] = 0;
263
+ for (y = 1; y < I; y += 2)
264
+ f[o[y]]++;
265
+ const a = D.m;
266
+ A = 0;
267
+ f[0] = 0;
268
+ for (r = 1; r <= j; r++) {
269
+ A = (A + f[r - 1]) << 1;
270
+ a[r] = A;
271
+ }
272
+ for (i = 0; i < I; i += 2) {
273
+ G = o[i + 1];
274
+ if (G != 0) {
275
+ o[i] = a[G];
276
+ a[G]++;
277
+ }
278
+ }
279
+ }
280
+ static t_inner(o, j, I) {
281
+ const D = Inflator.D;
282
+ const A = o.length;
283
+ const r = D.i;
284
+ for (let i = 0; i < A; i += 2)
285
+ if (o[i + 1] != 0) {
286
+ let y = i >> 1;
287
+ let G = o[i + 1];
288
+ let f = (y << 4) | G;
289
+ let a = j - G;
290
+ let k = o[i] << a;
291
+ const N = k + (1 << a);
292
+ while (k != N) {
293
+ const x = r[k] >>> (15 - j);
294
+ I[x] = f;
295
+ k++;
296
+ }
297
+ }
298
+ }
299
+ static H(o, j) {
300
+ const I = o.length;
301
+ if (j <= I)
302
+ return o;
303
+ const A = new Uint8Array(Math.max(I << 1, j));
304
+ A.set(o, 0);
305
+ return A;
306
+ }
307
+ static B(o, j, I, A, r, i) {
308
+ function w(o, j) {
309
+ return (o[j >>> 3] | (o[(j >>> 3) + 1] << 8) | (o[(j >>> 3) + 2] << 16)) >>> (j & 7);
310
+ }
311
+ function F(o, j, I) {
312
+ return ((o[j >>> 3] | (o[(j >>> 3) + 1] << 8)) >>> (j & 7)) & ((1 << I) - 1);
313
+ }
314
+ let y = 0;
315
+ while (y < I) {
316
+ const G = o[w(A, r) & j];
317
+ r += G & 15;
318
+ const f = G >>> 4;
319
+ if (f <= 15) {
320
+ i[y] = f;
321
+ y++;
322
+ }
323
+ else {
324
+ let a = 0;
325
+ let k = 0;
326
+ if (f == 16) {
327
+ k = 3 + F(A, r, 2);
328
+ r += 2;
329
+ a = i[y - 1];
330
+ }
331
+ else if (f == 17) {
332
+ k = 3 + F(A, r, 3);
333
+ r += 3;
334
+ }
335
+ else if (f == 18) {
336
+ k = 11 + F(A, r, 7);
337
+ r += 7;
338
+ }
339
+ const N = y + k;
340
+ while (y < N) {
341
+ i[y] = a;
342
+ y++;
343
+ }
344
+ }
345
+ }
346
+ return r;
347
+ }
348
+ static d(o, j, I, A) {
349
+ let r = 0;
350
+ let i = 0;
351
+ const y = A.length >>> 1;
352
+ while (i < I) {
353
+ const G = o[i + j];
354
+ A[i << 1] = 0;
355
+ A[(i << 1) + 1] = G;
356
+ if (G > r)
357
+ r = G;
358
+ i++;
359
+ }
360
+ while (i < y) {
361
+ A[i << 1] = 0;
362
+ A[(i << 1) + 1] = 0;
363
+ i++;
364
+ }
365
+ return r;
366
+ }
367
+ }
368
+ //# sourceMappingURL=inflator.js.map