Merge branch 'lzw' of git://github.com/notmasteryet/pdf.js.git

This commit is contained in:
notmasteryet 2011-08-14 18:22:01 -05:00
commit 03e111308f
2 changed files with 65 additions and 65 deletions

129
pdf.js
View File

@ -1989,18 +1989,23 @@ var LZWStream = (function() {
this.dict = str.dict; this.dict = str.dict;
this.cachedData = 0; this.cachedData = 0;
this.bitsCached = 0; this.bitsCached = 0;
this.earlyChange = earlyChange;
this.codeLength = 9; var maxLzwDictionarySize = 4097;
this.nextCode = 258; var lzwState = {
this.dictionary = []; earlyChange: earlyChange,
this.currentSequence = null; codeLength: 9,
for (var i = 0; i < 256; ++i) { nextCode: 258,
this.dictionary[i] = { dictionaryValues: new Uint8Array(maxLzwDictionarySize),
value: i, dictionaryLengths: new Uint16Array(maxLzwDictionarySize),
firstValue: i, dictionaryPrevCodes: new Uint16Array(maxLzwDictionarySize),
length: 1 currentSequence: new Uint8Array(maxLzwDictionarySize),
currentSequenceLength: 0
}; };
for (var i = 0; i < 256; ++i) {
lzwState.dictionaryValues[i] = i;
lzwState.dictionaryLengths[i] = 1;
} }
this.lzwState = lzwState;
DecodeStream.call(this); DecodeStream.call(this);
} }
@ -2027,34 +2032,44 @@ var LZWStream = (function() {
constructor.prototype.readBlock = function() { constructor.prototype.readBlock = function() {
var blockSize = 512; var blockSize = 512;
var decoded = []; var estimatedDecodedSize = blockSize * 2, decodedSizeDelta = blockSize;
var i, j, q; var i, j, q;
var earlyChange = this.earlyChange;
var nextCode = this.nextCode; var lzwState = this.lzwState;
var dictionary = this.dictionary; var earlyChange = lzwState.earlyChange;
var codeLength = this.codeLength; var nextCode = lzwState.nextCode;
var prevCode = this.prevCode; var dictionaryValues = lzwState.dictionaryValues;
var currentSequence = this.currentSequence; var dictionaryLengths = lzwState.dictionaryLengths;
var dictionaryPrevCodes = lzwState.dictionaryPrevCodes;
var codeLength = lzwState.codeLength;
var prevCode = lzwState.prevCode;
var currentSequence = lzwState.currentSequence;
var currentSequenceLength = lzwState.currentSequenceLength;
var decodedLength = 0;
var currentBufferLength = this.bufferLength;
var buffer = this.ensureBuffer(this.bufferLength + estimatedDecodedSize);
for (i = 0; i < blockSize; i++) { for (i = 0; i < blockSize; i++) {
var code = this.readBits(codeLength); var code = this.readBits(codeLength);
var hasPrev = !!currentSequence; var hasPrev = currentSequenceLength > 0;
if (code < 256) { if (code < 256) {
currentSequence = dictionary[code]; currentSequence[0] = code;
currentSequenceLength = 1;
} else if (code >= 258) { } else if (code >= 258) {
if (code < nextCode) { if (code < nextCode) {
currentSequence = dictionary[code]; currentSequenceLength = dictionaryLengths[code];
for (j = currentSequenceLength - 1, q = code; j >= 0; j--) {
currentSequence[j] = dictionaryValues[q];
q = dictionaryPrevCodes[q];
}
} else { } else {
currentSequence = { currentSequence[currentSequenceLength++] = currentSequence[0];
value: currentSequence.firstValue,
length: currentSequence.length + 1,
firstValue: currentSequence.firstValue,
prev: currentSequence
};
} }
} else if (code == 256) { } else if (code == 256) {
codeLength = 9; codeLength = 9;
nextCode = 258; nextCode = 258;
currentSequence = null; currentSequenceLength = 0;
continue; continue;
} else { } else {
this.eof = true; this.eof = true;
@ -2062,49 +2077,32 @@ var LZWStream = (function() {
} }
if (hasPrev) { if (hasPrev) {
var prevCodeSequence = dictionary[prevCode]; dictionaryPrevCodes[nextCode] = prevCode;
dictionary[nextCode] = { dictionaryLengths[nextCode] = dictionaryLengths[prevCode] + 1;
prev: prevCodeSequence, dictionaryValues[nextCode] = currentSequence[0];
length: prevCodeSequence.length + 1,
firstValue: prevCodeSequence.firstValue,
value: currentSequence.firstValue
};
nextCode++; nextCode++;
switch(nextCode + earlyChange) { codeLength = (nextCode + earlyChange) & (nextCode + earlyChange - 1) ?
case 512: codeLength : Math.min(Math.log(nextCode + earlyChange) /
codeLength = 10; 0.6931471805599453 + 1, 12) | 0;
break;
case 1024:
codeLength = 11;
break;
case 2048:
codeLength = 12;
break;
}
} }
prevCode = code; prevCode = code;
decoded.push(currentSequence);
}
this.nextCode = nextCode;
this.codeLength = codeLength;
this.prevCode = prevCode;
this.currentSequence = currentSequence;
var blockLength = 0; decodedLength += currentSequenceLength;
var length = decoded.length; if (estimatedDecodedSize < decodedLength) {
for (i = 0; i < length; i++) do {
blockLength += decoded[i].length; estimatedDecodedSize += decodedSizeDelta;
var bufferLength = this.bufferLength; } while (estimatedDecodedSize < decodedLength);
var buffer = this.ensureBuffer(bufferLength + blockLength); buffer = this.ensureBuffer(this.bufferLength + estimatedDecodedSize);
for (i = 0; i < length; i++) {
var p = decoded[i];
for (q = p.length - 1; q >= 0; q--) {
buffer[bufferLength + q] = p.value;
p = p.prev;
} }
bufferLength += decoded[i].length; for (j = 0; j < currentSequenceLength; j++)
buffer[currentBufferLength++] = currentSequence[j];
} }
this.bufferLength = bufferLength; lzwState.nextCode = nextCode;
lzwState.codeLength = codeLength;
lzwState.prevCode = prevCode;
lzwState.currentSequenceLength = currentSequenceLength;
this.bufferLength = currentBufferLength;
}; };
return constructor; return constructor;
@ -2752,7 +2750,8 @@ var Parser = (function() {
if (params) { if (params) {
if (params.has('EarlyChange')) if (params.has('EarlyChange'))
earlyChange = params.get('EarlyChange'); earlyChange = params.get('EarlyChange');
return new PredictorStream(new LZWStream(stream, earlyChange), params); return new PredictorStream(
new LZWStream(stream, earlyChange), params);
} }
return new LZWStream(stream, earlyChange); return new LZWStream(stream, earlyChange);
} else if (name == 'DCTDecode' || name == 'DCT') { } else if (name == 'DCTDecode' || name == 'DCT') {

View File

@ -40,6 +40,7 @@
// so we can use the TypedArray as well // so we can use the TypedArray as well
window.Uint32Array = TypedArray; window.Uint32Array = TypedArray;
window.Int32Array = TypedArray; window.Int32Array = TypedArray;
window.Uint16Array = TypedArray;
})(); })();
// Object.create() ? // Object.create() ?