UNPKG

image-js

Version:

Image processing and manipulation in JavaScript

21 lines 758 B
import { encode } from 'jpeg-js'; import { Mask } from '../Mask.js'; /** * Creates a JPEG buffer from an image. * @param image - The image instance. * @param options - JPEG encoding options. * @returns The buffer. */ export function encodeJpeg(image, options = {}) { const { quality = 50 } = options; if (image.colorModel !== 'RGBA' || image instanceof Mask) { image = image.convertColor('RGBA'); } if (image.bitDepth !== 8) { image = image.convertBitDepth(8); } // Image data after bit depth conversion will always be UInt8Array. const buffer = encode(image.getRawImage(), quality).data; return new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength); } //# sourceMappingURL=encodeJpeg.js.map