UNPKG

imagemagick-compare

Version:

Compare images with NodeJS using ImageMagick's Magick++

72 lines (71 loc) 2.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.compareSync = exports.compare = exports.version = void 0; const lib = require('../build/Release/compare.node'); /** * * @returns The version of the installed Magick++ library */ function version() { return lib.version(); } exports.version = version; /** * Compares two images using Magick++ assynchronously. * @param originalImage Image buffer of the original image * @param compareWith Image buffer of the image to compare with * @param options Define metric options * @param callback Optional callback function if you don't want to use promises * @returns Promise with metric result as a number */ function compare(originalImage, compareWith, options = {}, callback) { return new Promise((resolve, reject) => { const { metric, radius, sigma, k1, k2 } = options; try { lib.compare({ originalImage, compareWith, metric, radius, sigma, k1, k2 }, (err, res) => { if (typeof callback === 'function') { callback(err, res); } if (typeof err !== 'undefined') { return reject(err); } resolve(res); }); } catch (err) { if (typeof callback === 'function') { callback(err, undefined); } reject(err); } }); } exports.compare = compare; /** * Compares two images using Magick++ synchronously. * @param originalImage Image buffer of the original image * @param compareWith Image buffer of the image to compare with * @param options Define metric options * @returns Metric result as a number */ function compareSync(originalImage, compareWith, options = {}) { const { metric, radius, sigma, k1, k2 } = options; return lib.compare({ originalImage, compareWith, metric, radius, sigma, k1, k2 }); } exports.compareSync = compareSync;