Skip to content
This repository was archived by the owner on Feb 18, 2021. It is now read-only.

Commit f282385

Browse files
author
Nick Georgiev
committed
add 'threshold' option for comparison
The values of threshold must be between 0 and 100 and represents percentage of different pixels.
1 parent 0652fb7 commit f282385

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

lib/image-diff.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,20 @@ ImageDiff.createDiff = function (options, cb) {
7373
// DEV: According to http://www.imagemagick.org/discourse-server/viewtopic.php?f=1&t=17284
7474
// DEV: These values are the total square root mean square (RMSE) pixel difference across all pixels and its percentage
7575
// TODO: This is not very multi-lengual =(
76-
var resultInfo = stderr.match(/all: (\d+\.?\d*) \((.*?)\)/);
76+
var resultInfo = stderr.match(/all: (\d+\.?\d*) \((\d+\.?\d*e?\-?\d*)\)/);
7777

7878
// If there was no resultInfo, throw a fit
7979
if (!resultInfo) {
8080
return cb(new Error('Expected `image-diff\'s stderr` to contain \'all\' but received "' + stderr + '"'));
8181
}
8282

8383
// Callback with pass/fail
84-
var totalDifference = resultInfo[2]; // this is the percentage difference
85-
return cb(null, parseFloat(totalDifference) <= options.threshold, options.diffPath);
84+
var percentDifference = parseFloat(resultInfo[2], 10);
85+
var threshold = (typeof(options.threshold) === 'undefined' || isNaN(parseFloat(options.threshold, 10))) ? 0.0 : parseFloat(options.threshold);
86+
if (isNaN(percentDifference)) {
87+
return cb(new Error('Attempted to parse percentage difference from `image-diff\'s stderr` but got `NaN` from "' + resultInfo[2] + '"'));
88+
}
89+
return cb(null, percentDifference <= threshold);
8690
});
8791
};
8892
ImageDiff.prototype = {
@@ -190,9 +194,8 @@ ImageDiff.prototype = {
190194
expectedPath: expectedTmpPath,
191195
diffPath: diffPath,
192196
threshold: threshold
193-
}, function saveResult (err, _imagesAreSame, _diffPath) {
197+
}, function saveResult (err, _imagesAreSame) {
194198
imagesAreSame = _imagesAreSame;
195-
diffPath = _diffPath;
196199
cb(err);
197200
});
198201
}
@@ -205,7 +208,7 @@ ImageDiff.prototype = {
205208
fs.unlink(filepath, cb);
206209
}, function callOriginalCallback (_err) {
207210
// Callback with the imagesAreSame
208-
callback(err, imagesAreSame, diffPath);
211+
callback(err, imagesAreSame);
209212
});
210213
});
211214
}

0 commit comments

Comments
 (0)