Move the LZWStream
from src/core/stream.js
and into its own file
This commit is contained in:
parent
d10da907da
commit
6c1a321500
156
src/core/lzw_stream.js
Normal file
156
src/core/lzw_stream.js
Normal file
@ -0,0 +1,156 @@
|
|||||||
|
/* Copyright 2012 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.
|
||||||
|
*/
|
||||||
|
/* eslint-disable no-var */
|
||||||
|
|
||||||
|
import { DecodeStream } from "./stream.js";
|
||||||
|
|
||||||
|
var LZWStream = (function LZWStreamClosure() {
|
||||||
|
// eslint-disable-next-line no-shadow
|
||||||
|
function LZWStream(str, maybeLength, earlyChange) {
|
||||||
|
this.str = str;
|
||||||
|
this.dict = str.dict;
|
||||||
|
this.cachedData = 0;
|
||||||
|
this.bitsCached = 0;
|
||||||
|
|
||||||
|
var maxLzwDictionarySize = 4096;
|
||||||
|
var lzwState = {
|
||||||
|
earlyChange,
|
||||||
|
codeLength: 9,
|
||||||
|
nextCode: 258,
|
||||||
|
dictionaryValues: new Uint8Array(maxLzwDictionarySize),
|
||||||
|
dictionaryLengths: new Uint16Array(maxLzwDictionarySize),
|
||||||
|
dictionaryPrevCodes: new Uint16Array(maxLzwDictionarySize),
|
||||||
|
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, maybeLength);
|
||||||
|
}
|
||||||
|
|
||||||
|
LZWStream.prototype = Object.create(DecodeStream.prototype);
|
||||||
|
|
||||||
|
LZWStream.prototype.readBits = function LZWStream_readBits(n) {
|
||||||
|
var bitsCached = this.bitsCached;
|
||||||
|
var cachedData = this.cachedData;
|
||||||
|
while (bitsCached < n) {
|
||||||
|
var c = this.str.getByte();
|
||||||
|
if (c === -1) {
|
||||||
|
this.eof = true;
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
cachedData = (cachedData << 8) | c;
|
||||||
|
bitsCached += 8;
|
||||||
|
}
|
||||||
|
this.bitsCached = bitsCached -= n;
|
||||||
|
this.cachedData = cachedData;
|
||||||
|
this.lastCode = null;
|
||||||
|
return (cachedData >>> bitsCached) & ((1 << n) - 1);
|
||||||
|
};
|
||||||
|
|
||||||
|
LZWStream.prototype.readBlock = function LZWStream_readBlock() {
|
||||||
|
var blockSize = 512;
|
||||||
|
var estimatedDecodedSize = blockSize * 2,
|
||||||
|
decodedSizeDelta = blockSize;
|
||||||
|
var i, j, q;
|
||||||
|
|
||||||
|
var lzwState = this.lzwState;
|
||||||
|
if (!lzwState) {
|
||||||
|
return; // eof was found
|
||||||
|
}
|
||||||
|
|
||||||
|
var earlyChange = lzwState.earlyChange;
|
||||||
|
var nextCode = lzwState.nextCode;
|
||||||
|
var dictionaryValues = lzwState.dictionaryValues;
|
||||||
|
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++) {
|
||||||
|
var code = this.readBits(codeLength);
|
||||||
|
var hasPrev = currentSequenceLength > 0;
|
||||||
|
if (code < 256) {
|
||||||
|
currentSequence[0] = code;
|
||||||
|
currentSequenceLength = 1;
|
||||||
|
} else if (code >= 258) {
|
||||||
|
if (code < nextCode) {
|
||||||
|
currentSequenceLength = dictionaryLengths[code];
|
||||||
|
for (j = currentSequenceLength - 1, q = code; j >= 0; j--) {
|
||||||
|
currentSequence[j] = dictionaryValues[q];
|
||||||
|
q = dictionaryPrevCodes[q];
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
currentSequence[currentSequenceLength++] = currentSequence[0];
|
||||||
|
}
|
||||||
|
} else if (code === 256) {
|
||||||
|
codeLength = 9;
|
||||||
|
nextCode = 258;
|
||||||
|
currentSequenceLength = 0;
|
||||||
|
continue;
|
||||||
|
} else {
|
||||||
|
this.eof = true;
|
||||||
|
delete this.lzwState;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (hasPrev) {
|
||||||
|
dictionaryPrevCodes[nextCode] = prevCode;
|
||||||
|
dictionaryLengths[nextCode] = dictionaryLengths[prevCode] + 1;
|
||||||
|
dictionaryValues[nextCode] = currentSequence[0];
|
||||||
|
nextCode++;
|
||||||
|
codeLength =
|
||||||
|
(nextCode + earlyChange) & (nextCode + earlyChange - 1)
|
||||||
|
? codeLength
|
||||||
|
: Math.min(
|
||||||
|
Math.log(nextCode + earlyChange) / 0.6931471805599453 + 1,
|
||||||
|
12
|
||||||
|
) | 0;
|
||||||
|
}
|
||||||
|
prevCode = code;
|
||||||
|
|
||||||
|
decodedLength += currentSequenceLength;
|
||||||
|
if (estimatedDecodedSize < decodedLength) {
|
||||||
|
do {
|
||||||
|
estimatedDecodedSize += decodedSizeDelta;
|
||||||
|
} while (estimatedDecodedSize < decodedLength);
|
||||||
|
buffer = this.ensureBuffer(this.bufferLength + estimatedDecodedSize);
|
||||||
|
}
|
||||||
|
for (j = 0; j < currentSequenceLength; j++) {
|
||||||
|
buffer[currentBufferLength++] = currentSequence[j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
lzwState.nextCode = nextCode;
|
||||||
|
lzwState.codeLength = codeLength;
|
||||||
|
lzwState.prevCode = prevCode;
|
||||||
|
lzwState.currentSequenceLength = currentSequenceLength;
|
||||||
|
|
||||||
|
this.bufferLength = currentBufferLength;
|
||||||
|
};
|
||||||
|
|
||||||
|
return LZWStream;
|
||||||
|
})();
|
||||||
|
|
||||||
|
export { LZWStream };
|
@ -17,7 +17,6 @@ import {
|
|||||||
Ascii85Stream,
|
Ascii85Stream,
|
||||||
AsciiHexStream,
|
AsciiHexStream,
|
||||||
FlateStream,
|
FlateStream,
|
||||||
LZWStream,
|
|
||||||
NullStream,
|
NullStream,
|
||||||
PredictorStream,
|
PredictorStream,
|
||||||
RunLengthStream,
|
RunLengthStream,
|
||||||
@ -47,6 +46,7 @@ import { CCITTFaxStream } from "./ccitt_stream.js";
|
|||||||
import { Jbig2Stream } from "./jbig2_stream.js";
|
import { Jbig2Stream } from "./jbig2_stream.js";
|
||||||
import { JpegStream } from "./jpeg_stream.js";
|
import { JpegStream } from "./jpeg_stream.js";
|
||||||
import { JpxStream } from "./jpx_stream.js";
|
import { JpxStream } from "./jpx_stream.js";
|
||||||
|
import { LZWStream } from "./lzw_stream.js";
|
||||||
|
|
||||||
const MAX_LENGTH_TO_CACHE = 1000;
|
const MAX_LENGTH_TO_CACHE = 1000;
|
||||||
const MAX_ADLER32_LENGTH = 5552;
|
const MAX_ADLER32_LENGTH = 5552;
|
||||||
|
@ -1196,143 +1196,6 @@ var RunLengthStream = (function RunLengthStreamClosure() {
|
|||||||
return RunLengthStream;
|
return RunLengthStream;
|
||||||
})();
|
})();
|
||||||
|
|
||||||
var LZWStream = (function LZWStreamClosure() {
|
|
||||||
// eslint-disable-next-line no-shadow
|
|
||||||
function LZWStream(str, maybeLength, earlyChange) {
|
|
||||||
this.str = str;
|
|
||||||
this.dict = str.dict;
|
|
||||||
this.cachedData = 0;
|
|
||||||
this.bitsCached = 0;
|
|
||||||
|
|
||||||
var maxLzwDictionarySize = 4096;
|
|
||||||
var lzwState = {
|
|
||||||
earlyChange,
|
|
||||||
codeLength: 9,
|
|
||||||
nextCode: 258,
|
|
||||||
dictionaryValues: new Uint8Array(maxLzwDictionarySize),
|
|
||||||
dictionaryLengths: new Uint16Array(maxLzwDictionarySize),
|
|
||||||
dictionaryPrevCodes: new Uint16Array(maxLzwDictionarySize),
|
|
||||||
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, maybeLength);
|
|
||||||
}
|
|
||||||
|
|
||||||
LZWStream.prototype = Object.create(DecodeStream.prototype);
|
|
||||||
|
|
||||||
LZWStream.prototype.readBits = function LZWStream_readBits(n) {
|
|
||||||
var bitsCached = this.bitsCached;
|
|
||||||
var cachedData = this.cachedData;
|
|
||||||
while (bitsCached < n) {
|
|
||||||
var c = this.str.getByte();
|
|
||||||
if (c === -1) {
|
|
||||||
this.eof = true;
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
cachedData = (cachedData << 8) | c;
|
|
||||||
bitsCached += 8;
|
|
||||||
}
|
|
||||||
this.bitsCached = bitsCached -= n;
|
|
||||||
this.cachedData = cachedData;
|
|
||||||
this.lastCode = null;
|
|
||||||
return (cachedData >>> bitsCached) & ((1 << n) - 1);
|
|
||||||
};
|
|
||||||
|
|
||||||
LZWStream.prototype.readBlock = function LZWStream_readBlock() {
|
|
||||||
var blockSize = 512;
|
|
||||||
var estimatedDecodedSize = blockSize * 2,
|
|
||||||
decodedSizeDelta = blockSize;
|
|
||||||
var i, j, q;
|
|
||||||
|
|
||||||
var lzwState = this.lzwState;
|
|
||||||
if (!lzwState) {
|
|
||||||
return; // eof was found
|
|
||||||
}
|
|
||||||
|
|
||||||
var earlyChange = lzwState.earlyChange;
|
|
||||||
var nextCode = lzwState.nextCode;
|
|
||||||
var dictionaryValues = lzwState.dictionaryValues;
|
|
||||||
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++) {
|
|
||||||
var code = this.readBits(codeLength);
|
|
||||||
var hasPrev = currentSequenceLength > 0;
|
|
||||||
if (code < 256) {
|
|
||||||
currentSequence[0] = code;
|
|
||||||
currentSequenceLength = 1;
|
|
||||||
} else if (code >= 258) {
|
|
||||||
if (code < nextCode) {
|
|
||||||
currentSequenceLength = dictionaryLengths[code];
|
|
||||||
for (j = currentSequenceLength - 1, q = code; j >= 0; j--) {
|
|
||||||
currentSequence[j] = dictionaryValues[q];
|
|
||||||
q = dictionaryPrevCodes[q];
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
currentSequence[currentSequenceLength++] = currentSequence[0];
|
|
||||||
}
|
|
||||||
} else if (code === 256) {
|
|
||||||
codeLength = 9;
|
|
||||||
nextCode = 258;
|
|
||||||
currentSequenceLength = 0;
|
|
||||||
continue;
|
|
||||||
} else {
|
|
||||||
this.eof = true;
|
|
||||||
delete this.lzwState;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (hasPrev) {
|
|
||||||
dictionaryPrevCodes[nextCode] = prevCode;
|
|
||||||
dictionaryLengths[nextCode] = dictionaryLengths[prevCode] + 1;
|
|
||||||
dictionaryValues[nextCode] = currentSequence[0];
|
|
||||||
nextCode++;
|
|
||||||
codeLength =
|
|
||||||
(nextCode + earlyChange) & (nextCode + earlyChange - 1)
|
|
||||||
? codeLength
|
|
||||||
: Math.min(
|
|
||||||
Math.log(nextCode + earlyChange) / 0.6931471805599453 + 1,
|
|
||||||
12
|
|
||||||
) | 0;
|
|
||||||
}
|
|
||||||
prevCode = code;
|
|
||||||
|
|
||||||
decodedLength += currentSequenceLength;
|
|
||||||
if (estimatedDecodedSize < decodedLength) {
|
|
||||||
do {
|
|
||||||
estimatedDecodedSize += decodedSizeDelta;
|
|
||||||
} while (estimatedDecodedSize < decodedLength);
|
|
||||||
buffer = this.ensureBuffer(this.bufferLength + estimatedDecodedSize);
|
|
||||||
}
|
|
||||||
for (j = 0; j < currentSequenceLength; j++) {
|
|
||||||
buffer[currentBufferLength++] = currentSequence[j];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
lzwState.nextCode = nextCode;
|
|
||||||
lzwState.codeLength = codeLength;
|
|
||||||
lzwState.prevCode = prevCode;
|
|
||||||
lzwState.currentSequenceLength = currentSequenceLength;
|
|
||||||
|
|
||||||
this.bufferLength = currentBufferLength;
|
|
||||||
};
|
|
||||||
|
|
||||||
return LZWStream;
|
|
||||||
})();
|
|
||||||
|
|
||||||
var NullStream = (function NullStreamClosure() {
|
var NullStream = (function NullStreamClosure() {
|
||||||
// eslint-disable-next-line no-shadow
|
// eslint-disable-next-line no-shadow
|
||||||
function NullStream() {
|
function NullStream() {
|
||||||
@ -1350,7 +1213,6 @@ export {
|
|||||||
DecodeStream,
|
DecodeStream,
|
||||||
DecryptStream,
|
DecryptStream,
|
||||||
FlateStream,
|
FlateStream,
|
||||||
LZWStream,
|
|
||||||
NullStream,
|
NullStream,
|
||||||
PredictorStream,
|
PredictorStream,
|
||||||
RunLengthStream,
|
RunLengthStream,
|
||||||
|
Loading…
Reference in New Issue
Block a user