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/
BIN
test/fixtures/alpha-premultiply-1024x768-paper.png
vendored
Normal file
|
After Width: | Height: | Size: 255 KiB |
BIN
test/fixtures/alpha-premultiply-2048x1536-paper.png
vendored
Normal file
|
After Width: | Height: | Size: 640 KiB |
BIN
test/fixtures/expected/alpha-layer-01-imagemagick.png
vendored
Normal file
|
After Width: | Height: | Size: 234 KiB |
BIN
test/fixtures/expected/alpha-layer-01-low-alpha-imagemagick.png
vendored
Normal file
|
After Width: | Height: | Size: 186 KiB |
BIN
test/fixtures/expected/alpha-layer-01-low-alpha.png
vendored
Normal file
|
After Width: | Height: | Size: 188 KiB |
BIN
test/fixtures/expected/alpha-layer-01.png
vendored
|
Before Width: | Height: | Size: 234 KiB After Width: | Height: | Size: 234 KiB |
BIN
test/fixtures/expected/alpha-layer-012-imagemagick.png
vendored
Normal file
|
After Width: | Height: | Size: 259 KiB |
BIN
test/fixtures/expected/alpha-layer-012-low-alpha-imagemagick.png
vendored
Normal file
|
After Width: | Height: | Size: 208 KiB |
BIN
test/fixtures/expected/alpha-layer-012-low-alpha.png
vendored
|
Before Width: | Height: | Size: 178 KiB After Width: | Height: | Size: 209 KiB |
BIN
test/fixtures/expected/alpha-layer-012.png
vendored
|
Before Width: | Height: | Size: 260 KiB After Width: | Height: | Size: 260 KiB |
BIN
test/fixtures/expected/alpha-layer-12-imagemagick.png
vendored
Normal file
|
After Width: | Height: | Size: 221 KiB |
BIN
test/fixtures/expected/alpha-layer-12-low-alpha-imagemagick.png
vendored
Normal file
|
After Width: | Height: | Size: 179 KiB |
BIN
test/fixtures/expected/alpha-layer-12-low-alpha.png
vendored
Normal file
|
After Width: | Height: | Size: 179 KiB |
BIN
test/fixtures/expected/alpha-layer-12.png
vendored
Normal file
|
After Width: | Height: | Size: 222 KiB |
BIN
test/fixtures/expected/alpha-premultiply-enlargement-2048x1536-paper.png
vendored
Normal file
|
After Width: | Height: | Size: 922 KiB |
BIN
test/fixtures/expected/alpha-premultiply-reduction-1024x768-paper.png
vendored
Normal file
|
After Width: | Height: | Size: 208 KiB |
35
test/fixtures/index.js
vendored
@@ -2,9 +2,13 @@
|
||||
|
||||
var path = require('path');
|
||||
var assert = require('assert');
|
||||
|
||||
var sharp = require('../../index');
|
||||
|
||||
|
||||
// Constants
|
||||
var MAX_ALLOWED_MEAN_SQUARED_ERROR = 0.0005;
|
||||
|
||||
// Helpers
|
||||
var getPath = function(filename) {
|
||||
return path.join(__dirname, filename);
|
||||
};
|
||||
@@ -57,6 +61,8 @@ module.exports = {
|
||||
inputPngOverlayLayer2: getPath('alpha-layer-2-ink.png'),
|
||||
inputPngOverlayLayer1LowAlpha: getPath('alpha-layer-1-fill-low-alpha.png'),
|
||||
inputPngOverlayLayer2LowAlpha: getPath('alpha-layer-2-ink-low-alpha.png'),
|
||||
inputPngAlphaPremultiplicationSmall: getPath('alpha-premultiply-1024x768-paper.png'),
|
||||
inputPngAlphaPremultiplicationLarge: getPath('alpha-premultiply-2048x1536-paper.png'),
|
||||
|
||||
inputWebP: getPath('4.webp'), // http://www.gstatic.com/webp/gallery/4.webp
|
||||
inputTiff: getPath('G31D.TIF'), // http://www.fileformat.info/format/tiff/sample/e6c9a6e5253348f4aef6d17b534360ab/index.htm
|
||||
@@ -116,12 +122,37 @@ module.exports = {
|
||||
}
|
||||
|
||||
if (distance > options.threshold) {
|
||||
return callback(new Error('Maximum similarity distance: ' + options.threshold + '. Actual: ' + distance));
|
||||
return callback(new Error('Expected maximum similarity distance: ' + options.threshold + '. Actual: ' + distance + '.'));
|
||||
}
|
||||
|
||||
callback();
|
||||
});
|
||||
});
|
||||
},
|
||||
|
||||
assertEqual: function(actualImagePath, expectedImagePath, callback) {
|
||||
if (typeof actualImagePath !== 'string') {
|
||||
throw new TypeError('`actualImagePath` must be a string; got ' + actualImagePath);
|
||||
}
|
||||
|
||||
if (typeof expectedImagePath !== 'string') {
|
||||
throw new TypeError('`expectedImagePath` must be a string; got ' + expectedImagePath);
|
||||
}
|
||||
|
||||
if (typeof callback !== 'function') {
|
||||
throw new TypeError('`callback` must be a function');
|
||||
}
|
||||
|
||||
sharp.compare(actualImagePath, expectedImagePath, function (error, info) {
|
||||
if (error) return callback(error);
|
||||
|
||||
var meanSquaredError = info.meanSquaredError;
|
||||
if (typeof meanSquaredError !== 'undefined' && meanSquaredError > MAX_ALLOWED_MEAN_SQUARED_ERROR) {
|
||||
return callback(new Error('Expected images be equal. Mean squared error: ' + meanSquaredError + '.'));
|
||||
}
|
||||
|
||||
return callback(null, info);
|
||||
});
|
||||
}
|
||||
|
||||
};
|
||||
|
||||