@aboutgeo/pdiiif 0.0.0-stage → 0.2.7

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.
Files changed (69) hide show
  1. package/README.md +169 -2
  2. package/dist/__tests__/parser.test.d.ts +1 -0
  3. package/dist/__tests__/pdf.spec.d.ts +1 -0
  4. package/dist/__tests__/region.test.d.ts +1 -0
  5. package/dist/__tests__/setupJest.d.ts +0 -0
  6. package/dist/assets/optimization.worker-C8u4PFfM.js +886 -0
  7. package/dist/assets/optimization.worker-C8u4PFfM.js.map +1 -0
  8. package/dist/convert.d.ts +223 -0
  9. package/dist/download.d.ts +115 -0
  10. package/dist/iiif.d.ts +110 -0
  11. package/dist/index.cjs +65 -0
  12. package/dist/index.cjs.map +1 -0
  13. package/dist/index.d.ts +7 -0
  14. package/dist/index.mjs +11137 -0
  15. package/dist/index.mjs.map +1 -0
  16. package/dist/io.d.ts +75 -0
  17. package/dist/log.d.ts +21 -0
  18. package/dist/metrics.d.ts +10 -0
  19. package/dist/ocr.d.ts +36 -0
  20. package/dist/optimization.d.ts +48 -0
  21. package/dist/optimization.worker.d.ts +1 -0
  22. package/dist/pdf/annos-text.d.ts +19 -0
  23. package/dist/pdf/annos.d.ts +4 -0
  24. package/dist/pdf/common.d.ts +48 -0
  25. package/dist/pdf/generator.d.ts +197 -0
  26. package/dist/pdf/image.d.ts +34 -0
  27. package/dist/pdf/parser.d.ts +131 -0
  28. package/dist/pdf/pkzip.d.ts +23 -0
  29. package/dist/pdf/png.d.ts +34 -0
  30. package/dist/pdf/util.d.ts +18 -0
  31. package/dist/res/licenses.d.ts +7 -0
  32. package/dist/res/srgbColorspace.d.ts +2 -0
  33. package/dist/util.d.ts +8 -0
  34. package/dist/version.d.ts +2 -0
  35. package/package.json +149 -4
  36. package/src/__tests__/fixtures/deep_toc.json +2953 -0
  37. package/src/__tests__/fixtures/flat_toc.json +2917 -0
  38. package/src/__tests__/fixtures/physdim.json +294 -0
  39. package/src/__tests__/fixtures/region_target.json +58 -0
  40. package/src/__tests__/fixtures/wunder.jpg +0 -0
  41. package/src/__tests__/parser.test.ts +42 -0
  42. package/src/__tests__/pdf.spec.ts +216 -0
  43. package/src/__tests__/region.test.ts +194 -0
  44. package/src/__tests__/setupJest.ts +2 -0
  45. package/src/convert.ts +1098 -0
  46. package/src/download.ts +703 -0
  47. package/src/global.d.ts +10 -0
  48. package/src/iiif.ts +605 -0
  49. package/src/index.ts +7 -0
  50. package/src/io.ts +243 -0
  51. package/src/log.ts +51 -0
  52. package/src/metrics.ts +47 -0
  53. package/src/ocr.ts +176 -0
  54. package/src/optimization.ts +201 -0
  55. package/src/optimization.worker.ts +118 -0
  56. package/src/pdf/annos-text.ts +382 -0
  57. package/src/pdf/annos.ts +240 -0
  58. package/src/pdf/common.ts +158 -0
  59. package/src/pdf/generator.ts +1665 -0
  60. package/src/pdf/image.ts +340 -0
  61. package/src/pdf/parser.ts +811 -0
  62. package/src/pdf/pkzip.ts +228 -0
  63. package/src/pdf/png.ts +397 -0
  64. package/src/pdf/util.ts +111 -0
  65. package/src/res/licenses.ts +266 -0
  66. package/src/res/srgbColorspace.ts +405 -0
  67. package/src/types.d.ts +6 -0
  68. package/src/util.ts +55 -0
  69. package/src/version.ts +1 -0
