diff --git a/TODO b/TODO index b513d3e..ef53287 100644 --- a/TODO +++ b/TODO @@ -1,5 +1,3 @@ -Get rid of Image - - Replace with operations on Matrix and cv helper js functions - Matrix doesn't need to be eventemitter: @@ -10,17 +8,9 @@ Get rid of Image ############ Ideas ##################### - var mat = cv.readImage('foo.jpg'); - - //shortcut for cascade classifier - mat.detectFaces('data.xml', function(err, faces){ - for (var i=0;i target) { NODE_SET_PROTOTYPE_METHOD(constructor, "width", Width); NODE_SET_PROTOTYPE_METHOD(constructor, "height", Height); NODE_SET_PROTOTYPE_METHOD(constructor, "size", Size); - NODE_SET_PROTOTYPE_METHOD(constructor, "toBuffer", ToBuffer); NODE_SET_PROTOTYPE_METHOD(constructor, "ellipse", Ellipse); NODE_SET_PROTOTYPE_METHOD(constructor, "save", Save); @@ -64,17 +63,15 @@ Matrix::Matrix(int w, int h): ObjectWrap() { Handle Matrix::Empty(const Arguments& args){ - HandleScope scope; + SETUP_FUNCTION(Matrix) - Matrix *self = ObjectWrap::Unwrap(args.This()); return scope.Close(Boolean::New(self->mat.empty())); } Handle Matrix::Get(const Arguments& args){ - HandleScope scope; + SETUP_FUNCTION(Matrix) - Matrix *self = ObjectWrap::Unwrap(args.This()); int i = args[0]->IntegerValue(); int j = args[1]->IntegerValue(); @@ -84,9 +81,8 @@ Matrix::Get(const Arguments& args){ Handle Matrix::Set(const Arguments& args){ - HandleScope scope; + SETUP_FUNCTION(Matrix) - Matrix *self = ObjectWrap::Unwrap(args.This()); int i = args[0]->IntegerValue(); int j = args[1]->IntegerValue(); double val = args[2]->NumberValue(); @@ -99,9 +95,8 @@ Matrix::Set(const Arguments& args){ Handle Matrix::Size(const Arguments& args){ - HandleScope scope; + SETUP_FUNCTION(Matrix) - Matrix *self = ObjectWrap::Unwrap(args.This()); v8::Local arr = v8::Array::New(2); arr->Set(0, Number::New(self->mat.size().height)); arr->Set(1, Number::New(self->mat.size().width)); @@ -112,17 +107,15 @@ Matrix::Size(const Arguments& args){ Handle Matrix::Width(const Arguments& args){ - HandleScope scope; + SETUP_FUNCTION(Matrix) - Matrix *self = ObjectWrap::Unwrap(args.This()); return scope.Close(Number::New(self->mat.size().width)); } Handle Matrix::Height(const Arguments& args){ - HandleScope scope; + SETUP_FUNCTION(Matrix) - Matrix *self = ObjectWrap::Unwrap(args.This()); return scope.Close(Number::New(self->mat.size().height)); } @@ -130,9 +123,7 @@ Matrix::Height(const Arguments& args){ Handle Matrix::ToBuffer(const v8::Arguments& args){ - HandleScope scope; - - Matrix *self = ObjectWrap::Unwrap(args.This()); + SETUP_FUNCTION(Matrix) std::vector vec(0); std::vector params(0);//CV_IMWRITE_JPEG_QUALITY 90 @@ -155,9 +146,8 @@ Matrix::ToBuffer(const v8::Arguments& args){ // ellipse(x, y, wid, height, angle, startangle, endangle, color, thickness, linetype, shift) Handle Matrix::Ellipse(const v8::Arguments& args){ - HandleScope scope; + SETUP_FUNCTION(Matrix) - Matrix *self = ObjectWrap::Unwrap(args.This()); int x = args[0]->Uint32Value(); int y = args[1]->Uint32Value(); int width = args[2]->Uint32Value(); diff --git a/src/Matrix.h b/src/Matrix.h index e05dec4..0e5e0a1 100644 --- a/src/Matrix.h +++ b/src/Matrix.h @@ -52,19 +52,15 @@ class Matrix: public node::ObjectWrap { */ - static Handle Get(const Arguments& args); // at - static Handle Set(const Arguments& args); - - static Handle Size(const Arguments& args); - static Handle Width(const Arguments& args); - static Handle Height(const Arguments& args); - - static Handle ToBuffer(const Arguments& args); - - static Handle Ellipse(const Arguments& args); - static Handle Empty(const Arguments& args); - static Handle Save(const Arguments& args); - + JSFUNC(Get) // at + JSFUNC(Set) + JSFUNC(Size) + JSFUNC(Width) + JSFUNC(Height) + JSFUNC(ToBuffer) + JSFUNC(Ellipse) + JSFUNC(Empty) + JSFUNC(Save) }; diff --git a/src/OpenCV.cc b/src/OpenCV.cc index 64f21cb..953bcd4 100644 --- a/src/OpenCV.cc +++ b/src/OpenCV.cc @@ -20,38 +20,45 @@ OpenCV::Init(Handle target) { Handle OpenCV::ReadImage(const Arguments &args) { HandleScope scope; - - Local im_h = Matrix::constructor->GetFunction()->NewInstance(); - Matrix *img = ObjectWrap::Unwrap(im_h); - cv::Mat mat; - - if (args[0]->IsNumber() && args[1]->IsNumber()){ - int width, height; - - width = args[0]->Uint32Value(); - height = args[1]->Uint32Value(); - mat = *(new cv::Mat(width, height, CV_8UC1)); - - } else if (args[0]->IsString()) { + try{ - std::string filename = std::string(*v8::String::AsciiValue(args[0]->ToString())); - mat = cv::imread(filename, -1); + Local im_h = Matrix::constructor->GetFunction()->NewInstance(); + Matrix *img = ObjectWrap::Unwrap(im_h); + cv::Mat mat; - } else if (Buffer::HasInstance(args[0])){ - uint8_t *buf = (uint8_t *) Buffer::Data(args[0]->ToObject()); - unsigned len = Buffer::Length(args[0]->ToObject()); - cv::Mat *mbuf = new cv::Mat(len, 1, CV_8UC1, buf); - mat = cv::imdecode(*mbuf, -1); - - if (mat.empty()){ - return v8::ThrowException(v8::Exception::TypeError(v8::String::New("Error loading file"))); + if (args[0]->IsNumber() && args[1]->IsNumber()){ + int width, height; + + width = args[0]->Uint32Value(); + height = args[1]->Uint32Value(); + mat = *(new cv::Mat(width, height, CV_8UC1)); + + } else if (args[0]->IsString()) { + + std::string filename = std::string(*v8::String::AsciiValue(args[0]->ToString())); + mat = cv::imread(filename, -1); + + } else if (Buffer::HasInstance(args[0])){ + uint8_t *buf = (uint8_t *) Buffer::Data(args[0]->ToObject()); + unsigned len = Buffer::Length(args[0]->ToObject()); + + cv::Mat *mbuf = new cv::Mat(len, 1, CV_8UC1, buf); + mat = cv::imdecode(*mbuf, -1); + + if (mat.empty()){ + return v8::ThrowException(v8::Exception::TypeError(v8::String::New("Error loading file"))); + } } - } - img->mat = mat; - return scope.Close(im_h); + img->mat = mat; + return scope.Close(im_h); + + } catch( cv::Exception& e ){ + const char* err_msg = e.what(); + return v8::ThrowException(v8::Exception::Error(v8::String::New(err_msg))); + } }; diff --git a/src/OpenCV.h b/src/OpenCV.h index d62cb63..e64ddf4 100644 --- a/src/OpenCV.h +++ b/src/OpenCV.h @@ -20,6 +20,14 @@ using namespace node; Local VAR = Local::Cast(args[I]); +#define SETUP_FUNCTION(TYP) \ + HandleScope scope; \ + TYP *self = ObjectWrap::Unwrap(args.This()); + +#define JSFUNC(NAME) \ + static Handle NAME(const Arguments& args); + + class OpenCV: public node::ObjectWrap{ public: diff --git a/src/VideoCaptureWrap.cc b/src/VideoCaptureWrap.cc index a0589ab..20bba4c 100644 --- a/src/VideoCaptureWrap.cc +++ b/src/VideoCaptureWrap.cc @@ -51,19 +51,15 @@ VideoCaptureWrap::VideoCaptureWrap(int device){ Handle VideoCaptureWrap::GetFrame(const Arguments &args) { - HandleScope scope; - - VideoCaptureWrap *v = ObjectWrap::Unwrap(args.This()); + SETUP_FUNCTION(VideoCaptureWrap) cv::Mat frame; - v->cap.retrieve(frame); + self->cap.retrieve(frame); - - - Local im_h = Matrix::constructor->GetFunction()->NewInstance(); - Matrix *im = ObjectWrap::Unwrap(im_h); - im->mat = frame; - return scope.Close(im_h); + Local im_h = Matrix::constructor->GetFunction()->NewInstance(); + Matrix *im = ObjectWrap::Unwrap(im_h); + im->mat = frame; + return scope.Close(im_h); }