reamkit 1.25.1 → 1.26.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/README.md +5 -3
- package/dist/esm/pdf-reader/content.d.ts +2 -16
- package/dist/esm/pdf-reader/content.js +69 -3
- package/dist/esm/pdf-reader/font.js +21 -1
- package/dist/esm/pdf-reader/glyph-names.d.ts +6 -0
- package/dist/esm/pdf-reader/glyph-names.js +408 -0
- package/dist/esm/pdf-reader/image-decode.js +32 -1
- package/dist/esm/pdf-reader/jbig2.d.ts +86 -0
- package/dist/esm/pdf-reader/jbig2.js +2597 -0
- package/dist/esm/pdf-reader/shading.d.ts +13 -0
- package/dist/esm/pdf-reader/shading.js +68 -2
- package/dist/esm/pdf-reader/vector.js +5 -4
- package/dist/esm/word/docx-writer.js +140 -12
- package/package.json +1 -1
|
@@ -0,0 +1,2597 @@
|
|
|
1
|
+
import { decodeCcitt } from "./ccitt.js";
|
|
2
|
+
//#region src/pdf-reader/jbig2.ts
|
|
3
|
+
function makeBitmap(width, height, fill = 0) {
|
|
4
|
+
const data = new Uint8Array(width * height);
|
|
5
|
+
if (fill) data.fill(1);
|
|
6
|
+
return {
|
|
7
|
+
width,
|
|
8
|
+
height,
|
|
9
|
+
data
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* §E.1 Table E.1 — the MQ-coder's probability estimation state machine, as
|
|
14
|
+
* `[Qe, NMPS, NLPS, SWITCH]` per state. The same table drives JPEG 2000.
|
|
15
|
+
*/
|
|
16
|
+
var QE = [
|
|
17
|
+
[
|
|
18
|
+
22017,
|
|
19
|
+
1,
|
|
20
|
+
1,
|
|
21
|
+
1
|
|
22
|
+
],
|
|
23
|
+
[
|
|
24
|
+
13313,
|
|
25
|
+
2,
|
|
26
|
+
6,
|
|
27
|
+
0
|
|
28
|
+
],
|
|
29
|
+
[
|
|
30
|
+
6145,
|
|
31
|
+
3,
|
|
32
|
+
9,
|
|
33
|
+
0
|
|
34
|
+
],
|
|
35
|
+
[
|
|
36
|
+
2753,
|
|
37
|
+
4,
|
|
38
|
+
12,
|
|
39
|
+
0
|
|
40
|
+
],
|
|
41
|
+
[
|
|
42
|
+
1313,
|
|
43
|
+
5,
|
|
44
|
+
29,
|
|
45
|
+
0
|
|
46
|
+
],
|
|
47
|
+
[
|
|
48
|
+
545,
|
|
49
|
+
38,
|
|
50
|
+
33,
|
|
51
|
+
0
|
|
52
|
+
],
|
|
53
|
+
[
|
|
54
|
+
22017,
|
|
55
|
+
7,
|
|
56
|
+
6,
|
|
57
|
+
1
|
|
58
|
+
],
|
|
59
|
+
[
|
|
60
|
+
21505,
|
|
61
|
+
8,
|
|
62
|
+
14,
|
|
63
|
+
0
|
|
64
|
+
],
|
|
65
|
+
[
|
|
66
|
+
18433,
|
|
67
|
+
9,
|
|
68
|
+
14,
|
|
69
|
+
0
|
|
70
|
+
],
|
|
71
|
+
[
|
|
72
|
+
14337,
|
|
73
|
+
10,
|
|
74
|
+
14,
|
|
75
|
+
0
|
|
76
|
+
],
|
|
77
|
+
[
|
|
78
|
+
12289,
|
|
79
|
+
11,
|
|
80
|
+
17,
|
|
81
|
+
0
|
|
82
|
+
],
|
|
83
|
+
[
|
|
84
|
+
9217,
|
|
85
|
+
12,
|
|
86
|
+
18,
|
|
87
|
+
0
|
|
88
|
+
],
|
|
89
|
+
[
|
|
90
|
+
7169,
|
|
91
|
+
13,
|
|
92
|
+
20,
|
|
93
|
+
0
|
|
94
|
+
],
|
|
95
|
+
[
|
|
96
|
+
5633,
|
|
97
|
+
29,
|
|
98
|
+
21,
|
|
99
|
+
0
|
|
100
|
+
],
|
|
101
|
+
[
|
|
102
|
+
22017,
|
|
103
|
+
15,
|
|
104
|
+
14,
|
|
105
|
+
1
|
|
106
|
+
],
|
|
107
|
+
[
|
|
108
|
+
21505,
|
|
109
|
+
16,
|
|
110
|
+
14,
|
|
111
|
+
0
|
|
112
|
+
],
|
|
113
|
+
[
|
|
114
|
+
20737,
|
|
115
|
+
17,
|
|
116
|
+
15,
|
|
117
|
+
0
|
|
118
|
+
],
|
|
119
|
+
[
|
|
120
|
+
18433,
|
|
121
|
+
18,
|
|
122
|
+
16,
|
|
123
|
+
0
|
|
124
|
+
],
|
|
125
|
+
[
|
|
126
|
+
14337,
|
|
127
|
+
19,
|
|
128
|
+
17,
|
|
129
|
+
0
|
|
130
|
+
],
|
|
131
|
+
[
|
|
132
|
+
13313,
|
|
133
|
+
20,
|
|
134
|
+
18,
|
|
135
|
+
0
|
|
136
|
+
],
|
|
137
|
+
[
|
|
138
|
+
12289,
|
|
139
|
+
21,
|
|
140
|
+
19,
|
|
141
|
+
0
|
|
142
|
+
],
|
|
143
|
+
[
|
|
144
|
+
10241,
|
|
145
|
+
22,
|
|
146
|
+
19,
|
|
147
|
+
0
|
|
148
|
+
],
|
|
149
|
+
[
|
|
150
|
+
9217,
|
|
151
|
+
23,
|
|
152
|
+
20,
|
|
153
|
+
0
|
|
154
|
+
],
|
|
155
|
+
[
|
|
156
|
+
8705,
|
|
157
|
+
24,
|
|
158
|
+
21,
|
|
159
|
+
0
|
|
160
|
+
],
|
|
161
|
+
[
|
|
162
|
+
7169,
|
|
163
|
+
25,
|
|
164
|
+
22,
|
|
165
|
+
0
|
|
166
|
+
],
|
|
167
|
+
[
|
|
168
|
+
6145,
|
|
169
|
+
26,
|
|
170
|
+
23,
|
|
171
|
+
0
|
|
172
|
+
],
|
|
173
|
+
[
|
|
174
|
+
5633,
|
|
175
|
+
27,
|
|
176
|
+
24,
|
|
177
|
+
0
|
|
178
|
+
],
|
|
179
|
+
[
|
|
180
|
+
5121,
|
|
181
|
+
28,
|
|
182
|
+
25,
|
|
183
|
+
0
|
|
184
|
+
],
|
|
185
|
+
[
|
|
186
|
+
4609,
|
|
187
|
+
29,
|
|
188
|
+
26,
|
|
189
|
+
0
|
|
190
|
+
],
|
|
191
|
+
[
|
|
192
|
+
4353,
|
|
193
|
+
30,
|
|
194
|
+
27,
|
|
195
|
+
0
|
|
196
|
+
],
|
|
197
|
+
[
|
|
198
|
+
2753,
|
|
199
|
+
31,
|
|
200
|
+
28,
|
|
201
|
+
0
|
|
202
|
+
],
|
|
203
|
+
[
|
|
204
|
+
2497,
|
|
205
|
+
32,
|
|
206
|
+
29,
|
|
207
|
+
0
|
|
208
|
+
],
|
|
209
|
+
[
|
|
210
|
+
2209,
|
|
211
|
+
33,
|
|
212
|
+
30,
|
|
213
|
+
0
|
|
214
|
+
],
|
|
215
|
+
[
|
|
216
|
+
1313,
|
|
217
|
+
34,
|
|
218
|
+
31,
|
|
219
|
+
0
|
|
220
|
+
],
|
|
221
|
+
[
|
|
222
|
+
1089,
|
|
223
|
+
35,
|
|
224
|
+
32,
|
|
225
|
+
0
|
|
226
|
+
],
|
|
227
|
+
[
|
|
228
|
+
673,
|
|
229
|
+
36,
|
|
230
|
+
33,
|
|
231
|
+
0
|
|
232
|
+
],
|
|
233
|
+
[
|
|
234
|
+
545,
|
|
235
|
+
37,
|
|
236
|
+
34,
|
|
237
|
+
0
|
|
238
|
+
],
|
|
239
|
+
[
|
|
240
|
+
321,
|
|
241
|
+
38,
|
|
242
|
+
35,
|
|
243
|
+
0
|
|
244
|
+
],
|
|
245
|
+
[
|
|
246
|
+
273,
|
|
247
|
+
39,
|
|
248
|
+
36,
|
|
249
|
+
0
|
|
250
|
+
],
|
|
251
|
+
[
|
|
252
|
+
133,
|
|
253
|
+
40,
|
|
254
|
+
37,
|
|
255
|
+
0
|
|
256
|
+
],
|
|
257
|
+
[
|
|
258
|
+
73,
|
|
259
|
+
41,
|
|
260
|
+
38,
|
|
261
|
+
0
|
|
262
|
+
],
|
|
263
|
+
[
|
|
264
|
+
37,
|
|
265
|
+
42,
|
|
266
|
+
39,
|
|
267
|
+
0
|
|
268
|
+
],
|
|
269
|
+
[
|
|
270
|
+
21,
|
|
271
|
+
43,
|
|
272
|
+
40,
|
|
273
|
+
0
|
|
274
|
+
],
|
|
275
|
+
[
|
|
276
|
+
9,
|
|
277
|
+
44,
|
|
278
|
+
41,
|
|
279
|
+
0
|
|
280
|
+
],
|
|
281
|
+
[
|
|
282
|
+
5,
|
|
283
|
+
45,
|
|
284
|
+
42,
|
|
285
|
+
0
|
|
286
|
+
],
|
|
287
|
+
[
|
|
288
|
+
1,
|
|
289
|
+
45,
|
|
290
|
+
43,
|
|
291
|
+
0
|
|
292
|
+
],
|
|
293
|
+
[
|
|
294
|
+
22017,
|
|
295
|
+
46,
|
|
296
|
+
46,
|
|
297
|
+
0
|
|
298
|
+
]
|
|
299
|
+
];
|
|
300
|
+
/** §E.3.5 — a fresh set of `size` contexts, all at state 0 with MPS 0. */
|
|
301
|
+
function newContexts(size) {
|
|
302
|
+
return {
|
|
303
|
+
i: new Uint8Array(size),
|
|
304
|
+
mps: new Uint8Array(size)
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* §E.3 — the MQ arithmetic decoder.
|
|
309
|
+
*
|
|
310
|
+
* Written as a class with the spec's own register names (`c`, `a`, `ct`)
|
|
311
|
+
* because the procedures below are transcribed from its flow charts, and a
|
|
312
|
+
* renaming makes them impossible to check against it.
|
|
313
|
+
*/
|
|
314
|
+
var MQDecoder = class {
|
|
315
|
+
data;
|
|
316
|
+
bp;
|
|
317
|
+
end;
|
|
318
|
+
c = 0;
|
|
319
|
+
a = 0;
|
|
320
|
+
ct = 0;
|
|
321
|
+
constructor(data, start = 0, end = data.length) {
|
|
322
|
+
this.data = data;
|
|
323
|
+
this.bp = start;
|
|
324
|
+
this.end = end;
|
|
325
|
+
this.c = this.byteAt(this.bp) << 16 >>> 0;
|
|
326
|
+
this.byteIn();
|
|
327
|
+
this.c = this.c << 7 >>> 0;
|
|
328
|
+
this.ct -= 7;
|
|
329
|
+
this.a = 32768;
|
|
330
|
+
}
|
|
331
|
+
byteAt(i) {
|
|
332
|
+
return i < this.end ? this.data[i] ?? 255 : 255;
|
|
333
|
+
}
|
|
334
|
+
/** BYTEIN (§E.3.4) — 0xFF is the marker escape, so it feeds a shorter byte. */
|
|
335
|
+
byteIn() {
|
|
336
|
+
if (this.byteAt(this.bp) === 255) if (this.byteAt(this.bp + 1) > 143) {
|
|
337
|
+
this.c = this.c + 65280 >>> 0;
|
|
338
|
+
this.ct = 8;
|
|
339
|
+
} else {
|
|
340
|
+
this.bp++;
|
|
341
|
+
this.c = this.c + (this.byteAt(this.bp) << 9) >>> 0;
|
|
342
|
+
this.ct = 7;
|
|
343
|
+
}
|
|
344
|
+
else {
|
|
345
|
+
this.bp++;
|
|
346
|
+
this.c = this.c + (this.byteAt(this.bp) << 8) >>> 0;
|
|
347
|
+
this.ct = 8;
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
/**
|
|
351
|
+
* DECODE (§E.3.2) — one bit in context `k`.
|
|
352
|
+
*
|
|
353
|
+
* @param cx The context set the caller keeps for this decoding procedure.
|
|
354
|
+
* @param k Which context within it.
|
|
355
|
+
*/
|
|
356
|
+
decode(cx, k) {
|
|
357
|
+
let i = cx.i[k] ?? 0;
|
|
358
|
+
let mps = cx.mps[k] ?? 0;
|
|
359
|
+
const [qe, nmps, nlps, sw] = QE[i] ?? QE[0];
|
|
360
|
+
let d;
|
|
361
|
+
this.a = this.a - qe & 65535;
|
|
362
|
+
if ((this.c >>> 16 & 65535) < qe) if (this.a < qe) {
|
|
363
|
+
this.a = qe;
|
|
364
|
+
d = mps;
|
|
365
|
+
i = nmps;
|
|
366
|
+
} else {
|
|
367
|
+
this.a = qe;
|
|
368
|
+
d = 1 - mps;
|
|
369
|
+
if (sw === 1) mps = 1 - mps;
|
|
370
|
+
i = nlps;
|
|
371
|
+
}
|
|
372
|
+
else {
|
|
373
|
+
this.c = this.c - (qe << 16) >>> 0;
|
|
374
|
+
if ((this.a & 32768) !== 0) return mps;
|
|
375
|
+
if (this.a < qe) {
|
|
376
|
+
d = 1 - mps;
|
|
377
|
+
if (sw === 1) mps = 1 - mps;
|
|
378
|
+
i = nlps;
|
|
379
|
+
} else {
|
|
380
|
+
d = mps;
|
|
381
|
+
i = nmps;
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
do {
|
|
385
|
+
if (this.ct === 0) this.byteIn();
|
|
386
|
+
this.a = this.a << 1 & 65535;
|
|
387
|
+
this.c = this.c << 1 >>> 0;
|
|
388
|
+
this.ct--;
|
|
389
|
+
} while ((this.a & 32768) === 0);
|
|
390
|
+
cx.i[k] = i;
|
|
391
|
+
cx.mps[k] = mps;
|
|
392
|
+
return d;
|
|
393
|
+
}
|
|
394
|
+
};
|
|
395
|
+
/** The nominal AT positions per template (§6.2.5.3 figures 8-11). */
|
|
396
|
+
var NOMINAL_AT = [
|
|
397
|
+
[
|
|
398
|
+
{
|
|
399
|
+
x: 3,
|
|
400
|
+
y: -1
|
|
401
|
+
},
|
|
402
|
+
{
|
|
403
|
+
x: -3,
|
|
404
|
+
y: -1
|
|
405
|
+
},
|
|
406
|
+
{
|
|
407
|
+
x: 2,
|
|
408
|
+
y: -2
|
|
409
|
+
},
|
|
410
|
+
{
|
|
411
|
+
x: -2,
|
|
412
|
+
y: -2
|
|
413
|
+
}
|
|
414
|
+
],
|
|
415
|
+
[{
|
|
416
|
+
x: 3,
|
|
417
|
+
y: -1
|
|
418
|
+
}],
|
|
419
|
+
[{
|
|
420
|
+
x: 2,
|
|
421
|
+
y: -1
|
|
422
|
+
}],
|
|
423
|
+
[{
|
|
424
|
+
x: 2,
|
|
425
|
+
y: -1
|
|
426
|
+
}]
|
|
427
|
+
];
|
|
428
|
+
/**
|
|
429
|
+
* §6.2.5.7 — decode a generic region with the arithmetic coder.
|
|
430
|
+
*
|
|
431
|
+
* The context for a pixel is the pattern of already-decoded neighbours the
|
|
432
|
+
* template names, read MSB first in the order the figures give. `TPGDON` adds a
|
|
433
|
+
* per-row bit that says "this row is the same as the one above", which is what
|
|
434
|
+
* makes a page of white space nearly free.
|
|
435
|
+
*
|
|
436
|
+
* @param mq The decoder, positioned at the region's data.
|
|
437
|
+
* @param cx The 2^16 generic contexts (shared across a segment's regions).
|
|
438
|
+
* @param width Region width in pixels.
|
|
439
|
+
* @param height Region height in pixels.
|
|
440
|
+
* @param template Which of the four templates (0-3).
|
|
441
|
+
* @param at The adaptive pixels, one for templates 1-3 and four for 0.
|
|
442
|
+
* @param tpgdon Whether typical prediction is on.
|
|
443
|
+
* @param skip A bitmap of pixels to leave white without decoding (halftone).
|
|
444
|
+
*/
|
|
445
|
+
function decodeGenericRegion(mq, cx, width, height, template, at, tpgdon, skip) {
|
|
446
|
+
const bmp = makeBitmap(width, height);
|
|
447
|
+
const px = (x, y) => x < 0 || x >= width || y < 0 ? 0 : bmp.data[y * width + x] ?? 0;
|
|
448
|
+
const TPGD_CX = [
|
|
449
|
+
39717,
|
|
450
|
+
1941,
|
|
451
|
+
229,
|
|
452
|
+
405
|
|
453
|
+
];
|
|
454
|
+
let ltp = 0;
|
|
455
|
+
for (let y = 0; y < height; y++) {
|
|
456
|
+
if (tpgdon) {
|
|
457
|
+
ltp ^= mq.decode(cx, TPGD_CX[template] ?? 0);
|
|
458
|
+
if (ltp === 1) {
|
|
459
|
+
if (y > 0) bmp.data.copyWithin(y * width, (y - 1) * width, y * width);
|
|
460
|
+
continue;
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
for (let x = 0; x < width; x++) {
|
|
464
|
+
if (skip && skip.data[y * width + x] === 1) continue;
|
|
465
|
+
let ctx;
|
|
466
|
+
const a = at;
|
|
467
|
+
switch (template) {
|
|
468
|
+
case 0:
|
|
469
|
+
ctx = px(x - 1, y) << 0 | px(x - 2, y) << 1 | px(x - 3, y) << 2 | px(x - 4, y) << 3 | px(x + (a[0]?.x ?? 3), y + (a[0]?.y ?? -1)) << 4 | px(x + 2, y - 1) << 5 | px(x + 1, y - 1) << 6 | px(x, y - 1) << 7 | px(x - 1, y - 1) << 8 | px(x - 2, y - 1) << 9 | px(x + (a[1]?.x ?? -3), y + (a[1]?.y ?? -1)) << 10 | px(x + (a[2]?.x ?? 2), y + (a[2]?.y ?? -2)) << 11 | px(x + 1, y - 2) << 12 | px(x, y - 2) << 13 | px(x - 1, y - 2) << 14 | px(x + (a[3]?.x ?? -2), y + (a[3]?.y ?? -2)) << 15;
|
|
470
|
+
break;
|
|
471
|
+
case 1:
|
|
472
|
+
ctx = px(x - 1, y) << 0 | px(x - 2, y) << 1 | px(x - 3, y) << 2 | px(x + (a[0]?.x ?? 3), y + (a[0]?.y ?? -1)) << 3 | px(x + 2, y - 1) << 4 | px(x + 1, y - 1) << 5 | px(x, y - 1) << 6 | px(x - 1, y - 1) << 7 | px(x - 2, y - 1) << 8 | px(x + 2, y - 2) << 9 | px(x + 1, y - 2) << 10 | px(x, y - 2) << 11 | px(x - 1, y - 2) << 12;
|
|
473
|
+
break;
|
|
474
|
+
case 2:
|
|
475
|
+
ctx = px(x - 1, y) << 0 | px(x - 2, y) << 1 | px(x + (a[0]?.x ?? 2), y + (a[0]?.y ?? -1)) << 2 | px(x + 1, y - 1) << 3 | px(x, y - 1) << 4 | px(x - 1, y - 1) << 5 | px(x - 2, y - 1) << 6 | px(x + 1, y - 2) << 7 | px(x, y - 2) << 8 | px(x - 1, y - 2) << 9;
|
|
476
|
+
break;
|
|
477
|
+
default:
|
|
478
|
+
ctx = px(x - 1, y) << 0 | px(x - 2, y) << 1 | px(x - 3, y) << 2 | px(x - 4, y) << 3 | px(x + (a[0]?.x ?? 2), y + (a[0]?.y ?? -1)) << 4 | px(x + 1, y - 1) << 5 | px(x, y - 1) << 6 | px(x - 1, y - 1) << 7 | px(x - 2, y - 1) << 8 | px(x - 3, y - 1) << 9;
|
|
479
|
+
break;
|
|
480
|
+
}
|
|
481
|
+
bmp.data[y * width + x] = mq.decode(cx, ctx);
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
return bmp;
|
|
485
|
+
}
|
|
486
|
+
/** The nominal refinement AT positions (§6.3.5.3). */
|
|
487
|
+
var NOMINAL_AT_REFINE = [{
|
|
488
|
+
x: -1,
|
|
489
|
+
y: -1
|
|
490
|
+
}, {
|
|
491
|
+
x: -1,
|
|
492
|
+
y: -1
|
|
493
|
+
}];
|
|
494
|
+
/**
|
|
495
|
+
* §6.3.5.6 — refine `reference` into a bitmap of `width` × `height`.
|
|
496
|
+
*
|
|
497
|
+
* Refinement codes a bitmap against one already decoded — the same glyph at a
|
|
498
|
+
* lower resolution, or the same region in a previous pass — so only where they
|
|
499
|
+
* differ costs anything.
|
|
500
|
+
*
|
|
501
|
+
* @param dx Where the reference sits relative to the region (GRREFERENCEDX).
|
|
502
|
+
* @param dy Likewise, vertically.
|
|
503
|
+
*/
|
|
504
|
+
/**
|
|
505
|
+
* §6.3.5.3 figures 12-14 — the refinement templates, as the pixels they read
|
|
506
|
+
* and the ORDER they read them in: every pixel of the bitmap being coded, then
|
|
507
|
+
* every pixel of the reference, most significant bit first. Template 0 takes an
|
|
508
|
+
* adaptive pixel in each list; template 1 is fixed.
|
|
509
|
+
*/
|
|
510
|
+
var REFINE_TEMPLATE = [{
|
|
511
|
+
coding: [
|
|
512
|
+
{
|
|
513
|
+
x: 0,
|
|
514
|
+
y: -1
|
|
515
|
+
},
|
|
516
|
+
{
|
|
517
|
+
x: 1,
|
|
518
|
+
y: -1
|
|
519
|
+
},
|
|
520
|
+
{
|
|
521
|
+
x: -1,
|
|
522
|
+
y: 0
|
|
523
|
+
}
|
|
524
|
+
],
|
|
525
|
+
reference: [
|
|
526
|
+
{
|
|
527
|
+
x: 0,
|
|
528
|
+
y: -1
|
|
529
|
+
},
|
|
530
|
+
{
|
|
531
|
+
x: 1,
|
|
532
|
+
y: -1
|
|
533
|
+
},
|
|
534
|
+
{
|
|
535
|
+
x: -1,
|
|
536
|
+
y: 0
|
|
537
|
+
},
|
|
538
|
+
{
|
|
539
|
+
x: 0,
|
|
540
|
+
y: 0
|
|
541
|
+
},
|
|
542
|
+
{
|
|
543
|
+
x: 1,
|
|
544
|
+
y: 0
|
|
545
|
+
},
|
|
546
|
+
{
|
|
547
|
+
x: -1,
|
|
548
|
+
y: 1
|
|
549
|
+
},
|
|
550
|
+
{
|
|
551
|
+
x: 0,
|
|
552
|
+
y: 1
|
|
553
|
+
},
|
|
554
|
+
{
|
|
555
|
+
x: 1,
|
|
556
|
+
y: 1
|
|
557
|
+
}
|
|
558
|
+
]
|
|
559
|
+
}, {
|
|
560
|
+
coding: [
|
|
561
|
+
{
|
|
562
|
+
x: -1,
|
|
563
|
+
y: -1
|
|
564
|
+
},
|
|
565
|
+
{
|
|
566
|
+
x: 0,
|
|
567
|
+
y: -1
|
|
568
|
+
},
|
|
569
|
+
{
|
|
570
|
+
x: 1,
|
|
571
|
+
y: -1
|
|
572
|
+
},
|
|
573
|
+
{
|
|
574
|
+
x: -1,
|
|
575
|
+
y: 0
|
|
576
|
+
}
|
|
577
|
+
],
|
|
578
|
+
reference: [
|
|
579
|
+
{
|
|
580
|
+
x: 0,
|
|
581
|
+
y: -1
|
|
582
|
+
},
|
|
583
|
+
{
|
|
584
|
+
x: -1,
|
|
585
|
+
y: 0
|
|
586
|
+
},
|
|
587
|
+
{
|
|
588
|
+
x: 0,
|
|
589
|
+
y: 0
|
|
590
|
+
},
|
|
591
|
+
{
|
|
592
|
+
x: 1,
|
|
593
|
+
y: 0
|
|
594
|
+
},
|
|
595
|
+
{
|
|
596
|
+
x: 0,
|
|
597
|
+
y: 1
|
|
598
|
+
},
|
|
599
|
+
{
|
|
600
|
+
x: 1,
|
|
601
|
+
y: 1
|
|
602
|
+
}
|
|
603
|
+
]
|
|
604
|
+
}];
|
|
605
|
+
/** §6.3.5.6 — the context the TPGRON bit is read in, per template. */
|
|
606
|
+
var REFINE_TPGR_CX = [32, 8];
|
|
607
|
+
/**
|
|
608
|
+
* §6.3.5.6 — refine `reference` into a bitmap of `width` × `height`.
|
|
609
|
+
*
|
|
610
|
+
* Refinement codes a bitmap against one already decoded — the same glyph at a
|
|
611
|
+
* lower resolution, or the same region in an earlier pass — so only where the
|
|
612
|
+
* two differ costs anything.
|
|
613
|
+
*
|
|
614
|
+
* @param dx Where the reference sits relative to the region (GRREFERENCEDX).
|
|
615
|
+
* @param dy Likewise, vertically.
|
|
616
|
+
*/
|
|
617
|
+
function decodeRefinement(mq, cx, width, height, template, reference, dx, dy, at, tpgron) {
|
|
618
|
+
const bmp = makeBitmap(width, height);
|
|
619
|
+
const t = REFINE_TEMPLATE[template === 0 ? 0 : 1];
|
|
620
|
+
const coding = template === 0 ? [...t.coding, at[0] ?? NOMINAL_AT_REFINE[0]] : t.coding;
|
|
621
|
+
const ref = template === 0 ? [...t.reference, at[1] ?? NOMINAL_AT_REFINE[1]] : t.reference;
|
|
622
|
+
const px = (x, y) => x < 0 || x >= width || y < 0 || y >= height ? 0 : bmp.data[y * width + x] ?? 0;
|
|
623
|
+
const rf = (x, y) => {
|
|
624
|
+
const rx = x - dx;
|
|
625
|
+
const ry = y - dy;
|
|
626
|
+
return rx < 0 || rx >= reference.width || ry < 0 || ry >= reference.height ? 0 : reference.data[ry * reference.width + rx] ?? 0;
|
|
627
|
+
};
|
|
628
|
+
let ltp = 0;
|
|
629
|
+
for (let y = 0; y < height; y++) {
|
|
630
|
+
if (tpgron) ltp ^= mq.decode(cx, REFINE_TPGR_CX[template === 0 ? 0 : 1]);
|
|
631
|
+
for (let x = 0; x < width; x++) {
|
|
632
|
+
if (ltp === 1) {
|
|
633
|
+
const s = rf(x - 1, y - 1) + rf(x, y - 1) + rf(x + 1, y - 1) + rf(x - 1, y) + rf(x, y) + rf(x + 1, y) + rf(x - 1, y + 1) + rf(x, y + 1) + rf(x + 1, y + 1);
|
|
634
|
+
if (s === 0 || s === 9) {
|
|
635
|
+
bmp.data[y * width + x] = s === 9 ? 1 : 0;
|
|
636
|
+
continue;
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
let ctx = 0;
|
|
640
|
+
for (const p of coding) ctx = ctx << 1 | px(x + p.x, y + p.y);
|
|
641
|
+
for (const p of ref) ctx = ctx << 1 | rf(x + p.x, y + p.y);
|
|
642
|
+
bmp.data[y * width + x] = mq.decode(cx, ctx);
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
return bmp;
|
|
646
|
+
}
|
|
647
|
+
/**
|
|
648
|
+
* §B.3 — assign a prefix code to every line from its length, shortest first,
|
|
649
|
+
* which is the canonical Huffman assignment and needs no code lengths stored.
|
|
650
|
+
*/
|
|
651
|
+
function buildHuffTable(lines) {
|
|
652
|
+
const used = lines.filter((l) => l.prefLen > 0);
|
|
653
|
+
const maxLen = Math.max(0, ...used.map((l) => l.prefLen));
|
|
654
|
+
const countOf = new Array(maxLen + 1).fill(0);
|
|
655
|
+
for (const l of used) countOf[l.prefLen] = (countOf[l.prefLen] ?? 0) + 1;
|
|
656
|
+
const firstCode = new Array(maxLen + 2).fill(0);
|
|
657
|
+
for (let len = 1; len <= maxLen; len++) firstCode[len] = firstCode[len - 1] + (countOf[len - 1] ?? 0) << 1;
|
|
658
|
+
const next = [...firstCode];
|
|
659
|
+
const out = [];
|
|
660
|
+
for (let len = 1; len <= maxLen; len++) for (const l of used) {
|
|
661
|
+
if (l.prefLen !== len) continue;
|
|
662
|
+
out.push({
|
|
663
|
+
...l,
|
|
664
|
+
code: next[len]
|
|
665
|
+
});
|
|
666
|
+
next[len]++;
|
|
667
|
+
}
|
|
668
|
+
return { lines: out };
|
|
669
|
+
}
|
|
670
|
+
/** A most-significant-bit-first reader over a segment's data. */
|
|
671
|
+
var BitReader = class {
|
|
672
|
+
pos = 0;
|
|
673
|
+
bit = 0;
|
|
674
|
+
constructor(d) {
|
|
675
|
+
this.d = d;
|
|
676
|
+
}
|
|
677
|
+
read() {
|
|
678
|
+
if (this.pos >= this.d.length) return 0;
|
|
679
|
+
const v = (this.d[this.pos] ?? 0) >> 7 - this.bit & 1;
|
|
680
|
+
if (++this.bit === 8) {
|
|
681
|
+
this.bit = 0;
|
|
682
|
+
this.pos++;
|
|
683
|
+
}
|
|
684
|
+
return v;
|
|
685
|
+
}
|
|
686
|
+
readBits(n) {
|
|
687
|
+
let v = 0;
|
|
688
|
+
for (let i = 0; i < n; i++) v = v * 2 + this.read();
|
|
689
|
+
return v;
|
|
690
|
+
}
|
|
691
|
+
align() {
|
|
692
|
+
if (this.bit !== 0) {
|
|
693
|
+
this.bit = 0;
|
|
694
|
+
this.pos++;
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
get bytePos() {
|
|
698
|
+
return this.pos;
|
|
699
|
+
}
|
|
700
|
+
skipTo(byte) {
|
|
701
|
+
this.pos = byte;
|
|
702
|
+
this.bit = 0;
|
|
703
|
+
}
|
|
704
|
+
};
|
|
705
|
+
/** §B.4 — read one value through a table, or OOB where the table has one. */
|
|
706
|
+
function huffDecode(r, t) {
|
|
707
|
+
let code = 0;
|
|
708
|
+
let len = 0;
|
|
709
|
+
for (let guard = 0; guard < 32; guard++) {
|
|
710
|
+
code = code << 1 | r.read();
|
|
711
|
+
len++;
|
|
712
|
+
for (const line of t.lines) {
|
|
713
|
+
if (line.prefLen !== len || line.code !== code) continue;
|
|
714
|
+
if (line.kind === "oob") return OOB;
|
|
715
|
+
if (line.rangeLen === 32) {
|
|
716
|
+
const v = r.readBits(32);
|
|
717
|
+
return line.kind === "lower" ? line.rangeLow - v : line.rangeLow + v;
|
|
718
|
+
}
|
|
719
|
+
const v = r.readBits(line.rangeLen);
|
|
720
|
+
return line.kind === "lower" ? line.rangeLow - v : line.rangeLow + v;
|
|
721
|
+
}
|
|
722
|
+
}
|
|
723
|
+
return OOB;
|
|
724
|
+
}
|
|
725
|
+
/**
|
|
726
|
+
* §B.5 Tables B.1–B.15 — the standard tables, as `[prefLen, rangeLen,
|
|
727
|
+
* rangeLow]` with `'lower'` and `'oob'` on the two lines that need them.
|
|
728
|
+
*
|
|
729
|
+
* They are transcribed rather than derived, and the corpus is what checks
|
|
730
|
+
* them: a table off by one line decodes rubble, and the suite says so at once.
|
|
731
|
+
*/
|
|
732
|
+
var STANDARD_TABLES = [
|
|
733
|
+
[
|
|
734
|
+
{
|
|
735
|
+
prefLen: 1,
|
|
736
|
+
rangeLen: 4,
|
|
737
|
+
rangeLow: 0
|
|
738
|
+
},
|
|
739
|
+
{
|
|
740
|
+
prefLen: 2,
|
|
741
|
+
rangeLen: 8,
|
|
742
|
+
rangeLow: 16
|
|
743
|
+
},
|
|
744
|
+
{
|
|
745
|
+
prefLen: 3,
|
|
746
|
+
rangeLen: 16,
|
|
747
|
+
rangeLow: 272
|
|
748
|
+
},
|
|
749
|
+
{
|
|
750
|
+
prefLen: 3,
|
|
751
|
+
rangeLen: 32,
|
|
752
|
+
rangeLow: 65808
|
|
753
|
+
}
|
|
754
|
+
],
|
|
755
|
+
[
|
|
756
|
+
{
|
|
757
|
+
prefLen: 1,
|
|
758
|
+
rangeLen: 0,
|
|
759
|
+
rangeLow: 0
|
|
760
|
+
},
|
|
761
|
+
{
|
|
762
|
+
prefLen: 2,
|
|
763
|
+
rangeLen: 0,
|
|
764
|
+
rangeLow: 1
|
|
765
|
+
},
|
|
766
|
+
{
|
|
767
|
+
prefLen: 3,
|
|
768
|
+
rangeLen: 0,
|
|
769
|
+
rangeLow: 2
|
|
770
|
+
},
|
|
771
|
+
{
|
|
772
|
+
prefLen: 4,
|
|
773
|
+
rangeLen: 3,
|
|
774
|
+
rangeLow: 3
|
|
775
|
+
},
|
|
776
|
+
{
|
|
777
|
+
prefLen: 5,
|
|
778
|
+
rangeLen: 6,
|
|
779
|
+
rangeLow: 11
|
|
780
|
+
},
|
|
781
|
+
{
|
|
782
|
+
prefLen: 6,
|
|
783
|
+
rangeLen: 32,
|
|
784
|
+
rangeLow: 75
|
|
785
|
+
},
|
|
786
|
+
{
|
|
787
|
+
prefLen: 6,
|
|
788
|
+
rangeLen: 0,
|
|
789
|
+
rangeLow: 0,
|
|
790
|
+
kind: "oob"
|
|
791
|
+
}
|
|
792
|
+
],
|
|
793
|
+
[
|
|
794
|
+
{
|
|
795
|
+
prefLen: 8,
|
|
796
|
+
rangeLen: 8,
|
|
797
|
+
rangeLow: -256
|
|
798
|
+
},
|
|
799
|
+
{
|
|
800
|
+
prefLen: 1,
|
|
801
|
+
rangeLen: 0,
|
|
802
|
+
rangeLow: 0
|
|
803
|
+
},
|
|
804
|
+
{
|
|
805
|
+
prefLen: 2,
|
|
806
|
+
rangeLen: 0,
|
|
807
|
+
rangeLow: 1
|
|
808
|
+
},
|
|
809
|
+
{
|
|
810
|
+
prefLen: 3,
|
|
811
|
+
rangeLen: 0,
|
|
812
|
+
rangeLow: 2
|
|
813
|
+
},
|
|
814
|
+
{
|
|
815
|
+
prefLen: 4,
|
|
816
|
+
rangeLen: 3,
|
|
817
|
+
rangeLow: 3
|
|
818
|
+
},
|
|
819
|
+
{
|
|
820
|
+
prefLen: 5,
|
|
821
|
+
rangeLen: 6,
|
|
822
|
+
rangeLow: 11
|
|
823
|
+
},
|
|
824
|
+
{
|
|
825
|
+
prefLen: 8,
|
|
826
|
+
rangeLen: 32,
|
|
827
|
+
rangeLow: -257,
|
|
828
|
+
kind: "lower"
|
|
829
|
+
},
|
|
830
|
+
{
|
|
831
|
+
prefLen: 7,
|
|
832
|
+
rangeLen: 32,
|
|
833
|
+
rangeLow: 75
|
|
834
|
+
},
|
|
835
|
+
{
|
|
836
|
+
prefLen: 6,
|
|
837
|
+
rangeLen: 0,
|
|
838
|
+
rangeLow: 0,
|
|
839
|
+
kind: "oob"
|
|
840
|
+
}
|
|
841
|
+
],
|
|
842
|
+
[
|
|
843
|
+
{
|
|
844
|
+
prefLen: 1,
|
|
845
|
+
rangeLen: 0,
|
|
846
|
+
rangeLow: 1
|
|
847
|
+
},
|
|
848
|
+
{
|
|
849
|
+
prefLen: 2,
|
|
850
|
+
rangeLen: 0,
|
|
851
|
+
rangeLow: 2
|
|
852
|
+
},
|
|
853
|
+
{
|
|
854
|
+
prefLen: 3,
|
|
855
|
+
rangeLen: 0,
|
|
856
|
+
rangeLow: 3
|
|
857
|
+
},
|
|
858
|
+
{
|
|
859
|
+
prefLen: 4,
|
|
860
|
+
rangeLen: 3,
|
|
861
|
+
rangeLow: 4
|
|
862
|
+
},
|
|
863
|
+
{
|
|
864
|
+
prefLen: 5,
|
|
865
|
+
rangeLen: 6,
|
|
866
|
+
rangeLow: 12
|
|
867
|
+
},
|
|
868
|
+
{
|
|
869
|
+
prefLen: 5,
|
|
870
|
+
rangeLen: 32,
|
|
871
|
+
rangeLow: 76
|
|
872
|
+
}
|
|
873
|
+
],
|
|
874
|
+
[
|
|
875
|
+
{
|
|
876
|
+
prefLen: 7,
|
|
877
|
+
rangeLen: 8,
|
|
878
|
+
rangeLow: -255
|
|
879
|
+
},
|
|
880
|
+
{
|
|
881
|
+
prefLen: 1,
|
|
882
|
+
rangeLen: 0,
|
|
883
|
+
rangeLow: 1
|
|
884
|
+
},
|
|
885
|
+
{
|
|
886
|
+
prefLen: 2,
|
|
887
|
+
rangeLen: 0,
|
|
888
|
+
rangeLow: 2
|
|
889
|
+
},
|
|
890
|
+
{
|
|
891
|
+
prefLen: 3,
|
|
892
|
+
rangeLen: 0,
|
|
893
|
+
rangeLow: 3
|
|
894
|
+
},
|
|
895
|
+
{
|
|
896
|
+
prefLen: 4,
|
|
897
|
+
rangeLen: 3,
|
|
898
|
+
rangeLow: 4
|
|
899
|
+
},
|
|
900
|
+
{
|
|
901
|
+
prefLen: 5,
|
|
902
|
+
rangeLen: 6,
|
|
903
|
+
rangeLow: 12
|
|
904
|
+
},
|
|
905
|
+
{
|
|
906
|
+
prefLen: 7,
|
|
907
|
+
rangeLen: 32,
|
|
908
|
+
rangeLow: -256,
|
|
909
|
+
kind: "lower"
|
|
910
|
+
},
|
|
911
|
+
{
|
|
912
|
+
prefLen: 6,
|
|
913
|
+
rangeLen: 32,
|
|
914
|
+
rangeLow: 76
|
|
915
|
+
}
|
|
916
|
+
],
|
|
917
|
+
[
|
|
918
|
+
{
|
|
919
|
+
prefLen: 5,
|
|
920
|
+
rangeLen: 10,
|
|
921
|
+
rangeLow: -2048
|
|
922
|
+
},
|
|
923
|
+
{
|
|
924
|
+
prefLen: 4,
|
|
925
|
+
rangeLen: 9,
|
|
926
|
+
rangeLow: -1024
|
|
927
|
+
},
|
|
928
|
+
{
|
|
929
|
+
prefLen: 4,
|
|
930
|
+
rangeLen: 8,
|
|
931
|
+
rangeLow: -512
|
|
932
|
+
},
|
|
933
|
+
{
|
|
934
|
+
prefLen: 4,
|
|
935
|
+
rangeLen: 7,
|
|
936
|
+
rangeLow: -256
|
|
937
|
+
},
|
|
938
|
+
{
|
|
939
|
+
prefLen: 5,
|
|
940
|
+
rangeLen: 6,
|
|
941
|
+
rangeLow: -128
|
|
942
|
+
},
|
|
943
|
+
{
|
|
944
|
+
prefLen: 5,
|
|
945
|
+
rangeLen: 5,
|
|
946
|
+
rangeLow: -64
|
|
947
|
+
},
|
|
948
|
+
{
|
|
949
|
+
prefLen: 4,
|
|
950
|
+
rangeLen: 5,
|
|
951
|
+
rangeLow: -32
|
|
952
|
+
},
|
|
953
|
+
{
|
|
954
|
+
prefLen: 2,
|
|
955
|
+
rangeLen: 7,
|
|
956
|
+
rangeLow: 0
|
|
957
|
+
},
|
|
958
|
+
{
|
|
959
|
+
prefLen: 3,
|
|
960
|
+
rangeLen: 7,
|
|
961
|
+
rangeLow: 128
|
|
962
|
+
},
|
|
963
|
+
{
|
|
964
|
+
prefLen: 3,
|
|
965
|
+
rangeLen: 8,
|
|
966
|
+
rangeLow: 256
|
|
967
|
+
},
|
|
968
|
+
{
|
|
969
|
+
prefLen: 4,
|
|
970
|
+
rangeLen: 9,
|
|
971
|
+
rangeLow: 512
|
|
972
|
+
},
|
|
973
|
+
{
|
|
974
|
+
prefLen: 4,
|
|
975
|
+
rangeLen: 10,
|
|
976
|
+
rangeLow: 1024
|
|
977
|
+
},
|
|
978
|
+
{
|
|
979
|
+
prefLen: 6,
|
|
980
|
+
rangeLen: 32,
|
|
981
|
+
rangeLow: -2049,
|
|
982
|
+
kind: "lower"
|
|
983
|
+
},
|
|
984
|
+
{
|
|
985
|
+
prefLen: 6,
|
|
986
|
+
rangeLen: 32,
|
|
987
|
+
rangeLow: 2048
|
|
988
|
+
}
|
|
989
|
+
],
|
|
990
|
+
[
|
|
991
|
+
{
|
|
992
|
+
prefLen: 4,
|
|
993
|
+
rangeLen: 9,
|
|
994
|
+
rangeLow: -1024
|
|
995
|
+
},
|
|
996
|
+
{
|
|
997
|
+
prefLen: 3,
|
|
998
|
+
rangeLen: 8,
|
|
999
|
+
rangeLow: -512
|
|
1000
|
+
},
|
|
1001
|
+
{
|
|
1002
|
+
prefLen: 4,
|
|
1003
|
+
rangeLen: 7,
|
|
1004
|
+
rangeLow: -256
|
|
1005
|
+
},
|
|
1006
|
+
{
|
|
1007
|
+
prefLen: 5,
|
|
1008
|
+
rangeLen: 6,
|
|
1009
|
+
rangeLow: -128
|
|
1010
|
+
},
|
|
1011
|
+
{
|
|
1012
|
+
prefLen: 5,
|
|
1013
|
+
rangeLen: 5,
|
|
1014
|
+
rangeLow: -64
|
|
1015
|
+
},
|
|
1016
|
+
{
|
|
1017
|
+
prefLen: 4,
|
|
1018
|
+
rangeLen: 5,
|
|
1019
|
+
rangeLow: -32
|
|
1020
|
+
},
|
|
1021
|
+
{
|
|
1022
|
+
prefLen: 4,
|
|
1023
|
+
rangeLen: 9,
|
|
1024
|
+
rangeLow: 0
|
|
1025
|
+
},
|
|
1026
|
+
{
|
|
1027
|
+
prefLen: 5,
|
|
1028
|
+
rangeLen: 10,
|
|
1029
|
+
rangeLow: 512
|
|
1030
|
+
},
|
|
1031
|
+
{
|
|
1032
|
+
prefLen: 3,
|
|
1033
|
+
rangeLen: 10,
|
|
1034
|
+
rangeLow: 1536
|
|
1035
|
+
},
|
|
1036
|
+
{
|
|
1037
|
+
prefLen: 5,
|
|
1038
|
+
rangeLen: 32,
|
|
1039
|
+
rangeLow: -1025,
|
|
1040
|
+
kind: "lower"
|
|
1041
|
+
},
|
|
1042
|
+
{
|
|
1043
|
+
prefLen: 5,
|
|
1044
|
+
rangeLen: 32,
|
|
1045
|
+
rangeLow: 2560
|
|
1046
|
+
}
|
|
1047
|
+
],
|
|
1048
|
+
[
|
|
1049
|
+
{
|
|
1050
|
+
prefLen: 8,
|
|
1051
|
+
rangeLen: 3,
|
|
1052
|
+
rangeLow: -15
|
|
1053
|
+
},
|
|
1054
|
+
{
|
|
1055
|
+
prefLen: 9,
|
|
1056
|
+
rangeLen: 1,
|
|
1057
|
+
rangeLow: -7
|
|
1058
|
+
},
|
|
1059
|
+
{
|
|
1060
|
+
prefLen: 8,
|
|
1061
|
+
rangeLen: 1,
|
|
1062
|
+
rangeLow: -5
|
|
1063
|
+
},
|
|
1064
|
+
{
|
|
1065
|
+
prefLen: 9,
|
|
1066
|
+
rangeLen: 0,
|
|
1067
|
+
rangeLow: -3
|
|
1068
|
+
},
|
|
1069
|
+
{
|
|
1070
|
+
prefLen: 7,
|
|
1071
|
+
rangeLen: 0,
|
|
1072
|
+
rangeLow: -2
|
|
1073
|
+
},
|
|
1074
|
+
{
|
|
1075
|
+
prefLen: 4,
|
|
1076
|
+
rangeLen: 0,
|
|
1077
|
+
rangeLow: -1
|
|
1078
|
+
},
|
|
1079
|
+
{
|
|
1080
|
+
prefLen: 2,
|
|
1081
|
+
rangeLen: 1,
|
|
1082
|
+
rangeLow: 0
|
|
1083
|
+
},
|
|
1084
|
+
{
|
|
1085
|
+
prefLen: 5,
|
|
1086
|
+
rangeLen: 0,
|
|
1087
|
+
rangeLow: 2
|
|
1088
|
+
},
|
|
1089
|
+
{
|
|
1090
|
+
prefLen: 6,
|
|
1091
|
+
rangeLen: 0,
|
|
1092
|
+
rangeLow: 3
|
|
1093
|
+
},
|
|
1094
|
+
{
|
|
1095
|
+
prefLen: 3,
|
|
1096
|
+
rangeLen: 4,
|
|
1097
|
+
rangeLow: 4
|
|
1098
|
+
},
|
|
1099
|
+
{
|
|
1100
|
+
prefLen: 6,
|
|
1101
|
+
rangeLen: 1,
|
|
1102
|
+
rangeLow: 20
|
|
1103
|
+
},
|
|
1104
|
+
{
|
|
1105
|
+
prefLen: 4,
|
|
1106
|
+
rangeLen: 4,
|
|
1107
|
+
rangeLow: 22
|
|
1108
|
+
},
|
|
1109
|
+
{
|
|
1110
|
+
prefLen: 4,
|
|
1111
|
+
rangeLen: 5,
|
|
1112
|
+
rangeLow: 38
|
|
1113
|
+
},
|
|
1114
|
+
{
|
|
1115
|
+
prefLen: 5,
|
|
1116
|
+
rangeLen: 6,
|
|
1117
|
+
rangeLow: 70
|
|
1118
|
+
},
|
|
1119
|
+
{
|
|
1120
|
+
prefLen: 5,
|
|
1121
|
+
rangeLen: 7,
|
|
1122
|
+
rangeLow: 134
|
|
1123
|
+
},
|
|
1124
|
+
{
|
|
1125
|
+
prefLen: 6,
|
|
1126
|
+
rangeLen: 7,
|
|
1127
|
+
rangeLow: 262
|
|
1128
|
+
},
|
|
1129
|
+
{
|
|
1130
|
+
prefLen: 7,
|
|
1131
|
+
rangeLen: 8,
|
|
1132
|
+
rangeLow: 390
|
|
1133
|
+
},
|
|
1134
|
+
{
|
|
1135
|
+
prefLen: 6,
|
|
1136
|
+
rangeLen: 10,
|
|
1137
|
+
rangeLow: 646
|
|
1138
|
+
},
|
|
1139
|
+
{
|
|
1140
|
+
prefLen: 9,
|
|
1141
|
+
rangeLen: 32,
|
|
1142
|
+
rangeLow: -16,
|
|
1143
|
+
kind: "lower"
|
|
1144
|
+
},
|
|
1145
|
+
{
|
|
1146
|
+
prefLen: 9,
|
|
1147
|
+
rangeLen: 32,
|
|
1148
|
+
rangeLow: 1670
|
|
1149
|
+
},
|
|
1150
|
+
{
|
|
1151
|
+
prefLen: 2,
|
|
1152
|
+
rangeLen: 0,
|
|
1153
|
+
rangeLow: 0,
|
|
1154
|
+
kind: "oob"
|
|
1155
|
+
}
|
|
1156
|
+
],
|
|
1157
|
+
[
|
|
1158
|
+
{
|
|
1159
|
+
prefLen: 8,
|
|
1160
|
+
rangeLen: 4,
|
|
1161
|
+
rangeLow: -31
|
|
1162
|
+
},
|
|
1163
|
+
{
|
|
1164
|
+
prefLen: 9,
|
|
1165
|
+
rangeLen: 2,
|
|
1166
|
+
rangeLow: -15
|
|
1167
|
+
},
|
|
1168
|
+
{
|
|
1169
|
+
prefLen: 8,
|
|
1170
|
+
rangeLen: 2,
|
|
1171
|
+
rangeLow: -11
|
|
1172
|
+
},
|
|
1173
|
+
{
|
|
1174
|
+
prefLen: 9,
|
|
1175
|
+
rangeLen: 1,
|
|
1176
|
+
rangeLow: -7
|
|
1177
|
+
},
|
|
1178
|
+
{
|
|
1179
|
+
prefLen: 7,
|
|
1180
|
+
rangeLen: 1,
|
|
1181
|
+
rangeLow: -5
|
|
1182
|
+
},
|
|
1183
|
+
{
|
|
1184
|
+
prefLen: 4,
|
|
1185
|
+
rangeLen: 1,
|
|
1186
|
+
rangeLow: -3
|
|
1187
|
+
},
|
|
1188
|
+
{
|
|
1189
|
+
prefLen: 3,
|
|
1190
|
+
rangeLen: 1,
|
|
1191
|
+
rangeLow: -1
|
|
1192
|
+
},
|
|
1193
|
+
{
|
|
1194
|
+
prefLen: 3,
|
|
1195
|
+
rangeLen: 1,
|
|
1196
|
+
rangeLow: 1
|
|
1197
|
+
},
|
|
1198
|
+
{
|
|
1199
|
+
prefLen: 5,
|
|
1200
|
+
rangeLen: 1,
|
|
1201
|
+
rangeLow: 3
|
|
1202
|
+
},
|
|
1203
|
+
{
|
|
1204
|
+
prefLen: 6,
|
|
1205
|
+
rangeLen: 1,
|
|
1206
|
+
rangeLow: 5
|
|
1207
|
+
},
|
|
1208
|
+
{
|
|
1209
|
+
prefLen: 3,
|
|
1210
|
+
rangeLen: 5,
|
|
1211
|
+
rangeLow: 7
|
|
1212
|
+
},
|
|
1213
|
+
{
|
|
1214
|
+
prefLen: 6,
|
|
1215
|
+
rangeLen: 2,
|
|
1216
|
+
rangeLow: 39
|
|
1217
|
+
},
|
|
1218
|
+
{
|
|
1219
|
+
prefLen: 4,
|
|
1220
|
+
rangeLen: 5,
|
|
1221
|
+
rangeLow: 43
|
|
1222
|
+
},
|
|
1223
|
+
{
|
|
1224
|
+
prefLen: 4,
|
|
1225
|
+
rangeLen: 6,
|
|
1226
|
+
rangeLow: 75
|
|
1227
|
+
},
|
|
1228
|
+
{
|
|
1229
|
+
prefLen: 5,
|
|
1230
|
+
rangeLen: 7,
|
|
1231
|
+
rangeLow: 139
|
|
1232
|
+
},
|
|
1233
|
+
{
|
|
1234
|
+
prefLen: 5,
|
|
1235
|
+
rangeLen: 8,
|
|
1236
|
+
rangeLow: 267
|
|
1237
|
+
},
|
|
1238
|
+
{
|
|
1239
|
+
prefLen: 6,
|
|
1240
|
+
rangeLen: 8,
|
|
1241
|
+
rangeLow: 523
|
|
1242
|
+
},
|
|
1243
|
+
{
|
|
1244
|
+
prefLen: 7,
|
|
1245
|
+
rangeLen: 9,
|
|
1246
|
+
rangeLow: 779
|
|
1247
|
+
},
|
|
1248
|
+
{
|
|
1249
|
+
prefLen: 6,
|
|
1250
|
+
rangeLen: 11,
|
|
1251
|
+
rangeLow: 1291
|
|
1252
|
+
},
|
|
1253
|
+
{
|
|
1254
|
+
prefLen: 9,
|
|
1255
|
+
rangeLen: 32,
|
|
1256
|
+
rangeLow: -32,
|
|
1257
|
+
kind: "lower"
|
|
1258
|
+
},
|
|
1259
|
+
{
|
|
1260
|
+
prefLen: 9,
|
|
1261
|
+
rangeLen: 32,
|
|
1262
|
+
rangeLow: 3339
|
|
1263
|
+
},
|
|
1264
|
+
{
|
|
1265
|
+
prefLen: 2,
|
|
1266
|
+
rangeLen: 0,
|
|
1267
|
+
rangeLow: 0,
|
|
1268
|
+
kind: "oob"
|
|
1269
|
+
}
|
|
1270
|
+
],
|
|
1271
|
+
[
|
|
1272
|
+
{
|
|
1273
|
+
prefLen: 7,
|
|
1274
|
+
rangeLen: 4,
|
|
1275
|
+
rangeLow: -21
|
|
1276
|
+
},
|
|
1277
|
+
{
|
|
1278
|
+
prefLen: 8,
|
|
1279
|
+
rangeLen: 0,
|
|
1280
|
+
rangeLow: -5
|
|
1281
|
+
},
|
|
1282
|
+
{
|
|
1283
|
+
prefLen: 7,
|
|
1284
|
+
rangeLen: 0,
|
|
1285
|
+
rangeLow: -4
|
|
1286
|
+
},
|
|
1287
|
+
{
|
|
1288
|
+
prefLen: 5,
|
|
1289
|
+
rangeLen: 0,
|
|
1290
|
+
rangeLow: -3
|
|
1291
|
+
},
|
|
1292
|
+
{
|
|
1293
|
+
prefLen: 2,
|
|
1294
|
+
rangeLen: 2,
|
|
1295
|
+
rangeLow: -2
|
|
1296
|
+
},
|
|
1297
|
+
{
|
|
1298
|
+
prefLen: 5,
|
|
1299
|
+
rangeLen: 0,
|
|
1300
|
+
rangeLow: 2
|
|
1301
|
+
},
|
|
1302
|
+
{
|
|
1303
|
+
prefLen: 6,
|
|
1304
|
+
rangeLen: 0,
|
|
1305
|
+
rangeLow: 3
|
|
1306
|
+
},
|
|
1307
|
+
{
|
|
1308
|
+
prefLen: 7,
|
|
1309
|
+
rangeLen: 0,
|
|
1310
|
+
rangeLow: 4
|
|
1311
|
+
},
|
|
1312
|
+
{
|
|
1313
|
+
prefLen: 8,
|
|
1314
|
+
rangeLen: 0,
|
|
1315
|
+
rangeLow: 5
|
|
1316
|
+
},
|
|
1317
|
+
{
|
|
1318
|
+
prefLen: 2,
|
|
1319
|
+
rangeLen: 6,
|
|
1320
|
+
rangeLow: 6
|
|
1321
|
+
},
|
|
1322
|
+
{
|
|
1323
|
+
prefLen: 5,
|
|
1324
|
+
rangeLen: 5,
|
|
1325
|
+
rangeLow: 70
|
|
1326
|
+
},
|
|
1327
|
+
{
|
|
1328
|
+
prefLen: 6,
|
|
1329
|
+
rangeLen: 5,
|
|
1330
|
+
rangeLow: 102
|
|
1331
|
+
},
|
|
1332
|
+
{
|
|
1333
|
+
prefLen: 7,
|
|
1334
|
+
rangeLen: 6,
|
|
1335
|
+
rangeLow: 134
|
|
1336
|
+
},
|
|
1337
|
+
{
|
|
1338
|
+
prefLen: 8,
|
|
1339
|
+
rangeLen: 7,
|
|
1340
|
+
rangeLow: 198
|
|
1341
|
+
},
|
|
1342
|
+
{
|
|
1343
|
+
prefLen: 8,
|
|
1344
|
+
rangeLen: 8,
|
|
1345
|
+
rangeLow: 326
|
|
1346
|
+
},
|
|
1347
|
+
{
|
|
1348
|
+
prefLen: 8,
|
|
1349
|
+
rangeLen: 9,
|
|
1350
|
+
rangeLow: 582
|
|
1351
|
+
},
|
|
1352
|
+
{
|
|
1353
|
+
prefLen: 8,
|
|
1354
|
+
rangeLen: 10,
|
|
1355
|
+
rangeLow: 1094
|
|
1356
|
+
},
|
|
1357
|
+
{
|
|
1358
|
+
prefLen: 7,
|
|
1359
|
+
rangeLen: 11,
|
|
1360
|
+
rangeLow: 2118
|
|
1361
|
+
},
|
|
1362
|
+
{
|
|
1363
|
+
prefLen: 8,
|
|
1364
|
+
rangeLen: 32,
|
|
1365
|
+
rangeLow: -22,
|
|
1366
|
+
kind: "lower"
|
|
1367
|
+
},
|
|
1368
|
+
{
|
|
1369
|
+
prefLen: 8,
|
|
1370
|
+
rangeLen: 32,
|
|
1371
|
+
rangeLow: 4166
|
|
1372
|
+
},
|
|
1373
|
+
{
|
|
1374
|
+
prefLen: 2,
|
|
1375
|
+
rangeLen: 0,
|
|
1376
|
+
rangeLow: 0,
|
|
1377
|
+
kind: "oob"
|
|
1378
|
+
}
|
|
1379
|
+
],
|
|
1380
|
+
[
|
|
1381
|
+
{
|
|
1382
|
+
prefLen: 1,
|
|
1383
|
+
rangeLen: 0,
|
|
1384
|
+
rangeLow: 1
|
|
1385
|
+
},
|
|
1386
|
+
{
|
|
1387
|
+
prefLen: 2,
|
|
1388
|
+
rangeLen: 1,
|
|
1389
|
+
rangeLow: 2
|
|
1390
|
+
},
|
|
1391
|
+
{
|
|
1392
|
+
prefLen: 4,
|
|
1393
|
+
rangeLen: 0,
|
|
1394
|
+
rangeLow: 4
|
|
1395
|
+
},
|
|
1396
|
+
{
|
|
1397
|
+
prefLen: 4,
|
|
1398
|
+
rangeLen: 1,
|
|
1399
|
+
rangeLow: 5
|
|
1400
|
+
},
|
|
1401
|
+
{
|
|
1402
|
+
prefLen: 5,
|
|
1403
|
+
rangeLen: 1,
|
|
1404
|
+
rangeLow: 7
|
|
1405
|
+
},
|
|
1406
|
+
{
|
|
1407
|
+
prefLen: 5,
|
|
1408
|
+
rangeLen: 2,
|
|
1409
|
+
rangeLow: 9
|
|
1410
|
+
},
|
|
1411
|
+
{
|
|
1412
|
+
prefLen: 6,
|
|
1413
|
+
rangeLen: 2,
|
|
1414
|
+
rangeLow: 13
|
|
1415
|
+
},
|
|
1416
|
+
{
|
|
1417
|
+
prefLen: 7,
|
|
1418
|
+
rangeLen: 2,
|
|
1419
|
+
rangeLow: 17
|
|
1420
|
+
},
|
|
1421
|
+
{
|
|
1422
|
+
prefLen: 7,
|
|
1423
|
+
rangeLen: 3,
|
|
1424
|
+
rangeLow: 21
|
|
1425
|
+
},
|
|
1426
|
+
{
|
|
1427
|
+
prefLen: 7,
|
|
1428
|
+
rangeLen: 4,
|
|
1429
|
+
rangeLow: 29
|
|
1430
|
+
},
|
|
1431
|
+
{
|
|
1432
|
+
prefLen: 7,
|
|
1433
|
+
rangeLen: 5,
|
|
1434
|
+
rangeLow: 45
|
|
1435
|
+
},
|
|
1436
|
+
{
|
|
1437
|
+
prefLen: 7,
|
|
1438
|
+
rangeLen: 6,
|
|
1439
|
+
rangeLow: 77
|
|
1440
|
+
},
|
|
1441
|
+
{
|
|
1442
|
+
prefLen: 7,
|
|
1443
|
+
rangeLen: 32,
|
|
1444
|
+
rangeLow: 141
|
|
1445
|
+
}
|
|
1446
|
+
],
|
|
1447
|
+
[
|
|
1448
|
+
{
|
|
1449
|
+
prefLen: 1,
|
|
1450
|
+
rangeLen: 0,
|
|
1451
|
+
rangeLow: 1
|
|
1452
|
+
},
|
|
1453
|
+
{
|
|
1454
|
+
prefLen: 2,
|
|
1455
|
+
rangeLen: 0,
|
|
1456
|
+
rangeLow: 2
|
|
1457
|
+
},
|
|
1458
|
+
{
|
|
1459
|
+
prefLen: 3,
|
|
1460
|
+
rangeLen: 1,
|
|
1461
|
+
rangeLow: 3
|
|
1462
|
+
},
|
|
1463
|
+
{
|
|
1464
|
+
prefLen: 5,
|
|
1465
|
+
rangeLen: 0,
|
|
1466
|
+
rangeLow: 5
|
|
1467
|
+
},
|
|
1468
|
+
{
|
|
1469
|
+
prefLen: 5,
|
|
1470
|
+
rangeLen: 1,
|
|
1471
|
+
rangeLow: 6
|
|
1472
|
+
},
|
|
1473
|
+
{
|
|
1474
|
+
prefLen: 6,
|
|
1475
|
+
rangeLen: 1,
|
|
1476
|
+
rangeLow: 8
|
|
1477
|
+
},
|
|
1478
|
+
{
|
|
1479
|
+
prefLen: 7,
|
|
1480
|
+
rangeLen: 0,
|
|
1481
|
+
rangeLow: 10
|
|
1482
|
+
},
|
|
1483
|
+
{
|
|
1484
|
+
prefLen: 7,
|
|
1485
|
+
rangeLen: 1,
|
|
1486
|
+
rangeLow: 11
|
|
1487
|
+
},
|
|
1488
|
+
{
|
|
1489
|
+
prefLen: 7,
|
|
1490
|
+
rangeLen: 2,
|
|
1491
|
+
rangeLow: 13
|
|
1492
|
+
},
|
|
1493
|
+
{
|
|
1494
|
+
prefLen: 7,
|
|
1495
|
+
rangeLen: 3,
|
|
1496
|
+
rangeLow: 17
|
|
1497
|
+
},
|
|
1498
|
+
{
|
|
1499
|
+
prefLen: 7,
|
|
1500
|
+
rangeLen: 4,
|
|
1501
|
+
rangeLow: 25
|
|
1502
|
+
},
|
|
1503
|
+
{
|
|
1504
|
+
prefLen: 8,
|
|
1505
|
+
rangeLen: 5,
|
|
1506
|
+
rangeLow: 41
|
|
1507
|
+
},
|
|
1508
|
+
{
|
|
1509
|
+
prefLen: 8,
|
|
1510
|
+
rangeLen: 32,
|
|
1511
|
+
rangeLow: 73
|
|
1512
|
+
}
|
|
1513
|
+
],
|
|
1514
|
+
[
|
|
1515
|
+
{
|
|
1516
|
+
prefLen: 1,
|
|
1517
|
+
rangeLen: 0,
|
|
1518
|
+
rangeLow: 1
|
|
1519
|
+
},
|
|
1520
|
+
{
|
|
1521
|
+
prefLen: 3,
|
|
1522
|
+
rangeLen: 0,
|
|
1523
|
+
rangeLow: 2
|
|
1524
|
+
},
|
|
1525
|
+
{
|
|
1526
|
+
prefLen: 4,
|
|
1527
|
+
rangeLen: 0,
|
|
1528
|
+
rangeLow: 3
|
|
1529
|
+
},
|
|
1530
|
+
{
|
|
1531
|
+
prefLen: 5,
|
|
1532
|
+
rangeLen: 0,
|
|
1533
|
+
rangeLow: 4
|
|
1534
|
+
},
|
|
1535
|
+
{
|
|
1536
|
+
prefLen: 4,
|
|
1537
|
+
rangeLen: 1,
|
|
1538
|
+
rangeLow: 5
|
|
1539
|
+
},
|
|
1540
|
+
{
|
|
1541
|
+
prefLen: 3,
|
|
1542
|
+
rangeLen: 3,
|
|
1543
|
+
rangeLow: 7
|
|
1544
|
+
},
|
|
1545
|
+
{
|
|
1546
|
+
prefLen: 6,
|
|
1547
|
+
rangeLen: 1,
|
|
1548
|
+
rangeLow: 15
|
|
1549
|
+
},
|
|
1550
|
+
{
|
|
1551
|
+
prefLen: 6,
|
|
1552
|
+
rangeLen: 2,
|
|
1553
|
+
rangeLow: 17
|
|
1554
|
+
},
|
|
1555
|
+
{
|
|
1556
|
+
prefLen: 6,
|
|
1557
|
+
rangeLen: 3,
|
|
1558
|
+
rangeLow: 21
|
|
1559
|
+
},
|
|
1560
|
+
{
|
|
1561
|
+
prefLen: 6,
|
|
1562
|
+
rangeLen: 4,
|
|
1563
|
+
rangeLow: 29
|
|
1564
|
+
},
|
|
1565
|
+
{
|
|
1566
|
+
prefLen: 6,
|
|
1567
|
+
rangeLen: 5,
|
|
1568
|
+
rangeLow: 45
|
|
1569
|
+
},
|
|
1570
|
+
{
|
|
1571
|
+
prefLen: 7,
|
|
1572
|
+
rangeLen: 6,
|
|
1573
|
+
rangeLow: 77
|
|
1574
|
+
},
|
|
1575
|
+
{
|
|
1576
|
+
prefLen: 7,
|
|
1577
|
+
rangeLen: 32,
|
|
1578
|
+
rangeLow: 141
|
|
1579
|
+
}
|
|
1580
|
+
],
|
|
1581
|
+
[
|
|
1582
|
+
{
|
|
1583
|
+
prefLen: 3,
|
|
1584
|
+
rangeLen: 0,
|
|
1585
|
+
rangeLow: -2
|
|
1586
|
+
},
|
|
1587
|
+
{
|
|
1588
|
+
prefLen: 3,
|
|
1589
|
+
rangeLen: 0,
|
|
1590
|
+
rangeLow: -1
|
|
1591
|
+
},
|
|
1592
|
+
{
|
|
1593
|
+
prefLen: 1,
|
|
1594
|
+
rangeLen: 0,
|
|
1595
|
+
rangeLow: 0
|
|
1596
|
+
},
|
|
1597
|
+
{
|
|
1598
|
+
prefLen: 3,
|
|
1599
|
+
rangeLen: 0,
|
|
1600
|
+
rangeLow: 1
|
|
1601
|
+
},
|
|
1602
|
+
{
|
|
1603
|
+
prefLen: 3,
|
|
1604
|
+
rangeLen: 0,
|
|
1605
|
+
rangeLow: 2
|
|
1606
|
+
}
|
|
1607
|
+
],
|
|
1608
|
+
[
|
|
1609
|
+
{
|
|
1610
|
+
prefLen: 7,
|
|
1611
|
+
rangeLen: 4,
|
|
1612
|
+
rangeLow: -24
|
|
1613
|
+
},
|
|
1614
|
+
{
|
|
1615
|
+
prefLen: 6,
|
|
1616
|
+
rangeLen: 2,
|
|
1617
|
+
rangeLow: -8
|
|
1618
|
+
},
|
|
1619
|
+
{
|
|
1620
|
+
prefLen: 5,
|
|
1621
|
+
rangeLen: 1,
|
|
1622
|
+
rangeLow: -4
|
|
1623
|
+
},
|
|
1624
|
+
{
|
|
1625
|
+
prefLen: 4,
|
|
1626
|
+
rangeLen: 0,
|
|
1627
|
+
rangeLow: -2
|
|
1628
|
+
},
|
|
1629
|
+
{
|
|
1630
|
+
prefLen: 3,
|
|
1631
|
+
rangeLen: 0,
|
|
1632
|
+
rangeLow: -1
|
|
1633
|
+
},
|
|
1634
|
+
{
|
|
1635
|
+
prefLen: 1,
|
|
1636
|
+
rangeLen: 0,
|
|
1637
|
+
rangeLow: 0
|
|
1638
|
+
},
|
|
1639
|
+
{
|
|
1640
|
+
prefLen: 3,
|
|
1641
|
+
rangeLen: 0,
|
|
1642
|
+
rangeLow: 1
|
|
1643
|
+
},
|
|
1644
|
+
{
|
|
1645
|
+
prefLen: 4,
|
|
1646
|
+
rangeLen: 0,
|
|
1647
|
+
rangeLow: 2
|
|
1648
|
+
},
|
|
1649
|
+
{
|
|
1650
|
+
prefLen: 5,
|
|
1651
|
+
rangeLen: 1,
|
|
1652
|
+
rangeLow: 3
|
|
1653
|
+
},
|
|
1654
|
+
{
|
|
1655
|
+
prefLen: 6,
|
|
1656
|
+
rangeLen: 2,
|
|
1657
|
+
rangeLow: 5
|
|
1658
|
+
},
|
|
1659
|
+
{
|
|
1660
|
+
prefLen: 7,
|
|
1661
|
+
rangeLen: 4,
|
|
1662
|
+
rangeLow: 9
|
|
1663
|
+
},
|
|
1664
|
+
{
|
|
1665
|
+
prefLen: 7,
|
|
1666
|
+
rangeLen: 32,
|
|
1667
|
+
rangeLow: -25,
|
|
1668
|
+
kind: "lower"
|
|
1669
|
+
},
|
|
1670
|
+
{
|
|
1671
|
+
prefLen: 7,
|
|
1672
|
+
rangeLen: 32,
|
|
1673
|
+
rangeLow: 25
|
|
1674
|
+
}
|
|
1675
|
+
]
|
|
1676
|
+
];
|
|
1677
|
+
/** Table B.n, ready to read with. */
|
|
1678
|
+
function standardTable(n) {
|
|
1679
|
+
return buildHuffTable(STANDARD_TABLES[n - 1] ?? STANDARD_TABLES[0]);
|
|
1680
|
+
}
|
|
1681
|
+
/** §B.2 — a custom table, from a type-53 segment. */
|
|
1682
|
+
function parseCustomTable(data) {
|
|
1683
|
+
const r = new Reader(data);
|
|
1684
|
+
const flags = r.u8();
|
|
1685
|
+
const oob = (flags & 1) !== 0;
|
|
1686
|
+
const prefLenSize = (flags >> 1 & 7) + 1;
|
|
1687
|
+
const rangeLenSize = (flags >> 4 & 7) + 1;
|
|
1688
|
+
const low = r.u32() | 0;
|
|
1689
|
+
const high = r.u32() | 0;
|
|
1690
|
+
const bits = new BitReader(data.subarray(r.pos));
|
|
1691
|
+
const lines = [];
|
|
1692
|
+
let cur = low;
|
|
1693
|
+
while (cur < high) {
|
|
1694
|
+
const prefLen = bits.readBits(prefLenSize);
|
|
1695
|
+
const rangeLen = bits.readBits(rangeLenSize);
|
|
1696
|
+
lines.push({
|
|
1697
|
+
prefLen,
|
|
1698
|
+
rangeLen,
|
|
1699
|
+
rangeLow: cur
|
|
1700
|
+
});
|
|
1701
|
+
cur += 1 << rangeLen;
|
|
1702
|
+
if (lines.length > 4096) break;
|
|
1703
|
+
}
|
|
1704
|
+
lines.push({
|
|
1705
|
+
prefLen: bits.readBits(prefLenSize),
|
|
1706
|
+
rangeLen: 32,
|
|
1707
|
+
rangeLow: low - 1,
|
|
1708
|
+
kind: "lower"
|
|
1709
|
+
});
|
|
1710
|
+
lines.push({
|
|
1711
|
+
prefLen: bits.readBits(prefLenSize),
|
|
1712
|
+
rangeLen: 32,
|
|
1713
|
+
rangeLow: high
|
|
1714
|
+
});
|
|
1715
|
+
if (oob) lines.push({
|
|
1716
|
+
prefLen: bits.readBits(prefLenSize),
|
|
1717
|
+
rangeLen: 0,
|
|
1718
|
+
rangeLow: 0,
|
|
1719
|
+
kind: "oob"
|
|
1720
|
+
});
|
|
1721
|
+
return buildHuffTable(lines);
|
|
1722
|
+
}
|
|
1723
|
+
/** §A.2 returns this instead of a number when the coder signals out-of-band. */
|
|
1724
|
+
var OOB = Symbol("OOB");
|
|
1725
|
+
/**
|
|
1726
|
+
* §A.2 — the integer decoding procedure.
|
|
1727
|
+
*
|
|
1728
|
+
* Every count, coordinate and delta in a symbol dictionary or text region comes
|
|
1729
|
+
* through here: a sign bit, then a prefix that says how many magnitude bits
|
|
1730
|
+
* follow and what to add to them. The context is a walk down a binary tree of
|
|
1731
|
+
* the bits read so far, which is why each kind of integer (IADH, IADW, IADT …)
|
|
1732
|
+
* needs a context set of its own.
|
|
1733
|
+
*/
|
|
1734
|
+
var IntDecoder = class {
|
|
1735
|
+
cx = newContexts(512);
|
|
1736
|
+
constructor(mq) {
|
|
1737
|
+
this.mq = mq;
|
|
1738
|
+
}
|
|
1739
|
+
decode() {
|
|
1740
|
+
let prev = 1;
|
|
1741
|
+
const bit = () => {
|
|
1742
|
+
const b = this.mq.decode(this.cx, prev);
|
|
1743
|
+
prev = prev < 256 ? prev << 1 | b : ((prev << 1 | b) & 511 | 256) >>> 0;
|
|
1744
|
+
return b;
|
|
1745
|
+
};
|
|
1746
|
+
const sign = bit();
|
|
1747
|
+
const FIELDS = [
|
|
1748
|
+
[2, 0],
|
|
1749
|
+
[4, 4],
|
|
1750
|
+
[6, 20],
|
|
1751
|
+
[8, 84],
|
|
1752
|
+
[12, 340],
|
|
1753
|
+
[32, 4436]
|
|
1754
|
+
];
|
|
1755
|
+
let level = 0;
|
|
1756
|
+
while (level < 5 && bit() === 1) level++;
|
|
1757
|
+
const [n, offset] = FIELDS[level];
|
|
1758
|
+
let v = 0;
|
|
1759
|
+
for (let i = 0; i < n; i++) v = v << 1 | bit();
|
|
1760
|
+
v += offset;
|
|
1761
|
+
if (sign === 0) return v;
|
|
1762
|
+
return v === 0 ? OOB : -v;
|
|
1763
|
+
}
|
|
1764
|
+
};
|
|
1765
|
+
/** §A.3 — the symbol ID decoder, a fixed-width code down its own context tree. */
|
|
1766
|
+
var IdDecoder = class {
|
|
1767
|
+
cx;
|
|
1768
|
+
constructor(mq, codeLen) {
|
|
1769
|
+
this.mq = mq;
|
|
1770
|
+
this.codeLen = codeLen;
|
|
1771
|
+
this.cx = newContexts(1 << codeLen + 1);
|
|
1772
|
+
}
|
|
1773
|
+
decode() {
|
|
1774
|
+
let prev = 1;
|
|
1775
|
+
for (let i = 0; i < this.codeLen; i++) prev = prev << 1 | this.mq.decode(this.cx, prev);
|
|
1776
|
+
return prev - (1 << this.codeLen);
|
|
1777
|
+
}
|
|
1778
|
+
};
|
|
1779
|
+
/** How a text region places each instance against the point it names (§6.4.5). */
|
|
1780
|
+
var Corner = /* @__PURE__ */ function(Corner) {
|
|
1781
|
+
Corner[Corner["BottomLeft"] = 0] = "BottomLeft";
|
|
1782
|
+
Corner[Corner["TopLeft"] = 1] = "TopLeft";
|
|
1783
|
+
Corner[Corner["BottomRight"] = 2] = "BottomRight";
|
|
1784
|
+
Corner[Corner["TopRight"] = 3] = "TopRight";
|
|
1785
|
+
return Corner;
|
|
1786
|
+
}(Corner || {});
|
|
1787
|
+
/**
|
|
1788
|
+
* §6.4.5 — draw a text region: a run of strips, each holding instances of the
|
|
1789
|
+
* symbols in `symbols`, placed by running coordinates rather than absolute ones.
|
|
1790
|
+
*
|
|
1791
|
+
* This is what JBIG2 is for. A scanned page is not stored as pixels but as "the
|
|
1792
|
+
* shape called 37, here; the shape called 12, four pixels on" — so the letter
|
|
1793
|
+
* "e" costs its bitmap once and a few bits per occurrence after that.
|
|
1794
|
+
*/
|
|
1795
|
+
function decodeTextRegion(mq, symbols, p, stripsLog) {
|
|
1796
|
+
const bmp = makeBitmap(p.width, p.height, p.defPixel);
|
|
1797
|
+
const strips = 1 << stripsLog;
|
|
1798
|
+
const codeLen = Math.max(1, Math.ceil(Math.log2(Math.max(symbols.length, 1))));
|
|
1799
|
+
const iadt = new IntDecoder(mq);
|
|
1800
|
+
const iafs = new IntDecoder(mq);
|
|
1801
|
+
const iads = new IntDecoder(mq);
|
|
1802
|
+
const iait = new IntDecoder(mq);
|
|
1803
|
+
const iari = new IntDecoder(mq);
|
|
1804
|
+
const iardw = new IntDecoder(mq);
|
|
1805
|
+
const iardh = new IntDecoder(mq);
|
|
1806
|
+
const iardx = new IntDecoder(mq);
|
|
1807
|
+
const iardy = new IntDecoder(mq);
|
|
1808
|
+
const iaid = new IdDecoder(mq, codeLen);
|
|
1809
|
+
const refineCx = newContexts(8192);
|
|
1810
|
+
const num = (v) => v === OOB ? 0 : v;
|
|
1811
|
+
let stripT = -num(iadt.decode()) * strips;
|
|
1812
|
+
let firstS = 0;
|
|
1813
|
+
let placed = 0;
|
|
1814
|
+
while (placed < p.instances) {
|
|
1815
|
+
stripT += num(iadt.decode()) * strips;
|
|
1816
|
+
firstS += num(iafs.decode());
|
|
1817
|
+
let curS = firstS;
|
|
1818
|
+
for (;;) {
|
|
1819
|
+
const t = strips === 1 ? 0 : num(iait.decode());
|
|
1820
|
+
const tt = stripT + t;
|
|
1821
|
+
let sym = symbols[iaid.decode()] ?? makeBitmap(1, 1);
|
|
1822
|
+
if (p.refine && num(iari.decode()) !== 0) {
|
|
1823
|
+
const rdw = num(iardw.decode());
|
|
1824
|
+
const rdh = num(iardh.decode());
|
|
1825
|
+
const rdx = num(iardx.decode());
|
|
1826
|
+
const rdy = num(iardy.decode());
|
|
1827
|
+
const w = sym.width + rdw;
|
|
1828
|
+
const h = sym.height + rdh;
|
|
1829
|
+
if (w > 0 && h > 0 && w < 16384 && h < 16384) sym = decodeRefinement(mq, refineCx, w, h, p.rTemplate, sym, (rdw >> 1) + rdx, (rdh >> 1) + rdy, p.rAt, false);
|
|
1830
|
+
}
|
|
1831
|
+
const sw = sym.width;
|
|
1832
|
+
const sh = sym.height;
|
|
1833
|
+
let x;
|
|
1834
|
+
let y;
|
|
1835
|
+
if (p.transposed) {
|
|
1836
|
+
x = tt;
|
|
1837
|
+
y = curS;
|
|
1838
|
+
if (p.refCorner === Corner.BottomLeft || p.refCorner === Corner.BottomRight) {}
|
|
1839
|
+
if (p.refCorner === Corner.TopRight || p.refCorner === Corner.BottomRight) x = tt - sw + 1;
|
|
1840
|
+
} else {
|
|
1841
|
+
x = curS;
|
|
1842
|
+
y = tt;
|
|
1843
|
+
if (p.refCorner === Corner.BottomLeft || p.refCorner === Corner.BottomRight) y = tt - sh + 1;
|
|
1844
|
+
}
|
|
1845
|
+
compose(bmp, sym, x, y, p.combOp);
|
|
1846
|
+
curS += (p.transposed ? sh : sw) - 1;
|
|
1847
|
+
placed++;
|
|
1848
|
+
if (placed >= p.instances) break;
|
|
1849
|
+
const ids = iads.decode();
|
|
1850
|
+
if (ids === OOB) break;
|
|
1851
|
+
curS += ids + p.dsOffset;
|
|
1852
|
+
}
|
|
1853
|
+
}
|
|
1854
|
+
return bmp;
|
|
1855
|
+
}
|
|
1856
|
+
/**
|
|
1857
|
+
* §6.5 — decode a symbol dictionary: the shapes a text region will place.
|
|
1858
|
+
*
|
|
1859
|
+
* Shapes come in HEIGHT CLASSES, tallest dimension first: one delta says how
|
|
1860
|
+
* much taller this class is than the last, then a run of widths within it, each
|
|
1861
|
+
* ending when the width decoder signals out-of-band.
|
|
1862
|
+
*
|
|
1863
|
+
* @param input The symbols this dictionary inherits from the ones it refers to.
|
|
1864
|
+
*/
|
|
1865
|
+
function decodeSymbolDictionary(mq, input, newCount, exportCount, template, at, refAgg, rTemplate, rAt) {
|
|
1866
|
+
const iadh = new IntDecoder(mq);
|
|
1867
|
+
const iadw = new IntDecoder(mq);
|
|
1868
|
+
const iaex = new IntDecoder(mq);
|
|
1869
|
+
const iaai = new IntDecoder(mq);
|
|
1870
|
+
const iardx = new IntDecoder(mq);
|
|
1871
|
+
const iardy = new IntDecoder(mq);
|
|
1872
|
+
const genericCx = newContexts(65536);
|
|
1873
|
+
const refineCx = newContexts(8192);
|
|
1874
|
+
const newSymbols = [];
|
|
1875
|
+
const num = (v) => v === OOB ? 0 : v;
|
|
1876
|
+
let height = 0;
|
|
1877
|
+
let guard = 0;
|
|
1878
|
+
while (newSymbols.length < newCount && guard++ < 1e4) {
|
|
1879
|
+
height += num(iadh.decode());
|
|
1880
|
+
let width = 0;
|
|
1881
|
+
for (;;) {
|
|
1882
|
+
const dw = iadw.decode();
|
|
1883
|
+
if (dw === OOB) break;
|
|
1884
|
+
width += dw;
|
|
1885
|
+
if (newSymbols.length >= newCount) break;
|
|
1886
|
+
if (width <= 0 || height <= 0 || width > 16384 || height > 16384) {
|
|
1887
|
+
newSymbols.push(makeBitmap(1, 1));
|
|
1888
|
+
continue;
|
|
1889
|
+
}
|
|
1890
|
+
if (!refAgg) newSymbols.push(decodeGenericRegion(mq, genericCx, width, height, template, at, false));
|
|
1891
|
+
else {
|
|
1892
|
+
const instances = num(iaai.decode());
|
|
1893
|
+
if (instances === 1) {
|
|
1894
|
+
const all = [...input, ...newSymbols];
|
|
1895
|
+
const id = new IdDecoder(mq, Math.max(1, Math.ceil(Math.log2(Math.max(all.length + newCount, 1))))).decode();
|
|
1896
|
+
const rdx = num(iardx.decode());
|
|
1897
|
+
const rdy = num(iardy.decode());
|
|
1898
|
+
newSymbols.push(decodeRefinement(mq, refineCx, width, height, rTemplate, all[id] ?? makeBitmap(1, 1), rdx, rdy, rAt, false));
|
|
1899
|
+
} else {
|
|
1900
|
+
const all = [...input, ...newSymbols];
|
|
1901
|
+
newSymbols.push(decodeTextRegion(mq, all, {
|
|
1902
|
+
width,
|
|
1903
|
+
height,
|
|
1904
|
+
instances,
|
|
1905
|
+
stripT: 0,
|
|
1906
|
+
refCorner: Corner.TopLeft,
|
|
1907
|
+
transposed: false,
|
|
1908
|
+
combOp: 0,
|
|
1909
|
+
defPixel: 0,
|
|
1910
|
+
dsOffset: 0,
|
|
1911
|
+
refine: true,
|
|
1912
|
+
rTemplate,
|
|
1913
|
+
rAt
|
|
1914
|
+
}, 0));
|
|
1915
|
+
}
|
|
1916
|
+
}
|
|
1917
|
+
}
|
|
1918
|
+
}
|
|
1919
|
+
const all = [...input, ...newSymbols];
|
|
1920
|
+
const exported = [];
|
|
1921
|
+
let i = 0;
|
|
1922
|
+
let exporting = false;
|
|
1923
|
+
let steps = 0;
|
|
1924
|
+
while (i < all.length && exported.length < exportCount && steps++ < 1e4) {
|
|
1925
|
+
const run = num(iaex.decode());
|
|
1926
|
+
if (exporting) for (let k = 0; k < run && i + k < all.length; k++) exported.push(all[i + k]);
|
|
1927
|
+
i += run;
|
|
1928
|
+
exporting = !exporting;
|
|
1929
|
+
if (run === 0 && steps > all.length * 2 + 4) break;
|
|
1930
|
+
}
|
|
1931
|
+
return exported.length > 0 ? exported : newSymbols;
|
|
1932
|
+
}
|
|
1933
|
+
/**
|
|
1934
|
+
* §7.4.3.1.7 — the symbol ID codes, which are themselves Huffman-coded.
|
|
1935
|
+
*
|
|
1936
|
+
* A run of 35 four-bit lengths builds a table of RUN codes; those then say how
|
|
1937
|
+
* long each symbol's own code is, with three of them meaning "repeat the last
|
|
1938
|
+
* length", "repeat zero" and "repeat zero many times" — the same trick
|
|
1939
|
+
* DEFLATE uses, for the same reason: most symbols share a length.
|
|
1940
|
+
*/
|
|
1941
|
+
function readSymbolIdTable(bits, count) {
|
|
1942
|
+
const runLines = [];
|
|
1943
|
+
for (let i = 0; i < 35; i++) runLines.push({
|
|
1944
|
+
prefLen: bits.readBits(4),
|
|
1945
|
+
rangeLen: 0,
|
|
1946
|
+
rangeLow: i
|
|
1947
|
+
});
|
|
1948
|
+
const runTable = buildHuffTable(runLines);
|
|
1949
|
+
const lengths = new Array(count).fill(0);
|
|
1950
|
+
let prev = 0;
|
|
1951
|
+
for (let i = 0; i < count;) {
|
|
1952
|
+
const code = huffDecode(bits, runTable);
|
|
1953
|
+
if (code === OOB) break;
|
|
1954
|
+
if (code < 32) {
|
|
1955
|
+
lengths[i++] = code;
|
|
1956
|
+
prev = code;
|
|
1957
|
+
} else if (code === 32) {
|
|
1958
|
+
const n = bits.readBits(2) + 3;
|
|
1959
|
+
for (let k = 0; k < n && i < count; k++) lengths[i++] = prev;
|
|
1960
|
+
} else if (code === 33) {
|
|
1961
|
+
const n = bits.readBits(3) + 3;
|
|
1962
|
+
for (let k = 0; k < n && i < count; k++) lengths[i++] = 0;
|
|
1963
|
+
} else {
|
|
1964
|
+
const n = bits.readBits(7) + 11;
|
|
1965
|
+
for (let k = 0; k < n && i < count; k++) lengths[i++] = 0;
|
|
1966
|
+
}
|
|
1967
|
+
}
|
|
1968
|
+
bits.align();
|
|
1969
|
+
return buildHuffTable(lengths.map((prefLen, i) => ({
|
|
1970
|
+
prefLen,
|
|
1971
|
+
rangeLen: 0,
|
|
1972
|
+
rangeLow: i
|
|
1973
|
+
})));
|
|
1974
|
+
}
|
|
1975
|
+
/**
|
|
1976
|
+
* §6.4 — a Huffman-coded text region, the same placement walk as the
|
|
1977
|
+
* arithmetic one with every number read through a table instead.
|
|
1978
|
+
*/
|
|
1979
|
+
function decodeTextRegionHuff(bits, data, symbols, p, stripsLog, t) {
|
|
1980
|
+
const bmp = makeBitmap(p.width, p.height, p.defPixel);
|
|
1981
|
+
const strips = 1 << stripsLog;
|
|
1982
|
+
const refineCx = newContexts(8192);
|
|
1983
|
+
const num = (v) => v === OOB ? 0 : v;
|
|
1984
|
+
let stripT = -num(huffDecode(bits, t.dt)) * strips;
|
|
1985
|
+
let firstS = 0;
|
|
1986
|
+
let placed = 0;
|
|
1987
|
+
let guard = 0;
|
|
1988
|
+
while (placed < p.instances && guard++ < 1e5) {
|
|
1989
|
+
stripT += num(huffDecode(bits, t.dt)) * strips;
|
|
1990
|
+
firstS += num(huffDecode(bits, t.fs));
|
|
1991
|
+
let curS = firstS;
|
|
1992
|
+
for (;;) {
|
|
1993
|
+
const tOff = strips === 1 ? 0 : bits.readBits(stripsLog);
|
|
1994
|
+
let sym = symbols[num(huffDecode(bits, t.symbolIds))] ?? makeBitmap(1, 1);
|
|
1995
|
+
if (p.refine && bits.read() !== 0) {
|
|
1996
|
+
const rdw = num(huffDecode(bits, t.rdw));
|
|
1997
|
+
const rdh = num(huffDecode(bits, t.rdh));
|
|
1998
|
+
const rdx = num(huffDecode(bits, t.rdx));
|
|
1999
|
+
const rdy = num(huffDecode(bits, t.rdy));
|
|
2000
|
+
const size = num(huffDecode(bits, t.rsize));
|
|
2001
|
+
bits.align();
|
|
2002
|
+
const w = sym.width + rdw;
|
|
2003
|
+
const h = sym.height + rdh;
|
|
2004
|
+
const start = bits.bytePos;
|
|
2005
|
+
if (w > 0 && h > 0 && w < 16384 && h < 16384) sym = decodeRefinement(new MQDecoder(data.subarray(start)), refineCx, w, h, p.rTemplate, sym, (rdw >> 1) + rdx, (rdh >> 1) + rdy, p.rAt, false);
|
|
2006
|
+
bits.skipTo(start + size);
|
|
2007
|
+
}
|
|
2008
|
+
const sw = sym.width;
|
|
2009
|
+
const sh = sym.height;
|
|
2010
|
+
const tt = stripT + tOff;
|
|
2011
|
+
let x;
|
|
2012
|
+
let y;
|
|
2013
|
+
if (p.transposed) {
|
|
2014
|
+
x = tt;
|
|
2015
|
+
y = curS;
|
|
2016
|
+
if (p.refCorner === Corner.TopRight || p.refCorner === Corner.BottomRight) x = tt - sw + 1;
|
|
2017
|
+
} else {
|
|
2018
|
+
x = curS;
|
|
2019
|
+
y = tt;
|
|
2020
|
+
if (p.refCorner === Corner.BottomLeft || p.refCorner === Corner.BottomRight) y = tt - sh + 1;
|
|
2021
|
+
}
|
|
2022
|
+
compose(bmp, sym, x, y, p.combOp);
|
|
2023
|
+
curS += (p.transposed ? sh : sw) - 1;
|
|
2024
|
+
placed++;
|
|
2025
|
+
if (placed >= p.instances) break;
|
|
2026
|
+
const ids = huffDecode(bits, t.ds);
|
|
2027
|
+
if (ids === OOB) break;
|
|
2028
|
+
curS += ids + p.dsOffset;
|
|
2029
|
+
}
|
|
2030
|
+
}
|
|
2031
|
+
return bmp;
|
|
2032
|
+
}
|
|
2033
|
+
/**
|
|
2034
|
+
* §6.5.9 — a Huffman-coded symbol dictionary.
|
|
2035
|
+
*
|
|
2036
|
+
* The shapes of a height class are not coded one by one: their widths are read
|
|
2037
|
+
* first, then the whole class arrives as ONE collective bitmap, which is cut
|
|
2038
|
+
* apart by those widths. That is why a Huffman dictionary needs a size field
|
|
2039
|
+
* the arithmetic one does not.
|
|
2040
|
+
*/
|
|
2041
|
+
function decodeSymbolDictionaryHuff(bits, data, input, newCount, exportCount, dh, dw, bmSize) {
|
|
2042
|
+
const newSymbols = [];
|
|
2043
|
+
const num = (v) => v === OOB ? 0 : v;
|
|
2044
|
+
let height = 0;
|
|
2045
|
+
let guard = 0;
|
|
2046
|
+
while (newSymbols.length < newCount && guard++ < 1e4) {
|
|
2047
|
+
height += num(huffDecode(bits, dh));
|
|
2048
|
+
if (height <= 0 || height > 16384) break;
|
|
2049
|
+
let width = 0;
|
|
2050
|
+
let totalWidth = 0;
|
|
2051
|
+
const widths = [];
|
|
2052
|
+
for (;;) {
|
|
2053
|
+
const d = huffDecode(bits, dw);
|
|
2054
|
+
if (d === OOB) break;
|
|
2055
|
+
width += d;
|
|
2056
|
+
if (width <= 0 || width > 16384 || newSymbols.length + widths.length >= newCount + 1) break;
|
|
2057
|
+
widths.push(width);
|
|
2058
|
+
totalWidth += width;
|
|
2059
|
+
}
|
|
2060
|
+
if (widths.length === 0) continue;
|
|
2061
|
+
const size = num(huffDecode(bits, bmSize));
|
|
2062
|
+
bits.align();
|
|
2063
|
+
const start = bits.bytePos;
|
|
2064
|
+
let collective;
|
|
2065
|
+
if (size === 0) {
|
|
2066
|
+
collective = makeBitmap(totalWidth, height);
|
|
2067
|
+
const rowBytes = totalWidth + 7 >> 3;
|
|
2068
|
+
for (let y = 0; y < height; y++) for (let x = 0; x < totalWidth; x++) {
|
|
2069
|
+
const b = data[start + y * rowBytes + (x >> 3)] ?? 0;
|
|
2070
|
+
collective.data[y * totalWidth + x] = b >> 7 - (x & 7) & 1;
|
|
2071
|
+
}
|
|
2072
|
+
bits.skipTo(start + rowBytes * height);
|
|
2073
|
+
} else {
|
|
2074
|
+
const packed = decodeCcitt(data.subarray(start, start + size), {
|
|
2075
|
+
k: -1,
|
|
2076
|
+
columns: totalWidth,
|
|
2077
|
+
rows: height,
|
|
2078
|
+
byteAlign: false
|
|
2079
|
+
});
|
|
2080
|
+
collective = makeBitmap(totalWidth, height);
|
|
2081
|
+
if (packed) {
|
|
2082
|
+
const rowBytes = totalWidth + 7 >> 3;
|
|
2083
|
+
for (let y = 0; y < height; y++) for (let x = 0; x < totalWidth; x++) collective.data[y * totalWidth + x] = packed[y * rowBytes + (x >> 3)] >> 7 - (x & 7) & 1;
|
|
2084
|
+
}
|
|
2085
|
+
bits.skipTo(start + size);
|
|
2086
|
+
}
|
|
2087
|
+
let at = 0;
|
|
2088
|
+
for (const w of widths) {
|
|
2089
|
+
const cell = makeBitmap(w, height);
|
|
2090
|
+
for (let y = 0; y < height; y++) for (let x = 0; x < w; x++) cell.data[y * w + x] = collective.data[y * totalWidth + at + x] ?? 0;
|
|
2091
|
+
newSymbols.push(cell);
|
|
2092
|
+
at += w;
|
|
2093
|
+
}
|
|
2094
|
+
}
|
|
2095
|
+
const all = [...input, ...newSymbols];
|
|
2096
|
+
const exTable = standardTable(1);
|
|
2097
|
+
const exported = [];
|
|
2098
|
+
let i = 0;
|
|
2099
|
+
let exporting = false;
|
|
2100
|
+
let steps = 0;
|
|
2101
|
+
while (i < all.length && exported.length < exportCount && steps++ < 1e4) {
|
|
2102
|
+
const run = num(huffDecode(bits, exTable));
|
|
2103
|
+
if (exporting) for (let k = 0; k < run && i + k < all.length; k++) exported.push(all[i + k]);
|
|
2104
|
+
i += run;
|
|
2105
|
+
exporting = !exporting;
|
|
2106
|
+
if (run === 0 && steps > all.length * 2 + 4) break;
|
|
2107
|
+
}
|
|
2108
|
+
return exported.length > 0 ? exported : newSymbols;
|
|
2109
|
+
}
|
|
2110
|
+
/**
|
|
2111
|
+
* §6.7 — a pattern dictionary: the cells a halftone region tiles a picture out
|
|
2112
|
+
* of, from all-white through all-black.
|
|
2113
|
+
*
|
|
2114
|
+
* They are coded as ONE wide generic region — every pattern side by side — and
|
|
2115
|
+
* cut apart afterwards, which is why the first adaptive pixel sits a whole
|
|
2116
|
+
* pattern-width back: it makes each cell predict from the one before it.
|
|
2117
|
+
*/
|
|
2118
|
+
function decodePatternDictionary(data, mmr, template, pw, ph, grayMax) {
|
|
2119
|
+
const count = grayMax + 1;
|
|
2120
|
+
const width = count * pw;
|
|
2121
|
+
let collective;
|
|
2122
|
+
if (mmr) {
|
|
2123
|
+
const packed = decodeCcitt(data, {
|
|
2124
|
+
k: -1,
|
|
2125
|
+
columns: width,
|
|
2126
|
+
rows: ph,
|
|
2127
|
+
byteAlign: false
|
|
2128
|
+
});
|
|
2129
|
+
if (!packed) return [];
|
|
2130
|
+
collective = makeBitmap(width, ph);
|
|
2131
|
+
const rowBytes = width + 7 >> 3;
|
|
2132
|
+
for (let y = 0; y < ph; y++) for (let x = 0; x < width; x++) collective.data[y * width + x] = packed[y * rowBytes + (x >> 3)] >> 7 - (x & 7) & 1;
|
|
2133
|
+
} else collective = decodeGenericRegion(new MQDecoder(data), newContexts(65536), width, ph, template, [
|
|
2134
|
+
{
|
|
2135
|
+
x: -pw,
|
|
2136
|
+
y: 0
|
|
2137
|
+
},
|
|
2138
|
+
{
|
|
2139
|
+
x: -3,
|
|
2140
|
+
y: -1
|
|
2141
|
+
},
|
|
2142
|
+
{
|
|
2143
|
+
x: 2,
|
|
2144
|
+
y: -2
|
|
2145
|
+
},
|
|
2146
|
+
{
|
|
2147
|
+
x: -2,
|
|
2148
|
+
y: -2
|
|
2149
|
+
}
|
|
2150
|
+
], false);
|
|
2151
|
+
const out = [];
|
|
2152
|
+
for (let i = 0; i < count; i++) {
|
|
2153
|
+
const cell = makeBitmap(pw, ph);
|
|
2154
|
+
for (let y = 0; y < ph; y++) for (let x = 0; x < pw; x++) cell.data[y * pw + x] = collective.data[y * width + i * pw + x] ?? 0;
|
|
2155
|
+
out.push(cell);
|
|
2156
|
+
}
|
|
2157
|
+
return out;
|
|
2158
|
+
}
|
|
2159
|
+
/**
|
|
2160
|
+
* §C.5 — the grey-scale image a halftone region is built from: one bitmap per
|
|
2161
|
+
* BIT of the value, most significant first, Gray-coded so neighbouring levels
|
|
2162
|
+
* differ in one plane and the planes stay smooth enough to code well.
|
|
2163
|
+
*/
|
|
2164
|
+
function decodeGrayScale(mq, data, mmr, template, at, width, height, bits, skip) {
|
|
2165
|
+
const cx = newContexts(65536);
|
|
2166
|
+
const planes = new Array(bits);
|
|
2167
|
+
let mmrOffset = 0;
|
|
2168
|
+
const decoder = mq ?? new MQDecoder(data);
|
|
2169
|
+
for (let j = bits - 1; j >= 0; j--) if (mmr) {
|
|
2170
|
+
const packed = decodeCcitt(data.subarray(mmrOffset), {
|
|
2171
|
+
k: -1,
|
|
2172
|
+
columns: width,
|
|
2173
|
+
rows: height,
|
|
2174
|
+
byteAlign: false
|
|
2175
|
+
});
|
|
2176
|
+
const plane = makeBitmap(width, height);
|
|
2177
|
+
if (packed) {
|
|
2178
|
+
const rowBytes = width + 7 >> 3;
|
|
2179
|
+
for (let y = 0; y < height; y++) for (let x = 0; x < width; x++) plane.data[y * width + x] = packed[y * rowBytes + (x >> 3)] >> 7 - (x & 7) & 1;
|
|
2180
|
+
mmrOffset += packed.length;
|
|
2181
|
+
}
|
|
2182
|
+
planes[j] = plane;
|
|
2183
|
+
} else planes[j] = decodeGenericRegion(decoder, cx, width, height, template, at, false, skip);
|
|
2184
|
+
const values = new Array(width * height).fill(0);
|
|
2185
|
+
for (let i = 0; i < width * height; i++) {
|
|
2186
|
+
let bit = planes[bits - 1]?.data[i] ?? 0;
|
|
2187
|
+
let v = bit;
|
|
2188
|
+
for (let j = bits - 2; j >= 0; j--) {
|
|
2189
|
+
bit ^= planes[j]?.data[i] ?? 0;
|
|
2190
|
+
v = v * 2 + bit;
|
|
2191
|
+
}
|
|
2192
|
+
values[i] = v;
|
|
2193
|
+
}
|
|
2194
|
+
return values;
|
|
2195
|
+
}
|
|
2196
|
+
var Reader = class {
|
|
2197
|
+
pos = 0;
|
|
2198
|
+
constructor(d) {
|
|
2199
|
+
this.d = d;
|
|
2200
|
+
}
|
|
2201
|
+
u8() {
|
|
2202
|
+
return this.d[this.pos++] ?? 0;
|
|
2203
|
+
}
|
|
2204
|
+
u16() {
|
|
2205
|
+
return this.u8() << 8 | this.u8();
|
|
2206
|
+
}
|
|
2207
|
+
u32() {
|
|
2208
|
+
return (this.u8() << 24 | this.u16() << 8 | this.u8()) >>> 0;
|
|
2209
|
+
}
|
|
2210
|
+
i8() {
|
|
2211
|
+
const v = this.u8();
|
|
2212
|
+
return v > 127 ? v - 256 : v;
|
|
2213
|
+
}
|
|
2214
|
+
get done() {
|
|
2215
|
+
return this.pos >= this.d.length;
|
|
2216
|
+
}
|
|
2217
|
+
};
|
|
2218
|
+
/** §7.2 — walk the segment headers of an embedded (PDF) JBIG2 stream. */
|
|
2219
|
+
function parseSegments(data) {
|
|
2220
|
+
const r = new Reader(data);
|
|
2221
|
+
const out = [];
|
|
2222
|
+
while (r.pos + 11 <= data.length) {
|
|
2223
|
+
const number = r.u32();
|
|
2224
|
+
const flags = r.u8();
|
|
2225
|
+
const type = flags & 63;
|
|
2226
|
+
const pageSize = (flags & 64) !== 0 ? 4 : 1;
|
|
2227
|
+
let count = r.u8() >> 5;
|
|
2228
|
+
if (count === 7) {
|
|
2229
|
+
r.pos--;
|
|
2230
|
+
count = r.u32() & 536870911;
|
|
2231
|
+
r.pos += Math.ceil((count + 1) / 8);
|
|
2232
|
+
}
|
|
2233
|
+
const refSize = number <= 256 ? 1 : number <= 65536 ? 2 : 4;
|
|
2234
|
+
const referred = [];
|
|
2235
|
+
for (let i = 0; i < count; i++) referred.push(refSize === 1 ? r.u8() : refSize === 2 ? r.u16() : r.u32());
|
|
2236
|
+
const page = pageSize === 1 ? r.u8() : r.u32();
|
|
2237
|
+
const length = r.u32();
|
|
2238
|
+
const start = r.pos;
|
|
2239
|
+
const end = length === 4294967295 ? data.length : Math.min(data.length, start + length);
|
|
2240
|
+
out.push({
|
|
2241
|
+
number,
|
|
2242
|
+
type,
|
|
2243
|
+
referred,
|
|
2244
|
+
page,
|
|
2245
|
+
start,
|
|
2246
|
+
end
|
|
2247
|
+
});
|
|
2248
|
+
if (end <= start && length !== 0) break;
|
|
2249
|
+
r.pos = end;
|
|
2250
|
+
if (length === 4294967295) break;
|
|
2251
|
+
}
|
|
2252
|
+
return out;
|
|
2253
|
+
}
|
|
2254
|
+
function readRegionInfo(r) {
|
|
2255
|
+
return {
|
|
2256
|
+
width: r.u32(),
|
|
2257
|
+
height: r.u32(),
|
|
2258
|
+
x: r.u32(),
|
|
2259
|
+
y: r.u32(),
|
|
2260
|
+
combOp: r.u8() & 7
|
|
2261
|
+
};
|
|
2262
|
+
}
|
|
2263
|
+
/** §6.2.5.1 / §8.2 — paint `src` onto `dst` at (x, y) under a composition op. */
|
|
2264
|
+
function compose(dst, src, x0, y0, op) {
|
|
2265
|
+
for (let y = 0; y < src.height; y++) {
|
|
2266
|
+
const dy = y0 + y;
|
|
2267
|
+
if (dy < 0 || dy >= dst.height) continue;
|
|
2268
|
+
for (let x = 0; x < src.width; x++) {
|
|
2269
|
+
const dx = x0 + x;
|
|
2270
|
+
if (dx < 0 || dx >= dst.width) continue;
|
|
2271
|
+
const s = src.data[y * src.width + x] ?? 0;
|
|
2272
|
+
const i = dy * dst.width + dx;
|
|
2273
|
+
const d = dst.data[i] ?? 0;
|
|
2274
|
+
dst.data[i] = op === 0 ? d | s : op === 1 ? d & s : op === 2 ? d ^ s : op === 3 ? d ^ s ^ 1 : s;
|
|
2275
|
+
}
|
|
2276
|
+
}
|
|
2277
|
+
}
|
|
2278
|
+
/**
|
|
2279
|
+
* Decode an embedded JBIG2 image.
|
|
2280
|
+
*
|
|
2281
|
+
* @param data The `/JBIG2Decode` stream's own segments.
|
|
2282
|
+
* @param globals The `/JBIG2Globals` stream's segments, when the image names one.
|
|
2283
|
+
* @param width The image's `/Width`, which the page info may not state.
|
|
2284
|
+
* @param height Its `/Height`.
|
|
2285
|
+
* @returns A packed 1-bit-per-pixel bitmap (`rowBytes × height`, bit 1 = black,
|
|
2286
|
+
* MSB first) — the same shape {@link decodeCcitt} returns — or `undefined`
|
|
2287
|
+
* when nothing in the stream could be decoded.
|
|
2288
|
+
*/
|
|
2289
|
+
function decodeJbig2(data, globals, width, height) {
|
|
2290
|
+
if (width <= 0 || height <= 0 || width > 65536 || height > 65536) return void 0;
|
|
2291
|
+
const page = {
|
|
2292
|
+
width,
|
|
2293
|
+
height,
|
|
2294
|
+
defPixel: 0,
|
|
2295
|
+
defCombOp: 0,
|
|
2296
|
+
striped: false
|
|
2297
|
+
};
|
|
2298
|
+
let canvas = makeBitmap(width, height);
|
|
2299
|
+
const painted = { regions: 0 };
|
|
2300
|
+
const buffers = /* @__PURE__ */ new Map();
|
|
2301
|
+
const symbolsBySegment = /* @__PURE__ */ new Map();
|
|
2302
|
+
const patternsBySegment = /* @__PURE__ */ new Map();
|
|
2303
|
+
const tablesBySegment = /* @__PURE__ */ new Map();
|
|
2304
|
+
const run = (bytes) => {
|
|
2305
|
+
for (const seg of parseSegments(bytes)) {
|
|
2306
|
+
const r = new Reader(bytes.subarray(seg.start, seg.end));
|
|
2307
|
+
try {
|
|
2308
|
+
if (seg.type === 48) {
|
|
2309
|
+
const pw = r.u32();
|
|
2310
|
+
const ph = r.u32();
|
|
2311
|
+
r.u32();
|
|
2312
|
+
r.u32();
|
|
2313
|
+
const flags = r.u8();
|
|
2314
|
+
page.defPixel = flags >> 2 & 1;
|
|
2315
|
+
page.defCombOp = flags >> 3 & 3;
|
|
2316
|
+
page.striped = (r.u16() & 32768) !== 0;
|
|
2317
|
+
if (pw > 0 && pw !== 4294967295 && ph > 0 && ph !== 4294967295) {
|
|
2318
|
+
page.width = Math.min(pw, width);
|
|
2319
|
+
page.height = Math.min(ph, height);
|
|
2320
|
+
}
|
|
2321
|
+
canvas = makeBitmap(width, height, page.defPixel);
|
|
2322
|
+
continue;
|
|
2323
|
+
}
|
|
2324
|
+
if (seg.type === 53) {
|
|
2325
|
+
tablesBySegment.set(seg.number, parseCustomTable(bytes.subarray(seg.start, seg.end)));
|
|
2326
|
+
continue;
|
|
2327
|
+
}
|
|
2328
|
+
if (seg.type === 16) {
|
|
2329
|
+
const flags = r.u8();
|
|
2330
|
+
const mmr = (flags & 1) !== 0;
|
|
2331
|
+
const template = flags >> 1 & 3;
|
|
2332
|
+
const pw = r.u8();
|
|
2333
|
+
const ph = r.u8();
|
|
2334
|
+
const grayMax = r.u32();
|
|
2335
|
+
if (pw <= 0 || ph <= 0 || grayMax > 1e4) continue;
|
|
2336
|
+
patternsBySegment.set(seg.number, decodePatternDictionary(bytes.subarray(seg.start + r.pos, seg.end), mmr, template, pw, ph, grayMax));
|
|
2337
|
+
continue;
|
|
2338
|
+
}
|
|
2339
|
+
if (seg.type === 20 || seg.type === 22 || seg.type === 23) {
|
|
2340
|
+
const info = readRegionInfo(r);
|
|
2341
|
+
const flags = r.u8();
|
|
2342
|
+
const mmr = (flags & 1) !== 0;
|
|
2343
|
+
const template = flags >> 1 & 3;
|
|
2344
|
+
const enableSkip = (flags & 8) !== 0;
|
|
2345
|
+
const combOp = flags >> 4 & 7;
|
|
2346
|
+
const defPixel = flags >> 7 & 1;
|
|
2347
|
+
const gw = r.u32();
|
|
2348
|
+
const gh = r.u32();
|
|
2349
|
+
const gx = r.u32() | 0;
|
|
2350
|
+
const gy = r.u32() | 0;
|
|
2351
|
+
const rx = r.u16();
|
|
2352
|
+
const ry = r.u16();
|
|
2353
|
+
const patterns = seg.referred.flatMap((n) => patternsBySegment.get(n) ?? []);
|
|
2354
|
+
const w = Math.min(info.width, 65536);
|
|
2355
|
+
const h = Math.min(info.height, 65536);
|
|
2356
|
+
if (patterns.length === 0 || w <= 0 || h <= 0) continue;
|
|
2357
|
+
if (gw <= 0 || gh <= 0 || gw * gh > 1 << 24) continue;
|
|
2358
|
+
const bmp = makeBitmap(w, h, defPixel);
|
|
2359
|
+
const pw = patterns[0].width;
|
|
2360
|
+
const ph = patterns[0].height;
|
|
2361
|
+
let skip;
|
|
2362
|
+
if (enableSkip) {
|
|
2363
|
+
skip = makeBitmap(gw, gh);
|
|
2364
|
+
for (let m = 0; m < gh; m++) for (let n = 0; n < gw; n++) {
|
|
2365
|
+
const x = gx + m * ry + n * rx >> 8;
|
|
2366
|
+
const y = gy + m * rx - n * ry >> 8;
|
|
2367
|
+
if (x + pw <= 0 || x >= w || y + ph <= 0 || y >= h) skip.data[m * gw + n] = 1;
|
|
2368
|
+
}
|
|
2369
|
+
}
|
|
2370
|
+
const bits = Math.max(1, Math.ceil(Math.log2(patterns.length)));
|
|
2371
|
+
const gray = decodeGrayScale(void 0, bytes.subarray(seg.start + r.pos, seg.end), mmr, template, [
|
|
2372
|
+
{
|
|
2373
|
+
x: template <= 1 ? 3 : 2,
|
|
2374
|
+
y: -1
|
|
2375
|
+
},
|
|
2376
|
+
{
|
|
2377
|
+
x: -3,
|
|
2378
|
+
y: -1
|
|
2379
|
+
},
|
|
2380
|
+
{
|
|
2381
|
+
x: 2,
|
|
2382
|
+
y: -2
|
|
2383
|
+
},
|
|
2384
|
+
{
|
|
2385
|
+
x: -2,
|
|
2386
|
+
y: -2
|
|
2387
|
+
}
|
|
2388
|
+
], gw, gh, bits, skip);
|
|
2389
|
+
for (let m = 0; m < gh; m++) for (let n = 0; n < gw; n++) {
|
|
2390
|
+
if (skip?.data[m * gw + n] === 1) continue;
|
|
2391
|
+
const level = Math.min(gray[m * gw + n] ?? 0, patterns.length - 1);
|
|
2392
|
+
const x = gx + m * ry + n * rx >> 8;
|
|
2393
|
+
const y = gy + m * rx - n * ry >> 8;
|
|
2394
|
+
compose(bmp, patterns[level], x, y, combOp);
|
|
2395
|
+
}
|
|
2396
|
+
if (seg.type === 20) buffers.set(seg.number, bmp);
|
|
2397
|
+
else {
|
|
2398
|
+
compose(canvas, bmp, info.x, info.y, info.combOp);
|
|
2399
|
+
painted.regions++;
|
|
2400
|
+
}
|
|
2401
|
+
continue;
|
|
2402
|
+
}
|
|
2403
|
+
if (seg.type === 0) {
|
|
2404
|
+
const flags = r.u16();
|
|
2405
|
+
const huff = (flags & 1) !== 0;
|
|
2406
|
+
const refAgg = (flags & 2) !== 0;
|
|
2407
|
+
const template = flags >> 10 & 3;
|
|
2408
|
+
const rTemplate = flags >> 12 & 1;
|
|
2409
|
+
flags & 256;
|
|
2410
|
+
const at = [];
|
|
2411
|
+
if (!huff) {
|
|
2412
|
+
const n = template === 0 ? 4 : 1;
|
|
2413
|
+
for (let i = 0; i < n; i++) at.push({
|
|
2414
|
+
x: r.i8(),
|
|
2415
|
+
y: r.i8()
|
|
2416
|
+
});
|
|
2417
|
+
}
|
|
2418
|
+
const rAt = [];
|
|
2419
|
+
if (refAgg && rTemplate === 0) for (let i = 0; i < 2; i++) rAt.push({
|
|
2420
|
+
x: r.i8(),
|
|
2421
|
+
y: r.i8()
|
|
2422
|
+
});
|
|
2423
|
+
const exportCount = r.u32();
|
|
2424
|
+
const newCount = r.u32();
|
|
2425
|
+
if (newCount > 1e4) continue;
|
|
2426
|
+
const inherited = seg.referred.flatMap((n) => symbolsBySegment.get(n) ?? []);
|
|
2427
|
+
if (huff) {
|
|
2428
|
+
const custom = seg.referred.map((n) => tablesBySegment.get(n)).filter((t) => t !== void 0);
|
|
2429
|
+
let ci = 0;
|
|
2430
|
+
const pick = (sel, choices) => sel === 3 ? custom[ci++] ?? standardTable(choices[0]) : standardTable(choices[sel] ?? choices[0]);
|
|
2431
|
+
const dhSel = flags >> 2 & 3;
|
|
2432
|
+
const dwSel = flags >> 4 & 3;
|
|
2433
|
+
const bmSel = flags >> 6 & 1;
|
|
2434
|
+
const bits = new BitReader(bytes.subarray(seg.start + r.pos, seg.end));
|
|
2435
|
+
symbolsBySegment.set(seg.number, decodeSymbolDictionaryHuff(bits, bytes.subarray(seg.start + r.pos, seg.end), inherited, newCount, exportCount, pick(dhSel, [4, 5]), pick(dwSel, [2, 3]), bmSel === 1 ? custom[ci++] ?? standardTable(1) : standardTable(1)));
|
|
2436
|
+
continue;
|
|
2437
|
+
}
|
|
2438
|
+
symbolsBySegment.set(seg.number, decodeSymbolDictionary(new MQDecoder(bytes.subarray(seg.start + r.pos, seg.end)), inherited, newCount, exportCount, template, at.length > 0 ? at : NOMINAL_AT[template] ?? [], refAgg, rTemplate, rAt.length > 0 ? rAt : NOMINAL_AT_REFINE));
|
|
2439
|
+
continue;
|
|
2440
|
+
}
|
|
2441
|
+
if (seg.type === 4 || seg.type === 6 || seg.type === 7) {
|
|
2442
|
+
const info = readRegionInfo(r);
|
|
2443
|
+
const flags = r.u16();
|
|
2444
|
+
const huff = (flags & 1) !== 0;
|
|
2445
|
+
const refine = (flags & 2) !== 0;
|
|
2446
|
+
const stripsLog = flags >> 2 & 3;
|
|
2447
|
+
const refCorner = flags >> 4 & 3;
|
|
2448
|
+
const transposed = (flags & 64) !== 0;
|
|
2449
|
+
const combOp = flags >> 7 & 3;
|
|
2450
|
+
const defPixel = flags >> 9 & 1;
|
|
2451
|
+
let dsOffset = flags >> 10 & 31;
|
|
2452
|
+
if (dsOffset > 15) dsOffset -= 32;
|
|
2453
|
+
const rTemplate = flags >> 15 & 1;
|
|
2454
|
+
const huffFlags = huff ? r.u16() : 0;
|
|
2455
|
+
const rAt = [];
|
|
2456
|
+
if (refine && rTemplate === 0) for (let i = 0; i < 2; i++) rAt.push({
|
|
2457
|
+
x: r.i8(),
|
|
2458
|
+
y: r.i8()
|
|
2459
|
+
});
|
|
2460
|
+
const instances = r.u32();
|
|
2461
|
+
const symbols = seg.referred.flatMap((n) => symbolsBySegment.get(n) ?? []);
|
|
2462
|
+
if (symbols.length === 0 || instances > 1e5) continue;
|
|
2463
|
+
const w = Math.min(info.width, 65536);
|
|
2464
|
+
const h = Math.min(info.height, 65536);
|
|
2465
|
+
if (w <= 0 || h <= 0) continue;
|
|
2466
|
+
const params = {
|
|
2467
|
+
width: w,
|
|
2468
|
+
height: h,
|
|
2469
|
+
instances,
|
|
2470
|
+
stripT: 0,
|
|
2471
|
+
refCorner,
|
|
2472
|
+
transposed,
|
|
2473
|
+
combOp,
|
|
2474
|
+
defPixel,
|
|
2475
|
+
dsOffset,
|
|
2476
|
+
refine,
|
|
2477
|
+
rTemplate,
|
|
2478
|
+
rAt: rAt.length > 0 ? rAt : NOMINAL_AT_REFINE
|
|
2479
|
+
};
|
|
2480
|
+
const body = bytes.subarray(seg.start + r.pos, seg.end);
|
|
2481
|
+
let bmp;
|
|
2482
|
+
if (huff) {
|
|
2483
|
+
const custom = seg.referred.map((n) => tablesBySegment.get(n)).filter((t) => t !== void 0);
|
|
2484
|
+
let ci = 0;
|
|
2485
|
+
const pick = (sel, choices) => sel === 3 ? custom[ci++] ?? standardTable(choices[0]) : standardTable(choices[sel] ?? choices[0]);
|
|
2486
|
+
const bits = new BitReader(body);
|
|
2487
|
+
bmp = decodeTextRegionHuff(bits, body, symbols, params, stripsLog, {
|
|
2488
|
+
fs: pick(huffFlags & 3, [6, 7]),
|
|
2489
|
+
ds: pick(huffFlags >> 2 & 3, [
|
|
2490
|
+
8,
|
|
2491
|
+
9,
|
|
2492
|
+
10
|
|
2493
|
+
]),
|
|
2494
|
+
dt: pick(huffFlags >> 4 & 3, [
|
|
2495
|
+
11,
|
|
2496
|
+
12,
|
|
2497
|
+
13
|
|
2498
|
+
]),
|
|
2499
|
+
rdw: pick(huffFlags >> 6 & 3, [14, 15]),
|
|
2500
|
+
rdh: pick(huffFlags >> 8 & 3, [14, 15]),
|
|
2501
|
+
rdx: pick(huffFlags >> 10 & 3, [14, 15]),
|
|
2502
|
+
rdy: pick(huffFlags >> 12 & 3, [14, 15]),
|
|
2503
|
+
rsize: (huffFlags >> 14 & 1) === 1 ? custom[ci++] ?? standardTable(1) : standardTable(1),
|
|
2504
|
+
symbolIds: readSymbolIdTable(bits, symbols.length)
|
|
2505
|
+
});
|
|
2506
|
+
} else bmp = decodeTextRegion(new MQDecoder(body), symbols, params, stripsLog);
|
|
2507
|
+
if (seg.type === 4) buffers.set(seg.number, bmp);
|
|
2508
|
+
else {
|
|
2509
|
+
compose(canvas, bmp, info.x, info.y, info.combOp);
|
|
2510
|
+
painted.regions++;
|
|
2511
|
+
}
|
|
2512
|
+
continue;
|
|
2513
|
+
}
|
|
2514
|
+
if (seg.type === 36 || seg.type === 38 || seg.type === 39) {
|
|
2515
|
+
const info = readRegionInfo(r);
|
|
2516
|
+
const flags = r.u8();
|
|
2517
|
+
const mmr = (flags & 1) !== 0;
|
|
2518
|
+
const template = flags >> 1 & 3;
|
|
2519
|
+
const tpgdon = (flags & 8) !== 0;
|
|
2520
|
+
const at = [];
|
|
2521
|
+
if (!mmr) {
|
|
2522
|
+
const n = template === 0 ? 4 : 1;
|
|
2523
|
+
for (let i = 0; i < n; i++) at.push({
|
|
2524
|
+
x: r.i8(),
|
|
2525
|
+
y: r.i8()
|
|
2526
|
+
});
|
|
2527
|
+
}
|
|
2528
|
+
const body = bytes.subarray(seg.start + r.pos, seg.end);
|
|
2529
|
+
const w = Math.min(info.width, 65536);
|
|
2530
|
+
const h = Math.min(info.height === 4294967295 ? height : info.height, 65536);
|
|
2531
|
+
if (w <= 0 || h <= 0) continue;
|
|
2532
|
+
let bmp;
|
|
2533
|
+
if (mmr) {
|
|
2534
|
+
const packed = decodeCcitt(body, {
|
|
2535
|
+
k: -1,
|
|
2536
|
+
columns: w,
|
|
2537
|
+
rows: h,
|
|
2538
|
+
byteAlign: false
|
|
2539
|
+
});
|
|
2540
|
+
if (!packed) continue;
|
|
2541
|
+
bmp = makeBitmap(w, h);
|
|
2542
|
+
const rowBytes = w + 7 >> 3;
|
|
2543
|
+
for (let y = 0; y < h; y++) for (let x = 0; x < w; x++) bmp.data[y * w + x] = packed[y * rowBytes + (x >> 3)] >> 7 - (x & 7) & 1;
|
|
2544
|
+
} else bmp = decodeGenericRegion(new MQDecoder(body), newContexts(65536), w, h, template, at.length > 0 ? at : NOMINAL_AT[template] ?? [], tpgdon);
|
|
2545
|
+
if (seg.type === 36) buffers.set(seg.number, bmp);
|
|
2546
|
+
else {
|
|
2547
|
+
compose(canvas, bmp, info.x, info.y, info.combOp);
|
|
2548
|
+
painted.regions++;
|
|
2549
|
+
}
|
|
2550
|
+
continue;
|
|
2551
|
+
}
|
|
2552
|
+
if (seg.type === 40 || seg.type === 42 || seg.type === 43) {
|
|
2553
|
+
const info = readRegionInfo(r);
|
|
2554
|
+
const flags = r.u8();
|
|
2555
|
+
const template = flags & 1;
|
|
2556
|
+
const tpgron = (flags & 2) !== 0;
|
|
2557
|
+
const at = [];
|
|
2558
|
+
if (template === 0) for (let i = 0; i < 2; i++) at.push({
|
|
2559
|
+
x: r.i8(),
|
|
2560
|
+
y: r.i8()
|
|
2561
|
+
});
|
|
2562
|
+
const w = Math.min(info.width, 65536);
|
|
2563
|
+
const h = Math.min(info.height, 65536);
|
|
2564
|
+
if (w <= 0 || h <= 0) continue;
|
|
2565
|
+
let ref = seg.referred.map((n) => buffers.get(n)).find((b) => b !== void 0);
|
|
2566
|
+
if (!ref) {
|
|
2567
|
+
const crop = makeBitmap(w, h);
|
|
2568
|
+
for (let y = 0; y < h; y++) for (let x = 0; x < w; x++) {
|
|
2569
|
+
const sx = info.x + x;
|
|
2570
|
+
const sy = info.y + y;
|
|
2571
|
+
crop.data[y * w + x] = sx < canvas.width && sy < canvas.height ? canvas.data[sy * canvas.width + sx] ?? 0 : 0;
|
|
2572
|
+
}
|
|
2573
|
+
ref = crop;
|
|
2574
|
+
}
|
|
2575
|
+
const bmp = decodeRefinement(new MQDecoder(bytes.subarray(seg.start + r.pos, seg.end)), newContexts(8192), w, h, template, ref, 0, 0, at.length > 0 ? at : NOMINAL_AT_REFINE, tpgron);
|
|
2576
|
+
if (seg.type === 40) buffers.set(seg.number, bmp);
|
|
2577
|
+
else {
|
|
2578
|
+
compose(canvas, bmp, info.x, info.y, info.combOp);
|
|
2579
|
+
painted.regions++;
|
|
2580
|
+
}
|
|
2581
|
+
continue;
|
|
2582
|
+
}
|
|
2583
|
+
} catch {
|
|
2584
|
+
continue;
|
|
2585
|
+
}
|
|
2586
|
+
}
|
|
2587
|
+
};
|
|
2588
|
+
if (globals) run(globals);
|
|
2589
|
+
run(data);
|
|
2590
|
+
if (painted.regions === 0) return void 0;
|
|
2591
|
+
const rowBytes = width + 7 >> 3;
|
|
2592
|
+
const packed = new Uint8Array(rowBytes * height);
|
|
2593
|
+
for (let y = 0; y < height; y++) for (let x = 0; x < width; x++) if (canvas.data[y * width + x] === 1) packed[y * rowBytes + (x >> 3)] |= 128 >> (x & 7);
|
|
2594
|
+
return packed;
|
|
2595
|
+
}
|
|
2596
|
+
//#endregion
|
|
2597
|
+
export { decodeJbig2 };
|