@elgato-stream-deck/node-lib
Version:
Helpers for a npm module for interfacing with the Elgato Stream Deck
42 lines • 1.53 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.encodeJPEG = encodeJPEG;
const jpegJS = require("jpeg-js");
const DEFAULT_QUALITY = 95;
/**
* The default JPEG encoder.
* `@julusian/jpeg-turbo` will be used if it can be found, otherwise it will fall back to `jpeg-js`
* @param buffer The buffer to convert
* @param width Width of the image
* @param height Hieght of the image
*/
async function encodeJPEG(buffer, width, height, options) {
try {
const jpegTurbo = await Promise.resolve().then(() => require('@julusian/jpeg-turbo'));
// Try using jpeg-turbo if it is available
if (jpegTurbo.bufferSize && !!jpegTurbo.compress) {
const encodeOptions = {
format: jpegTurbo.FORMAT_RGBA,
width,
height,
quality: DEFAULT_QUALITY,
...options,
};
if (buffer.length === width * height * 4) {
const tmpBuffer = Buffer.alloc(jpegTurbo.bufferSize(encodeOptions));
return jpegTurbo.compress(Buffer.from(buffer), tmpBuffer, encodeOptions); // Future: avoid rewrap
}
}
}
catch (_e) {
// TODO - log error
}
// If jpeg-turbo is unavailable or fails, then fallback to jpeg-js
const jpegBuffer2 = jpegJS.encode({
width,
height,
data: buffer,
}, options ? options.quality : DEFAULT_QUALITY);
return jpegBuffer2.data;
}
//# sourceMappingURL=jpeg.js.map