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)

116 lines 3.96 kB
import { LibError } from '../error/lib-error.js'; import { Rational } from './rational.js'; export 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)); } } //# sourceMappingURL=array-utils.js.map