UNPKG

javascript-binary-converter

Version:

A utility package to quickly handle and convert various Javascript binary objects

43 lines 1.52 kB
import { bytesToIntegers, typedArrayToBytes } from "../utils/binary"; import { isNode } from "../utils/crossPlatform"; import { imageToBlob, imageToCanvas } from "../utils/image"; export default class ImageConverter { constructor(original) { this.original = original; if (isNode) { throw new Error('ImageConvertor is available only in the browser'); } } async toBlob(config) { return imageToBlob(this.original, { height: config?.height, width: config?.width }); } async toArrayBuffer(config) { const blob = await this.toBlob(config); return blob.arrayBuffer(); } async toUint8Array() { return new Uint8Array(await this.toArrayBuffer()); // } async toInt8Array() { return new Int8Array(await this.toArrayBuffer()); } /** * Returns an array of number-like strings, each representing 8 bits. */ async toBytes() { const uint8 = await this.toUint8Array(); const bytes = typedArrayToBytes(uint8); return bytes; } async toDecimalBytes() { const bytes = await this.toBytes(); return bytesToIntegers(bytes); } toCanvas(config) { const w = config?.width || this.original.width; const h = config?.height || this.original.height; const canvas = imageToCanvas(this.original, { width: w, height: h }); return canvas; } } //# sourceMappingURL=ImageConverter.js.map