Skip to content
This repository has been archived by the owner on May 1, 2020. It is now read-only.

Commit

Permalink
Merge pull request #6 from yahoo/image_buffer
Browse files Browse the repository at this point in the history
Add support for loading images from buffer
  • Loading branch information
marcelerz committed Nov 27, 2014
2 parents f7e394f + 8246e2d commit 46f449f
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 25 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG2.md → CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
78 changes: 60 additions & 18 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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);
});
});
});
});
Expand Down Expand Up @@ -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);
});
});
Expand Down
File renamed without changes
Binary file added test/test2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 46f449f

Please sign in to comment.