add diff image output functionality and tests

master
Cheng Lou 11 years ago
parent b5c7fd8b23
commit 9fa564ea59
  1. 1
      .gitignore
  2. BIN
      examples/imgs/noise1.png
  3. 20
      lib/compare.js
  4. 22
      test/compare.js

1
.gitignore vendored

@ -2,6 +2,7 @@
node_modules
examples/imgs/*
!examples/imgs/original.jpg
!examples/imgs/noise1.png
!examples/imgs/original.png
!examples/imgs/original.gif
!examples/imgs/originalSideways.jpg

Binary file not shown.

After

Width:  |  Height:  |  Size: 40 KiB

@ -1,7 +1,7 @@
// compare
var exec = require('child_process').exec;
var utils = require('./utils')
var utils = require('./utils');
/**
* Compare two images uses graphicsmagicks `compare` command.
@ -22,6 +22,22 @@ module.exports = exports = function compare (orig, compareTo, tolerance, cb) {
orig = utils.escape(orig);
compareTo = utils.escape(compareTo);
// outputting the diff image
if (typeof tolerance === 'object') {
var diffOptions = tolerance;
if (typeof diffOptions.file !== 'string') {
throw new TypeError('The path for the diff output is invalid');
}
// graphicsmagick defaults to red
var highlightColorOption = diffOptions.highlightColor
? ' -highlight-color ' + diffOptions.highlightColor + ' '
: ' ';
return exec('gm compare' + highlightColorOption + orig + ' ' + compareTo +
' -file ' + diffOptions.file, cb);
}
// else, output the mean square error (mse)
if ('function' == typeof tolerance) {
cb = tolerance;
tolerance = 0.4;
@ -38,5 +54,5 @@ module.exports = exports = function compare (orig, compareTo, tolerance, cb) {
var equality = parseFloat(match[1]);
cb(null, equality <= tolerance, equality, stdout);
})
});
}

@ -0,0 +1,22 @@
var assert = require('assert');
var fs = require('fs');
module.exports = function (gm, dir, finish, GM) {
GM.compare(dir + '/original.jpg', dir + '/original.png', function(err, same) {
if (err || !same) finish(err);
else outputDiff();
});
function outputDiff() {
var options = {
highlightColor: 'yellow',
file: dir + '/diff.png'
};
GM.compare(dir + '/original.jpg', dir + '/noise1.png', options, function(err) {
if (err) return finish(err);
fs.exists(options.file, function(exists) {
finish(!exists);
});
});
}
};
Loading…
Cancel
Save