Skip to content

Commit

Permalink
Merge pull request #86 from wouterverweirder/version/0.0.6
Browse files Browse the repository at this point in the history
pass in a raw pixelBuffer to the open command
  • Loading branch information
EyalAr committed Dec 11, 2014
2 parents 9a126b4 + f13c07c commit 4d9c498
Show file tree
Hide file tree
Showing 6 changed files with 241 additions and 5 deletions.
51 changes: 51 additions & 0 deletions examples/open_pixelbuffer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* Example for using LWIP to open a raw pixel buffer
*/

var path = require('path'),
lwip = require('../');

var w = 90;
var h = 90;
var channelSize = w * h;
var size = channelSize * 4;
var redChannelEnd = channelSize * 1;
var greenChannelEnd = channelSize * 2;
var blueChannelEnd = channelSize * 3;
var alphaChannelEnd = channelSize * 4;
var i, x;

var buffer = new Buffer(size);
for(i = blueChannelEnd; i < alphaChannelEnd; i++) {
buffer[i] = 100;
}
for(var y = 0; y < h; y++) {
for(x = 0; x < 30; x++) {
i = y * w + x;
buffer[i] = 255;
buffer[i + redChannelEnd] = 0;
buffer[i + greenChannelEnd] = 0;
}
for(x = 30; x < 60; x++) {
i = y * w + x;
buffer[i] = 0;
buffer[i + redChannelEnd] = 255;
buffer[i + greenChannelEnd] = 0;
}
for(x = 60; x < 90; x++) {
i = y * w + x;
buffer[i] = 0;
buffer[i + redChannelEnd] = 0;
buffer[i + greenChannelEnd] = 255;
}
}

lwip.open(buffer, { width: w, height: h }, function(err, image) {
if (err) return console.log("err open", err);
image.batch()
.blur(9)
.writeFile('image_from_pixelbuffer.png', function(err){
if (err) return console.log("err write", err);
console.log('done');
});
});
2 changes: 1 addition & 1 deletion lib/defs.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
type: '*'
}, {
name: 'type',
type: 'string',
types: ['string', 'raw-buffer-properties'],
optional: true
}, {
name: 'callback',
Expand Down
17 changes: 13 additions & 4 deletions lib/obtain.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,19 @@
});
});
} else if (source instanceof Buffer) {
var opener = getOpener(type);
opener(source, function(err, pixelsBuf, width, height, channels, trans) {
callback(err, err ? null : new Image(pixelsBuf, width, height, trans));
});
if(typeof type === 'object') {
var channelSize = type.width * type.height;
var numChannels = source.length / channelSize;
if (numChannels !== parseInt(numChannels) || numChannels < 1 || numChannels > 4) {
throw Error("Buffer size does not match width and height");
}
callback(null, new Image(source, type.width, type.height, (numChannels % 2 === 0)));
} else {
var opener = getOpener(type);
opener(source, function(err, pixelsBuf, width, height, channels, trans) {
callback(err, err ? null : new Image(pixelsBuf, width, height, trans));
});
}
} else throw Error("Invalid source");
});
}
Expand Down
11 changes: 11 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
decree.register('interpolation', validateInterpolation);
decree.register('axes', validateAxes);
decree.register('image', validateImage);
decree.register('raw-buffer-properties', validateRawBufferProperties);

function undefinedFilter(v) {
return v !== undefined;
Expand Down Expand Up @@ -49,6 +50,16 @@
return true;
}

function validateRawBufferProperties(rawBufferProperties) {
if (!(rawBufferProperties.width === parseInt(rawBufferProperties.width) && rawBufferProperties.width > 0)){
return false;
}
if (!(rawBufferProperties.height === parseInt(rawBufferProperties.height) && rawBufferProperties.height > 0)){
return false;
}
return true;
}

