UNPKG

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)

75 lines 2.39 kB
import { LibError } from '../../error/lib-error.js'; export class VP8LBitReader { get bitPos() { return this._bitPos; } set bitPos(v) { this._bitPos = v; } get isEOS() { return this._input.isEOS && this._bitPos >= VP8LBitReader.lBits; } constructor(input) { this._bitPos = 0; this._input = input; this._buffer = new Uint32Array(2); this._buffer8 = new Uint8Array(this._buffer.buffer); for (let i = 0; i < 8; i++) { this._buffer8[i] = this._input.read(); } } shiftBytes() { while (this._bitPos >= 8 && !this._input.isEOS) { const b = this._input.read(); this._buffer[0] = (this._buffer[0] >>> 8) + (this._buffer[1] & 0xff) * 0x1000000; this._buffer[1] >>>= 8; this._buffer[1] |= b * 0x1000000; this._bitPos -= 8; } } prefetchBits() { let b2 = 0; if (this._bitPos < 32) { b2 = (this._buffer[0] >>> this._bitPos) + (this._buffer[1] & VP8LBitReader.bitMask[this._bitPos]) * (VP8LBitReader.bitMask[32 - this._bitPos] + 1); } else if (this._bitPos === 32) { b2 = this._buffer[1]; } else { b2 = this._buffer[1] >>> (this._bitPos - 32); } return b2; } fillBitWindow() { if (this._bitPos >= VP8LBitReader.wBits) { this.shiftBytes(); } } readBits(numBits) { if (!this.isEOS && numBits < VP8LBitReader.maxNumBitRead) { const value = this.prefetchBits() & VP8LBitReader.bitMask[numBits]; this._bitPos += numBits; this.shiftBytes(); return value; } else { throw new LibError('Not enough data in input.'); } } } VP8LBitReader.valueSize = 8; VP8LBitReader.maxNumBitRead = 25; VP8LBitReader.lBits = 64; VP8LBitReader.wBits = 32; VP8LBitReader.log8WBits = 4; VP8LBitReader.bitMask = [ 0, 1, 3, 7, 15, 31, 63, 127, 255, 511, 1023, 2047, 4095, 8191, 16383, 32767, 65535, 131071, 262143, 524287, 1048575, 2097151, 4194303, 8388607, 16777215, 33554431, 67108863, 134217727, 268435455, 536870911, 1073741823, 2147483647, 4294967295, ]; //# sourceMappingURL=vp8l-bit-reader.js.map