Skip to content

Commit

Permalink
make diff optional in cli; clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
mourner committed Jun 6, 2019
1 parent 33dc04a commit b03a368
Showing 1 changed file with 27 additions and 20 deletions.
47 changes: 27 additions & 20 deletions bin/pixelmatch
Expand Up @@ -3,45 +3,52 @@

'use strict';

var PNG = require('pngjs').PNG,
fs = require('fs'),
match = require('../.');
var PNG = require('pngjs').PNG;
var fs = require('fs');
var match = require('../.');

if (process.argv.length < 5) {
console.log('Usage: pixelmatch image1.png image2.png output.png [threshold=0.005] [includeAA=false]');
if (process.argv.length < 4) {
console.log('Usage: imagematch image1.png image2.png [diff.png] [threshold=0.005] [includeAA=false]');
process.exit(64);
}

var threshold = isNaN(+process.argv[5]) ? undefined : +process.argv[5],
includeAA = process.argv[6] === 'true';
var diffPath = process.argv[4];
var threshold = isNaN(+process.argv[5]) ? undefined : +process.argv[5];
var includeAA = process.argv[6] === 'true';

var img1 = fs.createReadStream(process.argv[2]).pipe(new PNG()).on('parsed', doneReading);
var img2 = fs.createReadStream(process.argv[3]).pipe(new PNG()).on('parsed', doneReading);

function doneReading() {
if (!img1.data || !img2.data) return;

if (img1.width !== img2.width || img1.height !== img2.height) {
console.log('Image dimensions do not match: %dx%d vs %dx%d',
img1.width, img1.height, img2.width, img2.height);
var width = img1.width;
var height = img1.height;

if (img2.width !== width || img2.height !== height) {
console.log('Image dimensions do not match: %dx%d vs %dx%d', width, height, img2.width, img2.height);
process.exit(65);
}

var diff = new PNG({width: img1.width, height: img1.height});
var diff = diffPath ? new PNG({width: width, height: height}) : null;

console.time('match');
var diffs = match(img1.data, img2.data, diff.data, diff.width, diff.height, {
console.time('matched in');
var diffs = match(img1.data, img2.data, diff ? diff.data : null, width, height, {
threshold: threshold,
includeAA: includeAA
});
console.timeEnd('match');

var writeStream = diff.pack().pipe(fs.createWriteStream(process.argv[4]));
console.timeEnd('matched in');

console.log('different pixels: ' + diffs);
console.log('error: ' + (Math.round(100 * 100 * diffs / (diff.width * diff.height)) / 100) + '%');
console.log('error: ' + (Math.round(100 * 100 * diffs / (width * height)) / 100) + '%');

writeStream.on('close', function () {
process.exit(diffs ? 66 : 0);
});
var exitCode = diffs ? 66 : 0;

if (diff) {
diff.pack().pipe(fs.createWriteStream(diffPath)).on('close', function () {
process.exit(exitCode);
});
} else {
process.exit(exitCode);
}
}

0 comments on commit b03a368

Please sign in to comment.