Skip to content

Commit

Permalink
pass in a raw pixelBuffer to the open command
Browse files Browse the repository at this point in the history
  • Loading branch information
wouterverweirder committed Dec 10, 2014
1 parent 9a126b4 commit 9de8fae
Show file tree
Hide file tree
Showing 4 changed files with 82 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, trans: true }, 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', 'hash'],
optional: true
}, {
name: 'callback',
Expand Down
14 changes: 10 additions & 4 deletions lib/obtain.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,16 @@
});
});
} 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') {
if(type.width && type.height) {
callback(null, new Image(source, type.width, type.height, type.trans));
} else throw Error("Missing width and height");
} 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
20 changes: 20 additions & 0 deletions tests/02.operations/001.open.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,24 @@ describe('lwip.open', function() {
});

});

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

describe('raw pixel buffer', 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, trans: true }, function(err, img) {
should(err).not.be.Error;
img.should.be.OK;
done();
});
});
});

});
});

0 comments on commit 9de8fae

Please sign in to comment.