Skip to content

Commit

Permalink
closes #34. progress with #28
Browse files Browse the repository at this point in the history
  • Loading branch information
EyalAr committed Sep 11, 2014
1 parent 129c086 commit 60d370b
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 107 deletions.
54 changes: 1 addition & 53 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,6 @@
# LIB JPEG:
###########
"lib/jpeg/jmemnobs.c",
"lib/jpeg/jcapimin.c",
"lib/jpeg/jcapistd.c",
"lib/jpeg/jccoefct.c",
"lib/jpeg/jccolor.c",
"lib/jpeg/jcdctmgr.c",
"lib/jpeg/jchuff.c",
"lib/jpeg/jcinit.c",
"lib/jpeg/jcmainct.c",
"lib/jpeg/jcmarker.c",
"lib/jpeg/jcmaster.c",
"lib/jpeg/jcomapi.c",
"lib/jpeg/jcparam.c",
"lib/jpeg/jcprepct.c",
"lib/jpeg/jcsample.c",
"lib/jpeg/jdapimin.c",
"lib/jpeg/jdapistd.c",
"lib/jpeg/jdatadst.c",
Expand All @@ -48,10 +34,7 @@
"lib/jpeg/jidctint.c",
"lib/jpeg/jutils.c",
"lib/jpeg/jmemmgr.c",
"lib/jpeg/jcarith.c",
"lib/jpeg/jdarith.c",
"lib/jpeg/jquant1.c",
"lib/jpeg/jquant2.c",
"lib/jpeg/jdmerge.c",
"lib/jpeg/jaricom.c",
# LIB PNG:
Expand All @@ -61,31 +44,23 @@
"lib/png/pngget.c",
"lib/png/pngrutil.c",
"lib/png/pngtrans.c",
"lib/png/pngwutil.c",
"lib/png/pngread.c",
"lib/png/pngrio.c",
"lib/png/pngwio.c",
"lib/png/pngwrite.c",
"lib/png/pngrtran.c",
"lib/png/pngwtran.c",
"lib/png/pngmem.c",
"lib/png/pngerror.c",
"lib/png/pngpread.c",
# ZLIB:
#######
"lib/zlib/adler32.c",
"lib/zlib/compress.c",
"lib/zlib/crc32.c",
"lib/zlib/deflate.c",
"lib/zlib/gzclose.c",
"lib/zlib/gzlib.c",
"lib/zlib/gzread.c",
"lib/zlib/gzwrite.c",
"lib/zlib/infback.c",
"lib/zlib/inflate.c",
"lib/zlib/inftrees.c",
"lib/zlib/inffast.c",
"lib/zlib/trees.c",
"lib/zlib/uncompr.c",
"lib/zlib/zutil.c"
],
Expand Down Expand Up @@ -144,20 +119,6 @@
"lib/jpeg/jcparam.c",
"lib/jpeg/jcprepct.c",
"lib/jpeg/jcsample.c",
"lib/jpeg/jdapimin.c",
"lib/jpeg/jdapistd.c",
"lib/jpeg/jdatadst.c",
"lib/jpeg/jdatasrc.c",
"lib/jpeg/jdcoefct.c",
"lib/jpeg/jdcolor.c",
"lib/jpeg/jddctmgr.c",
"lib/jpeg/jdhuff.c",
"lib/jpeg/jdinput.c",
"lib/jpeg/jdmainct.c",
"lib/jpeg/jdmarker.c",
"lib/jpeg/jdmaster.c",
"lib/jpeg/jdpostct.c",
"lib/jpeg/jdsample.c",
"lib/jpeg/jerror.c",
"lib/jpeg/jfdctflt.c",
"lib/jpeg/jfdctfst.c",
Expand All @@ -168,7 +129,6 @@
"lib/jpeg/jutils.c",
"lib/jpeg/jmemmgr.c",
"lib/jpeg/jcarith.c",
"lib/jpeg/jdarith.c",
"lib/jpeg/jquant1.c",
"lib/jpeg/jquant2.c",
"lib/jpeg/jdmerge.c",
Expand All @@ -178,18 +138,13 @@
"lib/png/png.c",
"lib/png/pngset.c",
"lib/png/pngget.c",
"lib/png/pngrutil.c",
"lib/png/pngtrans.c",
"lib/png/pngwutil.c",
"lib/png/pngread.c",
"lib/png/pngrio.c",
"lib/png/pngwio.c",
"lib/png/pngwrite.c",
"lib/png/pngrtran.c",
"lib/png/pngwtran.c",
"lib/png/pngmem.c",
"lib/png/pngerror.c",
"lib/png/pngpread.c",
# ZLIB:
#######
"lib/zlib/adler32.c",
Expand All @@ -198,14 +153,8 @@
"lib/zlib/deflate.c",
"lib/zlib/gzclose.c",
"lib/zlib/gzlib.c",
"lib/zlib/gzread.c",
"lib/zlib/gzwrite.c",
"lib/zlib/infback.c",
"lib/zlib/inflate.c",
"lib/zlib/inftrees.c",
"lib/zlib/inffast.c",
"lib/zlib/trees.c",
"lib/zlib/uncompr.c",
"lib/zlib/zutil.c"
],
'include_dirs': [
Expand Down Expand Up @@ -239,12 +188,11 @@
}]
]
},{
"target_name": "lwip_image_builder",
"target_name": "lwip_image",
"sources": [
# LWIP:
#######
"src/image/init.cpp",
"src/image/build_worker.cpp",
"src/image/image.cpp",
],
'include_dirs': [
Expand Down
39 changes: 26 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
defs = require('./defs'),
decoder = require('./build/Release/lwip_decoder'),
encoder = require('./build/Release/lwip_encoder'),
image = require('./build/Release/lwip_image');
lwip_image = require('./build/Release/lwip_image');

var openers = [{
exts: ['jpg', 'jpeg'],
Expand All @@ -21,8 +21,8 @@
return v !== undefined;
}

function image(pixelsBuf) {
this.__lwip = lwipImage;
function image(pixelsBuf, width, height) {
this.__lwip = new lwip_image.LwipImage(pixelsBuf, width, height);
this.__locked = false;
}

Expand Down Expand Up @@ -270,10 +270,16 @@
params.quality = params.quality || defs.defaults.DEF_JPEG_QUALITY;
if (params.quality != parseInt(params.quality) || params.quality < 0 || params.quality > 100)
throw Error('Invalid JPEG quality');
return that.__lwip.toJpegBuffer(params.quality, function(err, buffer) {
that.__release();
callback(err, buffer);
});
return encoder.jpeg(
that.__lwip.buffer(),
that.__lwip.width(),
that.__lwip.height(),
params.quality,
function(err, buffer) {
that.__release();
callback(err, buffer);
}
);
} else if (type === 'png') {
params.compression = params.compression || defs.defaults.PNG_DEF_COMPRESSION;
if (params.compression === 'none') params.compression = 0;
Expand All @@ -282,10 +288,17 @@
else throw Error('Invalid PNG compression');
params.interlaced = params.interlaced || defs.defaults.PNG_DEF_INTERLACED;
if (typeof params.interlaced !== 'boolean') throw Error('PNG \'interlaced\' must be boolean');
return that.__lwip.toPngBuffer(params.compression, params.interlaced, function(err, buffer) {
that.__release();
callback(err, buffer);
});
return encoder.png(
that.__lwip.buffer(),
that.__lwip.width(),
that.__lwip.height(),
params.compression,
params.interlaced,
function(err, buffer) {
that.__release();
callback(err, buffer);
}
);
} else throw Error('Unknown type \'' + type + '\'');
});
} catch (e) {
Expand Down Expand Up @@ -509,8 +522,8 @@
function open() {
decree(defs.args.open)(arguments, function(impath, type, callback) {
type = type || path.extname(impath).slice(1);
getOpener(type)(impath, function(err, pixelsBuf) {
callback(err, err ? undefined : new image(pixelsBuf));
getOpener(type)(impath, function(err, pixelsBuf, width, height) {
callback(err, err ? undefined : new image(pixelsBuf, width, height));
});
});
}
Expand Down
20 changes: 0 additions & 20 deletions src/decoder/file_worker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,3 @@ void DecodeFileWorker::HandleOKCallback () {
};
callback->Call(6, argv);
}

NAN_METHOD(decodeJpegFile) {
NanScope();

NanUtf8String path = NanUtf8String(args[0]);
NanCallback * callback = new NanCallback(args[1].As<Function>());

NanAsyncQueueWorker(new DecodeFileWorker(callback, string(*path), "jpeg"));
NanReturnUndefined();
}

NAN_METHOD(decodePngFile) {
NanScope();

NanUtf8String path = NanUtf8String(args[0]);
NanCallback * callback = new NanCallback(args[1].As<Function>());

NanAsyncQueueWorker(new DecodeFileWorker(callback, string(*path), "png"));
NanReturnUndefined();
}
20 changes: 20 additions & 0 deletions src/decoder/init.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
#include "decoder.h"

NAN_METHOD(decodeJpegFile) {
NanScope();

NanUtf8String path = NanUtf8String(args[0]);
NanCallback * callback = new NanCallback(args[1].As<Function>());

NanAsyncQueueWorker(new DecodeFileWorker(callback, string(*path), "jpeg"));
NanReturnUndefined();
}

NAN_METHOD(decodePngFile) {
NanScope();

NanUtf8String path = NanUtf8String(args[0]);
NanCallback * callback = new NanCallback(args[1].As<Function>());

NanAsyncQueueWorker(new DecodeFileWorker(callback, string(*path), "png"));
NanReturnUndefined();
}

// create an init function for our node module
void InitAll(Handle<Object> exports) {
exports->Set(NanNew("jpegFile"),
Expand Down
15 changes: 13 additions & 2 deletions src/encoder/jpeg_worker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,18 @@ EncodeToJpegBufferWorker::EncodeToJpegBufferWorker(
size_t height,
int quality,
NanCallback * callback
): NanAsyncWorker(callback), _pixbuf(pixbuf), _width(width), _height(height),
_quality(quality), _jpegbuf(NULL), _jpegbufsize(0) {}
): NanAsyncWorker(callback), _width(width), _height(height),
_quality(quality), _jpegbuf(NULL), _jpegbufsize(0) {
// pixbuf needs to be copied, because the buffer may be gc'ed by
// V8 at any time.
_pixbuf = (unsigned char *) malloc(width * height * 3 * sizeof(unsigned char));
if (_pixbuf == NULL) {
// TODO: check - can I use SetErrorMessage here?
SetErrorMessage("Out of memory");
return;
}
memcpy(_pixbuf, pixbuf, width * height * 3 * sizeof(unsigned char));
}

EncodeToJpegBufferWorker::~EncodeToJpegBufferWorker() {}

Expand Down Expand Up @@ -44,6 +54,7 @@ void EncodeToJpegBufferWorker::Execute () {
jpeg_set_quality(&cinfo, _quality, TRUE);
jpeg_start_compress(&cinfo, TRUE);

// shared memory cimg. no new memory is allocated.
CImg<unsigned char> tmpimg(_pixbuf, _width, _height, 1, 3, true);
while (cinfo.next_scanline < cinfo.image_height) {
unsigned char * ptrd = tmp;
Expand Down
14 changes: 12 additions & 2 deletions src/encoder/png_worker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,19 @@ EncodeToPngBufferWorker::EncodeToPngBufferWorker(
int compression,
bool interlaced,
NanCallback * callback
): NanAsyncWorker(callback), _pixbuf(pixbuf), _width(width), _height(height),
): NanAsyncWorker(callback), _width(width), _height(height),
_compression(compression), _interlaced(interlaced), _pngbuf(NULL),
_pngbufsize(0) {}
_pngbufsize(0) {
// pixbuf needs to be copied, because the buffer may be gc'ed by
// V8 at any time.
_pixbuf = (unsigned char *) malloc(width * height * 3 * sizeof(unsigned char));
if (_pixbuf == NULL) {
// TODO: check - can I use SetErrorMessage here?
SetErrorMessage("Out of memory");
return;
}
memcpy(_pixbuf, pixbuf, width * height * 3 * sizeof(unsigned char));
}

EncodeToPngBufferWorker::~EncodeToPngBufferWorker() {}

Expand Down
1 change: 0 additions & 1 deletion src/image/build_worker.cpp

This file was deleted.

19 changes: 18 additions & 1 deletion src/image/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,29 @@

Persistent<FunctionTemplate> LwipImage::constructor;

void LwipImage::Init() {
void LwipImage::Init(Handle<Object> exports) {
// Prepare constructor template
Local<FunctionTemplate> tpl = NanNew<FunctionTemplate>(New);
tpl->InstanceTemplate()->SetInternalFieldCount(1);
tpl->SetClassName(NanNew("LwipImage"));
NODE_SET_PROTOTYPE_METHOD(tpl, "width", width);
NODE_SET_PROTOTYPE_METHOD(tpl, "height", height);
NODE_SET_PROTOTYPE_METHOD(tpl, "buffer", buffer);
NODE_SET_PROTOTYPE_METHOD(tpl, "resize", resize);
NODE_SET_PROTOTYPE_METHOD(tpl, "rotate", rotate);
NODE_SET_PROTOTYPE_METHOD(tpl, "blur", blur);
NODE_SET_PROTOTYPE_METHOD(tpl, "crop", crop);
NODE_SET_PROTOTYPE_METHOD(tpl, "mirror", mirror);
NODE_SET_PROTOTYPE_METHOD(tpl, "pad", pad);
NanAssignPersistent(constructor, tpl);
exports->Set(
NanNew("LwipImage"),
NanNew<FunctionTemplate>(constructor)->GetFunction()
);
}

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, 3, false);
}

Expand All @@ -41,6 +47,7 @@ NAN_METHOD(LwipImage::New) {
size_t width = args[1]->NumberValue();
size_t height = args[2]->NumberValue();
unsigned char * pixels = (unsigned char *)Buffer::Data(pixBuff);
// TODO: handle CImg exception
LwipImage * obj = new LwipImage(pixels, width, height);
obj->Wrap(args.This());
NanReturnValue(args.This());
Expand All @@ -65,6 +72,16 @@ Handle<Value> LwipImage::height(const Arguments & args) {
NanReturnValue(NanNew<Number>(obj->_cimg->height()));
}

// image.buffer():
// ---------------
Handle<Value> LwipImage::buffer(const Arguments & args) {
NanScope();
LwipImage * obj = ObjectWrap::Unwrap<LwipImage>(args.Holder());
// return a new buffer. don't use same memory an image. make a copy.
// image object may be gc'ed, but buffer needs to stay alive.
NanReturnValue(NanNewBufferHandle((char *)obj->_cimg->data(), obj->_cimg->size()));
}

// image.resize(width, height, inter, callback):
// ---------------------------------------------

Expand Down
3 changes: 2 additions & 1 deletion src/image/image.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ using namespace node;
*/
class LwipImage : public node::ObjectWrap {
public:
static void Init();
static void Init(Handle<Object> exports);
static Handle<Value> NewInstance();
static NAN_METHOD(New);
static NAN_METHOD(resize);
Expand All @@ -37,6 +37,7 @@ class LwipImage : public node::ObjectWrap {
static NAN_METHOD(pad);
static NAN_METHOD(width);
static NAN_METHOD(height);
static NAN_METHOD(buffer);
LwipImage(unsigned char * data, size_t width, size_t height);
~LwipImage();
CImg<unsigned char> * _cimg;
Expand Down
Loading

0 comments on commit 60d370b

Please sign in to comment.