UNPKG

@jaak.ai/file-uploader

Version:
1,636 lines (1,619 loc) 786 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); const index = require('./index-6bda10a9.js'); const utils = require('./utils-f78cd27d.js'); class MathUtils { static fract(x) { return x - Math.floor(x); } static smoothStep(edge0, edge1, x) { const t0 = (x - edge0) / (edge1 - edge0); const t = MathUtils.clamp(t0, 0, 1); return t * t * (3 - 2 * t); } static mix(x, y, a) { return x * (1 - a) + y * a; } static sign(x) { return x < 0 ? -1 : x > 0 ? 1 : 0; } static step(edge, x) { return x < edge ? 0 : 1; } static length3(x, y, z) { return Math.sqrt(x * x + y * y + z * z); } static gcd(x, y) { let _x = Math.abs(x); let _y = Math.abs(y); while (_y) { const t = _y; _y = _x % _y; _x = t; } return _x; } static clamp(num, low, high) { return Math.max(low, Math.min(num, high)); } static clampInt(num, low, high) { return Math.trunc(MathUtils.clamp(num, low, high)); } static clampInt255(num) { return Math.trunc(MathUtils.clamp(num, 0, 255)); } } class LibError extends Error { toString() { return `${this.constructor.name} (${this.message})`; } } var Format; (function (Format) { Format[Format["uint1"] = 0] = "uint1"; Format[Format["uint2"] = 1] = "uint2"; Format[Format["uint4"] = 2] = "uint4"; Format[Format["uint8"] = 3] = "uint8"; Format[Format["uint16"] = 4] = "uint16"; Format[Format["uint32"] = 5] = "uint32"; Format[Format["int8"] = 6] = "int8"; Format[Format["int16"] = 7] = "int16"; Format[Format["int32"] = 8] = "int32"; Format[Format["float16"] = 9] = "float16"; Format[Format["float32"] = 10] = "float32"; Format[Format["float64"] = 11] = "float64"; })(Format || (Format = {})); var FormatType; (function (FormatType) { FormatType[FormatType["uint"] = 0] = "uint"; FormatType[FormatType["int"] = 1] = "int"; FormatType[FormatType["float"] = 2] = "float"; })(FormatType || (FormatType = {})); const FormatSize = new Map([ [Format.uint1, 1], [Format.uint2, 1], [Format.uint4, 1], [Format.uint8, 1], [Format.uint16, 2], [Format.uint32, 4], [Format.int8, 1], [Format.int16, 2], [Format.int32, 4], [Format.float16, 2], [Format.float32, 4], [Format.float64, 8], ]); const FormatMaxValue = new Map([ [Format.uint1, 0x1], [Format.uint2, 0x3], [Format.uint4, 0xf], [Format.uint8, 0xff], [Format.uint16, 0xffff], [Format.uint32, 0xffffffff], [Format.int8, 0x7f], [Format.int16, 0x7fff], [Format.int32, 0x7fffffff], [Format.float16, 1], [Format.float32, 1], [Format.float64, 1], ]); function getRowStride(width, numChannels, format) { return format === Format.uint1 ? Math.ceil((width * numChannels) / 8) : format === Format.uint2 ? Math.ceil((width * numChannels) / 4) : format === Format.uint4 ? Math.ceil((width * numChannels) / 2) : width * numChannels * FormatSize.get(format); } function convertFormatValue(value, from, to) { if (from === to) { return value; } switch (from) { case Format.uint1: return value === 0 ? 0 : FormatMaxValue.get(to); case Format.uint2: switch (to) { case Format.uint1: return value === 0 ? 0 : 1; case Format.uint2: return value; case Format.uint4: return value * 5; case Format.uint8: return value * 75; case Format.uint16: return value * 21845; case Format.uint32: return value * 1431655765; case Format.int8: return value * 42; case Format.int16: return value * 10922; case Format.int32: return value * 715827882; case Format.float16: case Format.float32: case Format.float64: return value / 3; } break; case Format.uint4: switch (to) { case Format.uint1: return value === 0 ? 0 : 1; case Format.uint2: return Math.trunc(value) >>> 1; case Format.uint4: return value; case Format.uint8: return value * 17; case Format.uint16: return value * 4369; case Format.uint32: return value * 286331153; case Format.int8: return value * 8; case Format.int16: return value * 2184; case Format.int32: return value * 143165576; case Format.float16: case Format.float32: case Format.float64: return value / 3; } break; case Format.uint8: switch (to) { case Format.uint1: return value === 0 ? 0 : 1; case Format.uint2: return Math.trunc(value) >>> 6; case Format.uint4: return Math.trunc(value) >>> 4; case Format.uint8: return value; case Format.uint16: return value * 257; case Format.uint32: return value * 16843009; case Format.int8: return Math.trunc(value) >>> 1; case Format.int16: return value * 128; case Format.int32: return value * 8421504; case Format.float16: case Format.float32: case Format.float64: return value / 255; } break; case Format.uint16: switch (to) { case Format.uint1: return value === 0 ? 0 : 1; case Format.uint2: return Math.trunc(value) >>> 14; case Format.uint4: return Math.trunc(value) >>> 12; case Format.uint8: return Math.trunc(value) >>> 8; case Format.uint16: return value; case Format.uint32: return Math.trunc(value) << 8; case Format.int8: return Math.trunc(value) >>> 9; case Format.int16: return Math.trunc(value) >>> 1; case Format.int32: return value * 524296; case Format.float16: case Format.float32: case Format.float64: return value / 0xffff; } break; case Format.uint32: switch (to) { case Format.uint1: return value === 0 ? 0 : 1; case Format.uint2: return Math.trunc(value) >>> 30; case Format.uint4: return Math.trunc(value) >>> 28; case Format.uint8: return Math.trunc(value) >>> 24; case Format.uint16: return Math.trunc(value) >>> 16; case Format.uint32: return value; case Format.int8: return Math.trunc(value) >>> 25; case Format.int16: return Math.trunc(value) >>> 17; case Format.int32: return Math.trunc(value) >>> 1; case Format.float16: case Format.float32: case Format.float64: return value / 0xffffffff; } break; case Format.int8: switch (to) { case Format.uint1: return value === 0 ? 0 : 1; case Format.uint2: return value <= 0 ? 0 : Math.trunc(value) >>> 5; case Format.uint4: return value <= 0 ? 0 : Math.trunc(value) >>> 3; case Format.uint8: return value <= 0 ? 0 : Math.trunc(value) << 1; case Format.uint16: return value <= 0 ? 0 : Math.trunc(value) * 516; case Format.uint32: return value <= 0 ? 0 : Math.trunc(value) * 33818640; case Format.int8: return value; case Format.int16: return value * 258; case Format.int32: return value * 16909320; case Format.float16: case Format.float32: case Format.float64: return value / 127; } break; case Format.int16: switch (to) { case Format.uint1: return value === 0 ? 0 : 1; case Format.uint2: return value <= 0 ? 0 : Math.trunc(value) >>> 15; case Format.uint4: return value <= 0 ? 0 : Math.trunc(value) >>> 11; case Format.uint8: return value <= 0 ? 0 : Math.trunc(value) >>> 7; case Format.uint16: return value <= 0 ? 0 : Math.trunc(value) << 1; case Format.uint32: return value <= 0 ? 0 : Math.trunc(value) * 131076; case Format.int8: return Math.trunc(value) >>> 8; case Format.int16: return value; case Format.int32: return Math.trunc(value) * 65538; case Format.float16: case Format.float32: case Format.float64: return value / 0x7fff; } break; case Format.int32: switch (to) { case Format.uint1: return value === 0 ? 0 : 1; case Format.uint2: return value <= 0 ? 0 : Math.trunc(value) >>> 29; case Format.uint4: return value <= 0 ? 0 : Math.trunc(value) >>> 27; case Format.uint8: return value <= 0 ? 0 : Math.trunc(value) >>> 23; case Format.uint16: return value <= 0 ? 0 : Math.trunc(value) >>> 16; case Format.uint32: return value <= 0 ? 0 : Math.trunc(value) << 1; case Format.int8: return Math.trunc(value) >>> 24; case Format.int16: return Math.trunc(value) >>> 16; case Format.int32: return value; case Format.float16: case Format.float32: case Format.float64: return value / 0x7fffffff; } break; case Format.float16: case Format.float32: case Format.float64: switch (to) { case Format.uint1: return value === 0 ? 0 : 1; case Format.uint2: return Math.trunc(MathUtils.clamp(value, 0, 1) * 3); case Format.uint4: return Math.trunc(MathUtils.clamp(value, 0, 1) * 15); case Format.uint8: return Math.trunc(MathUtils.clamp(value, 0, 1) * 255); case Format.uint16: return Math.trunc(MathUtils.clamp(value, 0, 1) * 0xffff); case Format.uint32: return Math.trunc(MathUtils.clamp(value, 0, 1) * 0xffffffff); case Format.int8: return Math.trunc(value < 0 ? MathUtils.clamp(value, -1, 1) * 128 : MathUtils.clamp(value, -1, 1) * 127); case Format.int16: return Math.trunc(value < 0 ? MathUtils.clamp(value, -1, 1) * 32768 : MathUtils.clamp(value, -1, 1) * 32767); case Format.int32: return Math.trunc(value < 0 ? MathUtils.clamp(value, -1, 1) * 2147483648 : MathUtils.clamp(value, -1, 1) * 2147483647); case Format.float16: case Format.float32: case Format.float64: return value; } break; } throw new LibError('Unknown format.'); } class Rational { get numerator() { return this._numerator; } get denominator() { return this._denominator; } get toInt() { return this.denominator !== 0 ? Math.trunc(this.numerator / this.denominator) : 0; } get toDouble() { return this.denominator !== 0 ? this.numerator / this.denominator : 0; } constructor(numerator, denominator) { this._numerator = numerator; this._denominator = denominator; } simplify() { const d = MathUtils.gcd(this.numerator, this.denominator); if (d !== 0) { this._numerator = Math.trunc(this.numerator / d); this._denominator = Math.trunc(this.denominator / d); } } equals(other) { return (this._numerator === other._numerator && this._denominator === other._denominator); } toString() { return `${this.constructor.name} (${this._numerator}/${this._denominator})`; } } class ArrayUtils { static copyInt8(from, begin, end) { return Int8Array.from(from.subarray(begin, end)); } static copyUint8(from, begin, end) { return Uint8Array.from(from.subarray(begin, end)); } static copyInt16(from, begin, end) { return Int16Array.from(from.subarray(begin, end)); } static copyUint16(from, begin, end) { return Uint16Array.from(from.subarray(begin, end)); } static copyInt32(from, begin, end) { return Int32Array.from(from.subarray(begin, end)); } static copyUint32(from, begin, end) { return Uint32Array.from(from.subarray(begin, end)); } static copyFloat32(from, begin, end) { return Float32Array.from(from.subarray(begin, end)); } static copyFloat64(from, begin, end) { return Float64Array.from(from.subarray(begin, end)); } static copy(from, begin, end) { if (from instanceof Int8Array) { return ArrayUtils.copyInt8(from, begin, end); } else if (from instanceof Uint8Array) { return ArrayUtils.copyUint8(from, begin, end); } else if (from instanceof Int16Array) { return ArrayUtils.copyInt16(from, begin, end); } else if (from instanceof Uint16Array) { return ArrayUtils.copyUint16(from, begin, end); } else if (from instanceof Int32Array) { return ArrayUtils.copyInt32(from, begin, end); } else if (from instanceof Uint32Array) { return ArrayUtils.copyUint32(from, begin, end); } else if (from instanceof Float32Array) { return ArrayUtils.copyFloat32(from, begin, end); } else if (from instanceof Float64Array) { return ArrayUtils.copyFloat64(from, begin, end); } throw new LibError('Unknown array type'); } static copyRange(from, fromStart, to, toStart, length) { const viewFrom = from.subarray(fromStart, fromStart + length); to.set(viewFrom, toStart); } static fill(length, value) { const a = new Array(length); return a.fill(value); } static generate(length, func) { const a = new Array(length); for (let i = 0; i < length; ++i) { a[i] = func(i); } return a; } static equals(a1, a2) { if (a1 === a2) return true; if (a1.length !== a2.length) return false; for (let i = 0, l = a1.length; i < l; i++) { if (ArrayUtils.isNumArrayOrTypedArray(a1[i]) && ArrayUtils.isNumArrayOrTypedArray(a2[i])) { if (!ArrayUtils.equals(a1[i], a2[i])) return false; } else if (a1[i] !== a2[i]) { return false; } } return true; } static equalsRationalArray(a1, a2) { if (a1 === a2) return true; if (a1.length !== a2.length) return false; for (let i = 0, l = a1.length; i < l; i++) { if (!a1[i].equals(a2[i])) { return false; } } return true; } static getNumEnumValues(t) { return Object.values(t).filter((v) => typeof v === 'number'); } static isNumArrayOrTypedArray(obj) { return Boolean(obj && typeof obj === 'object' && ((Array.isArray(obj) && obj.every((v) => typeof v === 'number')) || (ArrayBuffer.isView(obj) && !(obj instanceof DataView)))); } static isArrayOfRational(obj) { return Boolean(obj && typeof obj === 'object' && Array.isArray(obj) && obj.every((v) => v instanceof Rational)); } } class BitUtils { static countTrailingZeroBits(v) { let c = 32; const _v = v & -v; if (_v !== 0) c--; if ((_v & 0x0000ffff) !== 0) c -= 16; if ((_v & 0x00ff00ff) !== 0) c -= 8; if ((_v & 0x0f0f0f0f) !== 0) c -= 4; if ((_v & 0x33333333) !== 0) c -= 2; if ((_v & 0x55555555) !== 0) c -= 1; return c; } static reverseByte(x) { return this._reverseByteTable[x]; } static sshR(v, n) { return v >> n; } static ushR(v, n) { return v >>> n; } static sshL(v, n) { return v << n; } static ushL(v, n) { return (v << n) >>> 0; } static uint8ToInt8(d) { this._uint8[0] = d; return this._uint8ToInt8[0]; } static int8ToUint8(d) { this._int8[0] = d; return this._int8ToUint8[0]; } static uint16ToInt16(d) { this._uint16[0] = d; return this._uint16ToInt16[0]; } static int16ToUint16(d) { this._int16[0] = d; return this._int16ToUint16[0]; } static uint32ToInt32(d) { this._uint32[0] = d; return this._uint32ToInt32[0]; } static uint32ToFloat32(d) { this._uint32[0] = d; return this._uint32ToFloat32[0]; } static uint64ToFloat64(d) { this._uint64[0] = d; return this._uint64ToFloat64[0]; } static int32ToUint32(d) { this._int32[0] = d; return this._int32ToUint32[0]; } static float32ToUint32(d) { this._float32[0] = d; return this._float32ToUint32[0]; } static debugBits32(value) { if (value === undefined) { return 'undefined'; } const bitCount = 32; let result = ''; for (let i = bitCount; i > -1; i--) { result += (value & (1 << i)) === 0 ? '0' : '1'; } return result; } } BitUtils._uint8 = new Uint8Array(1); BitUtils._uint8ToInt8 = new Int8Array(BitUtils._uint8.buffer); BitUtils._int8 = new Int8Array(1); BitUtils._int8ToUint8 = new Uint8Array(BitUtils._int8.buffer); BitUtils._uint16 = new Uint16Array(1); BitUtils._uint16ToInt16 = new Int16Array(BitUtils._uint16.buffer); BitUtils._int16 = new Int16Array(1); BitUtils._int16ToUint16 = new Uint16Array(BitUtils._int16.buffer); BitUtils._uint32 = new Uint32Array(1); BitUtils._uint32ToInt32 = new Int32Array(BitUtils._uint32.buffer); BitUtils._uint32ToFloat32 = new Float32Array(BitUtils._uint32.buffer); BitUtils._int32 = new Int32Array(1); BitUtils._int32ToUint32 = new Uint32Array(BitUtils._int32.buffer); BitUtils._float32 = new Float32Array(1); BitUtils._float32ToUint32 = new Uint32Array(BitUtils._float32.buffer); BitUtils._uint64 = new BigUint64Array(1); BitUtils._uint64ToFloat64 = new Float64Array(BitUtils._uint64.buffer); BitUtils._reverseByteTable = [ 0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0, 0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0, 0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8, 0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8, 0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4, 0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4, 0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec, 0x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc, 0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2, 0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2, 0x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0xea, 0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa, 0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6, 0x16, 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76, 0xf6, 0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee, 0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe, 0x01, 0x81, 0x41, 0xc1, 0x21, 0xa1, 0x61, 0xe1, 0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1, 0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9, 0x19, 0x99, 0x59, 0xd9, 0x39, 0xb9, 0x79, 0xf9, 0x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5, 0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5, 0x0d, 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed, 0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd, 0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3, 0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3, 0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb, 0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb, 0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7, 0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7, 0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef, 0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff, ]; class StringUtils { static getCodePoints(str) { const array = new Uint8Array(str.length); for (let i = 0; i < str.length; i++) { const codePoint = str.codePointAt(i); if (codePoint !== undefined) { if (0 <= codePoint && codePoint < 256) { array[i] = codePoint; } else { throw new LibError(`Error encoding text "${str}": unknown character code point ${codePoint}`); } } else { throw new LibError(`Error encoding text "${str}"`); } } return array; } } StringUtils.utf8Decoder = new TextDecoder('utf8'); StringUtils.latin1Decoder = new TextDecoder('latin1'); 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 = opt.length !== undefined ? this._start + opt.length : this._buffer.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 = length !== undefined ? other.offset + offsetFromOther + length : other._end; 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); } } var ChannelOrder; (function (ChannelOrder) { ChannelOrder[ChannelOrder["rgba"] = 0] = "rgba"; ChannelOrder[ChannelOrder["bgra"] = 1] = "bgra"; ChannelOrder[ChannelOrder["abgr"] = 2] = "abgr"; ChannelOrder[ChannelOrder["argb"] = 3] = "argb"; ChannelOrder[ChannelOrder["rgb"] = 4] = "rgb"; ChannelOrder[ChannelOrder["bgr"] = 5] = "bgr"; ChannelOrder[ChannelOrder["grayAlpha"] = 6] = "grayAlpha"; ChannelOrder[ChannelOrder["red"] = 7] = "red"; })(ChannelOrder || (ChannelOrder = {})); const ChannelOrderLength = new Map([ [ChannelOrder.rgba, 4], [ChannelOrder.bgra, 4], [ChannelOrder.abgr, 4], [ChannelOrder.argb, 4], [ChannelOrder.rgb, 3], [ChannelOrder.bgr, 3], [ChannelOrder.grayAlpha, 2], [ChannelOrder.red, 1], ]); var Channel; (function (Channel) { Channel[Channel["red"] = 0] = "red"; Channel[Channel["green"] = 1] = "green"; Channel[Channel["blue"] = 2] = "blue"; Channel[Channel["alpha"] = 3] = "alpha"; Channel[Channel["luminance"] = 4] = "luminance"; })(Channel || (Channel = {})); class Float16 { static get _toFloatFloat32() { return this._toFloatFloat32Data !== undefined ? this._toFloatFloat32Data : this.initialize(); } constructor(f) { this.bits = f !== undefined ? Float16.doubleToFloat16(f) : 0; } static convert(i) { const s = (i >>> 16) & 0x00008000; let e = ((i >>> 23) & 0x000000ff) - (127 - 15); let m = i & 0x007fffff; if (e <= 0) { if (e < -10) { return s; } m |= 0x00800000; const t = 14 - e; const a = (1 << (t - 1)) - 1; const b = (m >>> t) & 1; m = (m + a + b) >>> t; return s | m; } else if (e === 0xff - (127 - 15)) { if (m === 0) { return s | 0x7c00; } else { m >>>= 13; return s | 0x7c00 | m | (m === 0 ? 1 : 0); } } else { m = m + 0x00000fff + ((m >>> 13) & 1); if ((m & 0x00800000) !== 0) { m = 0; e += 1; } if (e > 30) { return s | 0x7c00; } return s | (e << 10) | (m >>> 13); } } static initialize() { if (this._toFloatFloat32Data !== undefined) { return this._toFloatFloat32Data; } const floatUint32Data = new Uint32Array(1 << 16); this._toFloatFloat32Data = new Float32Array(floatUint32Data.buffer); this._eLut = new Uint16Array(1 << 9); for (let i = 0; i < 0x100; i++) { const e = (i & 0x0ff) - (127 - 15); if (e <= 0 || e >= 30) { this._eLut[i] = 0; this._eLut[i | 0x100] = 0; } else { this._eLut[i] = e << 10; this._eLut[i | 0x100] = (e << 10) | 0x8000; } } const iMax = 1 << 16; for (let i = 0; i < iMax; i++) { floatUint32Data[i] = this.halfToFloat(i); } return this._toFloatFloat32Data; } static halfToFloat(y) { const s = (y >>> 15) & 0x00000001; let e = (y >>> 10) & 0x0000001f; let m = y & 0x000003ff; if (e === 0) { if (m === 0) { return s << 31; } else { while ((m & 0x00000400) === 0) { m <<= 1; e -= 1; } e += 1; m &= ~0x00000400; } } else if (e === 31) { if (m === 0) { return (s << 31) | 0x7f800000; } else { return (s << 31) | 0x7f800000 | (m << 13); } } e += 127 - 15; m <<= 13; return (s << 31) | (e << 23) | m; } static from(other) { const float16 = new Float16(); float16.bits = other.bits; return float16; } static fromBits(bits) { const float16 = new Float16(); float16.bits = bits; return float16; } static float16ToDouble(bits) { return this._toFloatFloat32[bits]; } static doubleToFloat16(n) { const f = n; const xI = BitUtils.float32ToUint32(f); if (f === 0) { return xI >>> 16; } if (this._toFloatFloat32Data === undefined) { this.initialize(); } let e = (xI >>> 23) & 0x000001ff; e = this._eLut[e]; if (e !== 0) { const m = xI & 0x007fffff; return e + ((m + 0x00000fff + ((m >>> 13) & 1)) >>> 13); } return this.convert(xI); } static posInf() { return Float16.fromBits(0x7c00); } static negInf() { return Float16.fromBits(0xfc00); } static qNan() { return Float16.fromBits(0x7fff); } static sNan() { return Float16.fromBits(0x7dff); } toDouble() { return Float16._toFloatFloat32[this.bits]; } minus() { return Float16.fromBits(this.bits ^ 0x8000); } add(f) { const d = f instanceof Float16 ? f.toDouble() : typeof f === 'number' ? f : 0; return new Float16(this.toDouble() + d); } sub(f) { const d = f instanceof Float16 ? f.toDouble() : typeof f === 'number' ? f : 0; return new Float16(this.toDouble() - d); } mul(f) { const d = f instanceof Float16 ? f.toDouble() : typeof f === 'number' ? f : 0; return new Float16(this.toDouble() * d); } div(f) { const d = f instanceof Float16 ? f.toDouble() : typeof f === 'number' ? f : 0; return new Float16(this.toDouble() / d); } round(n) { if (n >= 10) { return Float16.from(this); } const s = this.bits & 0x8000; let e = this.bits & 0x7fff; e >>>= 9 - n; e += e & 1; e <<= 9 - n; if (e >= 0x7c00) { e = this.bits; e >>>= 10 - n; e <<= 10 - n; } return Float16.fromBits(s | e); } isFinite() { const e = (this.bits >>> 10) & 0x001f; return e < 31; } isNormalized() { const e = (this.bits >>> 10) & 0x001f; return e > 0 && e < 31; } isDenormalized() { const e = (this.bits >>> 10) & 0x001f; const m = this.bits & 0x3ff; return e === 0 && m !== 0; } isZero() { return (this.bits & 0x7fff) === 0; } isNaN() { const e = (this.bits >>> 10) & 0x001f; const m = this.bits & 0x3ff; return e === 31 && m !== 0; } isInfinity() { const e = (this.bits >>> 10) & 0x001f; const m = this.bits & 0x3ff; return e === 31 && m === 0; } isNegative() { return (this.bits & 0x8000) !== 0; } } class ColorFloat16 { get format() { return Format.float16; } get length() { return this.data.length; } get maxChannelValue() { return 1; } get maxIndexValue() { return 1; } get isLdrFormat() { return false; } get isHdrFormat() { return true; } get hasPalette() { return false; } get palette() { return undefined; } get index() { return this.r; } set index(i) { this.r = i; } get r() { return this.getChannel(0); } set r(r) { this.setChannel(0, r); } get g() { return this.getChannel(1); } set g(g) { this.setChannel(1, g); } get b() { return this.getChannel(2); } set b(b) { this.setChannel(2, b); } get a() { return this.getChannel(3); } set a(a) { this.setChannel(3, a); } get rNormalized() { return this.r / this.maxChannelValue; } set rNormalized(v) { this.r = v * this.maxChannelValue; } get gNormalized() { return this.g / this.maxChannelValue; } set gNormalized(v) { this.g = v * this.maxChannelValue; } get bNormalized() { return this.b / this.maxChannelValue; } set bNormalized(v) { this.b = v * this.maxChannelValue; } get aNormalized() { return this.a / this.maxChannelValue; } set aNormalized(v) { this.a = v * this.maxChannelValue; } get luminance() { return ColorUtils.getLuminance(this); } get luminanceNormalized() { return ColorUtils.getLuminanceNormalized(this); } constructor(data) { if (typeof data === 'number') { this.data = new Uint16Array(data); } else { this.data = data.slice(); } } static from(other) { const c = new ColorFloat16(other.length); c.data = other.data; return c; } static fromArray(color) { const c = new ColorFloat16(color); const l = color.length; for (let i = 0; i < l; ++i) { c.data[i] = Float16.doubleToFloat16(color[i]); } return c; } static rgb(r, g, b) { const data = new Uint16Array([ Float16.doubleToFloat16(r), Float16.doubleToFloat16(g), Float16.doubleToFloat16(b), ]); return new ColorFloat16(data); } static rgba(r, g, b, a) { const data = new Uint16Array([ Float16.doubleToFloat16(r), Float16.doubleToFloat16(g), Float16.doubleToFloat16(b), Float16.doubleToFloat16(a), ]); return new ColorFloat16(data); } getChannel(channel) { if (channel === Channel.luminance) { return this.luminance; } else { return channel < this.data.length ? Float16.float16ToDouble(this.data[channel]) : 0; } } getChannelNormalized(channel) { return this.getChannel(channel) / this.maxChannelValue; } setChannel(index, value) { if (index < this.data.length) { this.data[index] = Float16.doubleToFloat16(value); } } set(c) { this.setRgba(c.r, c.g, c.b, c.a); } setRgb(r, g, b) { this.data[0] = Float16.doubleToFloat16(r); const nc = this.data.length; if (nc > 1) { this.data[1] = Float16.doubleToFloat16(g); if (nc > 2) { this.data[2] = Float16.doubleToFloat16(b); } } } setRgba(r, g, b, a) { this.data[0] = Float16.doubleToFloat16(r); const nc = this.data.length; if (nc > 1) { this.data[1] = Float16.doubleToFloat16(g); if (nc > 2) { this.data[2] = Float16.doubleToFloat16(b); if (nc > 3) { this.data[3] = Float16.doubleToFloat16(a); } } } } toArray() { return ArrayUtils.generate(this.length, (i) => this.getChannel(i)); } clone() { return ColorFloat16.from(this); } equals(other) { if (other.length !== this.length) { return false; } for (let i = 0; i < this.length; i++) { if (other.getChannel(i) !== this.getChannel(i)) { return false; } } return true; } convert(opt) { return ColorUtils.convertColor({ from: this, format: opt === null || opt === void 0 ? void 0 : opt.format, numChannels: opt === null || opt === void 0 ? void 0 : opt.numChannels, alpha: opt === null || opt === void 0 ? void 0 : opt.alpha, }); } } class ColorFloat32 { get format() { return Format.float32; } get length() { return this.data.length; } get maxChannelValue() { return 1; } get maxIndexValue() { return 1; } get isLdrFormat() { return false; } get isHdrFormat() { return true; } get hasPalette() { return false; } get palette() { return undefined; } get index() { return this.r; } set index(i) { this.r = i; } get r() { return this.getChannel(0); } set r(r) { this.setChannel(0, r); } get g() { return this.getChannel(1); } set g(g) { this.setChannel(1, g); } get b() { return this.getChannel(2); } set b(b) { this.setChannel(2, b); } get a() { return this.getChannel(3); } set a(a) { this.setChannel(3, a); } get rNormalized() { return this.r / this.maxChannelValue; } set rNormalized(v) { this.r = v * this.maxChannelValue; } get gNormalized() { return this.g / this.maxChannelValue; } set gNormalized(v) { this.g = v * this.maxChannelValue; } get bNormalized() { return this.b / this.maxChannelValue; } set bNormalized(v) { this.b = v * this.maxChannelValue; } get aNormalized() { return this.a / this.maxChannelValue; } set aNormalized(v) { this.a = v * this.maxChannelValue; } get luminance() { return ColorUtils.getLuminance(this); } get luminanceNormalized() { return ColorUtils.getLuminanceNormalized(this); } constructor(data) { if (typeof data === 'number') { this.data = new Float32Array(data); } else { this.data = data.slice(); } } static from(other) { const c = new ColorFloat32(other.length); c.data = other.data; return c; } static fromArray(color) { return new ColorFloat32(color); } static rgb(r, g, b) { const data = new Float32Array([r, g, b]); return new ColorFloat32(data); } static rgba(r, g, b, a) { const data = new Float32Array([r, g, b, a]); return new ColorFloat32(data); } getChannel(channel) { if (channel === Channel.luminance) { return this.luminance; } else { return channel < this.data.length ? this.data[channel] : 0; } } getChannelNormalized(channel) { return this.getChannel(channel) / this.maxChannelValue; } setChannel(index, value) { if (index < this.data.length) { this.data[index] = value; } } set(c) { this.setRgba(c.r, c.g, c.b, c.a); } setRgb(r, g, b) { this.data[0] = r; const nc = this.data.length; if (nc > 1) { this.data[1] = g; if (nc > 2) { this.data[2] = b; } } } setRgba(r, g, b, a) { this.data[0] = r; const nc = this.data.length; if (nc > 1) { this.data[1] = g; if (nc > 2) { this.data[2] = b; if (nc > 3) { this.data[3] = a; } } } } toArray() { return ArrayUtils.generate(this.length, (i) => this.getChannel(i)); } clone() { return ColorFloat32.from(this); } equals(other) { if (other.length !== this.length) { return false; } for (let i = 0; i < this.length; i++) { if (other.getChannel(i) !== this.getChannel(i)) { return false; } } return true; } convert(opt) { return ColorUtils.convertColor({ from: this, format: opt === null || opt === void 0 ? void 0 : opt.format, numChannels: opt === null || opt === void 0 ? void 0 : opt.numChannels, alpha: opt === null || opt === void 0 ? void 0 : opt.alpha, }); } } class ColorFloat64 { get format() { return Format.float64; } get length() { return this.data.length; } get maxChannelValue() { return 1; } get maxIndexValue() { return 1; } get isLdrFormat() { return true; } get isHdrFormat() { return false; } get hasPalette() { return false; } get palette() { return undefined; } get index() { return this.r; } set index(i) { this.r = i; } get r() { return this.getChannel(0); } set r(r) { this.setChannel(0, r); } get g() { return this.getChannel(1); } set g(g) { this.setChannel(1, g); } get b() { return this.getChannel(2); } set b(b) { this.setChannel(2, b); } get a() { return this.getChannel(3); } set a(a) { this.setChannel(3, a); } get rNormalized() { return this.r / this.maxChannelValue; } set rNormalized(v) { this.r = v * this.maxChannelValue; } get gNormalized() { return this.g / this.maxChannelValue; } set gNormalized(v) { this.g = v * this.maxChannelValue; } get bNormalized() { return this.b / this.maxChannelValue; } set bNormalized(v) { this.b = v * this.maxChannelValue; } get aNormalized() { return this.a / this.maxChannelValue; } set aNormalized(v) { this.a = v * this.maxChannelValue; } get luminance() { return ColorUtils.getLuminance(this); } get luminanceNormalized() { return ColorUtils.getLuminanceNormalized(this); } constructor(data) { if (typeof data === 'number') { this.data = new Float64Array(data); } else { this.data = data.slice(); } } static from(other) { const c = new ColorFloat64(other.length); c.data = other.data; return c; } static fromArray(color) { return new ColorFloat64(color); } static rgb(r, g, b) { const data = new Float64Array([r, g, b]); return new ColorFloat64(data); } static rgba(r, g, b, a) { const data = new Float64Array([r, g, b, a]); return new ColorFloat64(data); } getChannel(channel) { if (channel === Channel.luminance) { return this.luminance; } else { return channel < this.data.length ? this.data[channel] : 0; } } getChannelNormalized(channel) { return this.getChannel(channel) / this.maxChannelValue; } setChannel(index, value) { if (index < this.data.length) { this.data[index] = value; } }