UNPKG

60.5 kBJavaScriptView Raw
1/**
2 * @licstart The following is the entire license notice for the
3 * JavaScript code in this page
4 *
5 * Copyright 2022 Mozilla Foundation
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * @licend The above is the entire license notice for the
20 * JavaScript code in this page
21 */
22"use strict";
23
24Object.defineProperty(exports, "__esModule", {
25 value: true
26});
27exports.Jbig2Image = void 0;
28
29var _util = require("../shared/util.js");
30
31var _core_utils = require("./core_utils.js");
32
33var _arithmetic_decoder = require("./arithmetic_decoder.js");
34
35var _ccitt = require("./ccitt.js");
36
37class Jbig2Error extends _util.BaseException {
38 constructor(msg) {
39 super(`JBIG2 error: ${msg}`, "Jbig2Error");
40 }
41
42}
43
44class ContextCache {
45 getContexts(id) {
46 if (id in this) {
47 return this[id];
48 }
49
50 return this[id] = new Int8Array(1 << 16);
51 }
52
53}
54
55class DecodingContext {
56 constructor(data, start, end) {
57 this.data = data;
58 this.start = start;
59 this.end = end;
60 }
61
62 get decoder() {
63 const decoder = new _arithmetic_decoder.ArithmeticDecoder(this.data, this.start, this.end);
64 return (0, _util.shadow)(this, "decoder", decoder);
65 }
66
67 get contextCache() {
68 const cache = new ContextCache();
69 return (0, _util.shadow)(this, "contextCache", cache);
70 }
71
72}
73
74function decodeInteger(contextCache, procedure, decoder) {
75 const contexts = contextCache.getContexts(procedure);
76 let prev = 1;
77
78 function readBits(length) {
79 let v = 0;
80
81 for (let i = 0; i < length; i++) {
82 const bit = decoder.readBit(contexts, prev);
83 prev = prev < 256 ? prev << 1 | bit : (prev << 1 | bit) & 511 | 256;
84 v = v << 1 | bit;
85 }
86
87 return v >>> 0;
88 }
89
90 const sign = readBits(1);
91 const value = readBits(1) ? readBits(1) ? readBits(1) ? readBits(1) ? readBits(1) ? readBits(32) + 4436 : readBits(12) + 340 : readBits(8) + 84 : readBits(6) + 20 : readBits(4) + 4 : readBits(2);
92
93 if (sign === 0) {
94 return value;
95 } else if (value > 0) {
96 return -value;
97 }
98
99 return null;
100}
101
102function decodeIAID(contextCache, decoder, codeLength) {
103 const contexts = contextCache.getContexts("IAID");
104 let prev = 1;
105
106 for (let i = 0; i < codeLength; i++) {
107 const bit = decoder.readBit(contexts, prev);
108 prev = prev << 1 | bit;
109 }
110
111 if (codeLength < 31) {
112 return prev & (1 << codeLength) - 1;
113 }
114
115 return prev & 0x7fffffff;
116}
117
118const SegmentTypes = ["SymbolDictionary", null, null, null, "IntermediateTextRegion", null, "ImmediateTextRegion", "ImmediateLosslessTextRegion", null, null, null, null, null, null, null, null, "PatternDictionary", null, null, null, "IntermediateHalftoneRegion", null, "ImmediateHalftoneRegion", "ImmediateLosslessHalftoneRegion", null, null, null, null, null, null, null, null, null, null, null, null, "IntermediateGenericRegion", null, "ImmediateGenericRegion", "ImmediateLosslessGenericRegion", "IntermediateGenericRefinementRegion", null, "ImmediateGenericRefinementRegion", "ImmediateLosslessGenericRefinementRegion", null, null, null, null, "PageInformation", "EndOfPage", "EndOfStripe", "EndOfFile", "Profiles", "Tables", null, null, null, null, null, null, null, null, "Extension"];
119const CodingTemplates = [[{
120 x: -1,
121 y: -2
122}, {
123 x: 0,
124 y: -2
125}, {
126 x: 1,
127 y: -2
128}, {
129 x: -2,
130 y: -1
131}, {
132 x: -1,
133 y: -1
134}, {
135 x: 0,
136 y: -1
137}, {
138 x: 1,
139 y: -1
140}, {
141 x: 2,
142 y: -1
143}, {
144 x: -4,
145 y: 0
146}, {
147 x: -3,
148 y: 0
149}, {
150 x: -2,
151 y: 0
152}, {
153 x: -1,
154 y: 0
155}], [{
156 x: -1,
157 y: -2
158}, {
159 x: 0,
160 y: -2
161}, {
162 x: 1,
163 y: -2
164}, {
165 x: 2,
166 y: -2
167}, {
168 x: -2,
169 y: -1
170}, {
171 x: -1,
172 y: -1
173}, {
174 x: 0,
175 y: -1
176}, {
177 x: 1,
178 y: -1
179}, {
180 x: 2,
181 y: -1
182}, {
183 x: -3,
184 y: 0
185}, {
186 x: -2,
187 y: 0
188}, {
189 x: -1,
190 y: 0
191}], [{
192 x: -1,
193 y: -2
194}, {
195 x: 0,
196 y: -2
197}, {
198 x: 1,
199 y: -2
200}, {
201 x: -2,
202 y: -1
203}, {
204 x: -1,
205 y: -1
206}, {
207 x: 0,
208 y: -1
209}, {
210 x: 1,
211 y: -1
212}, {
213 x: -2,
214 y: 0
215}, {
216 x: -1,
217 y: 0
218}], [{
219 x: -3,
220 y: -1
221}, {
222 x: -2,
223 y: -1
224}, {
225 x: -1,
226 y: -1
227}, {
228 x: 0,
229 y: -1
230}, {
231 x: 1,
232 y: -1
233}, {
234 x: -4,
235 y: 0
236}, {
237 x: -3,
238 y: 0
239}, {
240 x: -2,
241 y: 0
242}, {
243 x: -1,
244 y: 0
245}]];
246const RefinementTemplates = [{
247 coding: [{
248 x: 0,
249 y: -1
250 }, {
251 x: 1,
252 y: -1
253 }, {
254 x: -1,
255 y: 0
256 }],
257 reference: [{
258 x: 0,
259 y: -1
260 }, {
261 x: 1,
262 y: -1
263 }, {
264 x: -1,
265 y: 0
266 }, {
267 x: 0,
268 y: 0
269 }, {
270 x: 1,
271 y: 0
272 }, {
273 x: -1,
274 y: 1
275 }, {
276 x: 0,
277 y: 1
278 }, {
279 x: 1,
280 y: 1
281 }]
282}, {
283 coding: [{
284 x: -1,
285 y: -1
286 }, {
287 x: 0,
288 y: -1
289 }, {
290 x: 1,
291 y: -1
292 }, {
293 x: -1,
294 y: 0
295 }],
296 reference: [{
297 x: 0,
298 y: -1
299 }, {
300 x: -1,
301 y: 0
302 }, {
303 x: 0,
304 y: 0
305 }, {
306 x: 1,
307 y: 0
308 }, {
309 x: 0,
310 y: 1
311 }, {
312 x: 1,
313 y: 1
314 }]
315}];
316const ReusedContexts = [0x9b25, 0x0795, 0x00e5, 0x0195];
317const RefinementReusedContexts = [0x0020, 0x0008];
318
319function decodeBitmapTemplate0(width, height, decodingContext) {
320 const decoder = decodingContext.decoder;
321 const contexts = decodingContext.contextCache.getContexts("GB");
322 const bitmap = [];
323 let contextLabel, i, j, pixel, row, row1, row2;
324 const OLD_PIXEL_MASK = 0x7bf7;
325
326 for (i = 0; i < height; i++) {
327 row = bitmap[i] = new Uint8Array(width);
328 row1 = i < 1 ? row : bitmap[i - 1];
329 row2 = i < 2 ? row : bitmap[i - 2];
330 contextLabel = row2[0] << 13 | row2[1] << 12 | row2[2] << 11 | row1[0] << 7 | row1[1] << 6 | row1[2] << 5 | row1[3] << 4;
331
332 for (j = 0; j < width; j++) {
333 row[j] = pixel = decoder.readBit(contexts, contextLabel);
334 contextLabel = (contextLabel & OLD_PIXEL_MASK) << 1 | (j + 3 < width ? row2[j + 3] << 11 : 0) | (j + 4 < width ? row1[j + 4] << 4 : 0) | pixel;
335 }
336 }
337
338 return bitmap;
339}
340
341function decodeBitmap(mmr, width, height, templateIndex, prediction, skip, at, decodingContext) {
342 if (mmr) {
343 const input = new Reader(decodingContext.data, decodingContext.start, decodingContext.end);
344 return decodeMMRBitmap(input, width, height, false);
345 }
346
347 if (templateIndex === 0 && !skip && !prediction && at.length === 4 && at[0].x === 3 && at[0].y === -1 && at[1].x === -3 && at[1].y === -1 && at[2].x === 2 && at[2].y === -2 && at[3].x === -2 && at[3].y === -2) {
348 return decodeBitmapTemplate0(width, height, decodingContext);
349 }
350
351 const useskip = !!skip;
352 const template = CodingTemplates[templateIndex].concat(at);
353 template.sort(function (a, b) {
354 return a.y - b.y || a.x - b.x;
355 });
356 const templateLength = template.length;
357 const templateX = new Int8Array(templateLength);
358 const templateY = new Int8Array(templateLength);
359 const changingTemplateEntries = [];
360 let reuseMask = 0,
361 minX = 0,
362 maxX = 0,
363 minY = 0;
364 let c, k;
365
366 for (k = 0; k < templateLength; k++) {
367 templateX[k] = template[k].x;
368 templateY[k] = template[k].y;
369 minX = Math.min(minX, template[k].x);
370 maxX = Math.max(maxX, template[k].x);
371 minY = Math.min(minY, template[k].y);
372
373 if (k < templateLength - 1 && template[k].y === template[k + 1].y && template[k].x === template[k + 1].x - 1) {
374 reuseMask |= 1 << templateLength - 1 - k;
375 } else {
376 changingTemplateEntries.push(k);
377 }
378 }
379
380 const changingEntriesLength = changingTemplateEntries.length;
381 const changingTemplateX = new Int8Array(changingEntriesLength);
382 const changingTemplateY = new Int8Array(changingEntriesLength);
383 const changingTemplateBit = new Uint16Array(changingEntriesLength);
384
385 for (c = 0; c < changingEntriesLength; c++) {
386 k = changingTemplateEntries[c];
387 changingTemplateX[c] = template[k].x;
388 changingTemplateY[c] = template[k].y;
389 changingTemplateBit[c] = 1 << templateLength - 1 - k;
390 }
391
392 const sbb_left = -minX;
393 const sbb_top = -minY;
394 const sbb_right = width - maxX;
395 const pseudoPixelContext = ReusedContexts[templateIndex];
396 let row = new Uint8Array(width);
397 const bitmap = [];
398 const decoder = decodingContext.decoder;
399 const contexts = decodingContext.contextCache.getContexts("GB");
400 let ltp = 0,
401 j,
402 i0,
403 j0,
404 contextLabel = 0,
405 bit,
406 shift;
407
408 for (let i = 0; i < height; i++) {
409 if (prediction) {
410 const sltp = decoder.readBit(contexts, pseudoPixelContext);
411 ltp ^= sltp;
412
413 if (ltp) {
414 bitmap.push(row);
415 continue;
416 }
417 }
418
419 row = new Uint8Array(row);
420 bitmap.push(row);
421
422 for (j = 0; j < width; j++) {
423 if (useskip && skip[i][j]) {
424 row[j] = 0;
425 continue;
426 }
427
428 if (j >= sbb_left && j < sbb_right && i >= sbb_top) {
429 contextLabel = contextLabel << 1 & reuseMask;
430
431 for (k = 0; k < changingEntriesLength; k++) {
432 i0 = i + changingTemplateY[k];
433 j0 = j + changingTemplateX[k];
434 bit = bitmap[i0][j0];
435
436 if (bit) {
437 bit = changingTemplateBit[k];
438 contextLabel |= bit;
439 }
440 }
441 } else {
442 contextLabel = 0;
443 shift = templateLength - 1;
444
445 for (k = 0; k < templateLength; k++, shift--) {
446 j0 = j + templateX[k];
447
448 if (j0 >= 0 && j0 < width) {
449 i0 = i + templateY[k];
450
451 if (i0 >= 0) {
452 bit = bitmap[i0][j0];
453
454 if (bit) {
455 contextLabel |= bit << shift;
456 }
457 }
458 }
459 }
460 }
461
462 const pixel = decoder.readBit(contexts, contextLabel);
463 row[j] = pixel;
464 }
465 }
466
467 return bitmap;
468}
469
470function decodeRefinement(width, height, templateIndex, referenceBitmap, offsetX, offsetY, prediction, at, decodingContext) {
471 let codingTemplate = RefinementTemplates[templateIndex].coding;
472
473 if (templateIndex === 0) {
474 codingTemplate = codingTemplate.concat([at[0]]);
475 }
476
477 const codingTemplateLength = codingTemplate.length;
478 const codingTemplateX = new Int32Array(codingTemplateLength);
479 const codingTemplateY = new Int32Array(codingTemplateLength);
480 let k;
481
482 for (k = 0; k < codingTemplateLength; k++) {
483 codingTemplateX[k] = codingTemplate[k].x;
484 codingTemplateY[k] = codingTemplate[k].y;
485 }
486
487 let referenceTemplate = RefinementTemplates[templateIndex].reference;
488
489 if (templateIndex === 0) {
490 referenceTemplate = referenceTemplate.concat([at[1]]);
491 }
492
493 const referenceTemplateLength = referenceTemplate.length;
494 const referenceTemplateX = new Int32Array(referenceTemplateLength);
495 const referenceTemplateY = new Int32Array(referenceTemplateLength);
496
497 for (k = 0; k < referenceTemplateLength; k++) {
498 referenceTemplateX[k] = referenceTemplate[k].x;
499 referenceTemplateY[k] = referenceTemplate[k].y;
500 }
501
502 const referenceWidth = referenceBitmap[0].length;
503 const referenceHeight = referenceBitmap.length;
504 const pseudoPixelContext = RefinementReusedContexts[templateIndex];
505 const bitmap = [];
506 const decoder = decodingContext.decoder;
507 const contexts = decodingContext.contextCache.getContexts("GR");
508 let ltp = 0;
509
510 for (let i = 0; i < height; i++) {
511 if (prediction) {
512 const sltp = decoder.readBit(contexts, pseudoPixelContext);
513 ltp ^= sltp;
514
515 if (ltp) {
516 throw new Jbig2Error("prediction is not supported");
517 }
518 }
519
520 const row = new Uint8Array(width);
521 bitmap.push(row);
522
523 for (let j = 0; j < width; j++) {
524 let i0, j0;
525 let contextLabel = 0;
526
527 for (k = 0; k < codingTemplateLength; k++) {
528 i0 = i + codingTemplateY[k];
529 j0 = j + codingTemplateX[k];
530
531 if (i0 < 0 || j0 < 0 || j0 >= width) {
532 contextLabel <<= 1;
533 } else {
534 contextLabel = contextLabel << 1 | bitmap[i0][j0];
535 }
536 }
537
538 for (k = 0; k < referenceTemplateLength; k++) {
539 i0 = i + referenceTemplateY[k] - offsetY;
540 j0 = j + referenceTemplateX[k] - offsetX;
541
542 if (i0 < 0 || i0 >= referenceHeight || j0 < 0 || j0 >= referenceWidth) {
543 contextLabel <<= 1;
544 } else {
545 contextLabel = contextLabel << 1 | referenceBitmap[i0][j0];
546 }
547 }
548
549 const pixel = decoder.readBit(contexts, contextLabel);
550 row[j] = pixel;
551 }
552 }
553
554 return bitmap;
555}
556
557function decodeSymbolDictionary(huffman, refinement, symbols, numberOfNewSymbols, numberOfExportedSymbols, huffmanTables, templateIndex, at, refinementTemplateIndex, refinementAt, decodingContext, huffmanInput) {
558 if (huffman && refinement) {
559 throw new Jbig2Error("symbol refinement with Huffman is not supported");
560 }
561
562 const newSymbols = [];
563 let currentHeight = 0;
564 let symbolCodeLength = (0, _core_utils.log2)(symbols.length + numberOfNewSymbols);
565 const decoder = decodingContext.decoder;
566 const contextCache = decodingContext.contextCache;
567 let tableB1, symbolWidths;
568
569 if (huffman) {
570 tableB1 = getStandardTable(1);
571 symbolWidths = [];
572 symbolCodeLength = Math.max(symbolCodeLength, 1);
573 }
574
575 while (newSymbols.length < numberOfNewSymbols) {
576 const deltaHeight = huffman ? huffmanTables.tableDeltaHeight.decode(huffmanInput) : decodeInteger(contextCache, "IADH", decoder);
577 currentHeight += deltaHeight;
578 let currentWidth = 0,
579 totalWidth = 0;
580 const firstSymbol = huffman ? symbolWidths.length : 0;
581
582 while (true) {
583 const deltaWidth = huffman ? huffmanTables.tableDeltaWidth.decode(huffmanInput) : decodeInteger(contextCache, "IADW", decoder);
584
585 if (deltaWidth === null) {
586 break;
587 }
588
589 currentWidth += deltaWidth;
590 totalWidth += currentWidth;
591 let bitmap;
592
593 if (refinement) {
594 const numberOfInstances = decodeInteger(contextCache, "IAAI", decoder);
595
596 if (numberOfInstances > 1) {
597 bitmap = decodeTextRegion(huffman, refinement, currentWidth, currentHeight, 0, numberOfInstances, 1, symbols.concat(newSymbols), symbolCodeLength, 0, 0, 1, 0, huffmanTables, refinementTemplateIndex, refinementAt, decodingContext, 0, huffmanInput);
598 } else {
599 const symbolId = decodeIAID(contextCache, decoder, symbolCodeLength);
600 const rdx = decodeInteger(contextCache, "IARDX", decoder);
601 const rdy = decodeInteger(contextCache, "IARDY", decoder);
602 const symbol = symbolId < symbols.length ? symbols[symbolId] : newSymbols[symbolId - symbols.length];
603 bitmap = decodeRefinement(currentWidth, currentHeight, refinementTemplateIndex, symbol, rdx, rdy, false, refinementAt, decodingContext);
604 }
605
606 newSymbols.push(bitmap);
607 } else if (huffman) {
608 symbolWidths.push(currentWidth);
609 } else {
610 bitmap = decodeBitmap(false, currentWidth, currentHeight, templateIndex, false, null, at, decodingContext);
611 newSymbols.push(bitmap);
612 }
613 }
614
615 if (huffman && !refinement) {
616 const bitmapSize = huffmanTables.tableBitmapSize.decode(huffmanInput);
617 huffmanInput.byteAlign();
618 let collectiveBitmap;
619
620 if (bitmapSize === 0) {
621 collectiveBitmap = readUncompressedBitmap(huffmanInput, totalWidth, currentHeight);
622 } else {
623 const originalEnd = huffmanInput.end;
624 const bitmapEnd = huffmanInput.position + bitmapSize;
625 huffmanInput.end = bitmapEnd;
626 collectiveBitmap = decodeMMRBitmap(huffmanInput, totalWidth, currentHeight, false);
627 huffmanInput.end = originalEnd;
628 huffmanInput.position = bitmapEnd;
629 }
630
631 const numberOfSymbolsDecoded = symbolWidths.length;
632
633 if (firstSymbol === numberOfSymbolsDecoded - 1) {
634 newSymbols.push(collectiveBitmap);
635 } else {
636 let i,
637 y,
638 xMin = 0,
639 xMax,
640 bitmapWidth,
641 symbolBitmap;
642
643 for (i = firstSymbol; i < numberOfSymbolsDecoded; i++) {
644 bitmapWidth = symbolWidths[i];
645 xMax = xMin + bitmapWidth;
646 symbolBitmap = [];
647
648 for (y = 0; y < currentHeight; y++) {
649 symbolBitmap.push(collectiveBitmap[y].subarray(xMin, xMax));
650 }
651
652 newSymbols.push(symbolBitmap);
653 xMin = xMax;
654 }
655 }
656 }
657 }
658
659 const exportedSymbols = [],
660 flags = [];
661 let currentFlag = false,
662 i,
663 ii;
664 const totalSymbolsLength = symbols.length + numberOfNewSymbols;
665
666 while (flags.length < totalSymbolsLength) {
667 let runLength = huffman ? tableB1.decode(huffmanInput) : decodeInteger(contextCache, "IAEX", decoder);
668
669 while (runLength--) {
670 flags.push(currentFlag);
671 }
672
673 currentFlag = !currentFlag;
674 }
675
676 for (i = 0, ii = symbols.length; i < ii; i++) {
677 if (flags[i]) {
678 exportedSymbols.push(symbols[i]);
679 }
680 }
681
682 for (let j = 0; j < numberOfNewSymbols; i++, j++) {
683 if (flags[i]) {
684 exportedSymbols.push(newSymbols[j]);
685 }
686 }
687
688 return exportedSymbols;
689}
690
691function decodeTextRegion(huffman, refinement, width, height, defaultPixelValue, numberOfSymbolInstances, stripSize, inputSymbols, symbolCodeLength, transposed, dsOffset, referenceCorner, combinationOperator, huffmanTables, refinementTemplateIndex, refinementAt, decodingContext, logStripSize, huffmanInput) {
692 if (huffman && refinement) {
693 throw new Jbig2Error("refinement with Huffman is not supported");
694 }
695
696 const bitmap = [];
697 let i, row;
698
699 for (i = 0; i < height; i++) {
700 row = new Uint8Array(width);
701
702 if (defaultPixelValue) {
703 for (let j = 0; j < width; j++) {
704 row[j] = defaultPixelValue;
705 }
706 }
707
708 bitmap.push(row);
709 }
710
711 const decoder = decodingContext.decoder;
712 const contextCache = decodingContext.contextCache;
713 let stripT = huffman ? -huffmanTables.tableDeltaT.decode(huffmanInput) : -decodeInteger(contextCache, "IADT", decoder);
714 let firstS = 0;
715 i = 0;
716
717 while (i < numberOfSymbolInstances) {
718 const deltaT = huffman ? huffmanTables.tableDeltaT.decode(huffmanInput) : decodeInteger(contextCache, "IADT", decoder);
719 stripT += deltaT;
720 const deltaFirstS = huffman ? huffmanTables.tableFirstS.decode(huffmanInput) : decodeInteger(contextCache, "IAFS", decoder);
721 firstS += deltaFirstS;
722 let currentS = firstS;
723
724 do {
725 let currentT = 0;
726
727 if (stripSize > 1) {
728 currentT = huffman ? huffmanInput.readBits(logStripSize) : decodeInteger(contextCache, "IAIT", decoder);
729 }
730
731 const t = stripSize * stripT + currentT;
732 const symbolId = huffman ? huffmanTables.symbolIDTable.decode(huffmanInput) : decodeIAID(contextCache, decoder, symbolCodeLength);
733 const applyRefinement = refinement && (huffman ? huffmanInput.readBit() : decodeInteger(contextCache, "IARI", decoder));
734 let symbolBitmap = inputSymbols[symbolId];
735 let symbolWidth = symbolBitmap[0].length;
736 let symbolHeight = symbolBitmap.length;
737
738 if (applyRefinement) {
739 const rdw = decodeInteger(contextCache, "IARDW", decoder);
740 const rdh = decodeInteger(contextCache, "IARDH", decoder);
741 const rdx = decodeInteger(contextCache, "IARDX", decoder);
742 const rdy = decodeInteger(contextCache, "IARDY", decoder);
743 symbolWidth += rdw;
744 symbolHeight += rdh;
745 symbolBitmap = decodeRefinement(symbolWidth, symbolHeight, refinementTemplateIndex, symbolBitmap, (rdw >> 1) + rdx, (rdh >> 1) + rdy, false, refinementAt, decodingContext);
746 }
747
748 const offsetT = t - (referenceCorner & 1 ? 0 : symbolHeight - 1);
749 const offsetS = currentS - (referenceCorner & 2 ? symbolWidth - 1 : 0);
750 let s2, t2, symbolRow;
751
752 if (transposed) {
753 for (s2 = 0; s2 < symbolHeight; s2++) {
754 row = bitmap[offsetS + s2];
755
756 if (!row) {
757 continue;
758 }
759
760 symbolRow = symbolBitmap[s2];
761 const maxWidth = Math.min(width - offsetT, symbolWidth);
762
763 switch (combinationOperator) {
764 case 0:
765 for (t2 = 0; t2 < maxWidth; t2++) {
766 row[offsetT + t2] |= symbolRow[t2];
767 }
768
769 break;
770
771 case 2:
772 for (t2 = 0; t2 < maxWidth; t2++) {
773 row[offsetT + t2] ^= symbolRow[t2];
774 }
775
776 break;
777
778 default:
779 throw new Jbig2Error(`operator ${combinationOperator} is not supported`);
780 }
781 }
782
783 currentS += symbolHeight - 1;
784 } else {
785 for (t2 = 0; t2 < symbolHeight; t2++) {
786 row = bitmap[offsetT + t2];
787
788 if (!row) {
789 continue;
790 }
791
792 symbolRow = symbolBitmap[t2];
793
794 switch (combinationOperator) {
795 case 0:
796 for (s2 = 0; s2 < symbolWidth; s2++) {
797 row[offsetS + s2] |= symbolRow[s2];
798 }
799
800 break;
801
802 case 2:
803 for (s2 = 0; s2 < symbolWidth; s2++) {
804 row[offsetS + s2] ^= symbolRow[s2];
805 }
806
807 break;
808
809 default:
810 throw new Jbig2Error(`operator ${combinationOperator} is not supported`);
811 }
812 }
813
814 currentS += symbolWidth - 1;
815 }
816
817 i++;
818 const deltaS = huffman ? huffmanTables.tableDeltaS.decode(huffmanInput) : decodeInteger(contextCache, "IADS", decoder);
819
820 if (deltaS === null) {
821 break;
822 }
823
824 currentS += deltaS + dsOffset;
825 } while (true);
826 }
827
828 return bitmap;
829}
830
831function decodePatternDictionary(mmr, patternWidth, patternHeight, maxPatternIndex, template, decodingContext) {
832 const at = [];
833
834 if (!mmr) {
835 at.push({
836 x: -patternWidth,
837 y: 0
838 });
839
840 if (template === 0) {
841 at.push({
842 x: -3,
843 y: -1
844 }, {
845 x: 2,
846 y: -2
847 }, {
848 x: -2,
849 y: -2
850 });
851 }
852 }
853
854 const collectiveWidth = (maxPatternIndex + 1) * patternWidth;
855 const collectiveBitmap = decodeBitmap(mmr, collectiveWidth, patternHeight, template, false, null, at, decodingContext);
856 const patterns = [];
857
858 for (let i = 0; i <= maxPatternIndex; i++) {
859 const patternBitmap = [];
860 const xMin = patternWidth * i;
861 const xMax = xMin + patternWidth;
862
863 for (let y = 0; y < patternHeight; y++) {
864 patternBitmap.push(collectiveBitmap[y].subarray(xMin, xMax));
865 }
866
867 patterns.push(patternBitmap);
868 }
869
870 return patterns;
871}
872
873function decodeHalftoneRegion(mmr, patterns, template, regionWidth, regionHeight, defaultPixelValue, enableSkip, combinationOperator, gridWidth, gridHeight, gridOffsetX, gridOffsetY, gridVectorX, gridVectorY, decodingContext) {
874 const skip = null;
875
876 if (enableSkip) {
877 throw new Jbig2Error("skip is not supported");
878 }
879
880 if (combinationOperator !== 0) {
881 throw new Jbig2Error(`operator "${combinationOperator}" is not supported in halftone region`);
882 }
883
884 const regionBitmap = [];
885 let i, j, row;
886
887 for (i = 0; i < regionHeight; i++) {
888 row = new Uint8Array(regionWidth);
889
890 if (defaultPixelValue) {
891 for (j = 0; j < regionWidth; j++) {
892 row[j] = defaultPixelValue;
893 }
894 }
895
896 regionBitmap.push(row);
897 }
898
899 const numberOfPatterns = patterns.length;
900 const pattern0 = patterns[0];
901 const patternWidth = pattern0[0].length,
902 patternHeight = pattern0.length;
903 const bitsPerValue = (0, _core_utils.log2)(numberOfPatterns);
904 const at = [];
905
906 if (!mmr) {
907 at.push({
908 x: template <= 1 ? 3 : 2,
909 y: -1
910 });
911
912 if (template === 0) {
913 at.push({
914 x: -3,
915 y: -1
916 }, {
917 x: 2,
918 y: -2
919 }, {
920 x: -2,
921 y: -2
922 });
923 }
924 }
925
926 const grayScaleBitPlanes = [];
927 let mmrInput, bitmap;
928
929 if (mmr) {
930 mmrInput = new Reader(decodingContext.data, decodingContext.start, decodingContext.end);
931 }
932
933 for (i = bitsPerValue - 1; i >= 0; i--) {
934 if (mmr) {
935 bitmap = decodeMMRBitmap(mmrInput, gridWidth, gridHeight, true);
936 } else {
937 bitmap = decodeBitmap(false, gridWidth, gridHeight, template, false, skip, at, decodingContext);
938 }
939
940 grayScaleBitPlanes[i] = bitmap;
941 }
942
943 let mg, ng, bit, patternIndex, patternBitmap, x, y, patternRow, regionRow;
944
945 for (mg = 0; mg < gridHeight; mg++) {
946 for (ng = 0; ng < gridWidth; ng++) {
947 bit = 0;
948 patternIndex = 0;
949
950 for (j = bitsPerValue - 1; j >= 0; j--) {
951 bit ^= grayScaleBitPlanes[j][mg][ng];
952 patternIndex |= bit << j;
953 }
954
955 patternBitmap = patterns[patternIndex];
956 x = gridOffsetX + mg * gridVectorY + ng * gridVectorX >> 8;
957 y = gridOffsetY + mg * gridVectorX - ng * gridVectorY >> 8;
958
959 if (x >= 0 && x + patternWidth <= regionWidth && y >= 0 && y + patternHeight <= regionHeight) {
960 for (i = 0; i < patternHeight; i++) {
961 regionRow = regionBitmap[y + i];
962 patternRow = patternBitmap[i];
963
964 for (j = 0; j < patternWidth; j++) {
965 regionRow[x + j] |= patternRow[j];
966 }
967 }
968 } else {
969 let regionX, regionY;
970
971 for (i = 0; i < patternHeight; i++) {
972 regionY = y + i;
973
974 if (regionY < 0 || regionY >= regionHeight) {
975 continue;
976 }
977
978 regionRow = regionBitmap[regionY];
979 patternRow = patternBitmap[i];
980
981 for (j = 0; j < patternWidth; j++) {
982 regionX = x + j;
983
984 if (regionX >= 0 && regionX < regionWidth) {
985 regionRow[regionX] |= patternRow[j];
986 }
987 }
988 }
989 }
990 }
991 }
992
993 return regionBitmap;
994}
995
996function readSegmentHeader(data, start) {
997 const segmentHeader = {};
998 segmentHeader.number = (0, _core_utils.readUint32)(data, start);
999 const flags = data[start + 4];
1000 const segmentType = flags & 0x3f;
1001
1002 if (!SegmentTypes[segmentType]) {
1003 throw new Jbig2Error("invalid segment type: " + segmentType);
1004 }
1005
1006 segmentHeader.type = segmentType;
1007 segmentHeader.typeName = SegmentTypes[segmentType];
1008 segmentHeader.deferredNonRetain = !!(flags & 0x80);
1009 const pageAssociationFieldSize = !!(flags & 0x40);
1010 const referredFlags = data[start + 5];
1011 let referredToCount = referredFlags >> 5 & 7;
1012 const retainBits = [referredFlags & 31];
1013 let position = start + 6;
1014
1015 if (referredFlags === 7) {
1016 referredToCount = (0, _core_utils.readUint32)(data, position - 1) & 0x1fffffff;
1017 position += 3;
1018 let bytes = referredToCount + 7 >> 3;
1019 retainBits[0] = data[position++];
1020
1021 while (--bytes > 0) {
1022 retainBits.push(data[position++]);
1023 }
1024 } else if (referredFlags === 5 || referredFlags === 6) {
1025 throw new Jbig2Error("invalid referred-to flags");
1026 }
1027
1028 segmentHeader.retainBits = retainBits;
1029 let referredToSegmentNumberSize = 4;
1030
1031 if (segmentHeader.number <= 256) {
1032 referredToSegmentNumberSize = 1;
1033 } else if (segmentHeader.number <= 65536) {
1034 referredToSegmentNumberSize = 2;
1035 }
1036
1037 const referredTo = [];
1038 let i, ii;
1039
1040 for (i = 0; i < referredToCount; i++) {
1041 let number;
1042
1043 if (referredToSegmentNumberSize === 1) {
1044 number = data[position];
1045 } else if (referredToSegmentNumberSize === 2) {
1046 number = (0, _core_utils.readUint16)(data, position);
1047 } else {
1048 number = (0, _core_utils.readUint32)(data, position);
1049 }
1050
1051 referredTo.push(number);
1052 position += referredToSegmentNumberSize;
1053 }
1054
1055 segmentHeader.referredTo = referredTo;
1056
1057 if (!pageAssociationFieldSize) {
1058 segmentHeader.pageAssociation = data[position++];
1059 } else {
1060 segmentHeader.pageAssociation = (0, _core_utils.readUint32)(data, position);
1061 position += 4;
1062 }
1063
1064 segmentHeader.length = (0, _core_utils.readUint32)(data, position);
1065 position += 4;
1066
1067 if (segmentHeader.length === 0xffffffff) {
1068 if (segmentType === 38) {
1069 const genericRegionInfo = readRegionSegmentInformation(data, position);
1070 const genericRegionSegmentFlags = data[position + RegionSegmentInformationFieldLength];
1071 const genericRegionMmr = !!(genericRegionSegmentFlags & 1);
1072 const searchPatternLength = 6;
1073 const searchPattern = new Uint8Array(searchPatternLength);
1074
1075 if (!genericRegionMmr) {
1076 searchPattern[0] = 0xff;
1077 searchPattern[1] = 0xac;
1078 }
1079
1080 searchPattern[2] = genericRegionInfo.height >>> 24 & 0xff;
1081 searchPattern[3] = genericRegionInfo.height >> 16 & 0xff;
1082 searchPattern[4] = genericRegionInfo.height >> 8 & 0xff;
1083 searchPattern[5] = genericRegionInfo.height & 0xff;
1084
1085 for (i = position, ii = data.length; i < ii; i++) {
1086 let j = 0;
1087
1088 while (j < searchPatternLength && searchPattern[j] === data[i + j]) {
1089 j++;
1090 }
1091
1092 if (j === searchPatternLength) {
1093 segmentHeader.length = i + searchPatternLength;
1094 break;
1095 }
1096 }
1097
1098 if (segmentHeader.length === 0xffffffff) {
1099 throw new Jbig2Error("segment end was not found");
1100 }
1101 } else {
1102 throw new Jbig2Error("invalid unknown segment length");
1103 }
1104 }
1105
1106 segmentHeader.headerEnd = position;
1107 return segmentHeader;
1108}
1109
1110function readSegments(header, data, start, end) {
1111 const segments = [];
1112 let position = start;
1113
1114 while (position < end) {
1115 const segmentHeader = readSegmentHeader(data, position);
1116 position = segmentHeader.headerEnd;
1117 const segment = {
1118 header: segmentHeader,
1119 data
1120 };
1121
1122 if (!header.randomAccess) {
1123 segment.start = position;
1124 position += segmentHeader.length;
1125 segment.end = position;
1126 }
1127
1128 segments.push(segment);
1129
1130 if (segmentHeader.type === 51) {
1131 break;
1132 }
1133 }
1134
1135 if (header.randomAccess) {
1136 for (let i = 0, ii = segments.length; i < ii; i++) {
1137 segments[i].start = position;
1138 position += segments[i].header.length;
1139 segments[i].end = position;
1140 }
1141 }
1142
1143 return segments;
1144}
1145
1146function readRegionSegmentInformation(data, start) {
1147 return {
1148 width: (0, _core_utils.readUint32)(data, start),
1149 height: (0, _core_utils.readUint32)(data, start + 4),
1150 x: (0, _core_utils.readUint32)(data, start + 8),
1151 y: (0, _core_utils.readUint32)(data, start + 12),
1152 combinationOperator: data[start + 16] & 7
1153 };
1154}
1155
1156const RegionSegmentInformationFieldLength = 17;
1157
1158function processSegment(segment, visitor) {
1159 const header = segment.header;
1160 const data = segment.data,
1161 end = segment.end;
1162 let position = segment.start;
1163 let args, at, i, atLength;
1164
1165 switch (header.type) {
1166 case 0:
1167 const dictionary = {};
1168 const dictionaryFlags = (0, _core_utils.readUint16)(data, position);
1169 dictionary.huffman = !!(dictionaryFlags & 1);
1170 dictionary.refinement = !!(dictionaryFlags & 2);
1171 dictionary.huffmanDHSelector = dictionaryFlags >> 2 & 3;
1172 dictionary.huffmanDWSelector = dictionaryFlags >> 4 & 3;
1173 dictionary.bitmapSizeSelector = dictionaryFlags >> 6 & 1;
1174 dictionary.aggregationInstancesSelector = dictionaryFlags >> 7 & 1;
1175 dictionary.bitmapCodingContextUsed = !!(dictionaryFlags & 256);
1176 dictionary.bitmapCodingContextRetained = !!(dictionaryFlags & 512);
1177 dictionary.template = dictionaryFlags >> 10 & 3;
1178 dictionary.refinementTemplate = dictionaryFlags >> 12 & 1;
1179 position += 2;
1180
1181 if (!dictionary.huffman) {
1182 atLength = dictionary.template === 0 ? 4 : 1;
1183 at = [];
1184
1185 for (i = 0; i < atLength; i++) {
1186 at.push({
1187 x: (0, _core_utils.readInt8)(data, position),
1188 y: (0, _core_utils.readInt8)(data, position + 1)
1189 });
1190 position += 2;
1191 }
1192
1193 dictionary.at = at;
1194 }
1195
1196 if (dictionary.refinement && !dictionary.refinementTemplate) {
1197 at = [];
1198
1199 for (i = 0; i < 2; i++) {
1200 at.push({
1201 x: (0, _core_utils.readInt8)(data, position),
1202 y: (0, _core_utils.readInt8)(data, position + 1)
1203 });
1204 position += 2;
1205 }
1206
1207 dictionary.refinementAt = at;
1208 }
1209
1210 dictionary.numberOfExportedSymbols = (0, _core_utils.readUint32)(data, position);
1211 position += 4;
1212 dictionary.numberOfNewSymbols = (0, _core_utils.readUint32)(data, position);
1213 position += 4;
1214 args = [dictionary, header.number, header.referredTo, data, position, end];
1215 break;
1216
1217 case 6:
1218 case 7:
1219 const textRegion = {};
1220 textRegion.info = readRegionSegmentInformation(data, position);
1221 position += RegionSegmentInformationFieldLength;
1222 const textRegionSegmentFlags = (0, _core_utils.readUint16)(data, position);
1223 position += 2;
1224 textRegion.huffman = !!(textRegionSegmentFlags & 1);
1225 textRegion.refinement = !!(textRegionSegmentFlags & 2);
1226 textRegion.logStripSize = textRegionSegmentFlags >> 2 & 3;
1227 textRegion.stripSize = 1 << textRegion.logStripSize;
1228 textRegion.referenceCorner = textRegionSegmentFlags >> 4 & 3;
1229 textRegion.transposed = !!(textRegionSegmentFlags & 64);
1230 textRegion.combinationOperator = textRegionSegmentFlags >> 7 & 3;
1231 textRegion.defaultPixelValue = textRegionSegmentFlags >> 9 & 1;
1232 textRegion.dsOffset = textRegionSegmentFlags << 17 >> 27;
1233 textRegion.refinementTemplate = textRegionSegmentFlags >> 15 & 1;
1234
1235 if (textRegion.huffman) {
1236 const textRegionHuffmanFlags = (0, _core_utils.readUint16)(data, position);
1237 position += 2;
1238 textRegion.huffmanFS = textRegionHuffmanFlags & 3;
1239 textRegion.huffmanDS = textRegionHuffmanFlags >> 2 & 3;
1240 textRegion.huffmanDT = textRegionHuffmanFlags >> 4 & 3;
1241 textRegion.huffmanRefinementDW = textRegionHuffmanFlags >> 6 & 3;
1242 textRegion.huffmanRefinementDH = textRegionHuffmanFlags >> 8 & 3;
1243 textRegion.huffmanRefinementDX = textRegionHuffmanFlags >> 10 & 3;
1244 textRegion.huffmanRefinementDY = textRegionHuffmanFlags >> 12 & 3;
1245 textRegion.huffmanRefinementSizeSelector = !!(textRegionHuffmanFlags & 0x4000);
1246 }
1247
1248 if (textRegion.refinement && !textRegion.refinementTemplate) {
1249 at = [];
1250
1251 for (i = 0; i < 2; i++) {
1252 at.push({
1253 x: (0, _core_utils.readInt8)(data, position),
1254 y: (0, _core_utils.readInt8)(data, position + 1)
1255 });
1256 position += 2;
1257 }
1258
1259 textRegion.refinementAt = at;
1260 }
1261
1262 textRegion.numberOfSymbolInstances = (0, _core_utils.readUint32)(data, position);
1263 position += 4;
1264 args = [textRegion, header.referredTo, data, position, end];
1265 break;
1266
1267 case 16:
1268 const patternDictionary = {};
1269 const patternDictionaryFlags = data[position++];
1270 patternDictionary.mmr = !!(patternDictionaryFlags & 1);
1271 patternDictionary.template = patternDictionaryFlags >> 1 & 3;
1272 patternDictionary.patternWidth = data[position++];
1273 patternDictionary.patternHeight = data[position++];
1274 patternDictionary.maxPatternIndex = (0, _core_utils.readUint32)(data, position);
1275 position += 4;
1276 args = [patternDictionary, header.number, data, position, end];
1277 break;
1278
1279 case 22:
1280 case 23:
1281 const halftoneRegion = {};
1282 halftoneRegion.info = readRegionSegmentInformation(data, position);
1283 position += RegionSegmentInformationFieldLength;
1284 const halftoneRegionFlags = data[position++];
1285 halftoneRegion.mmr = !!(halftoneRegionFlags & 1);
1286 halftoneRegion.template = halftoneRegionFlags >> 1 & 3;
1287 halftoneRegion.enableSkip = !!(halftoneRegionFlags & 8);
1288 halftoneRegion.combinationOperator = halftoneRegionFlags >> 4 & 7;
1289 halftoneRegion.defaultPixelValue = halftoneRegionFlags >> 7 & 1;
1290 halftoneRegion.gridWidth = (0, _core_utils.readUint32)(data, position);
1291 position += 4;
1292 halftoneRegion.gridHeight = (0, _core_utils.readUint32)(data, position);
1293 position += 4;
1294 halftoneRegion.gridOffsetX = (0, _core_utils.readUint32)(data, position) & 0xffffffff;
1295 position += 4;
1296 halftoneRegion.gridOffsetY = (0, _core_utils.readUint32)(data, position) & 0xffffffff;
1297 position += 4;
1298 halftoneRegion.gridVectorX = (0, _core_utils.readUint16)(data, position);
1299 position += 2;
1300 halftoneRegion.gridVectorY = (0, _core_utils.readUint16)(data, position);
1301 position += 2;
1302 args = [halftoneRegion, header.referredTo, data, position, end];
1303 break;
1304
1305 case 38:
1306 case 39:
1307 const genericRegion = {};
1308 genericRegion.info = readRegionSegmentInformation(data, position);
1309 position += RegionSegmentInformationFieldLength;
1310 const genericRegionSegmentFlags = data[position++];
1311 genericRegion.mmr = !!(genericRegionSegmentFlags & 1);
1312 genericRegion.template = genericRegionSegmentFlags >> 1 & 3;
1313 genericRegion.prediction = !!(genericRegionSegmentFlags & 8);
1314
1315 if (!genericRegion.mmr) {
1316 atLength = genericRegion.template === 0 ? 4 : 1;
1317 at = [];
1318
1319 for (i = 0; i < atLength; i++) {
1320 at.push({
1321 x: (0, _core_utils.readInt8)(data, position),
1322 y: (0, _core_utils.readInt8)(data, position + 1)
1323 });
1324 position += 2;
1325 }
1326
1327 genericRegion.at = at;
1328 }
1329
1330 args = [genericRegion, data, position, end];
1331 break;
1332
1333 case 48:
1334 const pageInfo = {
1335 width: (0, _core_utils.readUint32)(data, position),
1336 height: (0, _core_utils.readUint32)(data, position + 4),
1337 resolutionX: (0, _core_utils.readUint32)(data, position + 8),
1338 resolutionY: (0, _core_utils.readUint32)(data, position + 12)
1339 };
1340
1341 if (pageInfo.height === 0xffffffff) {
1342 delete pageInfo.height;
1343 }
1344
1345 const pageSegmentFlags = data[position + 16];
1346 (0, _core_utils.readUint16)(data, position + 17);
1347 pageInfo.lossless = !!(pageSegmentFlags & 1);
1348 pageInfo.refinement = !!(pageSegmentFlags & 2);
1349 pageInfo.defaultPixelValue = pageSegmentFlags >> 2 & 1;
1350 pageInfo.combinationOperator = pageSegmentFlags >> 3 & 3;
1351 pageInfo.requiresBuffer = !!(pageSegmentFlags & 32);
1352 pageInfo.combinationOperatorOverride = !!(pageSegmentFlags & 64);
1353 args = [pageInfo];
1354 break;
1355
1356 case 49:
1357 break;
1358
1359 case 50:
1360 break;
1361
1362 case 51:
1363 break;
1364
1365 case 53:
1366 args = [header.number, data, position, end];
1367 break;
1368
1369 case 62:
1370 break;
1371
1372 default:
1373 throw new Jbig2Error(`segment type ${header.typeName}(${header.type}) is not implemented`);
1374 }
1375
1376 const callbackName = "on" + header.typeName;
1377
1378 if (callbackName in visitor) {
1379 visitor[callbackName].apply(visitor, args);
1380 }
1381}
1382
1383function processSegments(segments, visitor) {
1384 for (let i = 0, ii = segments.length; i < ii; i++) {
1385 processSegment(segments[i], visitor);
1386 }
1387}
1388
1389function parseJbig2Chunks(chunks) {
1390 const visitor = new SimpleSegmentVisitor();
1391
1392 for (let i = 0, ii = chunks.length; i < ii; i++) {
1393 const chunk = chunks[i];
1394 const segments = readSegments({}, chunk.data, chunk.start, chunk.end);
1395 processSegments(segments, visitor);
1396 }
1397
1398 return visitor.buffer;
1399}
1400
1401function parseJbig2(data) {
1402 throw new Error("Not implemented: parseJbig2");
1403}
1404
1405class SimpleSegmentVisitor {
1406 onPageInformation(info) {
1407 this.currentPageInfo = info;
1408 const rowSize = info.width + 7 >> 3;
1409 const buffer = new Uint8ClampedArray(rowSize * info.height);
1410
1411 if (info.defaultPixelValue) {
1412 buffer.fill(0xff);
1413 }
1414
1415 this.buffer = buffer;
1416 }
1417
1418 drawBitmap(regionInfo, bitmap) {
1419 const pageInfo = this.currentPageInfo;
1420 const width = regionInfo.width,
1421 height = regionInfo.height;
1422 const rowSize = pageInfo.width + 7 >> 3;
1423 const combinationOperator = pageInfo.combinationOperatorOverride ? regionInfo.combinationOperator : pageInfo.combinationOperator;
1424 const buffer = this.buffer;
1425 const mask0 = 128 >> (regionInfo.x & 7);
1426 let offset0 = regionInfo.y * rowSize + (regionInfo.x >> 3);
1427 let i, j, mask, offset;
1428
1429 switch (combinationOperator) {
1430 case 0:
1431 for (i = 0; i < height; i++) {
1432 mask = mask0;
1433 offset = offset0;
1434
1435 for (j = 0; j < width; j++) {
1436 if (bitmap[i][j]) {
1437 buffer[offset] |= mask;
1438 }
1439
1440 mask >>= 1;
1441
1442 if (!mask) {
1443 mask = 128;
1444 offset++;
1445 }
1446 }
1447
1448 offset0 += rowSize;
1449 }
1450
1451 break;
1452
1453 case 2:
1454 for (i = 0; i < height; i++) {
1455 mask = mask0;
1456 offset = offset0;
1457
1458 for (j = 0; j < width; j++) {
1459 if (bitmap[i][j]) {
1460 buffer[offset] ^= mask;
1461 }
1462
1463 mask >>= 1;
1464
1465 if (!mask) {
1466 mask = 128;
1467 offset++;
1468 }
1469 }
1470
1471 offset0 += rowSize;
1472 }
1473
1474 break;
1475
1476 default:
1477 throw new Jbig2Error(`operator ${combinationOperator} is not supported`);
1478 }
1479 }
1480
1481 onImmediateGenericRegion(region, data, start, end) {
1482 const regionInfo = region.info;
1483 const decodingContext = new DecodingContext(data, start, end);
1484 const bitmap = decodeBitmap(region.mmr, regionInfo.width, regionInfo.height, region.template, region.prediction, null, region.at, decodingContext);
1485 this.drawBitmap(regionInfo, bitmap);
1486 }
1487
1488 onImmediateLosslessGenericRegion() {
1489 this.onImmediateGenericRegion.apply(this, arguments);
1490 }
1491
1492 onSymbolDictionary(dictionary, currentSegment, referredSegments, data, start, end) {
1493 let huffmanTables, huffmanInput;
1494
1495 if (dictionary.huffman) {
1496 huffmanTables = getSymbolDictionaryHuffmanTables(dictionary, referredSegments, this.customTables);
1497 huffmanInput = new Reader(data, start, end);
1498 }
1499
1500 let symbols = this.symbols;
1501
1502 if (!symbols) {
1503 this.symbols = symbols = {};
1504 }
1505
1506 const inputSymbols = [];
1507
1508 for (const referredSegment of referredSegments) {
1509 const referredSymbols = symbols[referredSegment];
1510
1511 if (referredSymbols) {
1512 inputSymbols.push(...referredSymbols);
1513 }
1514 }
1515
1516 const decodingContext = new DecodingContext(data, start, end);
1517 symbols[currentSegment] = decodeSymbolDictionary(dictionary.huffman, dictionary.refinement, inputSymbols, dictionary.numberOfNewSymbols, dictionary.numberOfExportedSymbols, huffmanTables, dictionary.template, dictionary.at, dictionary.refinementTemplate, dictionary.refinementAt, decodingContext, huffmanInput);
1518 }
1519
1520 onImmediateTextRegion(region, referredSegments, data, start, end) {
1521 const regionInfo = region.info;
1522 let huffmanTables, huffmanInput;
1523 const symbols = this.symbols;
1524 const inputSymbols = [];
1525
1526 for (const referredSegment of referredSegments) {
1527 const referredSymbols = symbols[referredSegment];
1528
1529 if (referredSymbols) {
1530 inputSymbols.push(...referredSymbols);
1531 }
1532 }
1533
1534 const symbolCodeLength = (0, _core_utils.log2)(inputSymbols.length);
1535
1536 if (region.huffman) {
1537 huffmanInput = new Reader(data, start, end);
1538 huffmanTables = getTextRegionHuffmanTables(region, referredSegments, this.customTables, inputSymbols.length, huffmanInput);
1539 }
1540
1541 const decodingContext = new DecodingContext(data, start, end);
1542 const bitmap = decodeTextRegion(region.huffman, region.refinement, regionInfo.width, regionInfo.height, region.defaultPixelValue, region.numberOfSymbolInstances, region.stripSize, inputSymbols, symbolCodeLength, region.transposed, region.dsOffset, region.referenceCorner, region.combinationOperator, huffmanTables, region.refinementTemplate, region.refinementAt, decodingContext, region.logStripSize, huffmanInput);
1543 this.drawBitmap(regionInfo, bitmap);
1544 }
1545
1546 onImmediateLosslessTextRegion() {
1547 this.onImmediateTextRegion.apply(this, arguments);
1548 }
1549
1550 onPatternDictionary(dictionary, currentSegment, data, start, end) {
1551 let patterns = this.patterns;
1552
1553 if (!patterns) {
1554 this.patterns = patterns = {};
1555 }
1556
1557 const decodingContext = new DecodingContext(data, start, end);
1558 patterns[currentSegment] = decodePatternDictionary(dictionary.mmr, dictionary.patternWidth, dictionary.patternHeight, dictionary.maxPatternIndex, dictionary.template, decodingContext);
1559 }
1560
1561 onImmediateHalftoneRegion(region, referredSegments, data, start, end) {
1562 const patterns = this.patterns[referredSegments[0]];
1563 const regionInfo = region.info;
1564 const decodingContext = new DecodingContext(data, start, end);
1565 const bitmap = decodeHalftoneRegion(region.mmr, patterns, region.template, regionInfo.width, regionInfo.height, region.defaultPixelValue, region.enableSkip, region.combinationOperator, region.gridWidth, region.gridHeight, region.gridOffsetX, region.gridOffsetY, region.gridVectorX, region.gridVectorY, decodingContext);
1566 this.drawBitmap(regionInfo, bitmap);
1567 }
1568
1569 onImmediateLosslessHalftoneRegion() {
1570 this.onImmediateHalftoneRegion.apply(this, arguments);
1571 }
1572
1573 onTables(currentSegment, data, start, end) {
1574 let customTables = this.customTables;
1575
1576 if (!customTables) {
1577 this.customTables = customTables = {};
1578 }
1579
1580 customTables[currentSegment] = decodeTablesSegment(data, start, end);
1581 }
1582
1583}
1584
1585class HuffmanLine {
1586 constructor(lineData) {
1587 if (lineData.length === 2) {
1588 this.isOOB = true;
1589 this.rangeLow = 0;
1590 this.prefixLength = lineData[0];
1591 this.rangeLength = 0;
1592 this.prefixCode = lineData[1];
1593 this.isLowerRange = false;
1594 } else {
1595 this.isOOB = false;
1596 this.rangeLow = lineData[0];
1597 this.prefixLength = lineData[1];
1598 this.rangeLength = lineData[2];
1599 this.prefixCode = lineData[3];
1600 this.isLowerRange = lineData[4] === "lower";
1601 }
1602 }
1603
1604}
1605
1606class HuffmanTreeNode {
1607 constructor(line) {
1608 this.children = [];
1609
1610 if (line) {
1611 this.isLeaf = true;
1612 this.rangeLength = line.rangeLength;
1613 this.rangeLow = line.rangeLow;
1614 this.isLowerRange = line.isLowerRange;
1615 this.isOOB = line.isOOB;
1616 } else {
1617 this.isLeaf = false;
1618 }
1619 }
1620
1621 buildTree(line, shift) {
1622 const bit = line.prefixCode >> shift & 1;
1623
1624 if (shift <= 0) {
1625 this.children[bit] = new HuffmanTreeNode(line);
1626 } else {
1627 let node = this.children[bit];
1628
1629 if (!node) {
1630 this.children[bit] = node = new HuffmanTreeNode(null);
1631 }
1632
1633 node.buildTree(line, shift - 1);
1634 }
1635 }
1636
1637 decodeNode(reader) {
1638 if (this.isLeaf) {
1639 if (this.isOOB) {
1640 return null;
1641 }
1642
1643 const htOffset = reader.readBits(this.rangeLength);
1644 return this.rangeLow + (this.isLowerRange ? -htOffset : htOffset);
1645 }
1646
1647 const node = this.children[reader.readBit()];
1648
1649 if (!node) {
1650 throw new Jbig2Error("invalid Huffman data");
1651 }
1652
1653 return node.decodeNode(reader);
1654 }
1655
1656}
1657
1658class HuffmanTable {
1659 constructor(lines, prefixCodesDone) {
1660 if (!prefixCodesDone) {
1661 this.assignPrefixCodes(lines);
1662 }
1663
1664 this.rootNode = new HuffmanTreeNode(null);
1665
1666 for (let i = 0, ii = lines.length; i < ii; i++) {
1667 const line = lines[i];
1668
1669 if (line.prefixLength > 0) {
1670 this.rootNode.buildTree(line, line.prefixLength - 1);
1671 }
1672 }
1673 }
1674
1675 decode(reader) {
1676 return this.rootNode.decodeNode(reader);
1677 }
1678
1679 assignPrefixCodes(lines) {
1680 const linesLength = lines.length;
1681 let prefixLengthMax = 0;
1682
1683 for (let i = 0; i < linesLength; i++) {
1684 prefixLengthMax = Math.max(prefixLengthMax, lines[i].prefixLength);
1685 }
1686
1687 const histogram = new Uint32Array(prefixLengthMax + 1);
1688
1689 for (let i = 0; i < linesLength; i++) {
1690 histogram[lines[i].prefixLength]++;
1691 }
1692
1693 let currentLength = 1,
1694 firstCode = 0,
1695 currentCode,
1696 currentTemp,
1697 line;
1698 histogram[0] = 0;
1699
1700 while (currentLength <= prefixLengthMax) {
1701 firstCode = firstCode + histogram[currentLength - 1] << 1;
1702 currentCode = firstCode;
1703 currentTemp = 0;
1704
1705 while (currentTemp < linesLength) {
1706 line = lines[currentTemp];
1707
1708 if (line.prefixLength === currentLength) {
1709 line.prefixCode = currentCode;
1710 currentCode++;
1711 }
1712
1713 currentTemp++;
1714 }
1715
1716 currentLength++;
1717 }
1718 }
1719
1720}
1721
1722function decodeTablesSegment(data, start, end) {
1723 const flags = data[start];
1724 const lowestValue = (0, _core_utils.readUint32)(data, start + 1) & 0xffffffff;
1725 const highestValue = (0, _core_utils.readUint32)(data, start + 5) & 0xffffffff;
1726 const reader = new Reader(data, start + 9, end);
1727 const prefixSizeBits = (flags >> 1 & 7) + 1;
1728 const rangeSizeBits = (flags >> 4 & 7) + 1;
1729 const lines = [];
1730 let prefixLength,
1731 rangeLength,
1732 currentRangeLow = lowestValue;
1733
1734 do {
1735 prefixLength = reader.readBits(prefixSizeBits);
1736 rangeLength = reader.readBits(rangeSizeBits);
1737 lines.push(new HuffmanLine([currentRangeLow, prefixLength, rangeLength, 0]));
1738 currentRangeLow += 1 << rangeLength;
1739 } while (currentRangeLow < highestValue);
1740
1741 prefixLength = reader.readBits(prefixSizeBits);
1742 lines.push(new HuffmanLine([lowestValue - 1, prefixLength, 32, 0, "lower"]));
1743 prefixLength = reader.readBits(prefixSizeBits);
1744 lines.push(new HuffmanLine([highestValue, prefixLength, 32, 0]));
1745
1746 if (flags & 1) {
1747 prefixLength = reader.readBits(prefixSizeBits);
1748 lines.push(new HuffmanLine([prefixLength, 0]));
1749 }
1750
1751 return new HuffmanTable(lines, false);
1752}
1753
1754const standardTablesCache = {};
1755
1756function getStandardTable(number) {
1757 let table = standardTablesCache[number];
1758
1759 if (table) {
1760 return table;
1761 }
1762
1763 let lines;
1764
1765 switch (number) {
1766 case 1:
1767 lines = [[0, 1, 4, 0x0], [16, 2, 8, 0x2], [272, 3, 16, 0x6], [65808, 3, 32, 0x7]];
1768 break;
1769
1770 case 2:
1771 lines = [[0, 1, 0, 0x0], [1, 2, 0, 0x2], [2, 3, 0, 0x6], [3, 4, 3, 0xe], [11, 5, 6, 0x1e], [75, 6, 32, 0x3e], [6, 0x3f]];
1772 break;
1773
1774 case 3:
1775 lines = [[-256, 8, 8, 0xfe], [0, 1, 0, 0x0], [1, 2, 0, 0x2], [2, 3, 0, 0x6], [3, 4, 3, 0xe], [11, 5, 6, 0x1e], [-257, 8, 32, 0xff, "lower"], [75, 7, 32, 0x7e], [6, 0x3e]];
1776 break;
1777
1778 case 4:
1779 lines = [[1, 1, 0, 0x0], [2, 2, 0, 0x2], [3, 3, 0, 0x6], [4, 4, 3, 0xe], [12, 5, 6, 0x1e], [76, 5, 32, 0x1f]];
1780 break;
1781
1782 case 5:
1783 lines = [[-255, 7, 8, 0x7e], [1, 1, 0, 0x0], [2, 2, 0, 0x2], [3, 3, 0, 0x6], [4, 4, 3, 0xe], [12, 5, 6, 0x1e], [-256, 7, 32, 0x7f, "lower"], [76, 6, 32, 0x3e]];
1784 break;
1785
1786 case 6:
1787 lines = [[-2048, 5, 10, 0x1c], [-1024, 4, 9, 0x8], [-512, 4, 8, 0x9], [-256, 4, 7, 0xa], [-128, 5, 6, 0x1d], [-64, 5, 5, 0x1e], [-32, 4, 5, 0xb], [0, 2, 7, 0x0], [128, 3, 7, 0x2], [256, 3, 8, 0x3], [512, 4, 9, 0xc], [1024, 4, 10, 0xd], [-2049, 6, 32, 0x3e, "lower"], [2048, 6, 32, 0x3f]];
1788 break;
1789
1790 case 7:
1791 lines = [[-1024, 4, 9, 0x8], [-512, 3, 8, 0x0], [-256, 4, 7, 0x9], [-128, 5, 6, 0x1a], [-64, 5, 5, 0x1b], [-32, 4, 5, 0xa], [0, 4, 5, 0xb], [32, 5, 5, 0x1c], [64, 5, 6, 0x1d], [128, 4, 7, 0xc], [256, 3, 8, 0x1], [512, 3, 9, 0x2], [1024, 3, 10, 0x3], [-1025, 5, 32, 0x1e, "lower"], [2048, 5, 32, 0x1f]];
1792 break;
1793
1794 case 8:
1795 lines = [[-15, 8, 3, 0xfc], [-7, 9, 1, 0x1fc], [-5, 8, 1, 0xfd], [-3, 9, 0, 0x1fd], [-2, 7, 0, 0x7c], [-1, 4, 0, 0xa], [0, 2, 1, 0x0], [2, 5, 0, 0x1a], [3, 6, 0, 0x3a], [4, 3, 4, 0x4], [20, 6, 1, 0x3b], [22, 4, 4, 0xb], [38, 4, 5, 0xc], [70, 5, 6, 0x1b], [134, 5, 7, 0x1c], [262, 6, 7, 0x3c], [390, 7, 8, 0x7d], [646, 6, 10, 0x3d], [-16, 9, 32, 0x1fe, "lower"], [1670, 9, 32, 0x1ff], [2, 0x1]];
1796 break;
1797
1798 case 9:
1799 lines = [[-31, 8, 4, 0xfc], [-15, 9, 2, 0x1fc], [-11, 8, 2, 0xfd], [-7, 9, 1, 0x1fd], [-5, 7, 1, 0x7c], [-3, 4, 1, 0xa], [-1, 3, 1, 0x2], [1, 3, 1, 0x3], [3, 5, 1, 0x1a], [5, 6, 1, 0x3a], [7, 3, 5, 0x4], [39, 6, 2, 0x3b], [43, 4, 5, 0xb], [75, 4, 6, 0xc], [139, 5, 7, 0x1b], [267, 5, 8, 0x1c], [523, 6, 8, 0x3c], [779, 7, 9, 0x7d], [1291, 6, 11, 0x3d], [-32, 9, 32, 0x1fe, "lower"], [3339, 9, 32, 0x1ff], [2, 0x0]];
1800 break;
1801
1802 case 10:
1803 lines = [[-21, 7, 4, 0x7a], [-5, 8, 0, 0xfc], [-4, 7, 0, 0x7b], [-3, 5, 0, 0x18], [-2, 2, 2, 0x0], [2, 5, 0, 0x19], [3, 6, 0, 0x36], [4, 7, 0, 0x7c], [5, 8, 0, 0xfd], [6, 2, 6, 0x1], [70, 5, 5, 0x1a], [102, 6, 5, 0x37], [134, 6, 6, 0x38], [198, 6, 7, 0x39], [326, 6, 8, 0x3a], [582, 6, 9, 0x3b], [1094, 6, 10, 0x3c], [2118, 7, 11, 0x7d], [-22, 8, 32, 0xfe, "lower"], [4166, 8, 32, 0xff], [2, 0x2]];
1804 break;
1805
1806 case 11:
1807 lines = [[1, 1, 0, 0x0], [2, 2, 1, 0x2], [4, 4, 0, 0xc], [5, 4, 1, 0xd], [7, 5, 1, 0x1c], [9, 5, 2, 0x1d], [13, 6, 2, 0x3c], [17, 7, 2, 0x7a], [21, 7, 3, 0x7b], [29, 7, 4, 0x7c], [45, 7, 5, 0x7d], [77, 7, 6, 0x7e], [141, 7, 32, 0x7f]];
1808 break;
1809
1810 case 12:
1811 lines = [[1, 1, 0, 0x0], [2, 2, 0, 0x2], [3, 3, 1, 0x6], [5, 5, 0, 0x1c], [6, 5, 1, 0x1d], [8, 6, 1, 0x3c], [10, 7, 0, 0x7a], [11, 7, 1, 0x7b], [13, 7, 2, 0x7c], [17, 7, 3, 0x7d], [25, 7, 4, 0x7e], [41, 8, 5, 0xfe], [73, 8, 32, 0xff]];
1812 break;
1813
1814 case 13:
1815 lines = [[1, 1, 0, 0x0], [2, 3, 0, 0x4], [3, 4, 0, 0xc], [4, 5, 0, 0x1c], [5, 4, 1, 0xd], [7, 3, 3, 0x5], [15, 6, 1, 0x3a], [17, 6, 2, 0x3b], [21, 6, 3, 0x3c], [29, 6, 4, 0x3d], [45, 6, 5, 0x3e], [77, 7, 6, 0x7e], [141, 7, 32, 0x7f]];
1816 break;
1817
1818 case 14:
1819 lines = [[-2, 3, 0, 0x4], [-1, 3, 0, 0x5], [0, 1, 0, 0x0], [1, 3, 0, 0x6], [2, 3, 0, 0x7]];
1820 break;
1821
1822 case 15:
1823 lines = [[-24, 7, 4, 0x7c], [-8, 6, 2, 0x3c], [-4, 5, 1, 0x1c], [-2, 4, 0, 0xc], [-1, 3, 0, 0x4], [0, 1, 0, 0x0], [1, 3, 0, 0x5], [2, 4, 0, 0xd], [3, 5, 1, 0x1d], [5, 6, 2, 0x3d], [9, 7, 4, 0x7d], [-25, 7, 32, 0x7e, "lower"], [25, 7, 32, 0x7f]];
1824 break;
1825
1826 default:
1827 throw new Jbig2Error(`standard table B.${number} does not exist`);
1828 }
1829
1830 for (let i = 0, ii = lines.length; i < ii; i++) {
1831 lines[i] = new HuffmanLine(lines[i]);
1832 }
1833
1834 table = new HuffmanTable(lines, true);
1835 standardTablesCache[number] = table;
1836 return table;
1837}
1838
1839class Reader {
1840 constructor(data, start, end) {
1841 this.data = data;
1842 this.start = start;
1843 this.end = end;
1844 this.position = start;
1845 this.shift = -1;
1846 this.currentByte = 0;
1847 }
1848
1849 readBit() {
1850 if (this.shift < 0) {
1851 if (this.position >= this.end) {
1852 throw new Jbig2Error("end of data while reading bit");
1853 }
1854
1855 this.currentByte = this.data[this.position++];
1856 this.shift = 7;
1857 }
1858
1859 const bit = this.currentByte >> this.shift & 1;
1860 this.shift--;
1861 return bit;
1862 }
1863
1864 readBits(numBits) {
1865 let result = 0,
1866 i;
1867
1868 for (i = numBits - 1; i >= 0; i--) {
1869 result |= this.readBit() << i;
1870 }
1871
1872 return result;
1873 }
1874
1875 byteAlign() {
1876 this.shift = -1;
1877 }
1878
1879 next() {
1880 if (this.position >= this.end) {
1881 return -1;
1882 }
1883
1884 return this.data[this.position++];
1885 }
1886
1887}
1888
1889function getCustomHuffmanTable(index, referredTo, customTables) {
1890 let currentIndex = 0;
1891
1892 for (let i = 0, ii = referredTo.length; i < ii; i++) {
1893 const table = customTables[referredTo[i]];
1894
1895 if (table) {
1896 if (index === currentIndex) {
1897 return table;
1898 }
1899
1900 currentIndex++;
1901 }
1902 }
1903
1904 throw new Jbig2Error("can't find custom Huffman table");
1905}
1906
1907function getTextRegionHuffmanTables(textRegion, referredTo, customTables, numberOfSymbols, reader) {
1908 const codes = [];
1909
1910 for (let i = 0; i <= 34; i++) {
1911 const codeLength = reader.readBits(4);
1912 codes.push(new HuffmanLine([i, codeLength, 0, 0]));
1913 }
1914
1915 const runCodesTable = new HuffmanTable(codes, false);
1916 codes.length = 0;
1917
1918 for (let i = 0; i < numberOfSymbols;) {
1919 const codeLength = runCodesTable.decode(reader);
1920
1921 if (codeLength >= 32) {
1922 let repeatedLength, numberOfRepeats, j;
1923
1924 switch (codeLength) {
1925 case 32:
1926 if (i === 0) {
1927 throw new Jbig2Error("no previous value in symbol ID table");
1928 }
1929
1930 numberOfRepeats = reader.readBits(2) + 3;
1931 repeatedLength = codes[i - 1].prefixLength;
1932 break;
1933
1934 case 33:
1935 numberOfRepeats = reader.readBits(3) + 3;
1936 repeatedLength = 0;
1937 break;
1938
1939 case 34:
1940 numberOfRepeats = reader.readBits(7) + 11;
1941 repeatedLength = 0;
1942 break;
1943
1944 default:
1945 throw new Jbig2Error("invalid code length in symbol ID table");
1946 }
1947
1948 for (j = 0; j < numberOfRepeats; j++) {
1949 codes.push(new HuffmanLine([i, repeatedLength, 0, 0]));
1950 i++;
1951 }
1952 } else {
1953 codes.push(new HuffmanLine([i, codeLength, 0, 0]));
1954 i++;
1955 }
1956 }
1957
1958 reader.byteAlign();
1959 const symbolIDTable = new HuffmanTable(codes, false);
1960 let customIndex = 0,
1961 tableFirstS,
1962 tableDeltaS,
1963 tableDeltaT;
1964
1965 switch (textRegion.huffmanFS) {
1966 case 0:
1967 case 1:
1968 tableFirstS = getStandardTable(textRegion.huffmanFS + 6);
1969 break;
1970
1971 case 3:
1972 tableFirstS = getCustomHuffmanTable(customIndex, referredTo, customTables);
1973 customIndex++;
1974 break;
1975
1976 default:
1977 throw new Jbig2Error("invalid Huffman FS selector");
1978 }
1979
1980 switch (textRegion.huffmanDS) {
1981 case 0:
1982 case 1:
1983 case 2:
1984 tableDeltaS = getStandardTable(textRegion.huffmanDS + 8);
1985 break;
1986
1987 case 3:
1988 tableDeltaS = getCustomHuffmanTable(customIndex, referredTo, customTables);
1989 customIndex++;
1990 break;
1991
1992 default:
1993 throw new Jbig2Error("invalid Huffman DS selector");
1994 }
1995
1996 switch (textRegion.huffmanDT) {
1997 case 0:
1998 case 1:
1999 case 2:
2000 tableDeltaT = getStandardTable(textRegion.huffmanDT + 11);
2001 break;
2002
2003 case 3:
2004 tableDeltaT = getCustomHuffmanTable(customIndex, referredTo, customTables);
2005 customIndex++;
2006 break;
2007
2008 default:
2009 throw new Jbig2Error("invalid Huffman DT selector");
2010 }
2011
2012 if (textRegion.refinement) {
2013 throw new Jbig2Error("refinement with Huffman is not supported");
2014 }
2015
2016 return {
2017 symbolIDTable,
2018 tableFirstS,
2019 tableDeltaS,
2020 tableDeltaT
2021 };
2022}
2023
2024function getSymbolDictionaryHuffmanTables(dictionary, referredTo, customTables) {
2025 let customIndex = 0,
2026 tableDeltaHeight,
2027 tableDeltaWidth;
2028
2029 switch (dictionary.huffmanDHSelector) {
2030 case 0:
2031 case 1:
2032 tableDeltaHeight = getStandardTable(dictionary.huffmanDHSelector + 4);
2033 break;
2034
2035 case 3:
2036 tableDeltaHeight = getCustomHuffmanTable(customIndex, referredTo, customTables);
2037 customIndex++;
2038 break;
2039
2040 default:
2041 throw new Jbig2Error("invalid Huffman DH selector");
2042 }
2043
2044 switch (dictionary.huffmanDWSelector) {
2045 case 0:
2046 case 1:
2047 tableDeltaWidth = getStandardTable(dictionary.huffmanDWSelector + 2);
2048 break;
2049
2050 case 3:
2051 tableDeltaWidth = getCustomHuffmanTable(customIndex, referredTo, customTables);
2052 customIndex++;
2053 break;
2054
2055 default:
2056 throw new Jbig2Error("invalid Huffman DW selector");
2057 }
2058
2059 let tableBitmapSize, tableAggregateInstances;
2060
2061 if (dictionary.bitmapSizeSelector) {
2062 tableBitmapSize = getCustomHuffmanTable(customIndex, referredTo, customTables);
2063 customIndex++;
2064 } else {
2065 tableBitmapSize = getStandardTable(1);
2066 }
2067
2068 if (dictionary.aggregationInstancesSelector) {
2069 tableAggregateInstances = getCustomHuffmanTable(customIndex, referredTo, customTables);
2070 } else {
2071 tableAggregateInstances = getStandardTable(1);
2072 }
2073
2074 return {
2075 tableDeltaHeight,
2076 tableDeltaWidth,
2077 tableBitmapSize,
2078 tableAggregateInstances
2079 };
2080}
2081
2082function readUncompressedBitmap(reader, width, height) {
2083 const bitmap = [];
2084
2085 for (let y = 0; y < height; y++) {
2086 const row = new Uint8Array(width);
2087 bitmap.push(row);
2088
2089 for (let x = 0; x < width; x++) {
2090 row[x] = reader.readBit();
2091 }
2092
2093 reader.byteAlign();
2094 }
2095
2096 return bitmap;
2097}
2098
2099function decodeMMRBitmap(input, width, height, endOfBlock) {
2100 const params = {
2101 K: -1,
2102 Columns: width,
2103 Rows: height,
2104 BlackIs1: true,
2105 EndOfBlock: endOfBlock
2106 };
2107 const decoder = new _ccitt.CCITTFaxDecoder(input, params);
2108 const bitmap = [];
2109 let currentByte,
2110 eof = false;
2111
2112 for (let y = 0; y < height; y++) {
2113 const row = new Uint8Array(width);
2114 bitmap.push(row);
2115 let shift = -1;
2116
2117 for (let x = 0; x < width; x++) {
2118 if (shift < 0) {
2119 currentByte = decoder.readNextChar();
2120
2121 if (currentByte === -1) {
2122 currentByte = 0;
2123 eof = true;
2124 }
2125
2126 shift = 7;
2127 }
2128
2129 row[x] = currentByte >> shift & 1;
2130 shift--;
2131 }
2132 }
2133
2134 if (endOfBlock && !eof) {
2135 const lookForEOFLimit = 5;
2136
2137 for (let i = 0; i < lookForEOFLimit; i++) {
2138 if (decoder.readNextChar() === -1) {
2139 break;
2140 }
2141 }
2142 }
2143
2144 return bitmap;
2145}
2146
2147class Jbig2Image {
2148 parseChunks(chunks) {
2149 return parseJbig2Chunks(chunks);
2150 }
2151
2152 parse(data) {
2153 throw new Error("Not implemented: Jbig2Image.parse");
2154 }
2155
2156}
2157
2158exports.Jbig2Image = Jbig2Image;
\No newline at end of file