function normalizeColor(color) {
if (typeof color === 'string') {
color = defs.colors[color];
Expand Down
97 changes: 97 additions & 0 deletions tests/00.argsValidation/001.open.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,101 @@ describe('lwip.open arguments validation', function() {

});

describe('pixelbuffer', function() {

var buffer;
before(function(done) {
buffer = new Buffer(120 * 120);
done();
});

describe('without width', function() {
it('should throw an error', function() {
lwip.open.bind(lwip, buffer, { height: 120 }, function() {}).should.throwError();
});
});

describe('without height', function() {
it('should throw an error', function() {
lwip.open.bind(lwip, buffer, { width: 120 }, function() {}).should.throwError();
});
});

describe('without width and height', function() {
it('should throw an error', function() {
lwip.open.bind(lwip, buffer, { }, function() {}).should.throwError();
});
});

describe('with non numeric width', function() {
it('should throw an error', function() {
lwip.open.bind(lwip, buffer, { width: "lorem", height: 120 }, function() {}).should.throwError();
});
});

describe('with non numeric height', function() {
it('should throw an error', function() {
lwip.open.bind(lwip, buffer, { width: 120, height: "lorem" }, function() {}).should.throwError();
});
});

describe('with non numeric width and height', function() {
it('should throw an error', function() {
lwip.open.bind(lwip, buffer, { width: "lorem", height: "ipsum" }, function() {}).should.throwError();
});
});

describe('with negative width', function() {
it('should throw an error', function() {
lwip.open.bind(lwip, buffer, { width: -120, height: 120 }, function() {}).should.throwError();
});
});

describe('with negative height', function() {
it('should throw an error', function() {
lwip.open.bind(lwip, buffer, { width: 120, height: -120 }, function() {}).should.throwError();
});
});

describe('with negative width and height', function() {
it('should throw an error', function() {
lwip.open.bind(lwip, buffer, { width: -120, height: -120 }, function() {}).should.throwError();
});
});

describe('with incorrect width and height', function() {
it('should throw an error', function() {
lwip.open.bind(lwip, buffer, { width: 123, height: 321 }, function() {}).should.throwError();
});
});

describe('with correct width and height for 1 channel', function() {
it('should succeed', function() {
lwip.open.bind(lwip, buffer, { width: 120, height: 120 }, function() {}).should.not.throw();
});
});

describe('with correct width and height for 2 channels', function() {
var newBuffer = new Buffer(120 * 120 * 2)
it('should succeed', function() {
lwip.open.bind(lwip, newBuffer, { width: 120, height: 120 }, function() {}).should.not.throw();
});
});

describe('with correct width and height for 3 channels', function() {
var newBuffer = new Buffer(120 * 120 * 3)
it('should succeed', function() {
lwip.open.bind(lwip, newBuffer, { width: 120, height: 120 }, function() {}).should.not.throw();
});
});

describe('with correct width and height for 4 channels', function() {
var newBuffer = new Buffer(120 * 120 * 4)
it('should succeed', function() {
lwip.open.bind(lwip, newBuffer, { width: 120, height: 120 }, function() {}).should.not.throw();
});
});

});

});
68 changes: 68 additions & 0 deletions tests/02.operations/001.open.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,72 @@ describe('lwip.open', function() {
});

});

describe('raw pixel buffer', function() {

describe('grayscale image', function(){
var buffer;
before(function(done) {
buffer = new Buffer(100 * 100);
done();
});

it('should succeed', function(done) {
lwip.open(buffer, { width: 100, height: 100 }, function(err, img) {
should(err).not.be.Error;
img.should.be.OK;
done();
});
});
});

describe('grayscale image with alpha', function(){
var buffer;
before(function(done) {
buffer = new Buffer(100 * 100 * 2);
done();
});

it('should succeed', function(done) {
lwip.open(buffer, { width: 100, height: 100 }, function(err, img) {
should(err).not.be.Error;
img.should.be.OK;
done();
});
});
});

describe('rgb image', function(){
var buffer;
before(function(done) {
buffer = new Buffer(100 * 100 * 3);
done();
});

it('should succeed', function(done) {
lwip.open(buffer, { width: 100, height: 100 }, function(err, img) {
should(err).not.be.Error;
img.should.be.OK;
done();
});
});
});

describe('rgb image with alpha', function(){
var buffer;
before(function(done) {
buffer = new Buffer(100 * 100 * 4);
done();
});

it('should succeed', function(done) {
lwip.open(buffer, { width: 100, height: 100 }, function(err, img) {
should(err).not.be.Error;
img.should.be.OK;
done();
});
});
});

});
});

0 comments on commit 4d9c498

Please sign in to comment.