Skip to content

Commit

Permalink
Merge pull request #4 from weareu/master
Browse files Browse the repository at this point in the history
This should be the last one.
  • Loading branch information
kant2002 committed Oct 3, 2015
2 parents 404d0ed + 5975b1f commit f37cc0e
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 31 deletions.
5 changes: 2 additions & 3 deletions lib/Image.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

var lwip_image = require('../build/Release/lwip_image');

function Image(pixelsBuf, width, height, trans, metadata, channels) {
if (!channels) { channels = 4; } //4 assumed if none provided
this.__lwip = new lwip_image.LwipImage(pixelsBuf, width, height, channels);
function Image(pixelsBuf, width, height, trans, metadata) {
this.__lwip = new lwip_image.LwipImage(pixelsBuf, width, height);
this.__locked = false;
this.__trans = trans;
this.__metadata = metadata;
Expand Down
4 changes: 2 additions & 2 deletions lib/obtain.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@
if (numChannels !== parseInt(numChannels) || numChannels < 1 || numChannels > 4)
throw Error("Buffer size does not match width and height");
if (numChannels !== 4) source = util.makeRgbaBuffer(source, numChannels);
callback(null, new Image(source, type.width, type.height, (numChannels % 2 === 0), null, numChannels));
callback(null, new Image(source, type.width, type.height, (numChannels % 2 === 0)));
} else if (typeof type === 'string') {
// it's an encoded image
opener = getOpener(type);
opener(source, function(err, pixelsBuf, width, height, channels, trans, metadata) {
callback(err, err ? null : new Image(pixelsBuf, width, height, trans, metadata, channels));
callback(err, err ? null : new Image(pixelsBuf, width, height, trans, metadata));
});
} else throw Error('Invalid type');
} else throw Error("Invalid source");
Expand Down
3 changes: 1 addition & 2 deletions src/image/crop_worker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ CropWorker::~CropWorker() {}

void CropWorker::Execute () {
try {
int channels = _cimg->spectrum();
_cimg->crop(_left, _top, 0, 0, _right, _bottom, 0, channels - 1);
_cimg->crop(_left, _top, 0, 0, _right, _bottom, 0, N_CHANNELS - 1);
} catch (CImgException e) {
SetErrorMessage("Unable to crop image");
return;
Expand Down
25 changes: 6 additions & 19 deletions src/image/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ void LwipImage::Init(Handle<Object> exports) {
);
}

LwipImage::LwipImage(unsigned char * data, size_t width, size_t height, int channels) {
LwipImage::LwipImage(unsigned char * data, size_t width, size_t height) {
// TODO: CImg constructor may throw an exception. handle it in LwipImage::New.
_cimg = new CImg<unsigned char>(data, width, height, 1, channels, false);
_cimg = new CImg<unsigned char>(data, width, height, 1, N_CHANNELS, false);
}

LwipImage::~LwipImage() {
Expand All @@ -50,24 +50,11 @@ NAN_METHOD(LwipImage::New) {
// info[0] - pixels buffer
// info[1,2] - width and height
Local<Object> pixBuff = info[0].As<Object>();
size_t width = Nan::To<uint32_t>(info[1]).FromJust();
size_t height = Nan::To<uint32_t>(info[2]).FromJust();
uint32_t channels = Nan::To<uint32_t>(info[3]).FromJust();

unsigned char * pixels = (unsigned char *)Buffer::Data(pixBuff);
size_t len = Buffer::Length(pixBuff);

//CImg expects the size of the buffer to match width*height as it will memcpy this much from buffer.
//If it doesnt' match buffer length then throw so that CImg does not read past our buffer.
int expectedSize = sizeof(unsigned char) * width * height * 1 * channels;
if (expectedSize != len)
{
Nan::ThrowError(Nan::New<String>("Invalid image size, channels or buffer provided.").ToLocalChecked());
return;
}

size_t width = info[1]->NumberValue();
size_t height = info[2]->NumberValue();
unsigned char * pixels = (unsigned char *)Buffer::Data(pixBuff);
// TODO: handle CImg exception
LwipImage * obj = new LwipImage(pixels, width, height, channels);
LwipImage * obj = new LwipImage(pixels, width, height);
obj->Wrap(info.This());
info.GetReturnValue().Set(info.This());
}
Expand Down
5 changes: 3 additions & 2 deletions src/image/image.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#define cimg_display 0
#define cimg_verbosity 0

#define N_CHANNELS 4

#include <string>
#include <cmath>
#include <node.h>
Expand Down Expand Up @@ -40,11 +42,10 @@ class LwipImage : public node::ObjectWrap {
static NAN_METHOD(paste);
static NAN_METHOD(width);
static NAN_METHOD(height);
static NAN_METHOD(channels);
static NAN_METHOD(getPixel);
static NAN_METHOD(buffer);
static NAN_METHOD(setPixel);
LwipImage(unsigned char * data, size_t width, size_t height, int channels);
LwipImage(unsigned char * data, size_t width, size_t height);
~LwipImage();
private:
static Nan::Persistent<v8::FunctionTemplate> constructor;
Expand Down
1 change: 0 additions & 1 deletion src/image/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

// create an init function for our node module
void InitAll(Handle<Object> exports) {
Nan::HandleScope scope;
LwipImage::Init(exports);
}

Expand Down
3 changes: 1 addition & 2 deletions src/image/pad_worker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@ PadWorker::~PadWorker() {}

void PadWorker::Execute () {
CImg<unsigned char> * res;
int channels = _cimg->spectrum();
size_t oldwidth = _cimg->width(),
oldheight = _cimg->height(),
newwidth = oldwidth + _left + _right,
newheight = oldheight + _top + _bottom;
if (oldwidth != newwidth || oldheight != newheight) {
try {
res = new CImg<unsigned char>(newwidth, newheight, 1, channels);
res = new CImg<unsigned char>(newwidth, newheight, 1, N_CHANNELS);
} catch (CImgException e) {
SetErrorMessage("Out of memory");
return;
Expand Down

0 comments on commit f37cc0e

Please sign in to comment.