@@ -0,0 +1,228 @@
1
+ import { crc32 } from '../util.js';
2
+ import { textEncoder } from './util.js';
3
+
4
+ export type LocalHeaderParams = {
5
+ creationDate?: Date;
6
+ filename: string;
7
+ data: Uint8Array;
8
+ compressedData?: Uint8Array;
9
+ extraDataLength: number;
10
+ };
11
+
12
+ export type CentralDirectoryFileSpec = {
13
+ localHeaderOffset: number;
14
+ deflated: boolean;
15
+ creationDate: Date;
16
+ crc32: number;
17
+ dataLength: number;
18
+ compressedDataLength: number;
19
+ filename: string;
20
+ };
21
+
22
+ export type CentralFileDirectorySpec = {
23
+ files: CentralDirectoryFileSpec[];
24
+ trailingLength: number;
25
+ offset: number;
26
+ };
27
+
28
+ export function buildLocalZipHeader({
29
+ creationDate = new Date(),
30
+ filename,
31
+ data,
32
+ compressedData,
33
+ extraDataLength,
34
+ }: LocalHeaderParams): Uint8Array {
35
+ const creationTimeZip =
36
+ (creationDate.getHours() << 11) |
37
+ (creationDate.getDay() << 5) |
38
+ Math.round(creationDate.getSeconds() / 2);
39
+ const creationDateZip =
40
+ ((creationDate.getFullYear() - 1980) << 9) |
41
+ ((creationDate.getMonth() + 1) << 5) |
42
+ creationDate.getDate();
43
+ const dataCrc = crc32(data);
44
+ const filenameEncoded = textEncoder.encode(filename);
45
+ // 2 bytes for zlib header
46
+ const compressedLength = compressedData ? (compressedData.length - 2) : data.length;
47
+ return new Uint8Array([
48
+ // PKZIP magic number
49
+ 0x50,
50
+ 0x4b,
51
+ // Local File Header magic
52
+ 0x03,
53
+ 0x04,
54
+ // Required PKZip version
55
+ 0x14,
56
+ 0x00,
57
+ // General purpose bit flag, 2 bytes
58
+ 0b00000000,
59
+ 0b00000000,
60
+ // Compression method, 2 bytes little endian
61
+ compressedData ? 0x08 : 0x00,
62
+ 0x00,
63
+ // Creation time, 2 bytes little endian
64
+ creationTimeZip & 0xff,
65
+ creationTimeZip >> 8,
66
+ // Creation date, 2 bytes little endian
67
+ creationDateZip & 0xff,
68
+ creationDateZip >> 8,
69
+ // CRC32 of data, 4 bytes little endian
70
+ dataCrc & 0xff,
71
+ (dataCrc >> 8) & 0xff,
72
+ (dataCrc >> 16) & 0xff,
73
+ (dataCrc >> 24) & 0xff,
74
+ // Compressed size, 4 bytes little endian
75
+ compressedLength & 0xff,
76
+ (compressedLength >> 8) & 0xff,
77
+ (compressedLength >> 16) & 0xff,
78
+ (compressedLength >> 24) & 0xff,
79
+ // Uncompressed size, 4 bytes little endian
80
+ data.length & 0xff,
81
+ (data.length >> 8) & 0xff,
82
+ (data.length >> 16) & 0xff,
83
+ (data.length >> 24) & 0xff,
84
+ // Filename length, 2 bytes little endian
85
+ filenameEncoded.length & 0xff,
86
+ filenameEncoded.length >> 8, // Filename length in little endian
87
+ // Extra field length, 2 bytes little endian
88
+ (extraDataLength + 4) & 0xff,
89
+ (extraDataLength + 4) >> 8,
90
+ // Encoded file name
91
+ ...filenameEncoded,
92
+ // Beginning of extra field: identifier, 2 bytes
93
+ 0xff,
94
+ 0xff,
95
+ // Beginning of extra field: length, 2 bytes little endian
96
+ extraDataLength & 0xff,
97
+ extraDataLength >> 8,
98
+ ]);
99
+ }
100
+
101
+ export function buildCentralFileDirectory({
102
+ files,
103
+ trailingLength,
104
+ offset
105
+ }: CentralFileDirectorySpec): Uint8Array {
106
+ const chunks = files.map(buildCentralDirectoryFileHeader);
107
+ const cdSize = chunks.reduce((acc, chunk) => acc + chunk.length, 0);
108
+ chunks.push(new Uint8Array([
109
+ // PKZIP magic number
110
+ 0x50,
111
+ 0x4b,
112
+ // End of central directory magic
113
+ 0x05,
114
+ 0x06,
115
+ // Disk number
116
+ 0x00,
117
+ 0x00,
118
+ // Disk with central directory
119
+ 0x00,
120
+ 0x00,
121
+ // Disk entries
122
+ files.length & 0xff,
123
+ files.length >> 8,
124
+ // Total entries
125
+ files.length & 0xff,
126
+ files.length >> 8,
127
+ // Central directory size,
128
+ cdSize & 0xff,
129
+ (cdSize >> 8) & 0xff,
130
+ (cdSize >> 16) & 0xff,
131
+ (cdSize >> 24) & 0xff,
132
+ // Offset of central directory in file
133
+ offset & 0xff,
134
+ (offset >> 8) & 0xff,
135
+ (offset >> 16) & 0xff,
136
+ (offset >> 24) & 0xff,
137
+ // Length of trailing comment
138
+ trailingLength & 0xff,
139
+ trailingLength >> 8,
140
+ ]));
141
+ return new Uint8Array(
142
+ chunks.reduce((acc: number[], curr) => {
143
+ acc.push(...curr);
144
+ return acc;
145
+ }, [])
146
+ );
147
+ }
148
+
149
+ function buildCentralDirectoryFileHeader(
150
+ spec: CentralDirectoryFileSpec
151
+ ): Uint8Array {
152
+ const creationTimeZip =
153
+ (spec.creationDate.getHours() << 11) |
154
+ (spec.creationDate.getMinutes() << 5) |
155
+ Math.round(spec.creationDate.getSeconds() / 2);
156
+ const creationDateZip =
157
+ ((spec.creationDate.getFullYear() - 1980) << 9) |
158
+ ((spec.creationDate.getMonth() +1) << 5) |
159
+ spec.creationDate.getDate();
160
+ const filenameEncoded = textEncoder.encode(spec.filename);
161
+ return new Uint8Array([
162
+ // PKZIP magic number
163
+ 0x50,
164
+ 0x4b,
165
+ // Central directory file header magic
166
+ 0x01,
167
+ 0x02,
168
+ // Version
169
+ 0x17,
170
+ 0x03,
171
+ // Version needed
172
+ 0x14,
173
+ 0x00,
174
+ // Flags, none set
175
+ 0x00,
176
+ 0x00,
177
+ // Compression method, 2 bytes little endian
178
+ spec.deflated ? 0x08 : 0x00,
179
+ 0x00,
180
+ // Creation time, 2 bytes little endian
181
+ creationTimeZip & 0xff,
182
+ creationTimeZip >> 8,
183
+ // Creation date, 2 bytes little endian
184
+ creationDateZip & 0xff,
185
+ creationDateZip >> 8,
186
+ // CRC32 of data, 4 bytes little endian
187
+ spec.crc32 & 0xff,
188
+ (spec.crc32 >> 8) & 0xff,
189
+ (spec.crc32 >> 16) & 0xff,
190
+ (spec.crc32 >> 24) & 0xff,
191
+ // Compressed size, 4 bytes little endian
192
+ spec.compressedDataLength & 0xff,
193
+ (spec.compressedDataLength >> 8) & 0xff,
194
+ (spec.compressedDataLength >> 16) & 0xff,
195
+ (spec.compressedDataLength >> 24) & 0xff,
196
+ // Uncompressed size, 4 bytes little endian
197
+ spec.dataLength & 0xff,
198
+ (spec.dataLength >> 8) & 0xff,
199
+ (spec.dataLength >> 16) & 0xff,
200
+ (spec.dataLength >> 24) & 0xff,
201
+ // Filename length, 2 bytes little endian
202
+ filenameEncoded.length & 0xff,
203
+ filenameEncoded.length >> 8, // Filename length in little endian
204
+ // Extra field length, 2 bytes little endian
205
+ 0x00,
206
+ 0x00,
207
+ // File comment length, 2 bytes little endian
208
+ 0x00,
209
+ 0x00,
210
+ // Disk # start, 2 bytes little endian
211
+ 0x00,
212
+ 0x00,
213
+ // Internal Attribute, 2 bytes little endian
214
+ 0x00,
215
+ 0x00,
216
+ // External Attribute, 4 bytes little endian
217
+ 0x00,
218
+ 0x00,
219
+ 0xa4,
220
+ 0x81,
221
+ // Offset of local header, 4 bytes little endian
222
+ spec.localHeaderOffset & 0xff,
223
+ (spec.localHeaderOffset >> 8) &0xff,
224
+ (spec.localHeaderOffset >> 16) &0xff,
225
+ (spec.localHeaderOffset >> 24) &0xff,
226
+ ...filenameEncoded,
227
+ ]);
228
+ }
package/src/pdf/png.ts ADDED
@@ -0,0 +1,397 @@
1
+ /*
2
+ Taken from Hopding/png-ts, which is a TypeScript port by Andrew Dillon of the
3
+ PNG decoder from pdfkit written by Devon Govett.
4
+ The original code can be found at:
5
+ https://github.com/Hopding/png-ts/blob/master/src/png.ts
6
+
7
+ Modified by me (@jbaiter):
8
+ - use `fflate` instead of `pako` for decompression.
9
+ - add support for decoding interlaced images
10
+
11
+ MIT License
12
+
13
+ Copyright (c) 2018 Andrew Dillon
14
+ Copyright (c) 2017 Devon Govett
15
+
16
+ Permission is hereby granted, free of charge, to any person obtaining a copy
17
+ of this software and associated documentation files (the "Software"), to deal
18
+ in the Software without restriction, including without limitation the rights
19
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
20
+ copies of the Software, and to permit persons to whom the Software is
21
+ furnished to do so, subject to the following conditions:
22
+
23
+ The above copyright notice and this permission notice shall be included in all
24
+ copies or substantial portions of the Software.
25
+
26
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
31
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32
+ SOFTWARE.
33
+ */
34
+ import { unzipSync, unzlibSync } from "fflate";
35
+
36
+ export type ColorSpace = 'DeviceGray' | 'DeviceRGB';
37
+
38
+ export default class PNG {
39
+ static load = (data: Uint8Array) => new PNG(data);
40
+
41
+ palette: number[];
42
+ imgData: Uint8Array;
43
+ transparency: {
44
+ indexed?: number[];
45
+ grayscale?: number;
46
+ rgb?: number[];
47
+ };
48
+ text: { [key: string]: string };
49
+ width!: number;
50
+ height!: number;
51
+ bits!: number;
52
+ colorType!: number;
53
+ compressionMethod!: number;
54
+ filterMethod!: number;
55
+ interlaceMethod!: number;
56
+ colors: 1 | 3;
57
+ hasAlphaChannel: boolean;
58
+ pixelBitlength: number;
59
+ colorSpace: ColorSpace;
60
+
61
+ private data: Uint8Array;
62
+ private pos: number;
63
+
64
+ constructor(data: Uint8Array) {
65
+ this.data = data;
66
+ this.pos = 8; // Skip the default header;
67
+
68
+ this.palette = [];
69
+ const imgDataBuff = [];
70
+ this.transparency = {};
71
+ this.text = {};
72
+
73
+ while (true) {
74
+ const chunkSize = this.readUInt32();
75
+
76
+ let section = '';
77
+ for (let i = 0; i < 4; i++) {
78
+ section += String.fromCharCode(this.data[this.pos++]);
79
+ }
80
+
81
+ switch (section) {
82
+ case 'IHDR': {
83
+ // We can grab interesting values from here (like width, height, etc...)
84
+ this.width = this.readUInt32();
85
+ this.height = this.readUInt32();
86
+ this.bits = this.data[this.pos++];
87
+ this.colorType = this.data[this.pos++];
88
+ this.compressionMethod = this.data[this.pos++];
89
+ this.filterMethod = this.data[this.pos++];
90
+ this.interlaceMethod = this.data[this.pos++];
91
+ break;
92
+ }
93
+
94
+ case 'PLTE': {
95
+ this.palette = this.read(chunkSize);
96
+ break;
97
+ }
98
+
99
+ case 'IDAT': {
100
+ for (let i = 0; i < chunkSize; i++) {
101
+ imgDataBuff.push(this.data[this.pos++]);
102
+ }
103
+ break;
104
+ }
105
+
106
+ case 'tRNS': {
107
+ // This chunk can only occur once and it must occur after the
108
+ // PLTE chunk and before the IDAT chunk.
109
+ this.transparency = {};
110
+ switch (this.colorType) {
111
+ case 3: {
112
+ // Indexed color, RGB. Each byte in this chunk is an alpha for the
113
+ // palette index in the PLTE ("palette") chunk up until the last
114
+ // non-opaque entry. Set up an array, stretching over all palette
115
+ // entries which will be 0 (opaque) or 1 (transparent).
116
+ this.transparency.indexed = this.read(chunkSize);
117
+ const short = 255 - this.transparency.indexed.length;
118
+ if (short > 0) {
119
+ for (let i = 0; i < 255; i++) {
120
+ this.transparency.indexed.push(255);
121
+ }
122
+ }
123
+ break;
124
+ }
125
+ case 0: {
126
+ // Greyscale. Corresponding to entries in the PLTE chunk.
127
+ // Grey is two bytes, range 0 .. (2 ^ bit-depth) - 1
128
+ this.transparency.grayscale = this.read(chunkSize)[0];
129
+ break;
130
+ }
131
+ case 2: {
132
+ // True color with proper alpha channel.
133
+ this.transparency.rgb = this.read(chunkSize);
134
+ break;
135
+ }
136
+ }
137
+ break;
138
+ }
139
+
140
+ case 'tEXt': {
141
+ const text = this.read(chunkSize);
142
+ const index = text.indexOf(0);
143
+ const key = String.fromCharCode(...text.slice(0, index));
144
+ this.text[key] = String.fromCharCode(...text.slice(index + 1));
145
+ break;
146
+ }
147
+
148
+ case 'IEND': {
149
+ // We've got everything we need!
150
+ const colorTypeMap: { [colorType: number]: 1 | 3 } = {
151
+ 0: 1,
152
+ 3: 1,
153
+ 4: 1,
154
+ 2: 3,
155
+ 6: 3,
156
+ };
157
+ this.colors = colorTypeMap[this.colorType];
158
+
159
+ this.hasAlphaChannel = [4, 6].includes(this.colorType);
160
+ const colors = this.colors + (this.hasAlphaChannel ? 1 : 0);
161
+ this.pixelBitlength = this.bits * colors;
162
+ this.colorSpace = {
163
+ 1: 'DeviceGray',
164
+ 3: 'DeviceRGB',
165
+ }[this.colors] as ColorSpace;
166
+ this.imgData = new Uint8Array(imgDataBuff);
167
+
168
+ return;
169
+ }
170
+
171
+ default: {
172
+ // Unknown (or unimportant) section, skip it
173
+ this.pos += chunkSize;
174
+ }
175
+ }
176
+
177
+ this.pos += 4; // Skip the CRC
178
+ if (this.pos > this.data.length) {
179
+ throw new Error('Incomplete or corrupt PNG file');
180
+ }
181
+ }
182
+ }
183
+
184
+ decode = () => {
185
+ const retVal = new Uint8Array(this.width * this.height * 4);
186
+ const pixels = this.decodePixels();
187
+ this.copyImageDataToBuffer(retVal, pixels);
188
+ return retVal;
189
+ };
190
+
191
+ copyImageDataToBuffer = (imageData: Uint8Array, pixels: Uint8Array): void => {
192
+ let colors: 1 | 3 | 4 = this.colors;
193
+ let palette;
194
+ let alpha = this.hasAlphaChannel;
195
+
196
+ if (this.palette.length) {
197
+ palette = this.decodePalette();
198
+ colors = 4;
199
+ alpha = true;
200
+ }
201
+
202
+ const data = imageData;
203
+ const length = data.length;
204
+ const input = palette || pixels;
205
+
206
+ let i = 0;
207
+ let j = 0;
208
+
209
+ if (colors === 1) {
210
+ while (i < length) {
211
+ let k = palette ? pixels[i / 4] * 4 : j;
212
+ const v = input[k++];
213
+ data[i++] = v;
214
+ data[i++] = v;
215
+ data[i++] = v;
216
+ data[i++] = alpha ? input[k++] : 255;
217
+ j = k;
218
+ }
219
+ } else {
220
+ while (i < length) {
221
+ let k = palette ? pixels[i / 4] * 4 : j;
222
+ data[i++] = input[k++];
223
+ data[i++] = input[k++];
224
+ data[i++] = input[k++];
225
+ data[i++] = alpha ? input[k++] : 255;
226
+ j = k;
227
+ }
228
+ }
229
+ };
230
+
231
+ decodePixels = (): Uint8Array => {
232
+ const data = unzlibSync(this.imgData);
233
+
234
+ const width = this.width;
235
+ const height = this.height;
236
+ const pixelBytes = this.pixelBitlength / 8;
237
+ const scanlineLength = pixelBytes * this.width;
238
+
239
+ const pixels = new Uint8Array(scanlineLength * this.height);
240
+ const length = data.length;
241
+ let row = 0;
242
+ let pos = 0;
243
+ let c = 0;
244
+
245
+ function pass(x0: number, y0: number, dx: number, dy: number, singlePass = false): void {
246
+ const w = Math.ceil((width - x0) / dx);
247
+ const h = Math.ceil((height - y0) / dy);
248
+ const scanlineLength = pixelBytes * w;
249
+ const buffer = singlePass ? pixels : new Uint8Array(scanlineLength * h);
250
+ let row = 0;
251
+ let c = 0;
252
+ while (row < h && pos < length) {
253
+ switch (data[pos++]) {
254
+ case 0: // None
255
+ for (let i = 0; i < scanlineLength; i++) {
256
+ buffer[c++] = data[pos++];
257
+ }
258
+ break;
259
+ case 1: // Sub
260
+ for (let i = 0; i < scanlineLength; i++) {
261
+ const byte = data[pos++];
262
+ const left = i < pixelBytes ? 0 : buffer[c - pixelBytes];
263
+ buffer[c++] = (byte + left) % 256;
264
+ }
265
+ break;
266
+ case 2: // Up
267
+ for (let i = 0; i < scanlineLength; i++) {
268
+ const byte = data[pos++];
269
+ const col = (i - (i % pixelBytes)) / pixelBytes;
270
+ const pixelsIdx = (row - 1) * scanlineLength + col * pixelBytes + (i % pixelBytes);
271
+ const upper = row && buffer[pixelsIdx];
272
+ buffer[c++] = (upper + byte) % 256;
273
+ }
274
+ break;
275
+ case 3: // Average
276
+ for (let i = 0; i < scanlineLength; i++) {
277
+ const byte = data[pos++];
278
+ const col = (i - (i % pixelBytes)) / pixelBytes;
279
+ const left = i < pixelBytes ? 0 : buffer[c - pixelBytes];
280
+ const pixelsIdx = (row - 1) * scanlineLength + col * pixelBytes + (i % pixelBytes);
281
+ const upper = row && buffer[pixelsIdx];
282
+ buffer[c++] = (byte + Math.floor((left + upper) / 2)) % 256;
283
+ }
284
+ break;
285
+ case 4: // Paeth
286
+ for (let i = 0; i < scanlineLength; i++) {
287
+ const byte = data[pos++];
288
+ const col = (i - (i % pixelBytes)) / pixelBytes;
289
+ const left = i < pixelBytes ? 0 : buffer[c - pixelBytes];
290
+ let upper;
291
+ let upperLeft;
292
+ if (row === 0) {
293
+ upper = upperLeft = 0;
294
+ } else {
295
+ const upperIdx = (row - 1) * scanlineLength + col * pixelBytes + (i % pixelBytes);
296
+ const upperLeftIdx = (row - 1) * scanlineLength + (col - 1) * pixelBytes + (i % pixelBytes);
297
+ upper = buffer[upperIdx];
298
+ upperLeft = col && buffer[upperLeftIdx];
299
+ }
300
+ const p = left + upper - upperLeft;
301
+ const pa = Math.abs(p - left);
302
+ const pb = Math.abs(p - upper);
303
+ const pc = Math.abs(p - upperLeft);
304
+ let paeth;
305
+ if (pa <= pb && pa <= pc) {
306
+ paeth = left;
307
+ } else if (pb <= pc) {
308
+ paeth = upper;
309
+ } else {
310
+ paeth = upperLeft;
311
+ }
312
+ buffer[c++] = (byte + paeth) % 256;
313
+ }
314
+ break;
315
+ default:
316
+ throw new Error(`Invalid filter algorithm: ${data[pos - 1]}`);
317
+ }
318
+
319
+ if (!singlePass) {
320
+ let pixelsPos = ((y0 + row * dy) * width + x0) * pixelBytes;
321
+ let bufferPos = row * scanlineLength;
322
+ for (let i = 0; i < w; i++) {
323
+ for (let j = 0; j < pixelBytes; j++) {
324
+ pixels[pixelsPos++] = buffer[bufferPos++];
325
+ }
326
+ pixelsPos += (dx - 1) * pixelBytes;
327
+ }
328
+ }
329
+
330
+ row++;
331
+ }
332
+ }
333
+
334
+ if (this.interlaceMethod === 0) {
335
+ // Single pass from top to bottom with no skips
336
+ pass(0, 0, 1, 1, true);
337
+ } else {
338
+ /*
339
+ Adam7 interlacing consists of 7 passes over the data, each one
340
+ starting at a different x,y offset and covering a different fraction of the pixels.
341
+
342
+ 1 6 4 6 2 6 4 6
343
+ 7 7 7 7 7 7 7 7
344
+ 5 6 5 6 5 6 5 6
345
+ 7 7 7 7 7 7 7 7
346
+ 3 6 4 6 3 6 4 6
347
+ 7 7 7 7 7 7 7 7
348
+ 5 6 5 6 5 6 5 6
349
+ 7 7 7 7 7 7 7 7
350
+ */
351
+ pass(0, 0, 8, 8); // 1
352
+ pass(4, 0, 8, 8); // 2
353
+ pass(0, 4, 4, 8); // 3
354
+ pass(2, 0, 4, 4); // 4
355
+ pass(0, 2, 2, 4); // 5
356
+ pass(1, 0, 2, 2); // 6
357
+ pass(0, 1, 1, 2); // 7
358
+ }
359
+
360
+
361
+ return pixels;
362
+ };
363
+
364
+ private read = (numBytes: number): number[] => {
365
+ const results = [];
366
+ for (let i = 0; i < numBytes; i++) {
367
+ results[i] = this.data[this.pos++];
368
+ }
369
+ return results;
370
+ };
371
+
372
+ private readUInt32 = (): number => {
373
+ const b1 = this.data[this.pos++] << 24;
374
+ const b2 = this.data[this.pos++] << 16;
375
+ const b3 = this.data[this.pos++] << 8;
376
+ const b4 = this.data[this.pos++];
377
+ return b1 | b2 | b3 | b4;
378
+ };
379
+
380
+ private decodePalette = (): Uint8Array => {
381
+ const palette = this.palette;
382
+ const transparency = this.transparency.indexed || [];
383
+ const retVal = new Uint8Array(transparency.length + palette.length);
384
+ let pos = 0;
385
+ let c = 0;
386
+
387
+ for (let i = 0; i < palette.length; i++) {
388
+ retVal[pos++] = palette[i];
389
+ retVal[pos++] = palette[i + 1];
390
+ retVal[pos++] = palette[i + 2];
391
+ const temp = transparency[c++];
392
+ retVal[pos++] = temp !== undefined ? temp : 255;
393
+ }
394
+
395
+ return retVal;
396
+ };
397
+ }