Cleaning up FaxStream

This commit is contained in:
sbarman 2011-06-22 18:56:33 -07:00
parent 8c3ebb8d9f
commit 325170a5fc

199
pdf.js
View File

@ -839,7 +839,7 @@ var Ascii85Stream = (function() {
return constructor; return constructor;
})(); })();
var CcittFaxStream = (function() { var CCITTFaxStream = (function() {
const ccittEOL = -2; const ccittEOL = -2;
const twoDimPass = 0; const twoDimPass = 0;
@ -1284,12 +1284,15 @@ var CcittFaxStream = (function() {
this.codingLine = new Uint8Array(this.columns + 1); this.codingLine = new Uint8Array(this.columns + 1);
this.codingLine[0] = this.columns; this.codingLine[0] = this.columns;
this.a0i = 0;
this.refLine = new Uint8Array(this.columns + 2); this.refLine = new Uint8Array(this.columns + 2);
this.row = 0; this.row = 0;
this.nextLine2D = this.encoding < 0; this.nextLine2D = this.encoding < 0;
this.inputBits = 0; this.inputBits = 0;
this.a0i = 0; this.inputBuf;
this.outputBits = 0; this.outputBits = 0;
this.buf = EOF;
var code1; var code1;
while ((code1 = this.lookBits(12)) == 0) { while ((code1 = this.lookBits(12)) == 0) {
@ -1310,57 +1313,81 @@ var CcittFaxStream = (function() {
constructor.prototype.readBlock = function() { constructor.prototype.readBlock = function() {
}; };
constructor.prototype.addPixels(a1, blackPixels) { 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) if (a1 > this.columns)
error("row is wrong length"); error("row is wrong length");
if (this.a0i & 1) ^ this.blackPixels) if (a0i & 1) ^ blackPixels) {
this.a0i++; ++a0i;
this.a0i = a0i;
}
this.codingLine[this.a0i] = a1; codingLine[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) { 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 refLine = this.refLine;
var codingLine = this.codingLine; var codingLine = this.codingLine;
var columns = this.columns; 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) { if (this.nextLine2D) {
for (var i = 0; codingLine[i] < columns; ++i) for (var i = 0; codingLine[i] < columns; ++i)
refLine[i] = codeLine[i]; refLine[i] = codingLine[i];
refLine[i++] = columns; refLine[i++] = columns;
refLine[i] = columns; refLine[i] = columns;
codingLine[0] = 0; codingLine[0] = 0;
this.a0i = 0; a0i = 0;
this.bl1 = 0; bl1 = 0;
this.blackPixels = 0; blackPixels = 0;
while (codingLine[this.a0i] < columns) { while (codingLine[a0i] < columns) {
var code1 = this.getTwoDumCode(); var code1 = this.getTwoDumCode();
switch (code1) { switch (code1) {
case twoDimPass: case twoDimPass:
this.addPixels(refLine[this.bli + 1], this.blackPixels); this.addPixels(refLine[b1i + 1], blackPixels);
if (refLine[this.bli + 1] < columns) a0i = this.a0i;
this.bli += 2; if (refLine[bli + 1] < columns)
bli += 2;
break; break;
case twoDimHoriz: case twoDimHoriz:
var code1 = 0, code2 = 0; var code1 = 0, code2 = 0;
if (this.blackPixels) { if (blackPixels) {
var code3; var code3;
do { do {
code1 += (code3 = this.getBlackCode()); code1 += (code3 = this.getBlackCode());
@ -1371,75 +1398,99 @@ var CcittFaxStream = (function() {
else { else {
var code3; var code3;
do { do {
code1 += (code3 = getWhiteCode()); code1 += (code3 = this.getWhiteCode());
} while (code3 >= 64); } while (code3 >= 64);
this.addPixels(codeLine[this.a0i + code1, this.blackPixels); do {
if (codeLine[a0i] < columns) { code2 += (code3 = this.getBlackCode());
addPixels(codeLine[a0i] + code2, this.blackPixels ^ 1); } while (code3 >= 64);
} }
while (refLine[this.bli] <= codingLine[a0i] this.addPixels(codingLine[a0i] + code1, blackPixels);
&& refLine[this.bli] < columns) { a0i = this.a0i;
this.bli += 2; 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; break;
case twoDimVertR2: case twoDimVertR2:
this.addPixels(this.refLine[this.bli], blackPixels); this.addPixels(refLine[b1i] + 2, blackPixels);
a0i = this.a0i;
blackPixels ^= 1; blackPixels ^= 1;
if (codeLine[this.a01] < columns) { if (codingLine[a01] < columns) {
this.bli++; ++bli;
while (refLine[this.bli] <= codeLine[this.a0i] && while (refLine[bli] <= codingLine[a0i] &&
refLine[bli] < columns) { refLine[bli] < columns) {
this.bli += 2; bli += 2;
} }
} }
break; break;
case twoDimVertR1: case twoDimVertR1:
this.addPixels(refLine[this.bli] + 1, blackPixels); this.addPixels(refLine[bli] + 1, blackPixels);
this.blackPixels ^= 1; a0i = this.a0i;
if (codeLine[this.a01] < columns) { blackPixels ^= 1;
this.bli++; if (codingLine[a01] < columns) {
while (refLine[this.bli] < codingLine[this.a0i] && ++bli;
refLine[this.bli] < columns) while (refLine[bli] < codingLine[a0i] &&
refLine[bli] < columns)
this.bli += 2; this.bli += 2;
} }
break; break;
case twoDimVert0: case twoDimVert0:
this.addPixels(refLine[this.bli], blackPixels); this.addPixels(refLine[bli], blackPixels);
this.blackPixels ^= 1; a0i = this.a0i;
if (codingLine[this.a0i] < columns) { blackPixels ^= 1;
this.bli++; if (codingLine[a0i] < columns) {
while (refLine[this.bli] <= codingLine[a0i] && ++bli;
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] && while (refLine[bli] <= codingLine[a0i] &&
refLine[bli] < columns) refLine[bli] < columns)
bli += 2; bli += 2;
} }
break; break;
case twoDimVertL2: case twoDimVertL3:
this.addPixelsNeg(refLine[bli] - 2, blackPixels); this.addPixelsNeg(refLine[bli] - 3, blackPixels);
a0i = this.a0i;
blackPixels ^= 1; blackPixels ^= 1;
if (codingLine[a0i] < columns) { if (codeLine[a0i] < columns) {
if (bli > 0) if (bli > 0)
--bli; --bli;
else else
++bli; ++bli;
while (refLine[bli] <= codingLine[a0i] && while (refLine[b1i] <= codingLine[a0i] &&
refLine[bl1] < columns) refLine[b1i] < columns)
bli += 2; bli += 2;
} }
break; 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: case twoDimVertL1:
this.addPixelsNeg(refLine[bli] - 1, blackPixels); this.addPixelsNeg(refLine[bli] - 1, blackPixels);
this.blackPixels ^= 1; this.blackPixels ^= 1;