Use image fingerprints in functional tests #122
BIN
test/fixtures/expected/exif-5.jpg
vendored
Normal file
|
After Width: | Height: | Size: 23 KiB |
BIN
test/fixtures/expected/exif-8.jpg
vendored
Normal file
|
After Width: | Height: | Size: 23 KiB |
BIN
test/fixtures/expected/extract-resize-crop-extract.jpg
vendored
Normal file
|
After Width: | Height: | Size: 1.1 KiB |
BIN
test/fixtures/expected/extract-resize.jpg
vendored
Normal file
|
After Width: | Height: | Size: 506 B |
BIN
test/fixtures/expected/extract-rotate.jpg
vendored
Normal file
|
After Width: | Height: | Size: 1.0 KiB |
BIN
test/fixtures/expected/extract.jpg
vendored
Normal file
|
After Width: | Height: | Size: 348 B |
BIN
test/fixtures/expected/extract.png
vendored
Normal file
|
After Width: | Height: | Size: 5.3 KiB |
BIN
test/fixtures/expected/extract.tiff
vendored
Normal file
BIN
test/fixtures/expected/extract.webp
vendored
Normal file
|
After Width: | Height: | Size: 4.7 KiB |
BIN
test/fixtures/expected/flatten-black.jpg
vendored
Normal file
|
After Width: | Height: | Size: 996 B |
BIN
test/fixtures/expected/flatten-orange.jpg
vendored
Normal file
|
After Width: | Height: | Size: 3.7 KiB |
BIN
test/fixtures/expected/flip-and-flop.jpg
vendored
Normal file
|
After Width: | Height: | Size: 15 KiB |
BIN
test/fixtures/expected/flip.jpg
vendored
Normal file
|
After Width: | Height: | Size: 15 KiB |
BIN
test/fixtures/expected/flop.jpg
vendored
Normal file
|
After Width: | Height: | Size: 15 KiB |
BIN
test/fixtures/expected/gravity-center.jpg
vendored
Normal file
|
After Width: | Height: | Size: 5.2 KiB |
BIN
test/fixtures/expected/gravity-centre.jpg
vendored
Normal file
|
After Width: | Height: | Size: 5.4 KiB |
BIN
test/fixtures/expected/gravity-east.jpg
vendored
Normal file
|
After Width: | Height: | Size: 3.8 KiB |
BIN
test/fixtures/expected/gravity-north.jpg
vendored
Normal file
|
After Width: | Height: | Size: 3.4 KiB |
BIN
test/fixtures/expected/gravity-south.jpg
vendored
Normal file
|
After Width: | Height: | Size: 5.2 KiB |
BIN
test/fixtures/expected/gravity-west.jpg
vendored
Normal file
|
After Width: | Height: | Size: 4.4 KiB |
BIN
test/fixtures/expected/resize-crop-extract.jpg
vendored
Normal file
|
After Width: | Height: | Size: 1.4 KiB |
BIN
test/fixtures/expected/rotate-extract.jpg
vendored
Normal file
|
After Width: | Height: | Size: 1.6 KiB |
57
test/fixtures/index.js
vendored
@@ -1,11 +1,42 @@
|
||||
'use strict';
|
||||
|
||||
var path = require('path');
|
||||
var assert = require('assert');
|
||||
|
||||
var sharp = require('../../index');
|
||||
|
||||
var getPath = function(filename) {
|
||||
return path.join(__dirname, filename);
|
||||
};
|
||||
|
||||
// Generates a 64-bit-as-binary-string image fingerprint
|
||||
// Based on the dHash gradient method - see http://www.hackerfactor.com/blog/index.php?/archives/529-Kind-of-Like-That.html
|
||||
var fingerprint = function(image, done) {
|
||||
sharp(image)
|
||||
.greyscale()
|
||||
.normalise()
|
||||
.resize(9, 8)
|
||||
.ignoreAspectRatio()
|
||||
.interpolateWith(sharp.interpolator.vertexSplitQuadraticBasisSpline)
|
||||
.raw()
|
||||
.toBuffer(function(err, data) {
|
||||
if (err) {
|
||||
done(err);
|
||||
} else {
|
||||
var fingerprint = '';
|
||||
for (var col = 0; col < 8; col++) {
|
||||
var gradient = 0;
|
||||
for (var row = 0; row < 8; row++) {
|
||||
var left = data[row * 8 + col];
|
||||
var right = data[row * 8 + col + 1];
|
||||
fingerprint = fingerprint + (left < right ? '1' : '0');
|
||||
}
|
||||
}
|
||||
done(null, fingerprint);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
|
||||
inputJpg: getPath('2569067123_aca715a2ee_o.jpg'), // http://www.flickr.com/photos/grizdave/2569067123/
|
||||
@@ -35,6 +66,30 @@ module.exports = {
|
||||
outputWebP: getPath('output.webp'),
|
||||
outputZoinks: getPath('output.zoinks'), // an 'unknown' file extension
|
||||
|
||||
path: getPath // allows tests to write files to fixtures directory (for testing with human eyes)
|
||||
// Path for tests requiring human inspection
|
||||
path: getPath,
|
||||
|
||||
// Path for expected output images
|
||||
expected: function(filename) {
|
||||
return getPath(path.join('expected', filename));
|
||||
},
|
||||
|
||||
// Verify similarity of expected vs actual images via fingerprint
|
||||
assertSimilar: function(expectedImage, actualImage, done) {
|
||||
fingerprint(expectedImage, function(err, expectedFingerprint) {
|
||||
if (err) throw err;
|
||||
fingerprint(actualImage, function(err, actualFingerprint) {
|
||||
if (err) throw err;
|
||||
var distance = 0;
|
||||
for (var i = 0; i < 64; i++) {
|
||||
if (expectedFingerprint[i] !== actualFingerprint[i]) {
|
||||
distance++;
|
||||
}
|
||||
}
|
||||
assert.strictEqual(true, distance <= 5); // ~7% threshold
|
||||
done();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
};
|
||||
|
||||