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)

239 lines 7.97 kB
import { LibError } from '../error/lib-error.js'; import { ArrayUtils } from './array-utils.js'; import { BitUtils } from './bit-utils.js'; import { StringUtils } from './string-utils.js'; export class InputBuffer { set buffer(v) { this._buffer = v; } get buffer() { return this._buffer; } set bigEndian(v) { this._bigEndian = v; } get bigEndian() { return this._bigEndian; } set offset(v) { this._offset = v; } get offset() { return this._offset; } get start() { return this._start; } get end() { return this._end; } get position() { return this._offset - this._start; } get length() { return this._end - this._offset; } get isEOS() { return this._offset >= this._end; } constructor(opt) { var _a, _b; this._buffer = opt.buffer; this._bigEndian = (_a = opt.bigEndian) !== null && _a !== void 0 ? _a : false; this._offset = (_b = opt.offset) !== null && _b !== void 0 ? _b : 0; this._start = this._offset; this._end = Math.min(this._buffer.length, opt.length === undefined ? this._buffer.length : this._offset + opt.length); } static from(other, offset, length) { const offsetFromOther = offset !== null && offset !== void 0 ? offset : 0; const result = new InputBuffer({ buffer: other._buffer, bigEndian: other._bigEndian, offset: other._offset + offsetFromOther, length: length, }); result._start = other._start; result._end = Math.min(other.buffer.length, length === undefined ? other._end : other.offset + offsetFromOther + length); return result; } rewind() { this._offset = this._start; } get(index) { return this._buffer[this._offset + index]; } set(index, value) { return (this._buffer[this._offset + index] = value); } memcpy(start, length, other, offset = 0) { if (other instanceof InputBuffer) { ArrayUtils.copyRange(other.buffer, other.offset + offset, this._buffer, this.offset + start, length); } else { ArrayUtils.copyRange(other, offset, this._buffer, this.offset + start, length); } } memset(start, length, value) { this._buffer.fill(value, this._offset + start, this._offset + start + length); } subarray(count, position, offset = 0) { let pos = position !== undefined ? this._start + position : this._offset; pos += offset; return new InputBuffer({ buffer: this._buffer, bigEndian: this._bigEndian, offset: pos, length: count, }); } indexOf(value, offset = 0) { const end = this.offset + this.length; for (let i = this.offset + offset; i < end; ++i) { if (this._buffer[i] === value) { return i - this._start; } } return -1; } peek(count, offset = 0) { return this.subarray(count, undefined, offset); } skip(count) { this._offset += count; } read() { return this._buffer[this._offset++]; } readRange(count) { const bytes = this.subarray(count); this._offset += bytes.length; return bytes; } readInt8() { return BitUtils.uint8ToInt8(this.read()); } readString(length) { if (length === undefined) { const codes = []; while (!this.isEOS) { const c = this.read(); if (c === 0) { return String.fromCodePoint(...codes); } codes.push(c); } throw new LibError('EOF reached without finding string terminator.'); } const s = this.readRange(length); const bytes = s.toUint8Array(); const result = String.fromCodePoint(...bytes); return result; } readStringLine(length = 256) { const codes = []; while (!this.isEOS) { const c = this.read(); codes.push(c); if (c === 10 || codes.length >= length) { return String.fromCodePoint(...codes); } } return String.fromCodePoint(...codes); } readStringUtf8() { const codes = []; while (!this.isEOS) { const c = this.read(); if (c === 0) { const array = new Uint8Array(codes); return StringUtils.utf8Decoder.decode(array); } codes.push(c); } const array = new Uint8Array(codes); return StringUtils.utf8Decoder.decode(array); } readUint16() { const b1 = this._buffer[this._offset++] & 0xff; const b2 = this._buffer[this._offset++] & 0xff; if (this._bigEndian) { return (b1 << 8) | b2; } return (b2 << 8) | b1; } readInt16() { return BitUtils.uint16ToInt16(this.readUint16()); } readUint24() { const b1 = this._buffer[this._offset++] & 0xff; const b2 = this._buffer[this._offset++] & 0xff; const b3 = this._buffer[this._offset++] & 0xff; if (this._bigEndian) { return b3 | (b2 << 8) | (b1 << 16); } return b1 | (b2 << 8) | (b3 << 16); } readUint32() { return BitUtils.int32ToUint32(this.readInt32()); } readInt32() { const b1 = this._buffer[this._offset++] & 0xff; const b2 = this._buffer[this._offset++] & 0xff; const b3 = this._buffer[this._offset++] & 0xff; const b4 = this._buffer[this._offset++] & 0xff; return this._bigEndian ? (b1 << 24) | (b2 << 16) | (b3 << 8) | b4 : (b4 << 24) | (b3 << 16) | (b2 << 8) | b1; } readFloat32() { return BitUtils.uint32ToFloat32(this.readUint32()); } readFloat64() { return BitUtils.uint64ToFloat64(this.readUint64()); } readUint64() { const b1 = this._buffer[this._offset++] & 0xff; const b2 = this._buffer[this._offset++] & 0xff; const b3 = this._buffer[this._offset++] & 0xff; const b4 = this._buffer[this._offset++] & 0xff; const b5 = this._buffer[this._offset++] & 0xff; const b6 = this._buffer[this._offset++] & 0xff; const b7 = this._buffer[this._offset++] & 0xff; const b8 = this._buffer[this._offset++] & 0xff; if (this._bigEndian) { return (BigInt(b1 << 56) | BigInt(b2 << 48) | BigInt(b3 << 40) | BigInt(b4 << 32) | BigInt(b5 << 24) | BigInt(b6 << 16) | BigInt(b7 << 8) | BigInt(b8)); } return (BigInt(b8 << 56) | BigInt(b7 << 48) | BigInt(b6 << 40) | BigInt(b5 << 32) | BigInt(b4 << 24) | BigInt(b3 << 16) | BigInt(b2 << 8) | BigInt(b1)); } toUint8Array(offset = 0, length) { const correctedLength = length !== null && length !== void 0 ? length : this.length - offset; if (this._buffer instanceof Uint8Array) { return new Uint8Array(this._buffer.buffer, this._buffer.byteOffset + this._offset + offset, correctedLength); } return Uint8Array.from(this._buffer.subarray(this._offset + offset, this._offset + offset + correctedLength)); } toUint32Array(offset = 0) { if (this._buffer instanceof Uint8Array) { return new Uint32Array(this._buffer.buffer, this._buffer.byteOffset + this._offset + offset); } const uint8array = this.toUint8Array(); return new Uint32Array(uint8array.buffer); } } //# sourceMappingURL=input-buffer.js.map