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)
141 lines • 4.77 kB
JavaScript
import { ArrayUtils } from './array-utils.js';
export class OutputBuffer {
get buffer() {
return this._buffer;
}
get bigEndian() {
return this._bigEndian;
}
set bigEndian(v) {
this._bigEndian = v;
}
get length() {
return this._length;
}
set length(v) {
this._length = v;
}
constructor(opt) {
var _a, _b;
this._bigEndian = (_a = opt === null || opt === void 0 ? void 0 : opt.bigEndian) !== null && _a !== void 0 ? _a : false;
this._buffer = new Uint8Array((_b = opt === null || opt === void 0 ? void 0 : opt.size) !== null && _b !== void 0 ? _b : OutputBuffer._blockSize);
this._length = 0;
}
expandBuffer(required) {
let blockSize = OutputBuffer._blockSize;
if (required !== undefined) {
blockSize = required;
}
else if (this._buffer.length > 0) {
blockSize = this._buffer.length * 2;
}
const newBuffer = new Uint8Array(this._buffer.length + blockSize);
ArrayUtils.copyRange(this._buffer, 0, newBuffer, 0, this._buffer.length);
this._buffer = newBuffer;
}
rewind() {
this._length = 0;
}
clear() {
this._buffer = new Uint8Array(OutputBuffer._blockSize);
this._length = 0;
}
getBytes() {
return new Uint8Array(this._buffer.buffer, 0, this._length);
}
writeByte(value) {
if (this._length === this._buffer.length) {
this.expandBuffer();
}
this._buffer[this._length++] = value & 0xff;
}
writeBytes(bytes, length) {
const bytesLength = length !== null && length !== void 0 ? length : bytes.length;
while (this._length + bytesLength > this._buffer.length) {
this.expandBuffer(this._length + bytesLength - this._buffer.length);
}
ArrayUtils.copyRange(bytes, 0, this._buffer, this._length, bytesLength);
this._length += bytesLength;
}
writeBuffer(bytes) {
const bytesLength = bytes.length;
const requiredLength = this._length + bytesLength;
while (requiredLength > this._buffer.length) {
this.expandBuffer(requiredLength - this._buffer.length);
}
ArrayUtils.copyRange(bytes.buffer, bytes.offset, this._buffer, this._length, bytesLength);
this._length += bytesLength;
}
writeUint16(value) {
if (this._bigEndian) {
this.writeByte((value >>> 8) & 0xff);
this.writeByte(value & 0xff);
return;
}
this.writeByte(value & 0xff);
this.writeByte((value >>> 8) & 0xff);
}
writeUint32(value) {
if (this._bigEndian) {
this.writeByte((value >>> 24) & 0xff);
this.writeByte((value >>> 16) & 0xff);
this.writeByte((value >>> 8) & 0xff);
this.writeByte(value & 0xff);
return;
}
this.writeByte(value & 0xff);
this.writeByte((value >>> 8) & 0xff);
this.writeByte((value >>> 16) & 0xff);
this.writeByte((value >>> 24) & 0xff);
}
writeFloat32(value) {
const fb = new Float32Array(1);
fb[0] = value;
const b = new Uint8Array(fb.buffer);
if (this._bigEndian) {
this.writeByte(b[3]);
this.writeByte(b[2]);
this.writeByte(b[1]);
this.writeByte(b[0]);
return;
}
this.writeByte(b[0]);
this.writeByte(b[1]);
this.writeByte(b[2]);
this.writeByte(b[3]);
}
writeFloat64(value) {
const fb = new Float64Array(1);
fb[0] = value;
const b = new Uint8Array(fb.buffer);
if (this._bigEndian) {
this.writeByte(b[7]);
this.writeByte(b[6]);
this.writeByte(b[5]);
this.writeByte(b[4]);
this.writeByte(b[3]);
this.writeByte(b[2]);
this.writeByte(b[1]);
this.writeByte(b[0]);
return;
}
this.writeByte(b[0]);
this.writeByte(b[1]);
this.writeByte(b[2]);
this.writeByte(b[3]);
this.writeByte(b[4]);
this.writeByte(b[5]);
this.writeByte(b[6]);
this.writeByte(b[7]);
}
subarray(start, end) {
const correctedStart = start >= 0 ? start : this._length + start;
let correctedEnd = end !== null && end !== void 0 ? end : this._length;
if (correctedEnd < 0) {
correctedEnd = this._length + correctedEnd;
}
return new Uint8Array(this._buffer.buffer, correctedStart, correctedEnd - correctedStart);
}
}
OutputBuffer._blockSize = 0x2000;
//# sourceMappingURL=output-buffer.js.map