From b03a3684cec86f4aa5fb56be947ffeb941d86b50 Mon Sep 17 00:00:00 2001 From: Vladimir Agafonkin Date: Thu, 6 Jun 2019 20:32:00 +0300 Subject: [PATCH] make diff optional in cli; clean up --- bin/pixelmatch | 47 +++++++++++++++++++++++++++-------------------- 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/bin/pixelmatch b/bin/pixelmatch index 135c2b3..3d8c00a 100755 --- a/bin/pixelmatch +++ b/bin/pixelmatch @@ -3,17 +3,18 @@ '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); @@ -21,27 +22,33 @@ var img2 = fs.createReadStream(process.argv[3]).pipe(new PNG()).on('parsed', don 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); + } }