async PNG read to support Node 0.10

This commit is contained in:
Vladimir Agafonkin 2015-10-18 00:11:52 +03:00
parent f3b4148ef6
commit ac726128dd
2 changed files with 28 additions and 26 deletions

View File

@ -6,26 +6,26 @@ var PNG = require('pngjs2').PNG,
match = require('../.');
if (process.argv.length < 5) {
console.log('Usage: imagematch image1.png image2.png output.png [threshold=0.005] [includeAA=false]');
return;
return console.log('Usage: imagematch image1.png image2.png output.png [threshold=0.005] [includeAA=false]');
}
var path1 = process.argv[2],
path2 = process.argv[3],
pathOut = process.argv[4],
threshold = +process.argv[5],
var threshold = +process.argv[5],
includeAA = process.argv[6] === 'true';
var img1 = PNG.sync.read(fs.readFileSync(path1));
var img2 = PNG.sync.read(fs.readFileSync(path2));
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);
var diff = new PNG({width: img1.width, height: img1.height});
function doneReading() {
if (!img1.data || !img2.data) return;
console.time('match');
var diffs = match(img1.data, img2.data, diff.data, diff.width, diff.height, threshold, includeAA);
console.timeEnd('match');
var diff = new PNG({width: this.width, height: this.height});
diff.pack().pipe(fs.createWriteStream(pathOut));
console.time('match');
var diffs = match(img1.data, this.data, diff.data, this.width, this.height, threshold, includeAA);
console.timeEnd('match');
console.log('different pixels: ' + diffs);
console.log('error: ' + (Math.round(100 * 100 * diffs / (diff.width * diff.height)) / 100) + '%');
diff.pack().pipe(fs.createWriteStream(process.argv[4]));
console.log('different pixels: ' + diffs);
console.log('error: ' + (Math.round(100 * 100 * diffs / (diff.width * diff.height)) / 100) + '%');
}

View File

@ -13,20 +13,22 @@ function diffTest(imgPath1, imgPath2, diffPath, threshold, includeAA, expectedMi
', threshold: ' + threshold + ', includeAA: ' + includeAA;
test(name, function (t) {
var img1 = readImage(imgPath1);
var img2 = readImage(imgPath2);
var expectedDiff = readImage(diffPath);
var img1 = readImage(imgPath1, function () {
var img2 = readImage(imgPath2, function () {
var expectedDiff = readImage(diffPath, function () {
var diff = new PNG({width: img1.width, height: img1.height});
var mismatch = match(img1.data, img2.data, diff.data, diff.width, diff.height, threshold, includeAA);
var diff = new PNG({width: img1.width, height: img1.height});
var mismatch = match(img1.data, img2.data, diff.data, diff.width, diff.height, threshold, includeAA);
t.same(diff.data, expectedDiff.data, 'diff image');
t.same(mismatch, expectedMismatch, 'number of mismatched pixels');
t.same(diff.data, expectedDiff.data, 'diff image');
t.same(mismatch, expectedMismatch, 'number of mismatched pixels');
t.end();
t.end();
});
});
});
});
}
function readImage(name) {
return PNG.sync.read(fs.readFileSync(path.join(__dirname, '/fixtures/' + name + '.png')));
function readImage(name, done) {
return fs.createReadStream(path.join(__dirname, '/fixtures/' + name + '.png')).pipe(new PNG()).on('parsed', done);
}