image-in-browser
Version:
Package for encoding / decoding images, transforming images, applying filters, drawing primitives on images on the client side (no need for server Node.js)
46 lines • 1.41 kB
JavaScript
export class TiffBitReader {
constructor(input) {
this._bitBuffer = 0;
this._bitPosition = 0;
this._input = input;
}
readBits(numBits) {
let nBits = numBits;
if (nBits === 0) {
return 0;
}
if (this._bitPosition === 0) {
this._bitPosition = 8;
this._bitBuffer = this._input.read();
}
let value = 0;
while (nBits > this._bitPosition) {
value =
(value << this._bitPosition) +
(this._bitBuffer & TiffBitReader._bitMask[this._bitPosition]);
nBits -= this._bitPosition;
this._bitPosition = 8;
this._bitBuffer = this._input.read();
}
if (nBits > 0) {
if (this._bitPosition === 0) {
this._bitPosition = 8;
this._bitBuffer = this._input.read();
}
value =
(value << nBits) +
((this._bitBuffer >>> (this._bitPosition - nBits)) &
TiffBitReader._bitMask[nBits]);
this._bitPosition -= nBits;
}
return value;
}
readByte() {
return this.readBits(8);
}
flushByte() {
return (this._bitPosition = 0);
}
}
TiffBitReader._bitMask = [0, 1, 3, 7, 15, 31, 63, 127, 255];
//# sourceMappingURL=tiff-bit-reader.js.map