Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// compare | ||
|
||
var exec = require('child_process').exec; | ||
var utils = require('./utils') | ||
|
||
/** | ||
* Compare two images uses graphicsmagicks `compare` command. | ||
* | ||
* gm.compare(img1, img2, 0.4, function (err, equal, equality) { | ||
* if (err) return handle(err); | ||
* console.log('The images are equal: %s', equal); | ||
* console.log('There equality was %d', equality); | ||
* }); | ||
* | ||
* @param {String} orig Path to an image. | ||
* @param {String} compareTo Path to another image to compare to `orig`. | ||
* @param {Number} [tolerance] Amount of difference to tolerate before failing - defaults to 0.4 | ||
* @param {Function} cb(err, Boolean, equality, rawOutput) | ||
*/ | ||
|
||
module.exports = exports = function compare (orig, compareTo, tolerance, cb) { | ||
orig = utils.escape(orig); | ||
compareTo = utils.escape(compareTo); | ||
|
||
if ('function' == typeof tolerance) { | ||
cb = tolerance; | ||
tolerance = 0.4; | ||
} | ||
|
||
exec('gm compare -metric mse ' + orig + ' ' + compareTo, function (err, stdout, stderr) { | ||
if (err) return cb(err); | ||
|
||
var match = /Total: (\d+\.?\d*)/m.exec(stdout); | ||
if (!match) { | ||
err = new Error('Unable to parse output.\nGot ' + stdout); | ||
return cb(err); | ||
} | ||
|
||
var equality = parseFloat(match[1]); | ||
cb(null, equality <= tolerance, equality, stdout); | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
var assert = require('assert') | ||
var fs = require('fs') | ||
|
||
module.exports = function (_, dir, finish, gm) { | ||
|
||
var original = dir + '/original.jpg'; | ||
var result = dir + '/fromBuffer.png'; | ||
|
||
var buf = fs.readFileSync(original); | ||
|
||
gm(buf) | ||
.rotate('red', 30) | ||
.write(result, function crop (err) { | ||
if (err) return finish(err); | ||
|
||
// tolerance defaults to 0.4 | ||
gm.compare(original, result, function (err, equal) { | ||
if (err) return finish(err); | ||
assert.ok(equal); | ||
|
||
// accepts tolerance argument | ||
gm.compare(original, result, 0.1, function (err, equal, equality, raw) { | ||
if (err) return finish(err); | ||
assert.ok(!equal); | ||
assert.ok(equality > 0.1); | ||
assert.ok(raw); | ||
finish(); | ||
}) | ||
}) | ||
}); | ||
} |