UNPKG

27.3 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.CCITTFaxDecoder = void 0;
28
29var _util = require("../shared/util.js");
30
31const ccittEOL = -2;
32const ccittEOF = -1;
33const twoDimPass = 0;
34const twoDimHoriz = 1;
35const twoDimVert0 = 2;
36const twoDimVertR1 = 3;
37const twoDimVertL1 = 4;
38const twoDimVertR2 = 5;
39const twoDimVertL2 = 6;
40const twoDimVertR3 = 7;
41const twoDimVertL3 = 8;
42const twoDimTable = [[-1, -1], [-1, -1], [7, twoDimVertL3], [7, twoDimVertR3], [6, twoDimVertL2], [6, twoDimVertL2], [6, twoDimVertR2], [6, twoDimVertR2], [4, twoDimPass], [4, twoDimPass], [4, twoDimPass], [4, twoDimPass], [4, twoDimPass], [4, twoDimPass], [4, twoDimPass], [4, twoDimPass], [3, twoDimHoriz], [3, twoDimHoriz], [3, twoDimHoriz], [3, twoDimHoriz], [3, twoDimHoriz], [3, twoDimHoriz], [3, twoDimHoriz], [3, twoDimHoriz], [3, twoDimHoriz], [3, twoDimHoriz], [3, twoDimHoriz], [3, twoDimHoriz], [3, twoDimHoriz], [3, twoDimHoriz], [3, twoDimHoriz], [3, twoDimHoriz], [3, twoDimVertL1], [3, twoDimVertL1], [3, twoDimVertL1], [3, twoDimVertL1], [3, twoDimVertL1], [3, twoDimVertL1], [3, twoDimVertL1], [3, twoDimVertL1], [3, twoDimVertL1], [3, twoDimVertL1], [3, twoDimVertL1], [3, twoDimVertL1], [3, twoDimVertL1], [3, twoDimVertL1], [3, twoDimVertL1], [3, twoDimVertL1], [3, twoDimVertR1], [3, twoDimVertR1], [3, twoDimVertR1], [3, twoDimVertR1], [3, twoDimVertR1], [3, twoDimVertR1], [3, twoDimVertR1], [3, twoDimVertR1], [3, twoDimVertR1], [3, twoDimVertR1], [3, twoDimVertR1], [3, twoDimVertR1], [3, twoDimVertR1], [3, twoDimVertR1], [3, twoDimVertR1], [3, twoDimVertR1], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0], [1, twoDimVert0]];
43const whiteTable1 = [[-1, -1], [12, ccittEOL], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [11, 1792], [11, 1792], [12, 1984], [12, 2048], [12, 2112], [12, 2176], [12, 2240], [12, 2304], [11, 1856], [11, 1856], [11, 1920], [11, 1920], [12, 2368], [12, 2432], [12, 2496], [12, 2560]];
44const whiteTable2 = [[-1, -1], [-1, -1], [-1, -1], [-1, -1], [8, 29], [8, 29], [8, 30], [8, 30], [8, 45], [8, 45], [8, 46], [8, 46], [7, 22], [7, 22], [7, 22], [7, 22], [7, 23], [7, 23], [7, 23], [7, 23], [8, 47], [8, 47], [8, 48], [8, 48], [6, 13], [6, 13], [6, 13], [6, 13], [6, 13], [6, 13], [6, 13], [6, 13], [7, 20], [7, 20], [7, 20], [7, 20], [8, 33], [8, 33], [8, 34], [8, 34], [8, 35], [8, 35], [8, 36], [8, 36], [8, 37], [8, 37], [8, 38], [8, 38], [7, 19], [7, 19], [7, 19], [7, 19], [8, 31], [8, 31], [8, 32], [8, 32], [6, 1], [6, 1], [6, 1], [6, 1], [6, 1], [6, 1], [6, 1], [6, 1], [6, 12], [6, 12], [6, 12], [6, 12], [6, 12], [6, 12], [6, 12], [6, 12], [8, 53], [8, 53], [8, 54], [8, 54], [7, 26], [7, 26], [7, 26], [7, 26], [8, 39], [8, 39], [8, 40], [8, 40], [8, 41], [8, 41], [8, 42], [8, 42], [8, 43], [8, 43], [8, 44], [8, 44], [7, 21], [7, 21], [7, 21], [7, 21], [7, 28], [7, 28], [7, 28], [7, 28], [8, 61], [8, 61], [8, 62], [8, 62], [8, 63], [8, 63], [8, 0], [8, 0], [8, 320], [8, 320], [8, 384], [8, 384], [5, 10], [5, 10], [5, 10], [5, 10], [5, 10], [5, 10], [5, 10], [5, 10], [5, 10], [5, 10], [5, 10], [5, 10], [5, 10], [5, 10], [5, 10], [5, 10], [5, 11], [5, 11], [5, 11], [5, 11], [5, 11], [5, 11], [5, 11], [5, 11], [5, 11], [5, 11], [5, 11], [5, 11], [5, 11], [5, 11], [5, 11], [5, 11], [7, 27], [7, 27], [7, 27], [7, 27], [8, 59], [8, 59], [8, 60], [8, 60], [9, 1472], [9, 1536], [9, 1600], [9, 1728], [7, 18], [7, 18], [7, 18], [7, 18], [7, 24], [7, 24], [7, 24], [7, 24], [8, 49], [8, 49], [8, 50], [8, 50], [8, 51], [8, 51], [8, 52], [8, 52], [7, 25], [7, 25], [7, 25], [7, 25], [8, 55], [8, 55], [8, 56], [8, 56], [8, 57], [8, 57], [8, 58], [8, 58], [6, 192], [6, 192], [6, 192], [6, 192], [6, 192], [6, 192], [6, 192], [6, 192], [6, 1664], [6, 1664], [6, 1664], [6, 1664], [6, 1664], [6, 1664], [6, 1664], [6, 1664], [8, 448], [8, 448], [8, 512], [8, 512], [9, 704], [9, 768], [8, 640], [8, 640], [8, 576], [8, 576], [9, 832], [9, 896], [9, 960], [9, 1024], [9, 1088], [9, 1152], [9, 1216], [9, 1280], [9, 1344], [9, 1408], [7, 256], [7, 256], [7, 256], [7, 256], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 2], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [4, 3], [5, 128], [5, 128], [5, 128], [5, 128], [5, 128], [5, 128], [5, 128], [5, 128], [5, 128], [5, 128], [5, 128], [5, 128], [5, 128], [5, 128], [5, 128], [5, 128], [5, 8], [5, 8], [5, 8], [5, 8], [5, 8], [5, 8], [5, 8], [5, 8], [5, 8], [5, 8], [5, 8], [5, 8], [5, 8], [5, 8], [5, 8], [5, 8], [5, 9], [5, 9], [5, 9], [5, 9], [5, 9], [5, 9], [5, 9], [5, 9], [5, 9], [5, 9], [5, 9], [5, 9], [5, 9], [5, 9], [5, 9], [5, 9], [6, 16], [6, 16], [6, 16], [6, 16], [6, 16], [6, 16], [6, 16], [6, 16], [6, 17], [6, 17], [6, 17], [6, 17], [6, 17], [6, 17], [6, 17], [6, 17], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 4], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [4, 5], [6, 14], [6, 14], [6, 14], [6, 14], [6, 14], [6, 14], [6, 14], [6, 14], [6, 15], [6, 15], [6, 15], [6, 15], [6, 15], [6, 15], [6, 15], [6, 15], [5, 64], [5, 64], [5, 64], [5, 64], [5, 64], [5, 64], [5, 64], [5, 64], [5, 64], [5, 64], [5, 64], [5, 64], [5, 64], [5, 64], [5, 64], [5, 64], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 6], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7], [4, 7]];
45const blackTable1 = [[-1, -1], [-1, -1], [12, ccittEOL], [12, ccittEOL], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [11, 1792], [11, 1792], [11, 1792], [11, 1792], [12, 1984], [12, 1984], [12, 2048], [12, 2048], [12, 2112], [12, 2112], [12, 2176], [12, 2176], [12, 2240], [12, 2240], [12, 2304], [12, 2304], [11, 1856], [11, 1856], [11, 1856], [11, 1856], [11, 1920], [11, 1920], [11, 1920], [11, 1920], [12, 2368], [12, 2368], [12, 2432], [12, 2432], [12, 2496], [12, 2496], [12, 2560], [12, 2560], [10, 18], [10, 18], [10, 18], [10, 18], [10, 18], [10, 18], [10, 18], [10, 18], [12, 52], [12, 52], [13, 640], [13, 704], [13, 768], [13, 832], [12, 55], [12, 55], [12, 56], [12, 56], [13, 1280], [13, 1344], [13, 1408], [13, 1472], [12, 59], [12, 59], [12, 60], [12, 60], [13, 1536], [13, 1600], [11, 24], [11, 24], [11, 24], [11, 24], [11, 25], [11, 25], [11, 25], [11, 25], [13, 1664], [13, 1728], [12, 320], [12, 320], [12, 384], [12, 384], [12, 448], [12, 448], [13, 512], [13, 576], [12, 53], [12, 53], [12, 54], [12, 54], [13, 896], [13, 960], [13, 1024], [13, 1088], [13, 1152], [13, 1216], [10, 64], [10, 64], [10, 64], [10, 64], [10, 64], [10, 64], [10, 64], [10, 64]];
46const blackTable2 = [[8, 13], [8, 13], [8, 13], [8, 13], [8, 13], [8, 13], [8, 13], [8, 13], [8, 13], [8, 13], [8, 13], [8, 13], [8, 13], [8, 13], [8, 13], [8, 13], [11, 23], [11, 23], [12, 50], [12, 51], [12, 44], [12, 45], [12, 46], [12, 47], [12, 57], [12, 58], [12, 61], [12, 256], [10, 16], [10, 16], [10, 16], [10, 16], [10, 17], [10, 17], [10, 17], [10, 17], [12, 48], [12, 49], [12, 62], [12, 63], [12, 30], [12, 31], [12, 32], [12, 33], [12, 40], [12, 41], [11, 22], [11, 22], [8, 14], [8, 14], [8, 14], [8, 14], [8, 14], [8, 14], [8, 14], [8, 14], [8, 14], [8, 14], [8, 14], [8, 14], [8, 14], [8, 14], [8, 14], [8, 14], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 10], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [7, 11], [9, 15], [9, 15], [9, 15], [9, 15], [9, 15], [9, 15], [9, 15], [9, 15], [12, 128], [12, 192], [12, 26], [12, 27], [12, 28], [12, 29], [11, 19], [11, 19], [11, 20], [11, 20], [12, 34], [12, 35], [12, 36], [12, 37], [12, 38], [12, 39], [11, 21], [11, 21], [12, 42], [12, 43], [10, 0], [10, 0], [10, 0], [10, 0], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12], [7, 12]];
47const blackTable3 = [[-1, -1], [-1, -1], [-1, -1], [-1, -1], [6, 9], [6, 8], [5, 7], [5, 7], [4, 6], [4, 6], [4, 6], [4, 6], [4, 5], [4, 5], [4, 5], [4, 5], [3, 1], [3, 1], [3, 1], [3, 1], [3, 1], [3, 1], [3, 1], [3, 1], [3, 4], [3, 4], [3, 4], [3, 4], [3, 4], [3, 4], [3, 4], [3, 4], [2, 3], [2, 3], [2, 3], [2, 3], [2, 3], [2, 3], [2, 3], [2, 3], [2, 3], [2, 3], [2, 3], [2, 3], [2, 3], [2, 3], [2, 3], [2, 3], [2, 2], [2, 2], [2, 2], [2, 2], [2, 2], [2, 2], [2, 2], [2, 2], [2, 2], [2, 2], [2, 2], [2, 2], [2, 2], [2, 2], [2, 2], [2, 2]];
48
49class CCITTFaxDecoder {
50 constructor(source, options = {}) {
51 if (!source || typeof source.next !== "function") {
52 throw new Error('CCITTFaxDecoder - invalid "source" parameter.');
53 }
54
55 this.source = source;
56 this.eof = false;
57 this.encoding = options.K || 0;
58 this.eoline = options.EndOfLine || false;
59 this.byteAlign = options.EncodedByteAlign || false;
60 this.columns = options.Columns || 1728;
61 this.rows = options.Rows || 0;
62 let eoblock = options.EndOfBlock;
63
64 if (eoblock === null || eoblock === undefined) {
65 eoblock = true;
66 }
67
68 this.eoblock = eoblock;
69 this.black = options.BlackIs1 || false;
70 this.codingLine = new Uint32Array(this.columns + 1);
71 this.refLine = new Uint32Array(this.columns + 2);
72 this.codingLine[0] = this.columns;
73 this.codingPos = 0;
74 this.row = 0;
75 this.nextLine2D = this.encoding < 0;
76 this.inputBits = 0;
77 this.inputBuf = 0;
78 this.outputBits = 0;
79 this.rowsDone = false;
80 let code1;
81
82 while ((code1 = this._lookBits(12)) === 0) {
83 this._eatBits(1);
84 }
85
86 if (code1 === 1) {
87 this._eatBits(12);
88 }
89
90 if (this.encoding > 0) {
91 this.nextLine2D = !this._lookBits(1);
92
93 this._eatBits(1);
94 }
95 }
96
97 readNextChar() {
98 if (this.eof) {
99 return -1;
100 }
101
102 const refLine = this.refLine;
103 const codingLine = this.codingLine;
104 const columns = this.columns;
105 let refPos, blackPixels, bits, i;
106
107 if (this.outputBits === 0) {
108 if (this.rowsDone) {
109 this.eof = true;
110 }
111
112 if (this.eof) {
113 return -1;
114 }
115
116 this.err = false;
117 let code1, code2, code3;
118
119 if (this.nextLine2D) {
120 for (i = 0; codingLine[i] < columns; ++i) {
121 refLine[i] = codingLine[i];
122 }
123
124 refLine[i++] = columns;
125 refLine[i] = columns;
126 codingLine[0] = 0;
127 this.codingPos = 0;
128 refPos = 0;
129 blackPixels = 0;
130
131 while (codingLine[this.codingPos] < columns) {
132 code1 = this._getTwoDimCode();
133
134 switch (code1) {
135 case twoDimPass:
136 this._addPixels(refLine[refPos + 1], blackPixels);
137
138 if (refLine[refPos + 1] < columns) {
139 refPos += 2;
140 }
141
142 break;
143
144 case twoDimHoriz:
145 code1 = code2 = 0;
146
147 if (blackPixels) {
148 do {
149 code1 += code3 = this._getBlackCode();
150 } while (code3 >= 64);
151
152 do {
153 code2 += code3 = this._getWhiteCode();
154 } while (code3 >= 64);
155 } else {
156 do {
157 code1 += code3 = this._getWhiteCode();
158 } while (code3 >= 64);
159
160 do {
161 code2 += code3 = this._getBlackCode();
162 } while (code3 >= 64);
163 }
164
165 this._addPixels(codingLine[this.codingPos] + code1, blackPixels);
166
167 if (codingLine[this.codingPos] < columns) {
168 this._addPixels(codingLine[this.codingPos] + code2, blackPixels ^ 1);
169 }
170
171 while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
172 refPos += 2;
173 }
174
175 break;
176
177 case twoDimVertR3:
178 this._addPixels(refLine[refPos] + 3, blackPixels);
179
180 blackPixels ^= 1;
181
182 if (codingLine[this.codingPos] < columns) {
183 ++refPos;
184
185 while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
186 refPos += 2;
187 }
188 }
189
190 break;
191
192 case twoDimVertR2:
193 this._addPixels(refLine[refPos] + 2, blackPixels);
194
195 blackPixels ^= 1;
196
197 if (codingLine[this.codingPos] < columns) {
198 ++refPos;
199
200 while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
201 refPos += 2;
202 }
203 }
204
205 break;
206
207 case twoDimVertR1:
208 this._addPixels(refLine[refPos] + 1, blackPixels);
209
210 blackPixels ^= 1;
211
212 if (codingLine[this.codingPos] < columns) {
213 ++refPos;
214
215 while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
216 refPos += 2;
217 }
218 }
219
220 break;
221
222 case twoDimVert0:
223 this._addPixels(refLine[refPos], blackPixels);
224
225 blackPixels ^= 1;
226
227 if (codingLine[this.codingPos] < columns) {
228 ++refPos;
229
230 while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
231 refPos += 2;
232 }
233 }
234
235 break;
236
237 case twoDimVertL3:
238 this._addPixelsNeg(refLine[refPos] - 3, blackPixels);
239
240 blackPixels ^= 1;
241
242 if (codingLine[this.codingPos] < columns) {
243 if (refPos > 0) {
244 --refPos;
245 } else {
246 ++refPos;
247 }
248
249 while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
250 refPos += 2;
251 }
252 }
253
254 break;
255
256 case twoDimVertL2:
257 this._addPixelsNeg(refLine[refPos] - 2, blackPixels);
258
259 blackPixels ^= 1;
260
261 if (codingLine[this.codingPos] < columns) {
262 if (refPos > 0) {
263 --refPos;
264 } else {
265 ++refPos;
266 }
267
268 while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
269 refPos += 2;
270 }
271 }
272
273 break;
274
275 case twoDimVertL1:
276 this._addPixelsNeg(refLine[refPos] - 1, blackPixels);
277
278 blackPixels ^= 1;
279
280 if (codingLine[this.codingPos] < columns) {
281 if (refPos > 0) {
282 --refPos;
283 } else {
284 ++refPos;
285 }
286
287 while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
288 refPos += 2;
289 }
290 }
291
292 break;
293
294 case ccittEOF:
295 this._addPixels(columns, 0);
296
297 this.eof = true;
298 break;
299
300 default:
301 (0, _util.info)("bad 2d code");
302
303 this._addPixels(columns, 0);
304
305 this.err = true;
306 }
307 }
308 } else {
309 codingLine[0] = 0;
310 this.codingPos = 0;
311 blackPixels = 0;
312
313 while (codingLine[this.codingPos] < columns) {
314 code1 = 0;
315
316 if (blackPixels) {
317 do {
318 code1 += code3 = this._getBlackCode();
319 } while (code3 >= 64);
320 } else {
321 do {
322 code1 += code3 = this._getWhiteCode();
323 } while (code3 >= 64);
324 }
325
326 this._addPixels(codingLine[this.codingPos] + code1, blackPixels);
327
328 blackPixels ^= 1;
329 }
330 }
331
332 let gotEOL = false;
333
334 if (this.byteAlign) {
335 this.inputBits &= ~7;
336 }
337
338 if (!this.eoblock && this.row === this.rows - 1) {
339 this.rowsDone = true;
340 } else {
341 code1 = this._lookBits(12);
342
343 if (this.eoline) {
344 while (code1 !== ccittEOF && code1 !== 1) {
345 this._eatBits(1);
346
347 code1 = this._lookBits(12);
348 }
349 } else {
350 while (code1 === 0) {
351 this._eatBits(1);
352
353 code1 = this._lookBits(12);
354 }
355 }
356
357 if (code1 === 1) {
358 this._eatBits(12);
359
360 gotEOL = true;
361 } else if (code1 === ccittEOF) {
362 this.eof = true;
363 }
364 }
365
366 if (!this.eof && this.encoding > 0 && !this.rowsDone) {
367 this.nextLine2D = !this._lookBits(1);
368
369 this._eatBits(1);
370 }
371
372 if (this.eoblock && gotEOL && this.byteAlign) {
373 code1 = this._lookBits(12);
374
375 if (code1 === 1) {
376 this._eatBits(12);
377
378 if (this.encoding > 0) {
379 this._lookBits(1);
380
381 this._eatBits(1);
382 }
383
384 if (this.encoding >= 0) {
385 for (i = 0; i < 4; ++i) {
386 code1 = this._lookBits(12);
387
388 if (code1 !== 1) {
389 (0, _util.info)("bad rtc code: " + code1);
390 }
391
392 this._eatBits(12);
393
394 if (this.encoding > 0) {
395 this._lookBits(1);
396
397 this._eatBits(1);
398 }
399 }
400 }
401
402 this.eof = true;
403 }
404 } else if (this.err && this.eoline) {
405 while (true) {
406 code1 = this._lookBits(13);
407
408 if (code1 === ccittEOF) {
409 this.eof = true;
410 return -1;
411 }
412
413 if (code1 >> 1 === 1) {
414 break;
415 }
416
417 this._eatBits(1);
418 }
419
420 this._eatBits(12);
421
422 if (this.encoding > 0) {
423 this._eatBits(1);
424
425 this.nextLine2D = !(code1 & 1);
426 }
427 }
428
429 if (codingLine[0] > 0) {
430 this.outputBits = codingLine[this.codingPos = 0];
431 } else {
432 this.outputBits = codingLine[this.codingPos = 1];
433 }
434
435 this.row++;
436 }
437
438 let c;
439
440 if (this.outputBits >= 8) {
441 c = this.codingPos & 1 ? 0 : 0xff;
442 this.outputBits -= 8;
443
444 if (this.outputBits === 0 && codingLine[this.codingPos] < columns) {
445 this.codingPos++;
446 this.outputBits = codingLine[this.codingPos] - codingLine[this.codingPos - 1];
447 }
448 } else {
449 bits = 8;
450 c = 0;
451
452 do {
453 if (typeof this.outputBits !== "number") {
454 throw new _util.FormatError('Invalid /CCITTFaxDecode data, "outputBits" must be a number.');
455 }
456
457 if (this.outputBits > bits) {
458 c <<= bits;
459
460 if (!(this.codingPos & 1)) {
461 c |= 0xff >> 8 - bits;
462 }
463
464 this.outputBits -= bits;
465 bits = 0;
466 } else {
467 c <<= this.outputBits;
468
469 if (!(this.codingPos & 1)) {
470 c |= 0xff >> 8 - this.outputBits;
471 }
472
473 bits -= this.outputBits;
474 this.outputBits = 0;
475
476 if (codingLine[this.codingPos] < columns) {
477 this.codingPos++;
478 this.outputBits = codingLine[this.codingPos] - codingLine[this.codingPos - 1];
479 } else if (bits > 0) {
480 c <<= bits;
481 bits = 0;
482 }
483 }
484 } while (bits);
485 }
486
487 if (this.black) {
488 c ^= 0xff;
489 }
490
491 return c;
492 }
493
494 _addPixels(a1, blackPixels) {
495 const codingLine = this.codingLine;
496 let codingPos = this.codingPos;
497
498 if (a1 > codingLine[codingPos]) {
499 if (a1 > this.columns) {
500 (0, _util.info)("row is wrong length");
501 this.err = true;
502 a1 = this.columns;
503 }
504
505 if (codingPos & 1 ^ blackPixels) {
506 ++codingPos;
507 }
508
509 codingLine[codingPos] = a1;
510 }
511
512 this.codingPos = codingPos;
513 }
514
515 _addPixelsNeg(a1, blackPixels) {
516 const codingLine = this.codingLine;
517 let codingPos = this.codingPos;
518
519 if (a1 > codingLine[codingPos]) {
520 if (a1 > this.columns) {
521 (0, _util.info)("row is wrong length");
522 this.err = true;
523 a1 = this.columns;
524 }
525
526 if (codingPos & 1 ^ blackPixels) {
527 ++codingPos;
528 }
529
530 codingLine[codingPos] = a1;
531 } else if (a1 < codingLine[codingPos]) {
532 if (a1 < 0) {
533 (0, _util.info)("invalid code");
534 this.err = true;
535 a1 = 0;
536 }
537
538 while (codingPos > 0 && a1 < codingLine[codingPos - 1]) {
539 --codingPos;
540 }
541
542 codingLine[codingPos] = a1;
543 }
544
545 this.codingPos = codingPos;
546 }
547
548 _findTableCode(start, end, table, limit) {
549 const limitValue = limit || 0;
550
551 for (let i = start; i <= end; ++i) {
552 let code = this._lookBits(i);
553
554 if (code === ccittEOF) {
555 return [true, 1, false];
556 }
557
558 if (i < end) {
559 code <<= end - i;
560 }
561
562 if (!limitValue || code >= limitValue) {
563 const p = table[code - limitValue];
564
565 if (p[0] === i) {
566 this._eatBits(i);
567
568 return [true, p[1], true];
569 }
570 }
571 }
572
573 return [false, 0, false];
574 }
575
576 _getTwoDimCode() {
577 let code = 0;
578 let p;
579
580 if (this.eoblock) {
581 code = this._lookBits(7);
582 p = twoDimTable[code];
583
584 if (p && p[0] > 0) {
585 this._eatBits(p[0]);
586
587 return p[1];
588 }
589 } else {
590 const result = this._findTableCode(1, 7, twoDimTable);
591
592 if (result[0] && result[2]) {
593 return result[1];
594 }
595 }
596
597 (0, _util.info)("Bad two dim code");
598 return ccittEOF;
599 }
600
601 _getWhiteCode() {
602 let code = 0;
603 let p;
604
605 if (this.eoblock) {
606 code = this._lookBits(12);
607
608 if (code === ccittEOF) {
609 return 1;
610 }
611
612 if (code >> 5 === 0) {
613 p = whiteTable1[code];
614 } else {
615 p = whiteTable2[code >> 3];
616 }
617
618 if (p[0] > 0) {
619 this._eatBits(p[0]);
620
621 return p[1];
622 }
623 } else {
624 let result = this._findTableCode(1, 9, whiteTable2);
625
626 if (result[0]) {
627 return result[1];
628 }
629
630 result = this._findTableCode(11, 12, whiteTable1);
631
632 if (result[0]) {
633 return result[1];
634 }
635 }
636
637 (0, _util.info)("bad white code");
638
639 this._eatBits(1);
640
641 return 1;
642 }
643
644 _getBlackCode() {
645 let code, p;
646
647 if (this.eoblock) {
648 code = this._lookBits(13);
649
650 if (code === ccittEOF) {
651 return 1;
652 }
653
654 if (code >> 7 === 0) {
655 p = blackTable1[code];
656 } else if (code >> 9 === 0 && code >> 7 !== 0) {
657 p = blackTable2[(code >> 1) - 64];
658 } else {
659 p = blackTable3[code >> 7];
660 }
661
662 if (p[0] > 0) {
663 this._eatBits(p[0]);
664
665 return p[1];
666 }
667 } else {
668 let result = this._findTableCode(2, 6, blackTable3);
669
670 if (result[0]) {
671 return result[1];
672 }
673
674 result = this._findTableCode(7, 12, blackTable2, 64);
675
676 if (result[0]) {
677 return result[1];
678 }
679
680 result = this._findTableCode(10, 13, blackTable1);
681
682 if (result[0]) {
683 return result[1];
684 }
685 }
686
687 (0, _util.info)("bad black code");
688
689 this._eatBits(1);
690
691 return 1;
692 }
693
694 _lookBits(n) {
695 let c;
696
697 while (this.inputBits < n) {
698 if ((c = this.source.next()) === -1) {
699 if (this.inputBits === 0) {
700 return ccittEOF;
701 }
702
703 return this.inputBuf << n - this.inputBits & 0xffff >> 16 - n;
704 }
705
706 this.inputBuf = this.inputBuf << 8 | c;
707 this.inputBits += 8;
708 }
709
710 return this.inputBuf >> this.inputBits - n & 0xffff >> 16 - n;
711 }
712
713 _eatBits(n) {
714 if ((this.inputBits -= n) < 0) {
715 this.inputBits = 0;
716 }
717 }
718
719}
720
721exports.CCITTFaxDecoder = CCITTFaxDecoder;
\No newline at end of file