From 8246e2d9b638fe34a168f3823306e54c6537eddd Mon Sep 17 00:00:00 2001 From: Marcel Erz Date: Thu, 27 Nov 2014 13:59:02 -0800 Subject: [PATCH] Add support for loading images from buffer (#5) --- CHANGELOG2.md => CHANGELOG.md | 5 ++- README.md | 6 ++- index.js | 8 ++-- package.json | 2 +- test/index.js | 78 ++++++++++++++++++++++++++-------- test/{test.png => test1.png} | Bin test/test2.png | Bin 0 -> 119 bytes 7 files changed, 74 insertions(+), 25 deletions(-) rename CHANGELOG2.md => CHANGELOG.md (65%) rename test/{test.png => test1.png} (100%) create mode 100644 test/test2.png diff --git a/CHANGELOG2.md b/CHANGELOG.md similarity index 65% rename from CHANGELOG2.md rename to CHANGELOG.md index fa95f4d..b6d39aa 100644 --- a/CHANGELOG2.md +++ b/CHANGELOG.md @@ -1,7 +1,10 @@ Changelog ========= -V1.0.2 - (11/09/14) +v1.0.3 +* Image loading from Buffer + +v1.0.2 - (11/09/14) * Add shift feature for issues with anti-aliasing v1.0.1 - Initial release (11/04/14) diff --git a/README.md b/README.md index 22382f8..b977173 100644 --- a/README.md +++ b/README.md @@ -110,9 +110,9 @@ diff.run(function (error) { All the parameters that were available in the command-line tool are also available through the class constructor, however they might use slightly different wording. The class exposes additional parameters that are not available from the command-line: * ```imageAPath``` Defines the path to the first image that should be compared (required; imageAPath or imageA is required - see example below) -* ```imageA``` Supplies first image that should be compared (required; imageAPath or imageA is required - see example below) +* ```imageA``` Supplies first image that should be compared (required; imageAPath or imageA is required - see example below) - This can be a PNGImage instance or a Buffer instance with PNG data * ```imageBPath``` Defines the path to the second image that should be compared (required; imageBPath or imageB is required - see example below) -* ```imageB``` Supplies second image that should be compared (required; imageBPath or imageB is required - see example below) +* ```imageB``` Supplies second image that should be compared (required; imageBPath or imageB is required - see example below) - This can be a PNGImage instance or a Buffer instance with PNG data * ```imageOutputPath``` Defines the path to the output-file. If you leaves this one off, then this feature is turned-off. * ```verbose``` Verbose output (default: false) * ```thresholdType``` Type of threshold check. This can be BlinkDiff.THRESHOLD_PIXEL and BlinkDiff.THRESHOLD_PERCENT (default: BlinkDiff.THRESHOLD_PIXEL) @@ -244,6 +244,8 @@ Also, even if you simply gave us an idea for a feature and did not actually writ ##Contributors * [sarbbottam](https://github.com/sarbbottam) * Documentation +* [koola](https://github.com/koola) + * Image loading from Buffer ##Third-party libraries diff --git a/index.js b/index.js index 06bdb37..9b1c860 100644 --- a/index.js +++ b/index.js @@ -11,9 +11,9 @@ var assert = require('assert'), * @constructor * @class BlinkDiff * @param {object} options - * @param {PNGImage} options.imageA Image object of first image + * @param {PNGImage|Buffer} options.imageA Image object of first image * @param {string} options.imageAPath Path to first image - * @param {PNGImage} options.imageB Image object of second image + * @param {PNGImage|Buffer} options.imageB Image object of second image * @param {string} options.imageBPath Path to second image * @param {string} [options.imageOutputPath=undefined] Path to output image file * @param {string} [options.thresholdType=BlinkDiff.THRESHOLD_PIXEL] Defines the threshold of the comparison @@ -375,8 +375,10 @@ BlinkDiff.prototype = { * @private */ _loadImage: function (path, image) { - if (image) { + if (image instanceof PNGImage) { return image; + } else if (image instanceof Buffer) { + return Promise.denodeify(PNGImage.loadImage)(image); } else { return Promise.denodeify(PNGImage.readImage)(path); } diff --git a/package.json b/package.json index a184c07..6051d54 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "blink-diff", - "version": "1.0.2", + "version": "1.0.3", "description": "A lightweight image comparison tool", "license": "MIT", "main": "index.js", diff --git a/test/index.js b/test/index.js index fe6053d..eb50630 100644 --- a/test/index.js +++ b/test/index.js @@ -75,6 +75,21 @@ function generateImage (type) { return image; } +function compareBuffer (buf1, buf2) { + + if (buf1.length !== buf2.length) { + return false; + } + + for(var i = 0, len = buf1.length; i < len; i++) { + if (buf1[i] !== buf2[i]) { + return false; + } + } + + return true; +} + describe('Blink-Diff', function () { describe('Default values', function () { @@ -246,23 +261,54 @@ describe('Blink-Diff', function () { describe('_loadImage', function () { beforeEach(function () { - this.image = generateImage('small-1'); + this.image = generateImage('medium-2'); }); - it('should use already loaded image', function () { - var result = this.instance._loadImage("pathToFile", this.image); - expect(result).to.be.equal(this.image) + describe('from Path', function () { + + it('should use already loaded image', function () { + var result = this.instance._loadImage("pathToFile", this.image); + + expect(result).to.be.an.instanceof(PNGImage); + expect(result).to.be.equal(this.image) + }); }); - it('should load image when path given', function (done) { - var result = this.instance._loadImage(__dirname + '/test.png', undefined); + describe('from Image', function () { - expect(result).to.be.an.instanceof(Promise); + it('should load image when only path given', function (done) { + var result = this.instance._loadImage(__dirname + '/test2.png'); - result.then(function () { - done(); - }, function (err) { - done(err); + expect(result).to.be.an.instanceof(Promise); + + result.then(function (image) { + var compare = compareBuffer(image.getImage().data, this.image.getImage().data); + expect(compare).to.be.true; + done(); + }.bind(this)).then(null, function (err) { + done(err); + }); + }); + }); + + describe('from Buffer', function () { + + beforeEach(function () { + this.buffer = fs.readFileSync(__dirname + '/test2.png'); + }); + + it('should load image from buffer if given', function () { + var result = this.instance._loadImage("pathToFile", this.buffer); + + expect(result).to.be.an.instanceof(Promise); + + result.then(function (image) { + var compare = compareBuffer(image.getImage().data, this.image.getImage().data); + expect(compare).to.be.true; + done(); + }.bind(this)).then(null, function (err) { + done(err); + }); }); }); }); @@ -698,13 +744,9 @@ describe('Blink-Diff', function () { expect(promise).to.be.instanceof(Promise); promise.then(function (result) { - try { - expect(result.code).to.be.equal(BlinkDiff.RESULT_DIFFERENT); - done(); - } catch (err) { - done(err); - } - }, function (err) { + expect(result.code).to.be.equal(BlinkDiff.RESULT_DIFFERENT); + done(); + }).then(null, function (err) { done(err); }); }); diff --git a/test/test.png b/test/test1.png similarity index 100% rename from test/test.png rename to test/test1.png diff --git a/test/test2.png b/test/test2.png new file mode 100644 index 0000000000000000000000000000000000000000..df3b74ab5633d5239af70fe35ec53559bbaff556 GIT binary patch literal 119 zcmeAS@N?(olHy`uVBq!ia0vp^%plCc1|-8Yw(bW~Or9=|Ar*{or0OmKc?KXJqdLQl z|IG3HOQo2tkFH;uxN<-9gIlJ}e$PL=E&097oO$ts{#}!}nm)H1{SMCe6XDLg`zRl% Om%-E3&t;ucLK6Tta3-7p literal 0 HcmV?d00001