diff --git a/lib/Image.js b/lib/Image.js index 7f2560ac..947bf42d 100644 --- a/lib/Image.js +++ b/lib/Image.js @@ -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; diff --git a/lib/obtain.js b/lib/obtain.js index eed2e178..6d85f13c 100644 --- a/lib/obtain.js +++ b/lib/obtain.js @@ -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"); diff --git a/src/image/crop_worker.cpp b/src/image/crop_worker.cpp index 6f9f7c46..809740ff 100644 --- a/src/image/crop_worker.cpp +++ b/src/image/crop_worker.cpp @@ -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; diff --git a/src/image/image.cpp b/src/image/image.cpp index e3fe18d6..70f758bc 100644 --- a/src/image/image.cpp +++ b/src/image/image.cpp @@ -29,9 +29,9 @@ void LwipImage::Init(Handle 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(data, width, height, 1, channels, false); + _cimg = new CImg(data, width, height, 1, N_CHANNELS, false); } LwipImage::~LwipImage() { @@ -50,24 +50,11 @@ NAN_METHOD(LwipImage::New) { // info[0] - pixels buffer // info[1,2] - width and height Local pixBuff = info[0].As(); - size_t width = Nan::To(info[1]).FromJust(); - size_t height = Nan::To(info[2]).FromJust(); - uint32_t channels = Nan::To(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("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()); } diff --git a/src/image/image.h b/src/image/image.h index 68437b47..c47cd798 100644 --- a/src/image/image.h +++ b/src/image/image.h @@ -4,6 +4,8 @@ #define cimg_display 0 #define cimg_verbosity 0 +#define N_CHANNELS 4 + #include #include #include @@ -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 constructor; diff --git a/src/image/init.cpp b/src/image/init.cpp index 76200f0f..090b912a 100644 --- a/src/image/init.cpp +++ b/src/image/init.cpp @@ -2,7 +2,6 @@ // create an init function for our node module void InitAll(Handle exports) { - Nan::HandleScope scope; LwipImage::Init(exports); } diff --git a/src/image/pad_worker.cpp b/src/image/pad_worker.cpp index 5f8b0b39..99637ca1 100644 --- a/src/image/pad_worker.cpp +++ b/src/image/pad_worker.cpp @@ -18,14 +18,13 @@ PadWorker::~PadWorker() {} void PadWorker::Execute () { CImg * 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(newwidth, newheight, 1, channels); + res = new CImg(newwidth, newheight, 1, N_CHANNELS); } catch (CImgException e) { SetErrorMessage("Out of memory"); return;