sharp/test/unit/compare.js
Daniel Gasienica ef8db1eebf Premultiply alpha channel to avoid dark artifacts during tranformation
Add `Sharp.compare(file1, file2, callback)` function for comparing images
using mean squared error (MSE). This is useful for unit tests.

See:
- https://github.com/jcupitt/libvips/issues/291
- http://entropymine.com/imageworsener/resizealpha/
2015-06-01 14:21:02 +01:00

78 lines
2.5 KiB
JavaScript

'use strict';
var assert = require('assert');
var fixtures = require('../fixtures');
var fs = require('fs');
var sharp = require('../../index');
sharp.cache(0);
// Constants
var MAX_ALLOWED_MEAN_SQUARED_ERROR = 0.0005;
// Tests
describe('sharp.compare', function() {
it('should report equality when comparing an image to itself', function(done) {
var image = fixtures.inputPngOverlayLayer0;
sharp.compare(image, image, function (error, info) {
if (error) return done(error);
assert.strictEqual(info.isEqual, true, 'image is equal to itself');
assert.strictEqual(info.status, 'success', 'status is correct');
assert(0 <= info.meanSquaredError &&
info.meanSquaredError <= MAX_ALLOWED_MEAN_SQUARED_ERROR,
'MSE is within tolerance');
done();
});
});
it('should report that two images have a mismatched number of bands (channels)', function(done) {
var actual = fixtures.inputPngOverlayLayer1;
var expected = fixtures.inputJpg;
sharp.compare(actual, expected, function (error, info) {
if (error) return done(error);
assert.strictEqual(info.isEqual, false);
assert.strictEqual(info.status, 'mismatchedBands');
assert(typeof info.meanSquaredError === 'undefined', 'MSE is undefined');
done();
});
});
it('should report that two images have a mismatched dimensions', function(done) {
var actual = fixtures.inputJpg;
var expected = fixtures.inputJpgWithExif;
sharp.compare(actual, expected, function (error, info) {
if (error) return done(error);
assert.strictEqual(info.isEqual, false);
assert.strictEqual(info.status, 'mismatchedDimensions');
assert(typeof info.meanSquaredError === 'undefined', 'MSE is undefined');
done();
});
});
it('should report the correct mean squared error for two different images', function(done) {
var actual = fixtures.inputPngOverlayLayer0;
var expected = fixtures.inputPngOverlayLayer1;
sharp.compare(actual, expected, function (error, info) {
if (error) return done(error);
var meanSquaredError = info.meanSquaredError;
assert.strictEqual(info.isEqual, false);
assert.strictEqual(info.status, 'success');
// ImageMagick reports: 42242.5
// `compare -metric mse 'actual' 'expected' comparison.png`
assert(41900 <= meanSquaredError && meanSquaredError <= 41950,
'Expected: 41900 <= meanSquaredError <= 41950. Actual: ' + meanSquaredError);
done();
});
});
});