You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
551 lines
28 KiB
551 lines
28 KiB
7 years ago
|
/* Copyright 2017 Mozilla Foundation
|
||
|
*
|
||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
* you may not use this file except in compliance with the License.
|
||
|
* You may obtain a copy of the License at
|
||
|
*
|
||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||
|
*
|
||
|
* Unless required by applicable law or agreed to in writing, software
|
||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
* See the License for the specific language governing permissions and
|
||
|
* limitations under the License.
|
||
|
*/
|
||
|
'use strict';
|
||
|
|
||
|
Object.defineProperty(exports, "__esModule", {
|
||
|
value: true
|
||
|
});
|
||
|
exports.CCITTFaxDecoder = undefined;
|
||
|
|
||
|
var _util = require('../shared/util');
|
||
|
|
||
|
var CCITTFaxDecoder = function CCITTFaxDecoder() {
|
||
|
var ccittEOL = -2;
|
||
|
var ccittEOF = -1;
|
||
|
var twoDimPass = 0;
|
||
|
var twoDimHoriz = 1;
|
||
|
var twoDimVert0 = 2;
|
||
|
var twoDimVertR1 = 3;
|
||
|
var twoDimVertL1 = 4;
|
||
|
var twoDimVertR2 = 5;
|
||
|
var twoDimVertL2 = 6;
|
||
|
var twoDimVertR3 = 7;
|
||
|
var twoDimVertL3 = 8;
|
||
|
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]];
|
||
|
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]];
|
||
|
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
|
||
|
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]];
|
||
|
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]];
|
||
|
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]];
|
||
|
function CCITTFaxDecoder(source) {
|
||
|
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
||
|
|
||
|
if (!source || typeof source.next !== 'function') {
|
||
|
throw new Error('CCITTFaxDecoder - invalid "source" parameter.');
|
||
|
}
|
||
|
this.source = source;
|
||
|
this.eof = false;
|
||
|
this.encoding = options['K'] || 0;
|
||
|
this.eoline = options['EndOfLine'] || false;
|
||
|
this.byteAlign = options['EncodedByteAlign'] || false;
|
||
|
this.columns = options['Columns'] || 1728;
|
||
|
this.rows = options['Rows'] || 0;
|
||
|
var eoblock = options['EndOfBlock'];
|
||
|
if (eoblock === null || eoblock === undefined) {
|
||
|
eoblock = true;
|
||
|
}
|
||
|
this.eoblock = eoblock;
|
||
|
this.black = options['BlackIs1'] || false;
|
||
|
this.codingLine = new Uint32Array(this.columns + 1);
|
||
|
this.refLine = new Uint32Array(this.columns + 2);
|
||
|
this.codingLine[0] = this.columns;
|
||
|
this.codingPos = 0;
|
||
|
this.row = 0;
|
||
|
this.nextLine2D = this.encoding < 0;
|
||
|
this.inputBits = 0;
|
||
|
this.inputBuf = 0;
|
||
|
this.outputBits = 0;
|
||
|
this.rowsDone = false;
|
||
|
var code1 = void 0;
|
||
|
while ((code1 = this._lookBits(12)) === 0) {
|
||
|
this._eatBits(1);
|
||
|
}
|
||
|
if (code1 === 1) {
|
||
|
this._eatBits(12);
|
||
|
}
|
||
|
if (this.encoding > 0) {
|
||
|
this.nextLine2D = !this._lookBits(1);
|
||
|
this._eatBits(1);
|
||
|
}
|
||
|
}
|
||
|
CCITTFaxDecoder.prototype = {
|
||
|
readNextChar: function readNextChar() {
|
||
|
if (this.eof) {
|
||
|
return -1;
|
||
|
}
|
||
|
var refLine = this.refLine;
|
||
|
var codingLine = this.codingLine;
|
||
|
var columns = this.columns;
|
||
|
var refPos = void 0,
|
||
|
blackPixels = void 0,
|
||
|
bits = void 0,
|
||
|
i = void 0;
|
||
|
if (this.outputBits === 0) {
|
||
|
if (this.rowsDone) {
|
||
|
this.eof = true;
|
||
|
}
|
||
|
if (this.eof) {
|
||
|
return -1;
|
||
|
}
|
||
|
this.err = false;
|
||
|
var code1 = void 0,
|
||
|
code2 = void 0,
|
||
|
code3 = void 0;
|
||
|
if (this.nextLine2D) {
|
||
|
for (i = 0; codingLine[i] < columns; ++i) {
|
||
|
refLine[i] = codingLine[i];
|
||
|
}
|
||
|
refLine[i++] = columns;
|
||
|
refLine[i] = columns;
|
||
|
codingLine[0] = 0;
|
||
|
this.codingPos = 0;
|
||
|
refPos = 0;
|
||
|
blackPixels = 0;
|
||
|
while (codingLine[this.codingPos] < columns) {
|
||
|
code1 = this._getTwoDimCode();
|
||
|
switch (code1) {
|
||
|
case twoDimPass:
|
||
|
this._addPixels(refLine[refPos + 1], blackPixels);
|
||
|
if (refLine[refPos + 1] < columns) {
|
||
|
refPos += 2;
|
||
|
}
|
||
|
break;
|
||
|
case twoDimHoriz:
|
||
|
code1 = code2 = 0;
|
||
|
if (blackPixels) {
|
||
|
do {
|
||
|
code1 += code3 = this._getBlackCode();
|
||
|
} while (code3 >= 64);
|
||
|
do {
|
||
|
code2 += code3 = this._getWhiteCode();
|
||
|
} while (code3 >= 64);
|
||
|
} else {
|
||
|
do {
|
||
|
code1 += code3 = this._getWhiteCode();
|
||
|
} while (code3 >= 64);
|
||
|
do {
|
||
|
code2 += code3 = this._getBlackCode();
|
||
|
} while (code3 >= 64);
|
||
|
}
|
||
|
this._addPixels(codingLine[this.codingPos] + code1, blackPixels);
|
||
|
if (codingLine[this.codingPos] < columns) {
|
||
|
this._addPixels(codingLine[this.codingPos] + code2, blackPixels ^ 1);
|
||
|
}
|
||
|
while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
|
||
|
refPos += 2;
|
||
|
}
|
||
|
break;
|
||
|
case twoDimVertR3:
|
||
|
this._addPixels(refLine[refPos] + 3, blackPixels);
|
||
|
blackPixels ^= 1;
|
||
|
if (codingLine[this.codingPos] < columns) {
|
||
|
++refPos;
|
||
|
while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
|
||
|
refPos += 2;
|
||
|
}
|
||
|
}
|
||
|
break;
|
||
|
case twoDimVertR2:
|
||
|
this._addPixels(refLine[refPos] + 2, blackPixels);
|
||
|
blackPixels ^= 1;
|
||
|
if (codingLine[this.codingPos] < columns) {
|
||
|
++refPos;
|
||
|
while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
|
||
|
refPos += 2;
|
||
|
}
|
||
|
}
|
||
|
break;
|
||
|
case twoDimVertR1:
|
||
|
this._addPixels(refLine[refPos] + 1, blackPixels);
|
||
|
blackPixels ^= 1;
|
||
|
if (codingLine[this.codingPos] < columns) {
|
||
|
++refPos;
|
||
|
while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
|
||
|
refPos += 2;
|
||
|
}
|
||
|
}
|
||
|
break;
|
||
|
case twoDimVert0:
|
||
|
this._addPixels(refLine[refPos], blackPixels);
|
||
|
blackPixels ^= 1;
|
||
|
if (codingLine[this.codingPos] < columns) {
|
||
|
++refPos;
|
||
|
while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
|
||
|
refPos += 2;
|
||
|
}
|
||
|
}
|
||
|
break;
|
||
|
case twoDimVertL3:
|
||
|
this._addPixelsNeg(refLine[refPos] - 3, blackPixels);
|
||
|
blackPixels ^= 1;
|
||
|
if (codingLine[this.codingPos] < columns) {
|
||
|
if (refPos > 0) {
|
||
|
--refPos;
|
||
|
} else {
|
||
|
++refPos;
|
||
|
}
|
||
|
while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
|
||
|
refPos += 2;
|
||
|
}
|
||
|
}
|
||
|
break;
|
||
|
case twoDimVertL2:
|
||
|
this._addPixelsNeg(refLine[refPos] - 2, blackPixels);
|
||
|
blackPixels ^= 1;
|
||
|
if (codingLine[this.codingPos] < columns) {
|
||
|
if (refPos > 0) {
|
||
|
--refPos;
|
||
|
} else {
|
||
|
++refPos;
|
||
|
}
|
||
|
while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
|
||
|
refPos += 2;
|
||
|
}
|
||
|
}
|
||
|
break;
|
||
|
case twoDimVertL1:
|
||
|
this._addPixelsNeg(refLine[refPos] - 1, blackPixels);
|
||
|
blackPixels ^= 1;
|
||
|
if (codingLine[this.codingPos] < columns) {
|
||
|
if (refPos > 0) {
|
||
|
--refPos;
|
||
|
} else {
|
||
|
++refPos;
|
||
|
}
|
||
|
while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) {
|
||
|
refPos += 2;
|
||
|
}
|
||
|
}
|
||
|
break;
|
||
|
case ccittEOF:
|
||
|
this._addPixels(columns, 0);
|
||
|
this.eof = true;
|
||
|
break;
|
||
|
default:
|
||
|
(0, _util.info)('bad 2d code');
|
||
|
this._addPixels(columns, 0);
|
||
|
this.err = true;
|
||
|
}
|
||
|
}
|
||
|
} else {
|
||
|
codingLine[0] = 0;
|
||
|
this.codingPos = 0;
|
||
|
blackPixels = 0;
|
||
|
while (codingLine[this.codingPos] < columns) {
|
||
|
code1 = 0;
|
||
|
if (blackPixels) {
|
||
|
do {
|
||
|
code1 += code3 = this._getBlackCode();
|
||
|
} while (code3 >= 64);
|
||
|
} else {
|
||
|
do {
|
||
|
code1 += code3 = this._getWhiteCode();
|
||
|
} while (code3 >= 64);
|
||
|
}
|
||
|
this._addPixels(codingLine[this.codingPos] + code1, blackPixels);
|
||
|
blackPixels ^= 1;
|
||
|
}
|
||
|
}
|
||
|
var gotEOL = false;
|
||
|
if (this.byteAlign) {
|
||
|
this.inputBits &= ~7;
|
||
|
}
|
||
|
if (!this.eoblock && this.row === this.rows - 1) {
|
||
|
this.rowsDone = true;
|
||
|
} else {
|
||
|
code1 = this._lookBits(12);
|
||
|
if (this.eoline) {
|
||
|
while (code1 !== ccittEOF && code1 !== 1) {
|
||
|
this._eatBits(1);
|
||
|
code1 = this._lookBits(12);
|
||
|
}
|
||
|
} else {
|
||
|
while (code1 === 0) {
|
||
|
this._eatBits(1);
|
||
|
code1 = this._lookBits(12);
|
||
|
}
|
||
|
}
|
||
|
if (code1 === 1) {
|
||
|
this._eatBits(12);
|
||
|
gotEOL = true;
|
||
|
} else if (code1 === ccittEOF) {
|
||
|
this.eof = true;
|
||
|
}
|
||
|
}
|
||
|
if (!this.eof && this.encoding > 0 && !this.rowsDone) {
|
||
|
this.nextLine2D = !this._lookBits(1);
|
||
|
this._eatBits(1);
|
||
|
}
|
||
|
if (this.eoblock && gotEOL && this.byteAlign) {
|
||
|
code1 = this._lookBits(12);
|
||
|
if (code1 === 1) {
|
||
|
this._eatBits(12);
|
||
|
if (this.encoding > 0) {
|
||
|
this._lookBits(1);
|
||
|
this._eatBits(1);
|
||
|
}
|
||
|
if (this.encoding >= 0) {
|
||
|
for (i = 0; i < 4; ++i) {
|
||
|
code1 = this._lookBits(12);
|
||
|
if (code1 !== 1) {
|
||
|
(0, _util.info)('bad rtc code: ' + code1);
|
||
|
}
|
||
|
this._eatBits(12);
|
||
|
if (this.encoding > 0) {
|
||
|
this._lookBits(1);
|
||
|
this._eatBits(1);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
this.eof = true;
|
||
|
}
|
||
|
} else if (this.err && this.eoline) {
|
||
|
while (true) {
|
||
|
code1 = this._lookBits(13);
|
||
|
if (code1 === ccittEOF) {
|
||
|
this.eof = true;
|
||
|
return -1;
|
||
|
}
|
||
|
if (code1 >> 1 === 1) {
|
||
|
break;
|
||
|
}
|
||
|
this._eatBits(1);
|
||
|
}
|
||
|
this._eatBits(12);
|
||
|
if (this.encoding > 0) {
|
||
|
this._eatBits(1);
|
||
|
this.nextLine2D = !(code1 & 1);
|
||
|
}
|
||
|
}
|
||
|
if (codingLine[0] > 0) {
|
||
|
this.outputBits = codingLine[this.codingPos = 0];
|
||
|
} else {
|
||
|
this.outputBits = codingLine[this.codingPos = 1];
|
||
|
}
|
||
|
this.row++;
|
||
|
}
|
||
|
var c = void 0;
|
||
|
if (this.outputBits >= 8) {
|
||
|
c = this.codingPos & 1 ? 0 : 0xFF;
|
||
|
this.outputBits -= 8;
|
||
|
if (this.outputBits === 0 && codingLine[this.codingPos] < columns) {
|
||
|
this.codingPos++;
|
||
|
this.outputBits = codingLine[this.codingPos] - codingLine[this.codingPos - 1];
|
||
|
}
|
||
|
} else {
|
||
|
bits = 8;
|
||
|
c = 0;
|
||
|
do {
|
||
|
if (this.outputBits > bits) {
|
||
|
c <<= bits;
|
||
|
if (!(this.codingPos & 1)) {
|
||
|
c |= 0xFF >> 8 - bits;
|
||
|
}
|
||
|
this.outputBits -= bits;
|
||
|
bits = 0;
|
||
|
} else {
|
||
|
c <<= this.outputBits;
|
||
|
if (!(this.codingPos & 1)) {
|
||
|
c |= 0xFF >> 8 - this.outputBits;
|
||
|
}
|
||
|
bits -= this.outputBits;
|
||
|
this.outputBits = 0;
|
||
|
if (codingLine[this.codingPos] < columns) {
|
||
|
this.codingPos++;
|
||
|
this.outputBits = codingLine[this.codingPos] - codingLine[this.codingPos - 1];
|
||
|
} else if (bits > 0) {
|
||
|
c <<= bits;
|
||
|
bits = 0;
|
||
|
}
|
||
|
}
|
||
|
} while (bits);
|
||
|
}
|
||
|
if (this.black) {
|
||
|
c ^= 0xFF;
|
||
|
}
|
||
|
return c;
|
||
|
},
|
||
|
_addPixels: function _addPixels(a1, blackPixels) {
|
||
|
var codingLine = this.codingLine;
|
||
|
var codingPos = this.codingPos;
|
||
|
if (a1 > codingLine[codingPos]) {
|
||
|
if (a1 > this.columns) {
|
||
|
(0, _util.info)('row is wrong length');
|
||
|
this.err = true;
|
||
|
a1 = this.columns;
|
||
|
}
|
||
|
if (codingPos & 1 ^ blackPixels) {
|
||
|
++codingPos;
|
||
|
}
|
||
|
codingLine[codingPos] = a1;
|
||
|
}
|
||
|
this.codingPos = codingPos;
|
||
|
},
|
||
|
_addPixelsNeg: function _addPixelsNeg(a1, blackPixels) {
|
||
|
var codingLine = this.codingLine;
|
||
|
var codingPos = this.codingPos;
|
||
|
if (a1 > codingLine[codingPos]) {
|
||
|
if (a1 > this.columns) {
|
||
|
(0, _util.info)('row is wrong length');
|
||
|
this.err = true;
|
||
|
a1 = this.columns;
|
||
|
}
|
||
|
if (codingPos & 1 ^ blackPixels) {
|
||
|
++codingPos;
|
||
|
}
|
||
|
codingLine[codingPos] = a1;
|
||
|
} else if (a1 < codingLine[codingPos]) {
|
||
|
if (a1 < 0) {
|
||
|
(0, _util.info)('invalid code');
|
||
|
this.err = true;
|
||
|
a1 = 0;
|
||
|
}
|
||
|
while (codingPos > 0 && a1 < codingLine[codingPos - 1]) {
|
||
|
--codingPos;
|
||
|
}
|
||
|
codingLine[codingPos] = a1;
|
||
|
}
|
||
|
this.codingPos = codingPos;
|
||
|
},
|
||
|
_findTableCode: function _findTableCode(start, end, table, limit) {
|
||
|
var limitValue = limit || 0;
|
||
|
for (var i = start; i <= end; ++i) {
|
||
|
var code = this._lookBits(i);
|
||
|
if (code === ccittEOF) {
|
||
|
return [true, 1, false];
|
||
|
}
|
||
|
if (i < end) {
|
||
|
code <<= end - i;
|
||
|
}
|
||
|
if (!limitValue || code >= limitValue) {
|
||
|
var p = table[code - limitValue];
|
||
|
if (p[0] === i) {
|
||
|
this._eatBits(i);
|
||
|
return [true, p[1], true];
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return [false, 0, false];
|
||
|
},
|
||
|
_getTwoDimCode: function _getTwoDimCode() {
|
||
|
var code = 0;
|
||
|
var p = void 0;
|
||
|
if (this.eoblock) {
|
||
|
code = this._lookBits(7);
|
||
|
p = twoDimTable[code];
|
||
|
if (p && p[0] > 0) {
|
||
|
this._eatBits(p[0]);
|
||
|
return p[1];
|
||
|
}
|
||
|
} else {
|
||
|
var result = this._findTableCode(1, 7, twoDimTable);
|
||
|
if (result[0] && result[2]) {
|
||
|
return result[1];
|
||
|
}
|
||
|
}
|
||
|
(0, _util.info)('Bad two dim code');
|
||
|
return ccittEOF;
|
||
|
},
|
||
|
_getWhiteCode: function _getWhiteCode() {
|
||
|
var code = 0;
|
||
|
var p = void 0;
|
||
|
if (this.eoblock) {
|
||
|
code = this._lookBits(12);
|
||
|
if (code === ccittEOF) {
|
||
|
return 1;
|
||
|
}
|
||
|
if (code >> 5 === 0) {
|
||
|
p = whiteTable1[code];
|
||
|
} else {
|
||
|
p = whiteTable2[code >> 3];
|
||
|
}
|
||
|
if (p[0] > 0) {
|
||
|
this._eatBits(p[0]);
|
||
|
return p[1];
|
||
|
}
|
||
|
} else {
|
||
|
var result = this._findTableCode(1, 9, whiteTable2);
|
||
|
if (result[0]) {
|
||
|
return result[1];
|
||
|
}
|
||
|
result = this._findTableCode(11, 12, whiteTable1);
|
||
|
if (result[0]) {
|
||
|
return result[1];
|
||
|
}
|
||
|
}
|
||
|
(0, _util.info)('bad white code');
|
||
|
this._eatBits(1);
|
||
|
return 1;
|
||
|
},
|
||
|
_getBlackCode: function _getBlackCode() {
|
||
|
var code = void 0,
|
||
|
p = void 0;
|
||
|
if (this.eoblock) {
|
||
|
code = this._lookBits(13);
|
||
|
if (code === ccittEOF) {
|
||
|
return 1;
|
||
|
}
|
||
|
if (code >> 7 === 0) {
|
||
|
p = blackTable1[code];
|
||
|
} else if (code >> 9 === 0 && code >> 7 !== 0) {
|
||
|
p = blackTable2[(code >> 1) - 64];
|
||
|
} else {
|
||
|
p = blackTable3[code >> 7];
|
||
|
}
|
||
|
if (p[0] > 0) {
|
||
|
this._eatBits(p[0]);
|
||
|
return p[1];
|
||
|
}
|
||
|
} else {
|
||
|
var result = this._findTableCode(2, 6, blackTable3);
|
||
|
if (result[0]) {
|
||
|
return result[1];
|
||
|
}
|
||
|
result = this._findTableCode(7, 12, blackTable2, 64);
|
||
|
if (result[0]) {
|
||
|
return result[1];
|
||
|
}
|
||
|
result = this._findTableCode(10, 13, blackTable1);
|
||
|
if (result[0]) {
|
||
|
return result[1];
|
||
|
}
|
||
|
}
|
||
|
(0, _util.info)('bad black code');
|
||
|
this._eatBits(1);
|
||
|
return 1;
|
||
|
},
|
||
|
_lookBits: function _lookBits(n) {
|
||
|
var c = void 0;
|
||
|
while (this.inputBits < n) {
|
||
|
if ((c = this.source.next()) === -1) {
|
||
|
if (this.inputBits === 0) {
|
||
|
return ccittEOF;
|
||
|
}
|
||
|
return this.inputBuf << n - this.inputBits & 0xFFFF >> 16 - n;
|
||
|
}
|
||
|
this.inputBuf = this.inputBuf << 8 | c;
|
||
|
this.inputBits += 8;
|
||
|
}
|
||
|
return this.inputBuf >> this.inputBits - n & 0xFFFF >> 16 - n;
|
||
|
},
|
||
|
_eatBits: function _eatBits(n) {
|
||
|
if ((this.inputBits -= n) < 0) {
|
||
|
this.inputBits = 0;
|
||
|
}
|
||
|
}
|
||
|
};
|
||
|
return CCITTFaxDecoder;
|
||
|
}();
|
||
|
exports.CCITTFaxDecoder = CCITTFaxDecoder;
|