UNPKG

28.5 kBJavaScriptView Raw
1/**
2 * @licstart The following is the entire license notice for the
3 * Javascript code in this page
4 *
5 * Copyright 2017 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 = undefined;
28
29var _util = require('../shared/util');
30
31var CCITTFaxDecoder = function CCITTFaxDecoder() {
32 var ccittEOL = -2;
33 var ccittEOF = -1;
34 var twoDimPass = 0;
35 var twoDimHoriz = 1;
36 var twoDimVert0 = 2;
37 var twoDimVertR1 = 3;
38 var twoDimVertL1 = 4;
39 var twoDimVertR2 = 5;
40 var twoDimVertL2 = 6;
41 var twoDimVertR3 = 7;
42 var twoDimVertL3 = 8;
43 var 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]];
44 var 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]];
45 var 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]];
46 var 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]];
47 var 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]];
48 var 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]];
49 function CCITTFaxDecoder(source) {
50 var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
51
52 if (!source || typeof source.next !== 'function') {
53 throw new Error('CCITTFaxDecoder - invalid "source" parameter.');
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 var eoblock = options['EndOfBlock'];
63 if (eoblock === null || eoblock === undefined) {
64 eoblock = true;
65 }
66 this.eoblock = eoblock;
67 this.black = options['BlackIs1'] || false;
68 this.codingLine = new Uint32Array(this.columns + 1);
69 this.refLine = new Uint32Array(this.columns + 2);
70 this.codingLine[0] = this.columns;
71 this.codingPos = 0;
72 this.row = 0;
73 this.nextLine2D = this.encoding < 0;
74 this.inputBits = 0;
75 this.inputBuf = 0;
76 this.outputBits = 0;
77 this.rowsDone = false;
78 var code1 = void 0;
79 while ((code1 = this._lookBits(12)) === 0) {
80 this._eatBits(1);
81 }
82 if (code1 === 1) {
83 this._eatBits(12);
84 }
85 if (this.encoding > 0) {
86 this.nextLine2D = !this._lookBits(1);
87 this._eatBits(1);
88 }
89 }
90 CCITTFaxDecoder.prototype = {
91 readNextChar: function readNextChar() {
92 if (this.eof) {
93 return -1;
94 }
95 var refLine = this.refLine;
96 var codingLine = this.codingLine;
97 var columns = this.columns;
98 var refPos = void 0,
99 blackPixels = void 0,
100 bits = void 0,
101 i = void 0;
102 if (this.outputBits === 0) {
103 if (this.rowsDone) {
104 this.eof = true;
105 }
106 if (this.eof) {
107 return -1;
108 }
109 this.err = false;
110 var code1 = void 0,
111 code2 = void 0,
112 code3 = void 0;
113 if (this.nextLine2D) {
114 for (i = 0; codingLine[i] < columns; ++i) {
115 refLine[i] = codingLine[i];
116 }
117 refLine[i++] = columns;
118 refLine[i] = columns;
119 codingLine[0] = 0;
120 this.codingPos = 0;
121 refPos = 0;
122 blackPixels = 0;
123 while (codingLine[this.codingPos] < columns) {
124 code1 = this._getTwoDimCode();
125 switch (code1) {
126 case twoDimPass:
127 this._addPixels(refLine[refPos + 1], blackPixels);
128 if (refLine[refPos + 1] < columns) {
129 refPos += 2;
130 }
131 break;
132 case twoDimHoriz:
133 code1 = code2 = 0;
134 if (blackPixels) {
135 do {
136 code1 += code3 = this._getBlackCode();
137 } while (code3 >= 64);
138 do {
139 code2 += code3 = this._getWhiteCode();
140 } while (code3 >= 64);
141 } else {
142 do {
143 code1 += code3 = this._getWhiteCode();
144 } while (code3 >= 64);
145 do {
146 code2 += code3 = this._getBlackCode();
147 } while (code3 >= 64);
148 }
149 this._addPixels(codingLine[this.codingPos] + code1, blackPixels);
150 if (codingLine[this.codingPos] < columns) {
151 this._addPixels(codingLine[this.codingPos] + code2, blackPixels ^ 1);
152 }
153 while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
154 refPos += 2;
155 }
156 break;
157 case twoDimVertR3:
158 this._addPixels(refLine[refPos] + 3, blackPixels);
159 blackPixels ^= 1;
160 if (codingLine[this.codingPos] < columns) {
161 ++refPos;
162 while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
163 refPos += 2;
164 }
165 }
166 break;
167 case twoDimVertR2:
168 this._addPixels(refLine[refPos] + 2, blackPixels);
169 blackPixels ^= 1;
170 if (codingLine[this.codingPos] < columns) {
171 ++refPos;
172 while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
173 refPos += 2;
174 }
175 }
176 break;
177 case twoDimVertR1:
178 this._addPixels(refLine[refPos] + 1, blackPixels);
179 blackPixels ^= 1;
180 if (codingLine[this.codingPos] < columns) {
181 ++refPos;
182 while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
183 refPos += 2;
184 }
185 }
186 break;
187 case twoDimVert0:
188 this._addPixels(refLine[refPos], blackPixels);
189 blackPixels ^= 1;
190 if (codingLine[this.codingPos] < columns) {
191 ++refPos;
192 while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
193 refPos += 2;
194 }
195 }
196 break;
197 case twoDimVertL3:
198 this._addPixelsNeg(refLine[refPos] - 3, blackPixels);
199 blackPixels ^= 1;
200 if (codingLine[this.codingPos] < columns) {
201 if (refPos > 0) {
202 --refPos;
203 } else {
204 ++refPos;
205 }
206 while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
207 refPos += 2;
208 }
209 }
210 break;
211 case twoDimVertL2:
212 this._addPixelsNeg(refLine[refPos] - 2, blackPixels);
213 blackPixels ^= 1;
214 if (codingLine[this.codingPos] < columns) {
215 if (refPos > 0) {
216 --refPos;
217 } else {
218 ++refPos;
219 }
220 while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
221 refPos += 2;
222 }
223 }
224 break;
225 case twoDimVertL1:
226 this._addPixelsNeg(refLine[refPos] - 1, blackPixels);
227 blackPixels ^= 1;
228 if (codingLine[this.codingPos] < columns) {
229 if (refPos > 0) {
230 --refPos;
231 } else {
232 ++refPos;
233 }
234 while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
235 refPos += 2;
236 }
237 }
238 break;
239 case ccittEOF:
240 this._addPixels(columns, 0);
241 this.eof = true;
242 break;
243 default:
244 (0, _util.info)('bad 2d code');
245 this._addPixels(columns, 0);
246 this.err = true;
247 }
248 }
249 } else {
250 codingLine[0] = 0;
251 this.codingPos = 0;
252 blackPixels = 0;
253 while (codingLine[this.codingPos] < columns) {
254 code1 = 0;
255 if (blackPixels) {
256 do {
257 code1 += code3 = this._getBlackCode();
258 } while (code3 >= 64);
259 } else {
260 do {
261 code1 += code3 = this._getWhiteCode();
262 } while (code3 >= 64);
263 }
264 this._addPixels(codingLine[this.codingPos] + code1, blackPixels);
265 blackPixels ^= 1;
266 }
267 }
268 var gotEOL = false;
269 if (this.byteAlign) {
270 this.inputBits &= ~7;
271 }
272 if (!this.eoblock && this.row === this.rows - 1) {
273 this.rowsDone = true;
274 } else {
275 code1 = this._lookBits(12);
276 if (this.eoline) {
277 while (code1 !== ccittEOF && code1 !== 1) {
278 this._eatBits(1);
279 code1 = this._lookBits(12);
280 }
281 } else {
282 while (code1 === 0) {
283 this._eatBits(1);
284 code1 = this._lookBits(12);
285 }
286 }
287 if (code1 === 1) {
288 this._eatBits(12);
289 gotEOL = true;
290 } else if (code1 === ccittEOF) {
291 this.eof = true;
292 }
293 }
294 if (!this.eof && this.encoding > 0 && !this.rowsDone) {
295 this.nextLine2D = !this._lookBits(1);
296 this._eatBits(1);
297 }
298 if (this.eoblock && gotEOL && this.byteAlign) {
299 code1 = this._lookBits(12);
300 if (code1 === 1) {
301 this._eatBits(12);
302 if (this.encoding > 0) {
303 this._lookBits(1);
304 this._eatBits(1);
305 }
306 if (this.encoding >= 0) {
307 for (i = 0; i < 4; ++i) {
308 code1 = this._lookBits(12);
309 if (code1 !== 1) {
310 (0, _util.info)('bad rtc code: ' + code1);
311 }
312 this._eatBits(12);
313 if (this.encoding > 0) {
314 this._lookBits(1);
315 this._eatBits(1);
316 }
317 }
318 }
319 this.eof = true;
320 }
321 } else if (this.err && this.eoline) {
322 while (true) {
323 code1 = this._lookBits(13);
324 if (code1 === ccittEOF) {
325 this.eof = true;
326 return -1;
327 }
328 if (code1 >> 1 === 1) {
329 break;
330 }
331 this._eatBits(1);
332 }
333 this._eatBits(12);
334 if (this.encoding > 0) {
335 this._eatBits(1);
336 this.nextLine2D = !(code1 & 1);
337 }
338 }
339 if (codingLine[0] > 0) {
340 this.outputBits = codingLine[this.codingPos = 0];
341 } else {
342 this.outputBits = codingLine[this.codingPos = 1];
343 }
344 this.row++;
345 }
346 var c = void 0;
347 if (this.outputBits >= 8) {
348 c = this.codingPos & 1 ? 0 : 0xFF;
349 this.outputBits -= 8;
350 if (this.outputBits === 0 && codingLine[this.codingPos] < columns) {
351 this.codingPos++;
352 this.outputBits = codingLine[this.codingPos] - codingLine[this.codingPos - 1];
353 }
354 } else {
355 bits = 8;
356 c = 0;
357 do {
358 if (this.outputBits > bits) {
359 c <<= bits;
360 if (!(this.codingPos & 1)) {
361 c |= 0xFF >> 8 - bits;
362 }
363 this.outputBits -= bits;
364 bits = 0;
365 } else {
366 c <<= this.outputBits;
367 if (!(this.codingPos & 1)) {
368 c |= 0xFF >> 8 - this.outputBits;
369 }
370 bits -= this.outputBits;
371 this.outputBits = 0;
372 if (codingLine[this.codingPos] < columns) {
373 this.codingPos++;
374 this.outputBits = codingLine[this.codingPos] - codingLine[this.codingPos - 1];
375 } else if (bits > 0) {
376 c <<= bits;
377 bits = 0;
378 }
379 }
380 } while (bits);
381 }
382 if (this.black) {
383 c ^= 0xFF;
384 }
385 return c;
386 },
387 _addPixels: function _addPixels(a1, blackPixels) {
388 var codingLine = this.codingLine;
389 var codingPos = this.codingPos;
390 if (a1 > codingLine[codingPos]) {
391 if (a1 > this.columns) {
392 (0, _util.info)('row is wrong length');
393 this.err = true;
394 a1 = this.columns;
395 }
396 if (codingPos & 1 ^ blackPixels) {
397 ++codingPos;
398 }
399 codingLine[codingPos] = a1;
400 }
401 this.codingPos = codingPos;
402 },
403 _addPixelsNeg: function _addPixelsNeg(a1, blackPixels) {
404 var codingLine = this.codingLine;
405 var codingPos = this.codingPos;
406 if (a1 > codingLine[codingPos]) {
407 if (a1 > this.columns) {
408 (0, _util.info)('row is wrong length');
409 this.err = true;
410 a1 = this.columns;
411 }
412 if (codingPos & 1 ^ blackPixels) {
413 ++codingPos;
414 }
415 codingLine[codingPos] = a1;
416 } else if (a1 < codingLine[codingPos]) {
417 if (a1 < 0) {
418 (0, _util.info)('invalid code');
419 this.err = true;
420 a1 = 0;
421 }
422 while (codingPos > 0 && a1 < codingLine[codingPos - 1]) {
423 --codingPos;
424 }
425 codingLine[codingPos] = a1;
426 }
427 this.codingPos = codingPos;
428 },
429 _findTableCode: function _findTableCode(start, end, table, limit) {
430 var limitValue = limit || 0;
431 for (var i = start; i <= end; ++i) {
432 var code = this._lookBits(i);
433 if (code === ccittEOF) {
434 return [true, 1, false];
435 }
436 if (i < end) {
437 code <<= end - i;
438 }
439 if (!limitValue || code >= limitValue) {
440 var p = table[code - limitValue];
441 if (p[0] === i) {
442 this._eatBits(i);
443 return [true, p[1], true];
444 }
445 }
446 }
447 return [false, 0, false];
448 },
449 _getTwoDimCode: function _getTwoDimCode() {
450 var code = 0;
451 var p = void 0;
452 if (this.eoblock) {
453 code = this._lookBits(7);
454 p = twoDimTable[code];
455 if (p && p[0] > 0) {
456 this._eatBits(p[0]);
457 return p[1];
458 }
459 } else {
460 var result = this._findTableCode(1, 7, twoDimTable);
461 if (result[0] && result[2]) {
462 return result[1];
463 }
464 }
465 (0, _util.info)('Bad two dim code');
466 return ccittEOF;
467 },
468 _getWhiteCode: function _getWhiteCode() {
469 var code = 0;
470 var p = void 0;
471 if (this.eoblock) {
472 code = this._lookBits(12);
473 if (code === ccittEOF) {
474 return 1;
475 }
476 if (code >> 5 === 0) {
477 p = whiteTable1[code];
478 } else {
479 p = whiteTable2[code >> 3];
480 }
481 if (p[0] > 0) {
482 this._eatBits(p[0]);
483 return p[1];
484 }
485 } else {
486 var result = this._findTableCode(1, 9, whiteTable2);
487 if (result[0]) {
488 return result[1];
489 }
490 result = this._findTableCode(11, 12, whiteTable1);
491 if (result[0]) {
492 return result[1];
493 }
494 }
495 (0, _util.info)('bad white code');
496 this._eatBits(1);
497 return 1;
498 },
499 _getBlackCode: function _getBlackCode() {
500 var code = void 0,
501 p = void 0;
502 if (this.eoblock) {
503 code = this._lookBits(13);
504 if (code === ccittEOF) {
505 return 1;
506 }
507 if (code >> 7 === 0) {
508 p = blackTable1[code];
509 } else if (code >> 9 === 0 && code >> 7 !== 0) {
510 p = blackTable2[(code >> 1) - 64];
511 } else {
512 p = blackTable3[code >> 7];
513 }
514 if (p[0] > 0) {
515 this._eatBits(p[0]);
516 return p[1];
517 }
518 } else {
519 var result = this._findTableCode(2, 6, blackTable3);
520 if (result[0]) {
521 return result[1];
522 }
523 result = this._findTableCode(7, 12, blackTable2, 64);
524 if (result[0]) {
525 return result[1];
526 }
527 result = this._findTableCode(10, 13, blackTable1);
528 if (result[0]) {
529 return result[1];
530 }
531 }
532 (0, _util.info)('bad black code');
533 this._eatBits(1);
534 return 1;
535 },
536 _lookBits: function _lookBits(n) {
537 var c = void 0;
538 while (this.inputBits < n) {
539 if ((c = this.source.next()) === -1) {
540 if (this.inputBits === 0) {
541 return ccittEOF;
542 }
543 return this.inputBuf << n - this.inputBits & 0xFFFF >> 16 - n;
544 }
545 this.inputBuf = this.inputBuf << 8 | c;
546 this.inputBits += 8;
547 }
548 return this.inputBuf >> this.inputBits - n & 0xFFFF >> 16 - n;
549 },
550 _eatBits: function _eatBits(n) {
551 if ((this.inputBits -= n) < 0) {
552 this.inputBits = 0;
553 }
554 }
555 };
556 return CCITTFaxDecoder;
557}();
558exports.CCITTFaxDecoder = CCITTFaxDecoder;
\No newline at end of file