From 1673d2497cdceeab223fd1706a8679fb2bde481b Mon Sep 17 00:00:00 2001 From: sbarman Date: Wed, 22 Jun 2011 14:26:59 -0700 Subject: [PATCH 01/13] not working version of ccittfaxstream --- pdf.js | 318 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 318 insertions(+) diff --git a/pdf.js b/pdf.js index ec8f74a3b..df9a7ee19 100644 --- a/pdf.js +++ b/pdf.js @@ -839,6 +839,324 @@ var Ascii85Stream = (function() { return constructor; })(); +var CcittFaxStream = (function() { + function constructor(str) { + this.str = str; + var dict = str.dict; + this.dict = dict; + + this.encoding = dict.get("K") || 0; + this.eol = dict.get("EndOfLine") || false; + this.byteAlign = dict.get("EncodedByteAlign") || false; + this.columns = dict.get("Columns") || 1728; + this.rows = dict.get("Rows") || 0; + var eob = dict.get("EndOfBlock"); + if (typeof eob == "undefined") + eob = true; + this.black = dict.get("BlackIs1") || false; + + this.codingLine = new Uint8Array(this.columns + 1); + this.codingLine[0] = this.columns; + this.refLine = new Uint8Array(this.columns + 2); + this.row = 0; + this.nextLine2D = this.encoding < 0; + this.inputBits = 0; + this.a0i = 0; + this.outputBits = 0; + + var code1; + 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); + } + + DecodeStream.call(this); + } + + constructor.prototype = Object.create(DecodeStream.prototype); + constructor.prototype.readBlock = function() { + }; + constructor.prototype.addPixels(a1, blackPixels) { + if (a1 > this.codingLine[this.a01]) { + if (a1 > this.columns) + error("row is wrong length"); + if (this.a0i & 1) ^ this.blackPixels) + this.a0i++; + + this.codingLine[this.a0i] = a1; + }; + constructor.prototype.addPixelsNeg(a1, blackPixels) { + if (a1 > this.codingLine[this.a01]) { + if (a1 > this.columns) + error("row is wrong length"); + if (this.a0i & 1) ^ this.blackPixels) + this.a0i++; + + this.codingLine[this.a0i] = a1; + } else if (at < this.codingLine[this.a0i]) { + if (a1 < 0) + error("invalid code"); + while (this.a0i > 0 && a1 < this.codingLine[this.a0i -i]) + this.a0i--; + this.codingLine[this.a0i] = a1; + } + }; + constructor.prototype.lookChar(a1, blackPixels) { + var refLine = this.refLine; + var codingLine = this.codingLine; + var columns = this.columns; + + if (this.outputBits == 0) { + if (this.nextLine2D) { + for (var i = 0; codingLine[i] < columns; ++i) + refLine[i] = codeLine[i]; + refLine[i++] = columns; + refLine[i] = columns; + codingLine[0] = 0; + this.a0i = 0; + this.bl1 = 0; + this.blackPixels = 0; + + while (codingLine[this.a0i] < columns) { + var code1 = this.getTwoDumCode(); + switch (code1) { + case twoDimPass: + this.addPixels(refLine[this.bli + 1], this.blackPixels); + if (refLine[this.bli + 1] < columns) + this.bli += 2; + break; + case twoDimHoriz: + var code1 = 0, code2 = 0; + if (this.blackPixels) { + var code3; + do { + code1 += (code3 = this.getBlackCode()); + } while (code3 >= 64); + do { + code2 += (code3 = this.getWhiteCode()); + } while (code3 >= 64); + else { + var code3; + do { + code1 += (code3 = getWhiteCode()); + } while (code3 >= 64); + this.addPixels(codeLine[this.a0i + code1, this.blackPixels); + if (codeLine[a0i] < columns) { + addPixels(codeLine[a0i] + code2, this.blackPixels ^ 1); + } + while (refLine[this.bli] <= codingLine[a0i] + && refLine[this.bli] < columns) { + this.bli += 2; + } + } + break; + case twoDimVertR2: + this.addPixels(this.refLine[this.bli], blackPixels); + blackPixels ^= 1; + if (codeLine[this.a01] < columns) { + this.bli++; + while (refLine[this.bli] <= codeLine[this.a0i] && + refLine[bli] < columns) { + this.bli += 2; + } + } + break; + case twoDimVertR1: + this.addPixels(refLine[this.bli] + 1, blackPixels); + this.blackPixels ^= 1; + if (codeLine[this.a01] < columns) { + this.bli++; + while (refLine[this.bli] < codingLine[this.a0i] && + refLine[this.bli] < columns) + this.bli += 2; + } + break; + case twoDimVert0: + this.addPixels(refLine[this.bli], blackPixels); + this.blackPixels ^= 1; + if (codingLine[this.a0i] < columns) { + this.bli++; + while (refLine[this.bli] <= codingLine[a0i] && + refLine[this.bli] < columns) + this.bli += 2; + } + break; + case twoDimVertL3: + this.addPixelsNeg(refLine[this.bli] - 3, blackPixels); + this.blackPixels ^= 1; + if (codeLine[a0i] < columns) { + if (bli > 0) + --bli; + else + ++bli; + while (refLine[bli] <= codingLine[a0i] && + refLine[bli] < columns) + bli += 2; + } + break; + case twoDimVertL2: + this.addPixelsNeg(refLine[bli] - 2, blackPixels); + blackPixels ^= 1; + if (codingLine[a0i] < columns) { + if (bli > 0) + --bli; + else + ++bli; + while (refLine[bli] <= codingLine[a0i] && + refLine[bl1] < columns) + bli += 2; + } + break; + case twoDimVertL1: + this.addPixelsNeg(refLine[bli] - 1, blackPixels); + this.blackPixels ^= 1; + if (codingLine[a0i] < columns) { + if (blu > 0) + --bli; + else + ++bli; + + while (refLine[bli] <= codeLine[a0i] && + refLine[bli] < columns) + bli += 2; + } + break; + case EOF: + addPixels(columns, 0); + this.eof = true; + break; + default: + error("bad 2d code"); + } + } + } else { + codingLine[0] = 0; + a0i = 0; + blackPixels = 0; + while(codeLine[a0i] < columns) { + code1 = 0; + if (blackPixels) { + do { + code1 += (code3 = getBlackCode()); + } while (code3 >= 64); + } else { + do { + code1 += (code3 = getWhiteCode()); + } while (code3 >= 64); + } + this.addPixels(codingLine[a0i] + code1, blackPixels); + blackPixels ^= 1; + } + } + + if (this.byteAlign) + inputBits &= ~7; + + getEol = false; + if (eob && row == rows - 1) { + eof = true; + } else { + code1 = lookBits(12); + while (code1 == 0) { + eatBits(1); + code1 = lookBits(12); + } + if (code1 = 1) { + eatBits(12); + gotEol = true; + } else if (code1 = EOF) { + eof = true; + } + } + + if (!oef && encoding > 0) { + nextLine2D = !lookBits(1); + eatBits(1); + } + + if (eob && gotEol) { + code1 = lookBits(12); + if (code1 == 1) { + eatBits(12); + if (encoding > 0) { + lookBits(1); + eatBits(1); + } + if (encoding >= 0) { + for (i = 0; i < 4; ++i) { + code1 = lookBits(12); + if (code1 != 1) + error("bad rtc code"); + eatBits(12); + if (encoding > 0) { + lookBits(1); + eatBits(1); + } + } + } + eof = true; + } + } + + if (codingLine[0] > 0) + outputBits = codingLine[a0i = 0]; + else + outputBits = codingLine[a0i = 1]; + ++row; + } + + if (outputBits >= 8) { + buf = (a0i & 1) ? 0 : 0xFF; + outputBits -= 8; + if (outputBits == 0&& codingLine[a0i] < columns) { + ++a0i; + outputBits = codingLine[a0i] - codingLine[a0i - 1]; + } + } else { + bits = 8; + buf = 0; + do { + if (outputBits > bits) { + buf << bits; + if (!(a0i & 1)) { + buf |= 0xFF >> (8 - bits); + } + outputBits -= bits; + bits = 0; + } else { + buf <<= outputBits; + if (!(a0i & 1)) { + buf |= 0xFF >> (8 - outputBits); + } + bits -= outputBits; + outputBits = 0; + if (codingLine[a0i] < columns) { + ++a0i; + outputBits = codingLine[a0i] - codingLine[a0i - 1]; + } else if (bits > 0) { + buf <<= bits; + bits = 0; + } + } + } while (bits); + } + if (black) { + buf ^= 0xFF; + } + return buf; + } + constructor.prototype.addPixels(a1, blackPixels) { + } + + return constructor; +})(); + var Name = (function() { function constructor(name) { this.name = name; From 8c3ebb8d9f2a0bec19d0ea7127cf16f6da8fbb99 Mon Sep 17 00:00:00 2001 From: sbarman Date: Wed, 22 Jun 2011 17:54:57 -0700 Subject: [PATCH 02/13] Added in the tables for faxstream --- pdf.js | 585 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 580 insertions(+), 5 deletions(-) diff --git a/pdf.js b/pdf.js index df9a7ee19..8cb1d07d1 100644 --- a/pdf.js +++ b/pdf.js @@ -840,19 +840,446 @@ var Ascii85Stream = (function() { })(); var CcittFaxStream = (function() { + + const ccittEOL = -2; + const twoDimPass = 0; + const twoDimHoriz = 1; + const twoDimVert0 = 2; + const twoDimVertR1 = 3; + const twoDimVertL1 = 4; + const twoDimVertR2 = 5; + const twoDimVertL2 = 6; + const twoDimVertR3 = 7; + const twoDimVertL3 = 8; + + const twoDimTable1 = [ + [-1, -1], [-1, -1], // 000000x + [7, twoDimVertL3], // 0000010 + [7, twoDimVertR3], // 0000011 + [6, twoDimVertL2], [6, twoDimVertL2], // 000010x + [6, twoDimVertR2], [6, twoDimVertR2], // 000011x + [4, twoDimPass], [4, twoDimPass], // 0001xxx + [4, twoDimPass], [4, twoDimPass], + [4, twoDimPass], [4, twoDimPass], + [4, twoDimPass], [4, twoDimPass], + [3, twoDimHoriz], [3, twoDimHoriz], // 001xxxx + [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], // 010xxxx + [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], // 011xxxx + [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], // 1xxxxxx + [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] + ]; + + const whiteTable1 = [ + [-1, -1], // 00000 + [12, ccittEOL], // 00001 + [-1, -1], [-1, -1], // 0001x + [-1, -1], [-1, -1], [-1, -1], [-1, -1], // 001xx + [-1, -1], [-1, -1], [-1, -1], [-1, -1], // 010xx + [-1, -1], [-1, -1], [-1, -1], [-1, -1], // 011xx + [11, 1792], [11, 1792], // 1000x + [12, 1984], // 10010 + [12, 2048], // 10011 + [12, 2112], // 10100 + [12, 2176], // 10101 + [12, 2240], // 10110 + [12, 2304], // 10111 + [11, 1856], [11, 1856], // 1100x + [11, 1920], [11, 1920], // 1101x + [12, 2368], // 11100 + [12, 2432], // 11101 + [12, 2496], // 11110 + [12, 2560] // 11111 + ]; + + const whiteTable2 = [ + [-1, -1], [-1, -1], [-1, -1], [-1, -1], // 0000000xx + [8, 29], [8, 29], // 00000010x + [8, 30], [8, 30], // 00000011x + [8, 45], [8, 45], // 00000100x + [8, 46], [8, 46], // 00000101x + [7, 22], [7, 22], [7, 22], [7, 22], // 0000011xx + [7, 23], [7, 23], [7, 23], [7, 23], // 0000100xx + [8, 47], [8, 47], // 00001010x + [8, 48], [8, 48], // 00001011x + [6, 13], [6, 13], [6, 13], [6, 13], // 000011xxx + [6, 13], [6, 13], [6, 13], [6, 13], + [7, 20], [7, 20], [7, 20], [7, 20], // 0001000xx + [8, 33], [8, 33], // 00010010x + [8, 34], [8, 34], // 00010011x + [8, 35], [8, 35], // 00010100x + [8, 36], [8, 36], // 00010101x + [8, 37], [8, 37], // 00010110x + [8, 38], [8, 38], // 00010111x + [7, 19], [7, 19], [7, 19], [7, 19], // 0001100xx + [8, 31], [8, 31], // 00011010x + [8, 32], [8, 32], // 00011011x + [6, 1], [6, 1], [6, 1], [6, 1], // 000111xxx + [6, 1], [6, 1], [6, 1], [6, 1], + [6, 12], [6, 12], [6, 12], [6, 12], // 001000xxx + [6, 12], [6, 12], [6, 12], [6, 12], + [8, 53], [8, 53], // 00100100x + [8, 54], [8, 54], // 00100101x + [7, 26], [7, 26], [7, 26], [7, 26], // 0010011xx + [8, 39], [8, 39], // 00101000x + [8, 40], [8, 40], // 00101001x + [8, 41], [8, 41], // 00101010x + [8, 42], [8, 42], // 00101011x + [8, 43], [8, 43], // 00101100x + [8, 44], [8, 44], // 00101101x + [7, 21], [7, 21], [7, 21], [7, 21], // 0010111xx + [7, 28], [7, 28], [7, 28], [7, 28], // 0011000xx + [8, 61], [8, 61], // 00110010x + [8, 62], [8, 62], // 00110011x + [8, 63], [8, 63], // 00110100x + [8, 0], [8, 0], // 00110101x + [8, 320], [8, 320], // 00110110x + [8, 384], [8, 384], // 00110111x + [5, 10], [5, 10], [5, 10], [5, 10], // 00111xxxx + [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], // 01000xxxx + [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], // 0100100xx + [8, 59], [8, 59], // 01001010x + [8, 60], [8, 60], // 01001011x + [9, 1472], // 010011000 + [9, 1536], // 010011001 + [9, 1600], // 010011010 + [9, 1728], // 010011011 + [7, 18], [7, 18], [7, 18], [7, 18], // 0100111xx + [7, 24], [7, 24], [7, 24], [7, 24], // 0101000xx + [8, 49], [8, 49], // 01010010x + [8, 50], [8, 50], // 01010011x + [8, 51], [8, 51], // 01010100x + [8, 52], [8, 52], // 01010101x + [7, 25], [7, 25], [7, 25], [7, 25], // 0101011xx + [8, 55], [8, 55], // 01011000x + [8, 56], [8, 56], // 01011001x + [8, 57], [8, 57], // 01011010x + [8, 58], [8, 58], // 01011011x + [6, 192], [6, 192], [6, 192], [6, 192], // 010111xxx + [6, 192], [6, 192], [6, 192], [6, 192], + [6, 1664], [6, 1664], [6, 1664], [6, 1664], // 011000xxx + [6, 1664], [6, 1664], [6, 1664], [6, 1664], + [8, 448], [8, 448], // 01100100x + [8, 512], [8, 512], // 01100101x + [9, 704], // 011001100 + [9, 768], // 011001101 + [8, 640], [8, 640], // 01100111x + [8, 576], [8, 576], // 01101000x + [9, 832], // 011010010 + [9, 896], // 011010011 + [9, 960], // 011010100 + [9, 1024], // 011010101 + [9, 1088], // 011010110 + [9, 1152], // 011010111 + [9, 1216], // 011011000 + [9, 1280], // 011011001 + [9, 1344], // 011011010 + [9, 1408], // 011011011 + [7, 256], [7, 256], [7, 256], [7, 256], // 0110111xx + [4, 2], [4, 2], [4, 2], [4, 2], // 0111xxxxx + [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], // 1000xxxxx + [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], // 10010xxxx + [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], // 10011xxxx + [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], // 10100xxxx + [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], // 101010xxx + [6, 16], [6, 16], [6, 16], [6, 16], + [6, 17], [6, 17], [6, 17], [6, 17], // 101011xxx + [6, 17], [6, 17], [6, 17], [6, 17], + [4, 4], [4, 4], [4, 4], [4, 4], // 1011xxxxx + [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], // 1100xxxxx + [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], // 110100xxx + [6, 14], [6, 14], [6, 14], [6, 14], + [6, 15], [6, 15], [6, 15], [6, 15], // 110101xxx + [6, 15], [6, 15], [6, 15], [6, 15], + [5, 64], [5, 64], [5, 64], [5, 64], // 11011xxxx + [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], // 1110xxxxx + [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], // 1111xxxxx + [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] + ]; + + const blackTable1 = [ + [-1, -1], [-1, -1], // 000000000000x + [12, ccittEOL], [12, ccittEOL], // 000000000001x + [-1, -1], [-1, -1], [-1, -1], [-1, -1], // 00000000001xx + [-1, -1], [-1, -1], [-1, -1], [-1, -1], // 00000000010xx + [-1, -1], [-1, -1], [-1, -1], [-1, -1], // 00000000011xx + [-1, -1], [-1, -1], [-1, -1], [-1, -1], // 00000000100xx + [-1, -1], [-1, -1], [-1, -1], [-1, -1], // 00000000101xx + [-1, -1], [-1, -1], [-1, -1], [-1, -1], // 00000000110xx + [-1, -1], [-1, -1], [-1, -1], [-1, -1], // 00000000111xx + [11, 1792], [11, 1792], [11, 1792], [11, 1792], // 00000001000xx + [12, 1984], [12, 1984], // 000000010010x + [12, 2048], [12, 2048], // 000000010011x + [12, 2112], [12, 2112], // 000000010100x + [12, 2176], [12, 2176], // 000000010101x + [12, 2240], [12, 2240], // 000000010110x + [12, 2304], [12, 2304], // 000000010111x + [11, 1856], [11, 1856], [11, 1856], [11, 1856], // 00000001100xx + [11, 1920], [11, 1920], [11, 1920], [11, 1920], // 00000001101xx + [12, 2368], [12, 2368], // 000000011100x + [12, 2432], [12, 2432], // 000000011101x + [12, 2496], [12, 2496], // 000000011110x + [12, 2560], [12, 2560], // 000000011111x + [10, 18], [10, 18], [10, 18], [10, 18], // 0000001000xxx + [10, 18], [10, 18], [10, 18], [10, 18], + [12, 52], [12, 52], // 000000100100x + [13, 640], // 0000001001010 + [13, 704], // 0000001001011 + [13, 768], // 0000001001100 + [13, 832], // 0000001001101 + [12, 55], [12, 55], // 000000100111x + [12, 56], [12, 56], // 000000101000x + [13, 1280], // 0000001010010 + [13, 1344], // 0000001010011 + [13, 1408], // 0000001010100 + [13, 1472], // 0000001010101 + [12, 59], [12, 59], // 000000101011x + [12, 60], [12, 60], // 000000101100x + [13, 1536], // 0000001011010 + [13, 1600], // 0000001011011 + [11, 24], [11, 24], [11, 24], [11, 24], // 00000010111xx + [11, 25], [11, 25], [11, 25], [11, 25], // 00000011000xx + [13, 1664], // 0000001100100 + [13, 1728], // 0000001100101 + [12, 320], [12, 320], // 000000110011x + [12, 384], [12, 384], // 000000110100x + [12, 448], [12, 448], // 000000110101x + [13, 512], // 0000001101100 + [13, 576], // 0000001101101 + [12, 53], [12, 53], // 000000110111x + [12, 54], [12, 54], // 000000111000x + [13, 896], // 0000001110010 + [13, 960], // 0000001110011 + [13, 1024], // 0000001110100 + [13, 1088], // 0000001110101 + [13, 1152], // 0000001110110 + [13, 1216], // 0000001110111 + [10, 64], [10, 64], [10, 64], [10, 64], // 0000001111xxx + [10, 64], [10, 64], [10, 64], [10, 64] + ]; + + const blackTable2 = [ + [8, 13], [8, 13], [8, 13], [8, 13], // 00000100xxxx + [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], // 00000101000x + [12, 50], // 000001010010 + [12, 51], // 000001010011 + [12, 44], // 000001010100 + [12, 45], // 000001010101 + [12, 46], // 000001010110 + [12, 47], // 000001010111 + [12, 57], // 000001011000 + [12, 58], // 000001011001 + [12, 61], // 000001011010 + [12, 256], // 000001011011 + [10, 16], [10, 16], [10, 16], [10, 16], // 0000010111xx + [10, 17], [10, 17], [10, 17], [10, 17], // 0000011000xx + [12, 48], // 000001100100 + [12, 49], // 000001100101 + [12, 62], // 000001100110 + [12, 63], // 000001100111 + [12, 30], // 000001101000 + [12, 31], // 000001101001 + [12, 32], // 000001101010 + [12, 33], // 000001101011 + [12, 40], // 000001101100 + [12, 41], // 000001101101 + [11, 22], [11, 22], // 00000110111x + [8, 14], [8, 14], [8, 14], [8, 14], // 00000111xxxx + [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], // 0000100xxxxx + [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], // 0000101xxxxx + [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], // 000011000xxx + [9, 15], [9, 15], [9, 15], [9, 15], + [12, 128], // 000011001000 + [12, 192], // 000011001001 + [12, 26], // 000011001010 + [12, 27], // 000011001011 + [12, 28], // 000011001100 + [12, 29], // 000011001101 + [11, 19], [11, 19], // 00001100111x + [11, 20], [11, 20], // 00001101000x + [12, 34], // 000011010010 + [12, 35], // 000011010011 + [12, 36], // 000011010100 + [12, 37], // 000011010101 + [12, 38], // 000011010110 + [12, 39], // 000011010111 + [11, 21], [11, 21], // 00001101100x + [12, 42], // 000011011010 + [12, 43], // 000011011011 + [10, 0], [10, 0], [10, 0], [10, 0], // 0000110111xx + [7, 12], [7, 12], [7, 12], [7, 12], // 0000111xxxxx + [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] + ]; + + const blackTable3 = [ + [-1, -1], [-1, -1], [-1, -1], [-1, -1], // 0000xx + [6, 9], // 000100 + [6, 8], // 000101 + [5, 7], [5, 7], // 00011x + [4, 6], [4, 6], [4, 6], [4, 6], // 0010xx + [4, 5], [4, 5], [4, 5], [4, 5], // 0011xx + [3, 1], [3, 1], [3, 1], [3, 1], // 010xxx + [3, 1], [3, 1], [3, 1], [3, 1], + [3, 4], [3, 4], [3, 4], [3, 4], // 011xxx + [3, 4], [3, 4], [3, 4], [3, 4], + [2, 3], [2, 3], [2, 3], [2, 3], // 10xxxx + [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], // 11xxxx + [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 constructor(str) { this.str = str; var dict = str.dict; this.dict = dict; this.encoding = dict.get("K") || 0; - this.eol = dict.get("EndOfLine") || false; + this.eoline = dict.get("EndOfLine") || false; this.byteAlign = dict.get("EncodedByteAlign") || false; this.columns = dict.get("Columns") || 1728; this.rows = dict.get("Rows") || 0; - var eob = dict.get("EndOfBlock"); - if (typeof eob == "undefined") - eob = true; + var eoblock = dict.get("EndOfBlock"); + if (typeof eoblock == "undefined") + eoblock = true; this.black = dict.get("BlackIs1") || false; this.codingLine = new Uint8Array(this.columns + 1); @@ -1151,7 +1578,155 @@ var CcittFaxStream = (function() { } return buf; } - constructor.prototype.addPixels(a1, blackPixels) { + constructor.prototype.getTwoDimCode() { + var code = 0; + var p; + if (this.eoblock) { + code = this.lookBits(7); + p = twoDimTable[code]; + if (p[0] > 0) { + eatBits(p[0]); + return p[1]; + } + } else { + for (var n = 1; n <= 7; ++n) { + code = lookBits(n); + if (n < 7) { + code <<= 7 - n; + } + p = twoDimTable[code]; + if (p[0] == n) { + eatBits(n); + return p[1]; + } + } + } + error("Bad two dim code"); + }; + + constructor.prototype.getWhiteCode() { + var code = 0; + var p; + var n; + if (this.eoblock) { + code = this.lookBits(12); + if (typeof code == "undefined") + 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 { + for (var n = 1; n <= 9; ++n) { + code = this.lookBits(n); + if (typeof code == "undefined") + return 1; + + if (n < 9) + code <<= 9 - n; + p = whiteTable2[code]; + if (p[0] == b) { + this.eatBits(n); + return p[0]; + } + } + for (var n = 11; n <= 12; ++n) { + code == this.lookBits(n); + if (typeof code == "undefined") + return 1; + if (n < 12) + code <<= 12 - n; + p = whiteTable1[code]; + if (p[0] == n) { + eatBits(n); + return p[1]; + } + } + } + error("bad white code"); + }; + constructor.prototype.getBlackCode() { + var code, p, n; + if (this.eoblock) { + code = this.lookBits(13); + if (typeof code == "undefined") + 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 { + for (var n = 2; n <= 6; ++n) { + code = this.lookBits(n); + if (typeof code == "undefined") + return 1; + if (n < 6) + code <<= 6 - n; + + p = blackTable3[code]; + if (p[0] == n) { + this.eatBits(n); + return p[1]; + } + } + for (var n = 7; n <= 12; ++n) { + code = this.lookBits(n); + if (typeof code == "undefined") + return 1; + if (n < 12) + code <<= 12 - n; + if (code >= 64) { + p = blackTable[code - 64]; + if (p[0] == n) { + this.eatBits(n); + return p[1]; + } + } + } + for (n = 10; n <= 13; ++n) { + code = lookBits(n); + if (code == EOF) + return 1; + if (n < 13) + code << 13 - n; + p = blackTable1[code]; + if (p[0] == n) { + this.eatBits(n); + return p[1]; + } + } + } + error("bad black code"); + }; + constructor.prototype.lookBits(n) { + var c; + while (inputBits < n) { + if (typeof (c = str.getByte()) == "undefined") { + if (inputBits == 0) + return EOF; + return (inputBuf << (n - inputBits)) & (0xFFFF >> (16 - n)); + } + inputBuf = (inputBuf << 8) + c; + inputBits += 8; + } + return (inputBuf >> (inputBits - n)) & (0xFFFF >> (16 - n)); + }; + constructor.prototype.eatBits(n) { + if ((inputBits -= n) < 0) + inputBits = 0; } return constructor; From 325170a5fcc5e5c33e5abd966d0d7e8d6914ec6b Mon Sep 17 00:00:00 2001 From: sbarman Date: Wed, 22 Jun 2011 18:56:33 -0700 Subject: [PATCH 03/13] Cleaning up FaxStream --- pdf.js | 199 ++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 125 insertions(+), 74 deletions(-) diff --git a/pdf.js b/pdf.js index 8cb1d07d1..a43dcf559 100644 --- a/pdf.js +++ b/pdf.js @@ -839,7 +839,7 @@ var Ascii85Stream = (function() { return constructor; })(); -var CcittFaxStream = (function() { +var CCITTFaxStream = (function() { const ccittEOL = -2; const twoDimPass = 0; @@ -1284,12 +1284,15 @@ var CcittFaxStream = (function() { this.codingLine = new Uint8Array(this.columns + 1); this.codingLine[0] = this.columns; + this.a0i = 0; + this.refLine = new Uint8Array(this.columns + 2); this.row = 0; this.nextLine2D = this.encoding < 0; this.inputBits = 0; - this.a0i = 0; + this.inputBuf; this.outputBits = 0; + this.buf = EOF; var code1; while ((code1 = this.lookBits(12)) == 0) { @@ -1310,57 +1313,81 @@ var CcittFaxStream = (function() { constructor.prototype.readBlock = function() { }; constructor.prototype.addPixels(a1, blackPixels) { - if (a1 > this.codingLine[this.a01]) { + var codingLine = this.codingLine; + var a0i = this.a0i; + + if (a1 > codingLine[a0i]) { if (a1 > this.columns) error("row is wrong length"); - if (this.a0i & 1) ^ this.blackPixels) - this.a0i++; + if (a0i & 1) ^ blackPixels) { + ++a0i; + this.a0i = a0i; + } - this.codingLine[this.a0i] = a1; - }; - constructor.prototype.addPixelsNeg(a1, blackPixels) { - if (a1 > this.codingLine[this.a01]) { - if (a1 > this.columns) - error("row is wrong length"); - if (this.a0i & 1) ^ this.blackPixels) - this.a0i++; - - this.codingLine[this.a0i] = a1; - } else if (at < this.codingLine[this.a0i]) { - if (a1 < 0) - error("invalid code"); - while (this.a0i > 0 && a1 < this.codingLine[this.a0i -i]) - this.a0i--; - this.codingLine[this.a0i] = a1; + codingLine[a0i] = a1; } }; - constructor.prototype.lookChar(a1, blackPixels) { + constructor.prototype.addPixelsNeg(a1, blackPixels) { + var codingLine = this.codingLine; + var a0i = this.a0i; + + if (a1 > codingLine[a0i]) { + if (a1 > this.columns) + error("row is wrong length"); + if (a0i & 1) ^ blackPixels) + ++a0i; + + codingLine[a0i] = a1; + } else if (a1 < codingLine[a0i]) { + if (a1 < 0) + error("invalid code"); + while (a0i > 0 && a1 < codingLine[a0i - 1]) + --a0i; + this.codingLine[a0i] = a1; + } + + this.a0i = a0i; + }; + constructor.prototype.lookChar() { var refLine = this.refLine; var codingLine = this.codingLine; var columns = this.columns; - if (this.outputBits == 0) { + var outputBits = this.outputBits; + var a0i = this.a0i; + + var b1i, blackPixels, bits; + + if (this.buf != EOF) + return buf; + + if (outputBits == 0) { + if (this.eof) + return; + if (this.nextLine2D) { for (var i = 0; codingLine[i] < columns; ++i) - refLine[i] = codeLine[i]; + refLine[i] = codingLine[i]; + refLine[i++] = columns; refLine[i] = columns; codingLine[0] = 0; - this.a0i = 0; - this.bl1 = 0; - this.blackPixels = 0; + a0i = 0; + bl1 = 0; + blackPixels = 0; - while (codingLine[this.a0i] < columns) { + while (codingLine[a0i] < columns) { var code1 = this.getTwoDumCode(); switch (code1) { case twoDimPass: - this.addPixels(refLine[this.bli + 1], this.blackPixels); - if (refLine[this.bli + 1] < columns) - this.bli += 2; + this.addPixels(refLine[b1i + 1], blackPixels); + a0i = this.a0i; + if (refLine[bli + 1] < columns) + bli += 2; break; case twoDimHoriz: var code1 = 0, code2 = 0; - if (this.blackPixels) { + if (blackPixels) { var code3; do { code1 += (code3 = this.getBlackCode()); @@ -1371,75 +1398,99 @@ var CcittFaxStream = (function() { else { var code3; do { - code1 += (code3 = getWhiteCode()); + code1 += (code3 = this.getWhiteCode()); } while (code3 >= 64); - this.addPixels(codeLine[this.a0i + code1, this.blackPixels); - if (codeLine[a0i] < columns) { - addPixels(codeLine[a0i] + code2, this.blackPixels ^ 1); - } - while (refLine[this.bli] <= codingLine[a0i] - && refLine[this.bli] < columns) { - this.bli += 2; - } + do { + code2 += (code3 = this.getBlackCode()); + } while (code3 >= 64); + } + this.addPixels(codingLine[a0i] + code1, blackPixels); + a0i = this.a0i; + if (codingLine[a0i] < columns) { + this.addPixels(codeLine[a0i] + code2, + blackPixels ^ 1); + a0i = this.a0i; + } + while (refLine[b1i] <= codingLine[a0i] + && refLine[bli] < columns) { + bli += 2; + } + break; + case twoDimVertR3: + this.addPixels(refLine[b1i] + 3, blackPixels); + a0i = this.a0i; + blackPixels ^= 1; + if (codingLine[a0i] < columns) { + ++b1i; + while (refLine[b1i] <= codingLine[a0i] && + refLine[b1i] < columns) + b1i += 2; } break; case twoDimVertR2: - this.addPixels(this.refLine[this.bli], blackPixels); + this.addPixels(refLine[b1i] + 2, blackPixels); + a0i = this.a0i; blackPixels ^= 1; - if (codeLine[this.a01] < columns) { - this.bli++; - while (refLine[this.bli] <= codeLine[this.a0i] && + if (codingLine[a01] < columns) { + ++bli; + while (refLine[bli] <= codingLine[a0i] && refLine[bli] < columns) { - this.bli += 2; + bli += 2; } } break; case twoDimVertR1: - this.addPixels(refLine[this.bli] + 1, blackPixels); - this.blackPixels ^= 1; - if (codeLine[this.a01] < columns) { - this.bli++; - while (refLine[this.bli] < codingLine[this.a0i] && - refLine[this.bli] < columns) + this.addPixels(refLine[bli] + 1, blackPixels); + a0i = this.a0i; + blackPixels ^= 1; + if (codingLine[a01] < columns) { + ++bli; + while (refLine[bli] < codingLine[a0i] && + refLine[bli] < columns) this.bli += 2; } break; case twoDimVert0: - this.addPixels(refLine[this.bli], blackPixels); - this.blackPixels ^= 1; - if (codingLine[this.a0i] < columns) { - this.bli++; - while (refLine[this.bli] <= codingLine[a0i] && - refLine[this.bli] < columns) - this.bli += 2; - } - break; - case twoDimVertL3: - this.addPixelsNeg(refLine[this.bli] - 3, blackPixels); - this.blackPixels ^= 1; - if (codeLine[a0i] < columns) { - if (bli > 0) - --bli; - else - ++bli; + this.addPixels(refLine[bli], blackPixels); + a0i = this.a0i; + blackPixels ^= 1; + if (codingLine[a0i] < columns) { + ++bli; while (refLine[bli] <= codingLine[a0i] && refLine[bli] < columns) bli += 2; } break; - case twoDimVertL2: - this.addPixelsNeg(refLine[bli] - 2, blackPixels); + case twoDimVertL3: + this.addPixelsNeg(refLine[bli] - 3, blackPixels); + a0i = this.a0i; blackPixels ^= 1; - if (codingLine[a0i] < columns) { + if (codeLine[a0i] < columns) { if (bli > 0) --bli; else ++bli; - while (refLine[bli] <= codingLine[a0i] && - refLine[bl1] < columns) + while (refLine[b1i] <= codingLine[a0i] && + refLine[b1i] < columns) bli += 2; } break; + case twoDimVertL2: + this.addPixelsNeg(refLine[b1i] - 2, blackPixels); + a0i = this.a0i; + blackPixels ^= 1; + if (codingLine[a0i] < columns) { + if (b1i > 0) + --b1i; + else + ++bli; + while (refLine[b1i] <= codingLine[a0i] && + refLine[b11] < columns) + b1i += 2; + } + break; + +///////// STOPPED HERE case twoDimVertL1: this.addPixelsNeg(refLine[bli] - 1, blackPixels); this.blackPixels ^= 1; From 8c3f5df3beceb98af8f30140a602925a9de6e8d1 Mon Sep 17 00:00:00 2001 From: sbarman Date: Thu, 23 Jun 2011 10:35:08 -0700 Subject: [PATCH 04/13] faxstream compiles --- pdf.js | 252 +++++++++++++++++++++++++++++---------------------------- 1 file changed, 130 insertions(+), 122 deletions(-) diff --git a/pdf.js b/pdf.js index d8891a380..37bdc5a6f 100644 --- a/pdf.js +++ b/pdf.js @@ -1315,7 +1315,7 @@ var CCITTFaxStream = (function() { this.codingLine = new Uint8Array(this.columns + 1); this.codingLine[0] = this.columns; - this.a0i = 0; + this.codingPos = 0; this.refLine = new Uint8Array(this.columns + 2); this.row = 0; @@ -1342,52 +1342,60 @@ var CCITTFaxStream = (function() { constructor.prototype = Object.create(DecodeStream.prototype); constructor.prototype.readBlock = function() { - }; - constructor.prototype.addPixels(a1, blackPixels) { - var codingLine = this.codingLine; - var a0i = this.a0i; + while (!this.eof) { + var c = this.lookChar(); + this.buf = EOF; + if (this.bufferLength <= this.pos) + this.ensureBuffer(this.pos + 1); - if (a1 > codingLine[a0i]) { + this.buffer[this.pos++] = c; + } + }; + constructor.prototype.addPixels = function(a1, blackPixels) { + var codingLine = this.codingLine; + var codingPos = this.codingPos; + + if (a1 > codingLine[codingPos]) { if (a1 > this.columns) error("row is wrong length"); - if (a0i & 1) ^ blackPixels) { - ++a0i; - this.a0i = a0i; + if ((codingPos & 1) ^ blackPixels) { + ++codingPos; + this.codingPos = codingPos; } - codingLine[a0i] = a1; + codingLine[codingPos] = a1; } }; - constructor.prototype.addPixelsNeg(a1, blackPixels) { + constructor.prototype.addPixelsNeg = function(a1, blackPixels) { var codingLine = this.codingLine; - var a0i = this.a0i; + var codingPos = this.codingPos; - if (a1 > codingLine[a0i]) { + if (a1 > codingLine[codingPos]) { if (a1 > this.columns) error("row is wrong length"); - if (a0i & 1) ^ blackPixels) - ++a0i; + if ((codingPos & 1) ^ blackPixels) + ++codingPos; - codingLine[a0i] = a1; - } else if (a1 < codingLine[a0i]) { + codingLine[codingPos] = a1; + } else if (a1 < codingLine[codingPos]) { if (a1 < 0) error("invalid code"); - while (a0i > 0 && a1 < codingLine[a0i - 1]) - --a0i; - this.codingLine[a0i] = a1; + while (codingPos > 0 && a1 < codingLine[codingPos - 1]) + --codingPos; + this.codingLine[codingPos] = a1; } - this.a0i = a0i; + this.codingPos = codingPos; }; - constructor.prototype.lookChar() { + constructor.prototype.lookChar = function() { var refLine = this.refLine; var codingLine = this.codingLine; var columns = this.columns; var outputBits = this.outputBits; - var a0i = this.a0i; + var codingPos = this.codingPos; - var b1i, blackPixels, bits; + var refPos, blackPixels, bits; if (this.buf != EOF) return buf; @@ -1403,18 +1411,18 @@ var CCITTFaxStream = (function() { refLine[i++] = columns; refLine[i] = columns; codingLine[0] = 0; - a0i = 0; - bl1 = 0; + codingPos = 0; + refPos = 0; blackPixels = 0; - while (codingLine[a0i] < columns) { + while (codingLine[codingPos] < columns) { var code1 = this.getTwoDumCode(); switch (code1) { case twoDimPass: - this.addPixels(refLine[b1i + 1], blackPixels); - a0i = this.a0i; - if (refLine[bli + 1] < columns) - bli += 2; + this.addPixels(refLine[refPos + 1], blackPixels); + codingPos = this.codingPos; + if (refLine[refPos + 1] < columns) + refPos += 2; break; case twoDimHoriz: var code1 = 0, code2 = 0; @@ -1426,7 +1434,7 @@ var CCITTFaxStream = (function() { do { code2 += (code3 = this.getWhiteCode()); } while (code3 >= 64); - else { + } else { var code3; do { code1 += (code3 = this.getWhiteCode()); @@ -1435,105 +1443,105 @@ var CCITTFaxStream = (function() { code2 += (code3 = this.getBlackCode()); } while (code3 >= 64); } - this.addPixels(codingLine[a0i] + code1, blackPixels); - a0i = this.a0i; - if (codingLine[a0i] < columns) { - this.addPixels(codeLine[a0i] + code2, + this.addPixels(codingLine[codingPos] + code1, blackPixels); + codingPos = this.codingPos; + if (codingLine[codingPos] < columns) { + this.addPixels(codeLine[codingPos] + code2, blackPixels ^ 1); - a0i = this.a0i; + codingPos = this.codingPos; } - while (refLine[b1i] <= codingLine[a0i] - && refLine[bli] < columns) { - bli += 2; + while (refLine[refPos] <= codingLine[codingPos] + && refLine[refPos] < columns) { + refPos += 2; } break; case twoDimVertR3: - this.addPixels(refLine[b1i] + 3, blackPixels); - a0i = this.a0i; + this.addPixels(refLine[refPos] + 3, blackPixels); + codingPos = this.codingPos; blackPixels ^= 1; - if (codingLine[a0i] < columns) { - ++b1i; - while (refLine[b1i] <= codingLine[a0i] && - refLine[b1i] < columns) - b1i += 2; + if (codingLine[codingPos] < columns) { + ++refPos; + while (refLine[refPos] <= codingLine[codingPos] && + refLine[refPos] < columns) + refPos += 2; } break; case twoDimVertR2: - this.addPixels(refLine[b1i] + 2, blackPixels); - a0i = this.a0i; + this.addPixels(refLine[refPos] + 2, blackPixels); + codingPos = this.codingPos; blackPixels ^= 1; - if (codingLine[a01] < columns) { - ++bli; - while (refLine[bli] <= codingLine[a0i] && - refLine[bli] < columns) { - bli += 2; + if (codingLine[codingPos] < columns) { + ++refPos; + while (refLine[refPos] <= codingLine[codingPos] && + refLine[refPos] < columns) { + refPos += 2; } } break; case twoDimVertR1: - this.addPixels(refLine[bli] + 1, blackPixels); - a0i = this.a0i; + this.addPixels(refLine[refPos] + 1, blackPixels); + codingPos = this.codingPos; blackPixels ^= 1; - if (codingLine[a01] < columns) { - ++bli; - while (refLine[bli] < codingLine[a0i] && - refLine[bli] < columns) - this.bli += 2; + if (codingLine[codingPos] < columns) { + ++refPos; + while (refLine[refPos] < codingLine[codingPos] && + refLine[refPos] < columns) + refPos += 2; } break; case twoDimVert0: - this.addPixels(refLine[bli], blackPixels); - a0i = this.a0i; + this.addPixels(refLine[refPos], blackPixels); + codingPos = this.codingPos; blackPixels ^= 1; - if (codingLine[a0i] < columns) { - ++bli; - while (refLine[bli] <= codingLine[a0i] && - refLine[bli] < columns) - bli += 2; + if (codingLine[codingPos] < columns) { + ++refPos; + while (refLine[refPos] <= codingLine[codingPos] && + refLine[refPos] < columns) + refPos += 2; } break; case twoDimVertL3: - this.addPixelsNeg(refLine[bli] - 3, blackPixels); - a0i = this.a0i; + this.addPixelsNeg(refLine[refPos] - 3, blackPixels); + codingPos = this.codingPos; blackPixels ^= 1; - if (codeLine[a0i] < columns) { - if (bli > 0) - --bli; + if (codeLine[codingPos] < columns) { + if (refPos > 0) + --refPos; else - ++bli; - while (refLine[b1i] <= codingLine[a0i] && - refLine[b1i] < columns) - bli += 2; + ++refPos; + while (refLine[refPos] <= codingLine[codingPos] && + refLine[refPos] < columns) + refPos += 2; } break; case twoDimVertL2: - this.addPixelsNeg(refLine[b1i] - 2, blackPixels); - a0i = this.a0i; + this.addPixelsNeg(refLine[refPos] - 2, blackPixels); + codingPos = this.codingPos; blackPixels ^= 1; - if (codingLine[a0i] < columns) { - if (b1i > 0) - --b1i; + if (codingLine[codingPos] < columns) { + if (refPos > 0) + --refPos; else - ++bli; - while (refLine[b1i] <= codingLine[a0i] && + ++refPos; + while (refLine[refPos] <= codingLine[codingPos] && refLine[b11] < columns) - b1i += 2; + refPos += 2; } break; ///////// STOPPED HERE case twoDimVertL1: - this.addPixelsNeg(refLine[bli] - 1, blackPixels); + this.addPixelsNeg(refLine[refPos] - 1, blackPixels); this.blackPixels ^= 1; - if (codingLine[a0i] < columns) { - if (blu > 0) - --bli; + if (codingLine[codingPos] < columns) { + if (refPos > 0) + --refPos; else - ++bli; + ++refPos; - while (refLine[bli] <= codeLine[a0i] && - refLine[bli] < columns) - bli += 2; + while (refLine[refPos] <= codeLine[codingPos] && + refLine[refPos] < columns) + refPos += 2; } break; case EOF: @@ -1546,9 +1554,9 @@ var CCITTFaxStream = (function() { } } else { codingLine[0] = 0; - a0i = 0; + codingPos = 0; blackPixels = 0; - while(codeLine[a0i] < columns) { + while(codeLine[codingPos] < columns) { code1 = 0; if (blackPixels) { do { @@ -1559,7 +1567,7 @@ var CCITTFaxStream = (function() { code1 += (code3 = getWhiteCode()); } while (code3 >= 64); } - this.addPixels(codingLine[a0i] + code1, blackPixels); + this.addPixels(codingLine[codingPos] + code1, blackPixels); blackPixels ^= 1; } } @@ -1574,7 +1582,7 @@ var CCITTFaxStream = (function() { code1 = lookBits(12); while (code1 == 0) { eatBits(1); - code1 = lookBits(12); + code1 = this.lookBits(12); } if (code1 = 1) { eatBits(12); @@ -1585,27 +1593,27 @@ var CCITTFaxStream = (function() { } if (!oef && encoding > 0) { - nextLine2D = !lookBits(1); + nextLine2D = !this.lookBits(1); eatBits(1); } if (eob && gotEol) { - code1 = lookBits(12); + code1 = this.lookBits(12); if (code1 == 1) { eatBits(12); if (encoding > 0) { - lookBits(1); - eatBits(1); + this.lookBits(1); + this.eatBits(1); } if (encoding >= 0) { for (i = 0; i < 4; ++i) { - code1 = lookBits(12); + code1 = this.lookBits(12); if (code1 != 1) error("bad rtc code"); eatBits(12); if (encoding > 0) { - lookBits(1); - eatBits(1); + this.lookBits(1); + this.eatBits(1); } } } @@ -1614,18 +1622,18 @@ var CCITTFaxStream = (function() { } if (codingLine[0] > 0) - outputBits = codingLine[a0i = 0]; + outputBits = codingLine[codingPos = 0]; else - outputBits = codingLine[a0i = 1]; + outputBits = codingLine[codingPos = 1]; ++row; } if (outputBits >= 8) { - buf = (a0i & 1) ? 0 : 0xFF; + buf = (codingPos & 1) ? 0 : 0xFF; outputBits -= 8; - if (outputBits == 0&& codingLine[a0i] < columns) { - ++a0i; - outputBits = codingLine[a0i] - codingLine[a0i - 1]; + if (outputBits == 0&& codingLine[codingPos] < columns) { + ++codingPos; + outputBits = codingLine[codingPos] - codingLine[codingPos - 1]; } } else { bits = 8; @@ -1633,21 +1641,21 @@ var CCITTFaxStream = (function() { do { if (outputBits > bits) { buf << bits; - if (!(a0i & 1)) { + if (!(codingPos & 1)) { buf |= 0xFF >> (8 - bits); } outputBits -= bits; bits = 0; } else { buf <<= outputBits; - if (!(a0i & 1)) { + if (!(codingPos & 1)) { buf |= 0xFF >> (8 - outputBits); } bits -= outputBits; outputBits = 0; - if (codingLine[a0i] < columns) { - ++a0i; - outputBits = codingLine[a0i] - codingLine[a0i - 1]; + if (codingLine[codingPos] < columns) { + ++codingPos; + outputBits = codingLine[codingPos] - codingLine[codingPos - 1]; } else if (bits > 0) { buf <<= bits; bits = 0; @@ -1659,8 +1667,8 @@ var CCITTFaxStream = (function() { buf ^= 0xFF; } return buf; - } - constructor.prototype.getTwoDimCode() { + }; + constructor.prototype.getTwoDimCode = function() { var code = 0; var p; if (this.eoblock) { @@ -1686,7 +1694,7 @@ var CCITTFaxStream = (function() { error("Bad two dim code"); }; - constructor.prototype.getWhiteCode() { + constructor.prototype.getWhiteCode = function() { var code = 0; var p; var n; @@ -1726,14 +1734,14 @@ var CCITTFaxStream = (function() { code <<= 12 - n; p = whiteTable1[code]; if (p[0] == n) { - eatBits(n); + this.eatBits(n); return p[1]; } } } error("bad white code"); }; - constructor.prototype.getBlackCode() { + constructor.prototype.getBlackCode = function() { var code, p, n; if (this.eoblock) { code = this.lookBits(13); @@ -1793,7 +1801,7 @@ var CCITTFaxStream = (function() { } error("bad black code"); }; - constructor.prototype.lookBits(n) { + constructor.prototype.lookBits = function(n) { var c; while (inputBits < n) { if (typeof (c = str.getByte()) == "undefined") { @@ -1806,7 +1814,7 @@ var CCITTFaxStream = (function() { } return (inputBuf >> (inputBits - n)) & (0xFFFF >> (16 - n)); }; - constructor.prototype.eatBits(n) { + constructor.prototype.eatBits = function(n) { if ((inputBits -= n) < 0) inputBits = 0; } From 6c8e545e5efc4830d22429603ad59b62b0f68a72 Mon Sep 17 00:00:00 2001 From: sbarman Date: Fri, 24 Jun 2011 07:51:59 -0700 Subject: [PATCH 05/13] Still fixing faxstream --- pdf.js | 310 ++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 188 insertions(+), 122 deletions(-) diff --git a/pdf.js b/pdf.js index 37bdc5a6f..00d07df6f 100644 --- a/pdf.js +++ b/pdf.js @@ -1324,6 +1324,7 @@ var CCITTFaxStream = (function() { this.inputBuf; this.outputBits = 0; this.buf = EOF; + this.err = false; var code1; while ((code1 = this.lookBits(12)) == 0) { @@ -1356,33 +1357,42 @@ var CCITTFaxStream = (function() { var codingPos = this.codingPos; if (a1 > codingLine[codingPos]) { - if (a1 > this.columns) - error("row is wrong length"); + if (a1 > this.columns) { + warn("row is wrong length"); + this.err = true; + a1 = this.columns; + } if ((codingPos & 1) ^ blackPixels) { ++codingPos; - this.codingPos = codingPos; } codingLine[codingPos] = a1; } + this.codingPos = codingPos; }; constructor.prototype.addPixelsNeg = function(a1, blackPixels) { var codingLine = this.codingLine; var codingPos = this.codingPos; if (a1 > codingLine[codingPos]) { - if (a1 > this.columns) - error("row is wrong length"); + if (a1 > this.columns) { + warn("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) - error("invalid code"); + if (a1 < 0) { + warn("invalid code"); + this.err = true; + a1 = 0; + } while (codingPos > 0 && a1 < codingLine[codingPos - 1]) --codingPos; - this.codingLine[codingPos] = a1; + codingLine[codingPos] = a1; } this.codingPos = codingPos; @@ -1392,15 +1402,14 @@ var CCITTFaxStream = (function() { var codingLine = this.codingLine; var columns = this.columns; - var outputBits = this.outputBits; - var codingPos = this.codingPos; - var refPos, blackPixels, bits; if (this.buf != EOF) return buf; - if (outputBits == 0) { + this.err = false; + + if (this.outputBits == 0) { if (this.eof) return; @@ -1411,16 +1420,15 @@ var CCITTFaxStream = (function() { refLine[i++] = columns; refLine[i] = columns; codingLine[0] = 0; - codingPos = 0; + this.codingPos = 0; refPos = 0; blackPixels = 0; - while (codingLine[codingPos] < columns) { + while (codingLine[this.codingPos] < columns) { var code1 = this.getTwoDumCode(); switch (code1) { case twoDimPass: this.addPixels(refLine[refPos + 1], blackPixels); - codingPos = this.codingPos; if (refLine[refPos + 1] < columns) refPos += 2; break; @@ -1443,36 +1451,32 @@ var CCITTFaxStream = (function() { code2 += (code3 = this.getBlackCode()); } while (code3 >= 64); } - this.addPixels(codingLine[codingPos] + code1, blackPixels); - codingPos = this.codingPos; - if (codingLine[codingPos] < columns) { - this.addPixels(codeLine[codingPos] + code2, + this.addPixels(codingLine[this.codingPos] + code1, blackPixels); + if (codingLine[this.codingPos] < columns) { + this.addPixels(codingLine[this.codingPos] + code2, blackPixels ^ 1); - codingPos = this.codingPos; } - while (refLine[refPos] <= codingLine[codingPos] + while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) { refPos += 2; } break; case twoDimVertR3: this.addPixels(refLine[refPos] + 3, blackPixels); - codingPos = this.codingPos; blackPixels ^= 1; - if (codingLine[codingPos] < columns) { + if (codingLine[this.codingPos] < columns) { ++refPos; - while (refLine[refPos] <= codingLine[codingPos] && + while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) refPos += 2; } break; case twoDimVertR2: this.addPixels(refLine[refPos] + 2, blackPixels); - codingPos = this.codingPos; blackPixels ^= 1; - if (codingLine[codingPos] < columns) { + if (codingLine[this.codingPos] < columns) { ++refPos; - while (refLine[refPos] <= codingLine[codingPos] && + while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) { refPos += 2; } @@ -1480,94 +1484,92 @@ var CCITTFaxStream = (function() { break; case twoDimVertR1: this.addPixels(refLine[refPos] + 1, blackPixels); - codingPos = this.codingPos; blackPixels ^= 1; - if (codingLine[codingPos] < columns) { + if (codingLine[this.codingPos] < columns) { ++refPos; - while (refLine[refPos] < codingLine[codingPos] && + while (refLine[refPos] < codingLine[this.codingPos] && refLine[refPos] < columns) refPos += 2; } break; case twoDimVert0: this.addPixels(refLine[refPos], blackPixels); - codingPos = this.codingPos; blackPixels ^= 1; - if (codingLine[codingPos] < columns) { + if (codingLine[this.codingPos] < columns) { ++refPos; - while (refLine[refPos] <= codingLine[codingPos] && + while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) refPos += 2; } break; case twoDimVertL3: this.addPixelsNeg(refLine[refPos] - 3, blackPixels); - codingPos = this.codingPos; blackPixels ^= 1; - if (codeLine[codingPos] < columns) { + if (codingLine[this.codingPos] < columns) { if (refPos > 0) --refPos; else ++refPos; - while (refLine[refPos] <= codingLine[codingPos] && + while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) refPos += 2; } break; case twoDimVertL2: this.addPixelsNeg(refLine[refPos] - 2, blackPixels); - codingPos = this.codingPos; blackPixels ^= 1; - if (codingLine[codingPos] < columns) { + if (codingLine[this.codingPos] < columns) { if (refPos > 0) --refPos; else ++refPos; - while (refLine[refPos] <= codingLine[codingPos] && - refLine[b11] < columns) + while (refLine[refPos] <= codingLine[this.codingPos] && + refLine[redPos] < columns) refPos += 2; } break; -///////// STOPPED HERE case twoDimVertL1: this.addPixelsNeg(refLine[refPos] - 1, blackPixels); - this.blackPixels ^= 1; - if (codingLine[codingPos] < columns) { + blackPixels ^= 1; + if (codingLine[this.codingPos] < columns) { if (refPos > 0) --refPos; else ++refPos; - while (refLine[refPos] <= codeLine[codingPos] && + while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) refPos += 2; } break; case EOF: - addPixels(columns, 0); + this.addPixels(columns, 0); this.eof = true; break; default: - error("bad 2d code"); + warn("bad 2d code"); + addPixels(columns, 0); + this.err = true; + break; } } } else { codingLine[0] = 0; - codingPos = 0; + this.codingPos = 0; blackPixels = 0; - while(codeLine[codingPos] < columns) { + while(codingLine[this.codingPos] < columns) { code1 = 0; if (blackPixels) { do { - code1 += (code3 = getBlackCode()); + code1 += (code3 = this.getBlackCode()); } while (code3 >= 64); } else { do { - code1 += (code3 = getWhiteCode()); + code1 += (code3 = this.getWhiteCode()); } while (code3 >= 64); } - this.addPixels(codingLine[codingPos] + code1, blackPixels); + this.addPixels(codingLine[this.codingPos] + code1, blackPixels); blackPixels ^= 1; } } @@ -1575,98 +1577,117 @@ var CCITTFaxStream = (function() { if (this.byteAlign) inputBits &= ~7; - getEol = false; - if (eob && row == rows - 1) { - eof = true; + var gotEOL = false; + + if (this.eoblock && this.row == this.rows - 1) { + this.eof = true; } else { - code1 = lookBits(12); + code1 = this.lookBits(12); while (code1 == 0) { - eatBits(1); + this.eatBits(1); code1 = this.lookBits(12); } - if (code1 = 1) { - eatBits(12); - gotEol = true; - } else if (code1 = EOF) { - eof = true; + if (code1 == 1) { + this.eatBits(12); + gotEOL = true; + } else if (code1 == EOF) { + this.eof = true; } } - if (!oef && encoding > 0) { - nextLine2D = !this.lookBits(1); - eatBits(1); + if (!this.eof && this.encoding > 0) { + this.nextLine2D = !this.lookBits(1); + this.eatBits(1); } - if (eob && gotEol) { + if (this.eoblock && gotEOL) { code1 = this.lookBits(12); if (code1 == 1) { eatBits(12); - if (encoding > 0) { + if (this.encoding > 0) { this.lookBits(1); this.eatBits(1); } - if (encoding >= 0) { - for (i = 0; i < 4; ++i) { + if (this.encoding >= 0) { + for (var i = 0; i < 4; ++i) { code1 = this.lookBits(12); if (code1 != 1) error("bad rtc code"); - eatBits(12); - if (encoding > 0) { + this.eatBits(12); + if (this.encoding > 0) { this.lookBits(1); this.eatBits(1); } } } - eof = true; + this.eof = true; + } + } else if (this.err && this.eolin) { + var code1; + while (true) { + code1 = this.lookBits(13); + if (code1 == EOF) { + this.eof = true; + return; + } + if ((code1 >> 1) == 1) { + break; + } + this.eatBits(1); + } + this.eatBits(12); + if (this.encoding > 0) { + this.eatBits(1); + this.nextLine2 = !(code1 & 1); } } if (codingLine[0] > 0) - outputBits = codingLine[codingPos = 0]; + this.outputBits = codingLine[this.codingPos = 0]; else - outputBits = codingLine[codingPos = 1]; - ++row; + this.outputBits = codingLine[this.codingPos = 1]; + this.row++; } - if (outputBits >= 8) { - buf = (codingPos & 1) ? 0 : 0xFF; - outputBits -= 8; - if (outputBits == 0&& codingLine[codingPos] < columns) { - ++codingPos; - outputBits = codingLine[codingPos] - codingLine[codingPos - 1]; + if (this.outputBits >= 8) { + this.buf = (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; - buf = 0; + this.bits = 8; + this.buf = 0; do { - if (outputBits > bits) { - buf << bits; - if (!(codingPos & 1)) { - buf |= 0xFF >> (8 - bits); + if (this.outputBits > this.bits) { + this.buf << this.bits; + if (!(this.codingPos & 1)) { + this.buf |= 0xFF >> (8 - this.bits); } - outputBits -= bits; - bits = 0; + this.outputBits -= this.bits; + this.bits = 0; } else { - buf <<= outputBits; - if (!(codingPos & 1)) { - buf |= 0xFF >> (8 - outputBits); + this.buf <<= this.outputBits; + if (!(this.codingPos & 1)) { + this.buf |= 0xFF >> (8 - this.outputBits); } - bits -= outputBits; - outputBits = 0; - if (codingLine[codingPos] < columns) { - ++codingPos; - outputBits = codingLine[codingPos] - codingLine[codingPos - 1]; - } else if (bits > 0) { - buf <<= bits; - bits = 0; + this.bits -= this.outputBits; + this.outputBits = 0; + if (codingLine[this.codingPos] < columns) { + this.codingPos++; + this.outputBits = codingLine[this.codingPos] - codingLine[this.codingPos - 1]; + } else if (this.bits > 0) { + this.buf <<= this.bits; + this.bits = 0; } } - } while (bits); + } while (this.bits); } - if (black) { - buf ^= 0xFF; + if (this.black) { + this.buf ^= 0xFF; } - return buf; + return this.buf; }; constructor.prototype.getTwoDimCode = function() { var code = 0; @@ -1675,23 +1696,24 @@ var CCITTFaxStream = (function() { code = this.lookBits(7); p = twoDimTable[code]; if (p[0] > 0) { - eatBits(p[0]); + this.eatBits(p[0]); return p[1]; } } else { for (var n = 1; n <= 7; ++n) { - code = lookBits(n); + code = this.lookBits(n); if (n < 7) { code <<= 7 - n; } p = twoDimTable[code]; if (p[0] == n) { - eatBits(n); + this.eatBits(n); return p[1]; } } } - error("Bad two dim code"); + warn("Bad two dim code"); + return EOF; }; constructor.prototype.getWhiteCode = function() { @@ -1721,7 +1743,7 @@ var CCITTFaxStream = (function() { if (n < 9) code <<= 9 - n; p = whiteTable2[code]; - if (p[0] == b) { + if (p[0] == n) { this.eatBits(n); return p[0]; } @@ -1739,7 +1761,9 @@ var CCITTFaxStream = (function() { } } } - error("bad white code"); + warn("bad white code"); + this.eatBits(1); + return 1; }; constructor.prototype.getBlackCode = function() { var code, p, n; @@ -1779,7 +1803,7 @@ var CCITTFaxStream = (function() { if (n < 12) code <<= 12 - n; if (code >= 64) { - p = blackTable[code - 64]; + p = blackTable2[code - 64]; if (p[0] == n) { this.eatBits(n); return p[1]; @@ -1787,7 +1811,7 @@ var CCITTFaxStream = (function() { } } for (n = 10; n <= 13; ++n) { - code = lookBits(n); + code = this.lookBits(n); if (code == EOF) return 1; if (n < 13) @@ -1799,24 +1823,27 @@ var CCITTFaxStream = (function() { } } } - error("bad black code"); + warn("bad black code"); + this.eatBits(1); + return 1; }; constructor.prototype.lookBits = function(n) { var c; - while (inputBits < n) { - if (typeof (c = str.getByte()) == "undefined") { - if (inputBits == 0) + while (this.inputBits < n) { + if (typeof (c = this.str.getByte()) == "undefined") { + if (this.inputBits == 0) return EOF; - return (inputBuf << (n - inputBits)) & (0xFFFF >> (16 - n)); + return (this.inputBuf << (n - this.inputBits)) + & (0xFFFF >> (16 - n)); } - inputBuf = (inputBuf << 8) + c; - inputBits += 8; + this.inputBuf = (this.inputBuf << 8) + c; + this.inputBits += 8; } - return (inputBuf >> (inputBits - n)) & (0xFFFF >> (16 - n)); + return (this.inputBuf >> (this.inputBits - n)) & (0xFFFF >> (16 - n)); }; constructor.prototype.eatBits = function(n) { - if ((inputBits -= n) < 0) - inputBits = 0; + if ((this.inputBits -= n) < 0) + this.inputBits = 0; } return constructor; @@ -2433,7 +2460,7 @@ var Parser = (function() { return new Ascii85Stream(stream); } else if (name == "CCITTFaxDecode") { TODO("implement fax stream"); - return new FakeStream(stream); + return new CCITTFaxStream(stream); } else { error("filter '" + name + "' not supported yet"); } @@ -4186,6 +4213,45 @@ var CanvasGraphics = (function() { } } + if (bitsPerComponent == 1) { + var xref = this.xref; + var csStream = dict.get2("ColorSpace", "CS"); + csStream = xref.fetchIfRef(csStream); + if (IsName(csStream) && inline) + csStream = colorSpaces.get(csStream); + var colorSpace = new ColorSpace(xref, csStream); + var numComps = colorSpace.numComps; + if (numComps != 1) + error("worng numComps"); + + var imgArray = image.getBytes((numComps * w * h) >> 3); + var imgIdx = 0; + + var tmpCanvas = document.createElement("canvas"); + tmpCanvas.width = w; + tmpCanvas.height = h; + var tmpCtx = tmpCanvas.getContext("2d"); + var imgData = tmpCtx.getImageData(0, 0, w, h); + var pixels = imgData.data; + + var mask = 128; + var b = 0; + for (var i = 0; i < length; i += 4) { + mask <<= 1; + if (mask >= 256) { + b = imgArray[imgIdx++]; + mask = 1; + } + + var p = b & mask; + pixels[i] = 255 * p; + pixels[i+1] = 255 * p; + pixels[i+2] = 255 * p; + pixels[i+3] = 255; + } + return; + } + if (bitsPerComponent !== 8) error("Unsupported bpc"); From 383bc110cc65951cfb44d454188e673a8b5c0f0a Mon Sep 17 00:00:00 2001 From: sbarman Date: Fri, 24 Jun 2011 10:24:31 -0700 Subject: [PATCH 06/13] draws a broken image --- pdf.js | 57 ++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 34 insertions(+), 23 deletions(-) diff --git a/pdf.js b/pdf.js index dea681265..0be4abc7c 100644 --- a/pdf.js +++ b/pdf.js @@ -4,7 +4,7 @@ "use strict"; var ERRORS = 0, WARNINGS = 1, TODOS = 5; -var verbosity = WARNINGS; +var verbosity = ERRORS; function log(msg) { if (console && console.log) @@ -889,7 +889,7 @@ var CCITTFaxStream = (function() { const twoDimVertR3 = 7; const twoDimVertL3 = 8; - const twoDimTable1 = [ + const twoDimTable = [ [-1, -1], [-1, -1], // 000000x [7, twoDimVertL3], // 0000010 [7, twoDimVertR3], // 0000011 @@ -1304,33 +1304,34 @@ var CCITTFaxStream = (function() { [2, 2], [2, 2], [2, 2], [2, 2] ]; - function constructor(str) { + function constructor(str, params) { this.str = str; - var dict = str.dict; - this.dict = dict; + this.dict = str.dict; - this.encoding = dict.get("K") || 0; - this.eoline = dict.get("EndOfLine") || false; - this.byteAlign = dict.get("EncodedByteAlign") || false; - this.columns = dict.get("Columns") || 1728; - this.rows = dict.get("Rows") || 0; - var eoblock = dict.get("EndOfBlock"); + params = params || new Dict(); + + this.encoding = params.get("K") || 0; + this.eoline = params.get("EndOfLine") || false; + this.byteAlign = params.get("EncodedByteAlign") || false; + this.columns = params.get("Columns") || 1728; + this.rows = params.get("Rows") || 0; + var eoblock = params.get("EndOfBlock"); if (typeof eoblock == "undefined") eoblock = true; - this.black = dict.get("BlackIs1") || false; + this.eoblock = eoblock; + this.black = params.get("BlackIs1") || false; - this.codingLine = new Uint8Array(this.columns + 1); + this.codingLine = new Uint32Array(this.columns + 1); this.codingLine[0] = this.columns; this.codingPos = 0; - this.refLine = new Uint8Array(this.columns + 2); + this.refLine = new Uint32Array(this.columns + 2); this.row = 0; this.nextLine2D = this.encoding < 0; this.inputBits = 0; this.inputBuf; this.outputBits = 0; this.buf = EOF; - this.err = false; var code1; while ((code1 = this.lookBits(12)) == 0) { @@ -1352,10 +1353,8 @@ var CCITTFaxStream = (function() { while (!this.eof) { var c = this.lookChar(); this.buf = EOF; - if (this.bufferLength <= this.pos) - this.ensureBuffer(this.pos + 1); - - this.buffer[this.pos++] = c; + this.ensureBuffer(this.bufferLength + 1); + this.buffer[this.bufferLength++] = c; } }; constructor.prototype.addPixels = function(a1, blackPixels) { @@ -1419,6 +1418,8 @@ var CCITTFaxStream = (function() { if (this.eof) return; + this.err = false; + if (this.nextLine2D) { for (var i = 0; codingLine[i] < columns; ++i) refLine[i] = codingLine[i]; @@ -1431,7 +1432,7 @@ var CCITTFaxStream = (function() { blackPixels = 0; while (codingLine[this.codingPos] < columns) { - var code1 = this.getTwoDumCode(); + var code1 = this.getTwoDimCode(); switch (code1) { case twoDimPass: this.addPixels(refLine[refPos + 1], blackPixels); @@ -1530,7 +1531,7 @@ var CCITTFaxStream = (function() { else ++refPos; while (refLine[refPos] <= codingLine[this.codingPos] && - refLine[redPos] < columns) + refLine[refPos] < columns) refPos += 2; } break; @@ -2466,7 +2467,7 @@ var Parser = (function() { return new Ascii85Stream(stream); } else if (name == "CCITTFaxDecode") { TODO("implement fax stream"); - return new CCITTFaxStream(stream); + return new CCITTFaxStream(stream, params); } else { error("filter '" + name + "' not supported yet"); } @@ -4272,12 +4273,14 @@ var CanvasGraphics = (function() { tmpCanvas.width = w; tmpCanvas.height = h; var tmpCtx = tmpCanvas.getContext("2d"); + tmpCtx.fillStyle = "#000"; + tmpCtx.fillRect(0, 0, w, h); var imgData = tmpCtx.getImageData(0, 0, w, h); var pixels = imgData.data; var mask = 128; var b = 0; - for (var i = 0; i < length; i += 4) { + for (var i = 0, length = imgArray.length*8*4; i < length; i += 4) { mask <<= 1; if (mask >= 256) { b = imgArray[imgIdx++]; @@ -4285,11 +4288,19 @@ var CanvasGraphics = (function() { } var p = b & mask; + if (p > 0) + p = 1; + else + p = 0; pixels[i] = 255 * p; pixels[i+1] = 255 * p; pixels[i+2] = 255 * p; pixels[i+3] = 255; } + + tmpCtx.putImageData(imgData, 0, 0); + ctx.drawImage(tmpCanvas, 0, -h); + this.restore(); return; } From 363a631356f3c68fd497d44b7b5f7124aba40180 Mon Sep 17 00:00:00 2001 From: sbarman Date: Fri, 24 Jun 2011 13:59:37 -0700 Subject: [PATCH 07/13] working version of faxstream --- pdf.js | 77 ++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 43 insertions(+), 34 deletions(-) diff --git a/pdf.js b/pdf.js index 0be4abc7c..7b0889485 100644 --- a/pdf.js +++ b/pdf.js @@ -1322,10 +1322,11 @@ var CCITTFaxStream = (function() { this.black = params.get("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.refLine = new Uint32Array(this.columns + 2); this.row = 0; this.nextLine2D = this.encoding < 0; this.inputBits = 0; @@ -1350,12 +1351,20 @@ var CCITTFaxStream = (function() { constructor.prototype = Object.create(DecodeStream.prototype); constructor.prototype.readBlock = function() { + var vals = []; while (!this.eof) { var c = this.lookChar(); + vals.push(c); this.buf = EOF; this.ensureBuffer(this.bufferLength + 1); this.buffer[this.bufferLength++] = c; } + var all = ""; + for (var i = 0; i < vals.length; ++i) + all += vals[i] + " \n"; + + var text = document.getElementById("text"); + text.value = all; }; constructor.prototype.addPixels = function(a1, blackPixels) { var codingLine = this.codingLine; @@ -1412,8 +1421,6 @@ var CCITTFaxStream = (function() { if (this.buf != EOF) return buf; - this.err = false; - if (this.outputBits == 0) { if (this.eof) return; @@ -1494,7 +1501,7 @@ var CCITTFaxStream = (function() { blackPixels ^= 1; if (codingLine[this.codingPos] < columns) { ++refPos; - while (refLine[refPos] < codingLine[this.codingPos] && + while (refLine[refPos] <= codingLine[this.codingPos] && refLine[refPos] < columns) refPos += 2; } @@ -1535,7 +1542,6 @@ var CCITTFaxStream = (function() { refPos += 2; } break; - case twoDimVertL1: this.addPixelsNeg(refLine[refPos] - 1, blackPixels); blackPixels ^= 1; @@ -1556,7 +1562,7 @@ var CCITTFaxStream = (function() { break; default: warn("bad 2d code"); - addPixels(columns, 0); + this.addPixels(columns, 0); this.err = true; break; } @@ -1586,7 +1592,7 @@ var CCITTFaxStream = (function() { var gotEOL = false; - if (this.eoblock && this.row == this.rows - 1) { + if (!this.eoblock && this.row == this.rows - 1) { this.eof = true; } else { code1 = this.lookBits(12); @@ -1610,7 +1616,7 @@ var CCITTFaxStream = (function() { if (this.eoblock && gotEOL) { code1 = this.lookBits(12); if (code1 == 1) { - eatBits(12); + this.eatBits(12); if (this.encoding > 0) { this.lookBits(1); this.eatBits(1); @@ -1619,7 +1625,7 @@ var CCITTFaxStream = (function() { for (var i = 0; i < 4; ++i) { code1 = this.lookBits(12); if (code1 != 1) - error("bad rtc code"); + warning("bad rtc code"); this.eatBits(12); if (this.encoding > 0) { this.lookBits(1); @@ -1629,7 +1635,7 @@ var CCITTFaxStream = (function() { } this.eof = true; } - } else if (this.err && this.eolin) { + } else if (this.err && this.eoline) { var code1; while (true) { code1 = this.lookBits(13); @@ -1645,7 +1651,7 @@ var CCITTFaxStream = (function() { this.eatBits(12); if (this.encoding > 0) { this.eatBits(1); - this.nextLine2 = !(code1 & 1); + this.nextLine2D = !(code1 & 1); } } @@ -1664,32 +1670,32 @@ var CCITTFaxStream = (function() { this.outputBits = codingLine[this.codingPos] - codingLine[this.codingPos - 1]; } } else { - this.bits = 8; + var bits = 8; this.buf = 0; do { - if (this.outputBits > this.bits) { - this.buf << this.bits; + if (this.outputBits > bits) { + this.buf <<= bits; if (!(this.codingPos & 1)) { - this.buf |= 0xFF >> (8 - this.bits); + this.buf |= 0xFF >> (8 - bits); } - this.outputBits -= this.bits; - this.bits = 0; + this.outputBits -= bits; + bits = 0; } else { this.buf <<= this.outputBits; if (!(this.codingPos & 1)) { this.buf |= 0xFF >> (8 - this.outputBits); } - this.bits -= 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 (this.bits > 0) { - this.buf <<= this.bits; - this.bits = 0; + } else if (bits > 0) { + this.buf <<= bits; + bits = 0; } } - } while (this.bits); + } while (bits); } if (this.black) { this.buf ^= 0xFF; @@ -1729,7 +1735,7 @@ var CCITTFaxStream = (function() { var n; if (this.eoblock) { code = this.lookBits(12); - if (typeof code == "undefined") + if (code == EOF) return 1; if ((code >> 5) == 0) @@ -1744,20 +1750,20 @@ var CCITTFaxStream = (function() { } else { for (var n = 1; n <= 9; ++n) { code = this.lookBits(n); - if (typeof code == "undefined") + if (code == EOF) return 1; if (n < 9) code <<= 9 - n; p = whiteTable2[code]; if (p[0] == n) { - this.eatBits(n); + this.eatBits(n); return p[0]; } } for (var n = 11; n <= 12; ++n) { code == this.lookBits(n); - if (typeof code == "undefined") + if (code == EOF) return 1; if (n < 12) code <<= 12 - n; @@ -1776,7 +1782,7 @@ var CCITTFaxStream = (function() { var code, p, n; if (this.eoblock) { code = this.lookBits(13); - if (typeof code == "undefined") + if (code == EOF) return 1; if ((code >> 7) == 0) p = blackTable1[code]; @@ -1792,7 +1798,7 @@ var CCITTFaxStream = (function() { } else { for (var n = 2; n <= 6; ++n) { code = this.lookBits(n); - if (typeof code == "undefined") + if (code == EOF) return 1; if (n < 6) code <<= 6 - n; @@ -1805,7 +1811,7 @@ var CCITTFaxStream = (function() { } for (var n = 7; n <= 12; ++n) { code = this.lookBits(n); - if (typeof code == "undefined") + if (code == EOF) return 1; if (n < 12) code <<= 12 - n; @@ -4266,6 +4272,9 @@ var CanvasGraphics = (function() { if (numComps != 1) error("worng numComps"); + ctx.strokeStyle = "#090"; + ctx.strokeRect(0 + 1, -h + 1, w / 2, h -2 ); + var imgArray = image.getBytes((numComps * w * h) >> 3); var imgIdx = 0; @@ -4278,13 +4287,13 @@ var CanvasGraphics = (function() { var imgData = tmpCtx.getImageData(0, 0, w, h); var pixels = imgData.data; - var mask = 128; + var mask = 1; var b = 0; - for (var i = 0, length = imgArray.length*8*4; i < length; i += 4) { - mask <<= 1; - if (mask >= 256) { + for (var i = 0, length = 4 * w * h; i < length; i += 4) { + mask >>= 1; + if (mask <= 0) { b = imgArray[imgIdx++]; - mask = 1; + mask = 128; } var p = b & mask; From 6df5714ec3ca465964e25263c35b180153b47d9b Mon Sep 17 00:00:00 2001 From: sbarman Date: Fri, 24 Jun 2011 14:10:25 -0700 Subject: [PATCH 08/13] removed debug code --- pdf.js | 8 -------- 1 file changed, 8 deletions(-) diff --git a/pdf.js b/pdf.js index 7b0889485..c90e35bab 100644 --- a/pdf.js +++ b/pdf.js @@ -1351,20 +1351,12 @@ var CCITTFaxStream = (function() { constructor.prototype = Object.create(DecodeStream.prototype); constructor.prototype.readBlock = function() { - var vals = []; while (!this.eof) { var c = this.lookChar(); - vals.push(c); this.buf = EOF; this.ensureBuffer(this.bufferLength + 1); this.buffer[this.bufferLength++] = c; } - var all = ""; - for (var i = 0; i < vals.length; ++i) - all += vals[i] + " \n"; - - var text = document.getElementById("text"); - text.value = all; }; constructor.prototype.addPixels = function(a1, blackPixels) { var codingLine = this.codingLine; From cde62c85bcb2ccfcc7aa518e81995e77ee26bd9d Mon Sep 17 00:00:00 2001 From: sbarman Date: Fri, 24 Jun 2011 19:07:42 -0700 Subject: [PATCH 09/13] fully working faxstreams --- pdf.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pdf.js b/pdf.js index c90e35bab..341d8fe06 100644 --- a/pdf.js +++ b/pdf.js @@ -4282,6 +4282,11 @@ var CanvasGraphics = (function() { var mask = 1; var b = 0; for (var i = 0, length = 4 * w * h; i < length; i += 4) { + if (i % (4*w) == 0) { + mask = 1; + b = 0; + } + mask >>= 1; if (mask <= 0) { b = imgArray[imgIdx++]; From d863286438d367845aed6e6b0041f1dfc7429087 Mon Sep 17 00:00:00 2001 From: sbarman Date: Fri, 24 Jun 2011 21:40:54 -0700 Subject: [PATCH 10/13] Fixed problem with dict --- pdf.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/pdf.js b/pdf.js index 180d96588..906ca4270 100644 --- a/pdf.js +++ b/pdf.js @@ -81,7 +81,7 @@ var Stream = (function() { }, getByte: function stream_getByte() { if (this.pos >= this.end) - return null; + return; return this.bytes[this.pos++]; }, // returns subarray of original buffer @@ -103,12 +103,12 @@ var Stream = (function() { }, lookChar: function stream_lookChar() { if (this.pos >= this.end) - return null; + return; return String.fromCharCode(this.bytes[this.pos]); }, getChar: function stream_getChar() { if (this.pos >= this.end) - return null; + return ; return String.fromCharCode(this.bytes[this.pos++]); }, skip: function stream_skip(n) { @@ -171,7 +171,7 @@ var DecodeStream = (function() { var pos = this.pos; while (this.bufferLength <= pos) { if (this.eof) - return null; + return ; this.readBlock(); } return this.buffer[this.pos++]; @@ -203,7 +203,7 @@ var DecodeStream = (function() { var pos = this.pos; while (this.bufferLength <= pos) { if (this.eof) - return null; + return ; this.readBlock(); } return String.fromCharCode(this.buffer[this.pos]); @@ -212,7 +212,7 @@ var DecodeStream = (function() { var pos = this.pos; while (this.bufferLength <= pos) { if (this.eof) - return null; + return ; this.readBlock(); } return String.fromCharCode(this.buffer[this.pos++]); @@ -1348,7 +1348,7 @@ var CCITTFaxStream = (function() { this.columns = params.get("Columns") || 1728; this.rows = params.get("Rows") || 0; var eoblock = params.get("EndOfBlock"); - if (typeof eoblock == "undefined") + if (eoblock == null) eoblock = true; this.eoblock = eoblock; this.black = params.get("BlackIs1") || false; From 12ab3d733834a40d6f6cba106d7495780f528e18 Mon Sep 17 00:00:00 2001 From: sbarman Date: Fri, 24 Jun 2011 21:42:22 -0700 Subject: [PATCH 11/13] changed undefined to null check --- pdf.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pdf.js b/pdf.js index 906ca4270..7f81cbe07 100644 --- a/pdf.js +++ b/pdf.js @@ -1867,7 +1867,7 @@ var CCITTFaxStream = (function() { constructor.prototype.lookBits = function(n) { var c; while (this.inputBits < n) { - if (typeof (c = this.str.getByte()) == "undefined") { + if ((c = this.str.getByte()) == null) { if (this.inputBits == 0) return EOF; return (this.inputBuf << (n - this.inputBits)) From c2dce23025ecf9a55103011af5d583f9bd36e1d9 Mon Sep 17 00:00:00 2001 From: sbarman Date: Sat, 25 Jun 2011 22:52:41 -0700 Subject: [PATCH 12/13] Removed bpc=1 code --- pdf.js | 61 ++-------------------------------------------------------- 1 file changed, 2 insertions(+), 59 deletions(-) diff --git a/pdf.js b/pdf.js index 7f81cbe07..d9adda494 100644 --- a/pdf.js +++ b/pdf.js @@ -4,7 +4,7 @@ "use strict"; var ERRORS = 0, WARNINGS = 1, TODOS = 5; -var verbosity = ERRORS; +var verbosity = WARNINGS; function log(msg) { if (console && console.log) @@ -4273,65 +4273,8 @@ var CanvasGraphics = (function() { } } - if (bitsPerComponent == 1) { - var xref = this.xref; - var csStream = dict.get2("ColorSpace", "CS"); - csStream = xref.fetchIfRef(csStream); - if (IsName(csStream) && inline) - csStream = colorSpaces.get(csStream); - var colorSpace = new ColorSpace(xref, csStream); - var numComps = colorSpace.numComps; - if (numComps != 1) - error("worng numComps"); - - ctx.strokeStyle = "#090"; - ctx.strokeRect(0 + 1, -h + 1, w / 2, h -2 ); - - var imgArray = image.getBytes((numComps * w * h) >> 3); - var imgIdx = 0; - - var tmpCanvas = document.createElement("canvas"); - tmpCanvas.width = w; - tmpCanvas.height = h; - var tmpCtx = tmpCanvas.getContext("2d"); - tmpCtx.fillStyle = "#000"; - tmpCtx.fillRect(0, 0, w, h); - var imgData = tmpCtx.getImageData(0, 0, w, h); - var pixels = imgData.data; - - var mask = 1; - var b = 0; - for (var i = 0, length = 4 * w * h; i < length; i += 4) { - if (i % (4*w) == 0) { - mask = 1; - b = 0; - } - - mask >>= 1; - if (mask <= 0) { - b = imgArray[imgIdx++]; - mask = 128; - } - - var p = b & mask; - if (p > 0) - p = 1; - else - p = 0; - pixels[i] = 255 * p; - pixels[i+1] = 255 * p; - pixels[i+2] = 255 * p; - pixels[i+3] = 255; - } - - tmpCtx.putImageData(imgData, 0, 0); - ctx.drawImage(tmpCanvas, 0, -h); - this.restore(); - return; - } - if (bitsPerComponent !== 8) - error("Unsupported bpc"); + TODO("Support bpc="+ bitsPerComponent); var xref = this.xref; var colorSpaces = this.colorSpaces; From d29e666cbfb94a29d9bda05ea3a5bc976135bf8d Mon Sep 17 00:00:00 2001 From: sbarman Date: Sat, 25 Jun 2011 22:56:51 -0700 Subject: [PATCH 13/13] clean up code to before --- pdf.js | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/pdf.js b/pdf.js index d9adda494..db13acfee 100644 --- a/pdf.js +++ b/pdf.js @@ -81,7 +81,7 @@ var Stream = (function() { }, getByte: function stream_getByte() { if (this.pos >= this.end) - return; + return null; return this.bytes[this.pos++]; }, // returns subarray of original buffer @@ -103,12 +103,12 @@ var Stream = (function() { }, lookChar: function stream_lookChar() { if (this.pos >= this.end) - return; + return null; return String.fromCharCode(this.bytes[this.pos]); }, getChar: function stream_getChar() { if (this.pos >= this.end) - return ; + return null; return String.fromCharCode(this.bytes[this.pos++]); }, skip: function stream_skip(n) { @@ -171,7 +171,7 @@ var DecodeStream = (function() { var pos = this.pos; while (this.bufferLength <= pos) { if (this.eof) - return ; + return null; this.readBlock(); } return this.buffer[this.pos++]; @@ -203,7 +203,7 @@ var DecodeStream = (function() { var pos = this.pos; while (this.bufferLength <= pos) { if (this.eof) - return ; + return null; this.readBlock(); } return String.fromCharCode(this.buffer[this.pos]); @@ -212,7 +212,7 @@ var DecodeStream = (function() { var pos = this.pos; while (this.bufferLength <= pos) { if (this.eof) - return ; + return null; this.readBlock(); } return String.fromCharCode(this.buffer[this.pos++]); @@ -4273,8 +4273,11 @@ var CanvasGraphics = (function() { } } - if (bitsPerComponent !== 8) + if (bitsPerComponent !== 8) { TODO("Support bpc="+ bitsPerComponent); + this.restore(); + return; + } var xref = this.xref; var colorSpaces = this.colorSpaces;