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)
36 lines • 1.11 kB
JavaScript
export class WebPBitWriter {
constructor() {
this._bytes = [];
this._currentByte = 0;
this._usedBits = 0;
}
writeBits(value, numBits) {
let _value = value;
let _numBits = numBits;
while (_numBits > 0) {
const available = 8 - this._usedBits;
const bitsToWrite = _numBits < available ? _numBits : available;
const mask = (1 << bitsToWrite) - 1;
this._currentByte |= (_value & mask) << this._usedBits;
_value >>= bitsToWrite;
_numBits -= bitsToWrite;
this._usedBits += bitsToWrite;
if (this._usedBits === 8) {
this._bytes.push(this._currentByte);
this._currentByte = 0;
this._usedBits = 0;
}
}
}
flush() {
if (this._usedBits > 0) {
this._bytes.push(this._currentByte);
this._currentByte = 0;
this._usedBits = 0;
}
}
getBytes() {
return Uint8Array.from(this._bytes);
}
}
//# sourceMappingURL=webp-bit-writer.js.map