|
|
|
@ -1,7 +1,6 @@ |
|
|
|
|
// compare
|
|
|
|
|
|
|
|
|
|
var exec = require('child_process').exec; |
|
|
|
|
var utils = require('./utils'); |
|
|
|
|
var spawn = require('child_process').spawn; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Compare two images uses graphicsmagicks `compare` command. |
|
|
|
@ -20,13 +19,11 @@ var utils = require('./utils'); |
|
|
|
|
|
|
|
|
|
module.exports = exports = function (proto) { |
|
|
|
|
function compare(orig, compareTo, options, cb) { |
|
|
|
|
orig = utils.escape(orig); |
|
|
|
|
compareTo = utils.escape(compareTo); |
|
|
|
|
|
|
|
|
|
var isImageMagick = this._options && this._options.imageMagick; |
|
|
|
|
// compare binary for IM is `compare`, for GM it's `gm compare`
|
|
|
|
|
var bin = isImageMagick ? '' : 'gm '; |
|
|
|
|
var execCmd = bin + 'compare -metric mse ' + orig + ' ' + compareTo; |
|
|
|
|
var args = ['compare', '-metric', 'mse', orig, compareTo] |
|
|
|
|
var tolerance = 0.4; |
|
|
|
|
// outputting the diff image
|
|
|
|
|
if (typeof options === 'object') { |
|
|
|
@ -40,16 +37,19 @@ module.exports = exports = function (proto) { |
|
|
|
|
throw new TypeError('The path for the diff output is invalid'); |
|
|
|
|
} |
|
|
|
|
// graphicsmagick defaults to red
|
|
|
|
|
var highlightColorOption = options.highlightColor |
|
|
|
|
? ' -highlight-color ' + options.highlightColor + ' ' |
|
|
|
|
: ' '; |
|
|
|
|
var highlightStyleOption = options.highlightStyle |
|
|
|
|
? ' -highlight-style ' + options.highlightStyle + ' ' |
|
|
|
|
: ' '; |
|
|
|
|
var diffFilename = utils.escape(options.file); |
|
|
|
|
if (options.highlightColour) { |
|
|
|
|
args.push('-highlight-color'); |
|
|
|
|
args.push(options.highlightColor); |
|
|
|
|
} |
|
|
|
|
if (options.highlightStyle) { |
|
|
|
|
args.push('-highlight-style') |
|
|
|
|
args.push(options.highlightStyle) |
|
|
|
|
} |
|
|
|
|
// For IM, filename is the last argument. For GM it's `-file <filename>`
|
|
|
|
|
var diffOpt = isImageMagick ? diffFilename : ('-file ' + diffFilename); |
|
|
|
|
execCmd += highlightColorOption + highlightStyleOption + ' ' + diffOpt; |
|
|
|
|
if (isImageMagick) { |
|
|
|
|
args.push('-file'); |
|
|
|
|
} |
|
|
|
|
args.push(options.file); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (typeof options.tolerance != 'undefined') { |
|
|
|
@ -60,7 +60,9 @@ module.exports = exports = function (proto) { |
|
|
|
|
}
|
|
|
|
|
} else { |
|
|
|
|
// For ImageMagick diff file is required but we don't care about it, so null it out
|
|
|
|
|
isImageMagick && (execCmd += ' null:'); |
|
|
|
|
if (isImageMagick) { |
|
|
|
|
args.push('null:'); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (typeof options == 'function') { |
|
|
|
|
cb = options; // tolerance value not provided, flip the cb place
|
|
|
|
@ -69,19 +71,27 @@ module.exports = exports = function (proto) { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
exec(execCmd, function (err, stdout, stderr) { |
|
|
|
|
var proc = spawn('/usr/bin/gm', args); |
|
|
|
|
var stdout = ''; |
|
|
|
|
var stderr = ''; |
|
|
|
|
proc.stdout.on('data',function(data) { stdout+=data }); |
|
|
|
|
proc.stderr.on('data',function(data) { stderr+=data }); |
|
|
|
|
proc.on('close', function (code) { |
|
|
|
|
// ImageMagick returns err code 2 if err, 0 if similar, 1 if dissimilar
|
|
|
|
|
if (isImageMagick) { |
|
|
|
|
if (!err) { |
|
|
|
|
if (code === 0) { |
|
|
|
|
return cb(null, 0 <= tolerance, 0, stdout); |
|
|
|
|
} |
|
|
|
|
if (err.code === 1) { |
|
|
|
|
else if (code === 1) { |
|
|
|
|
err = null; |
|
|
|
|
stdout = stderr; |
|
|
|
|
} else { |
|
|
|
|
return cb(stderr); |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
if(code !== 0) { |
|
|
|
|
return cb(stderr); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
if (err) { |
|
|
|
|
return cb(err); |
|
|
|
|
} |
|
|
|
|
// Since ImageMagick similar gives err code 0 and no stdout, there's really no matching
|
|
|
|
|
// Otherwise, output format for IM is `12.00 (0.123)` and for GM it's `Total: 0.123`
|
|
|
|
@ -93,7 +103,7 @@ module.exports = exports = function (proto) { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
var equality = parseFloat(match[1]); |
|
|
|
|
cb(null, equality <= tolerance, equality, stdout, utils.unescape(orig), utils.unescape(compareTo)); |
|
|
|
|
cb(null, equality <= tolerance, equality, stdout, orig, compareTo); |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|