Use image fingerprints in functional tests #122

This commit is contained in:
Lovell Fuller
2015-04-30 16:59:12 +01:00
parent 75d72cfded
commit b50fb53f27
28 changed files with 142 additions and 101 deletions

BIN
test/fixtures/expected/exif-5.jpg vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

BIN
test/fixtures/expected/exif-8.jpg vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 506 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

BIN
test/fixtures/expected/extract.jpg vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 348 B

BIN
test/fixtures/expected/extract.png vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.3 KiB

BIN
test/fixtures/expected/extract.tiff vendored Normal file

Binary file not shown.

BIN
test/fixtures/expected/extract.webp vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.7 KiB

BIN
test/fixtures/expected/flatten-black.jpg vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 996 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.7 KiB

BIN
test/fixtures/expected/flip-and-flop.jpg vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

BIN
test/fixtures/expected/flip.jpg vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

BIN
test/fixtures/expected/flop.jpg vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.4 KiB

BIN
test/fixtures/expected/gravity-east.jpg vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.8 KiB

BIN
test/fixtures/expected/gravity-north.jpg vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.4 KiB

BIN
test/fixtures/expected/gravity-south.jpg vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.2 KiB

BIN
test/fixtures/expected/gravity-west.jpg vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

View File

@@ -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();
});
});
}
};