- migration of OpenCV.h/.cc done

- migration of init.cc done
- migration of Matrix.cc about half done
- migration of Contours.cc in progress
This commit is contained in:
Mark Moissette 2014-09-20 16:00:20 +02:00
parent cd303829de
commit d0351f336f
5 changed files with 316 additions and 1112 deletions

View File

@ -1,5 +1,6 @@
#include "Contours.h"
#include "OpenCV.h"
#include <nan.h>
#include <iostream>
@ -8,56 +9,63 @@ v8::Persistent<FunctionTemplate> Contour::constructor;
void
Contour::Init(Handle<Object> target) {
HandleScope scope;
NanScope();
//Class
v8::Local<v8::FunctionTemplate> m = v8::FunctionTemplate::New(New);
/*v8::Local<v8::FunctionTemplate> m = v8::FunctionTemplate::New(New);
m->SetClassName(v8::String::NewSymbol("Contours"));
// Constructor
constructor = Persistent<FunctionTemplate>::New(m);
constructor->InstanceTemplate()->SetInternalFieldCount(1);
constructor->SetClassName(String::NewSymbol("Contours"));
constructor->SetClassName(String::NewSymbol("Contours"));*/
Local<FunctionTemplate> ctor = NanNew<FunctionTemplate>(Contour::New);
NanAssignPersistent(constructor, ctor);
ctor->InstanceTemplate()->SetInternalFieldCount(1);
ctor->SetClassName(NanNew("Contours"));
// Prototype
//Local<ObjectTemplate> proto = constructor->PrototypeTemplate();
NODE_SET_PROTOTYPE_METHOD(constructor, "point", Point);
NODE_SET_PROTOTYPE_METHOD(constructor, "size", Size);
NODE_SET_PROTOTYPE_METHOD(constructor, "cornerCount", CornerCount);
NODE_SET_PROTOTYPE_METHOD(constructor, "area", Area);
NODE_SET_PROTOTYPE_METHOD(constructor, "arcLength", ArcLength);
NODE_SET_PROTOTYPE_METHOD(constructor, "approxPolyDP", ApproxPolyDP);
NODE_SET_PROTOTYPE_METHOD(constructor, "convexHull", ConvexHull);
NODE_SET_PROTOTYPE_METHOD(constructor, "boundingRect", BoundingRect);
NODE_SET_PROTOTYPE_METHOD(constructor, "minAreaRect", MinAreaRect);
NODE_SET_PROTOTYPE_METHOD(constructor, "isConvex", IsConvex);
NODE_SET_PROTOTYPE_METHOD(constructor, "moments", Moments);
target->Set(String::NewSymbol("Contours"), m->GetFunction());
NODE_SET_PROTOTYPE_METHOD(ctor, "point", Point);
NODE_SET_PROTOTYPE_METHOD(ctor, "size", Size);
NODE_SET_PROTOTYPE_METHOD(ctor, "cornerCount", CornerCount);
NODE_SET_PROTOTYPE_METHOD(ctor, "area", Area);
NODE_SET_PROTOTYPE_METHOD(ctor, "arcLength", ArcLength);
NODE_SET_PROTOTYPE_METHOD(ctor, "approxPolyDP", ApproxPolyDP);
NODE_SET_PROTOTYPE_METHOD(ctor, "convexHull", ConvexHull);
NODE_SET_PROTOTYPE_METHOD(ctor, "boundingRect", BoundingRect);
NODE_SET_PROTOTYPE_METHOD(ctor, "minAreaRect", MinAreaRect);
NODE_SET_PROTOTYPE_METHOD(ctor, "isConvex", IsConvex);
NODE_SET_PROTOTYPE_METHOD(ctor, "moments", Moments);
//target->Set(String::NewSymbol("Contours"), m->GetFunction());
target->Set(NanNew("Contours"), ctor->GetFunction());
};
NAN_METHOD(Contour::New() {
HandleScope scope;
NAN_METHOD(Contour::New) {
NanScope();
if (args.This()->InternalFieldCount() == 0)
return v8::ThrowException(v8::Exception::TypeError(v8::String::New("Cannot instantiate without new")));
NanThrowTypeError("Cannot instantiate without new");
Contour *contours;
contours = new Contour;
contours->Wrap(args.Holder());
return scope.Close(args.Holder());
NanReturnValue(args.Holder());
}
Contour::Contour(): ObjectWrap() {
}
NAN_METHOD(Contour::Point() {
HandleScope scope;
NAN_METHOD(Contour::Point) {
NanScope();
Contour *self = ObjectWrap::Unwrap<Contour>(args.This());
int pos = args[0]->NumberValue();
@ -72,19 +80,20 @@ NAN_METHOD(Contour::Point() {
return scope.Close(data);
}
/*
// FIXME: this sould better be called "Length" as ``Contours`` is an Array like structure
// also, this would allow to use ``Size`` for the function returning the number of corners
// in the contour for better consistency with OpenCV.
NAN_METHOD(Contour::Size() {
HandleScope scope;
NAN_METHOD(Contour::Size) {
NanScope();
Contour *self = ObjectWrap::Unwrap<Contour>(args.This());
return scope.Close(Number::New(self->contours.size()));
}
NAN_METHOD(Contour::CornerCount() {
HandleScope scope;
NAN_METHOD(Contour::CornerCount) {
NanScope();
Contour *self = ObjectWrap::Unwrap<Contour>(args.This());
int pos = args[0]->NumberValue();
@ -92,8 +101,8 @@ NAN_METHOD(Contour::CornerCount() {
return scope.Close(Number::New(self->contours[pos].size()));
}
NAN_METHOD(Contour::Area() {
HandleScope scope;
NAN_METHOD(Contour::Area) {
NanScope();
Contour *self = ObjectWrap::Unwrap<Contour>(args.This());
int pos = args[0]->NumberValue();
@ -103,8 +112,8 @@ NAN_METHOD(Contour::Area() {
}
NAN_METHOD(Contour::ArcLength() {
HandleScope scope;
NAN_METHOD(Contour::ArcLength) {
NanScope();
Contour *self = ObjectWrap::Unwrap<Contour>(args.This());
int pos = args[0]->NumberValue();
@ -114,8 +123,8 @@ NAN_METHOD(Contour::ArcLength() {
}
NAN_METHOD(Contour::ApproxPolyDP() {
HandleScope scope;
NAN_METHOD(Contour::ApproxPolyDP) {
NanScope();
Contour *self = ObjectWrap::Unwrap<Contour>(args.This());
int pos = args[0]->NumberValue();
@ -130,8 +139,8 @@ NAN_METHOD(Contour::ApproxPolyDP() {
}
NAN_METHOD(Contour::ConvexHull() {
HandleScope scope;
NAN_METHOD(Contour::ConvexHull) {
NanScope();
Contour *self = ObjectWrap::Unwrap<Contour>(args.This());
@ -146,8 +155,8 @@ NAN_METHOD(Contour::ConvexHull() {
}
NAN_METHOD(Contour::BoundingRect() {
HandleScope scope;
NAN_METHOD(Contour::BoundingRect) {
NanScope();
Contour *self = ObjectWrap::Unwrap<Contour>(args.This());
int pos = args[0]->NumberValue();
@ -164,8 +173,8 @@ NAN_METHOD(Contour::BoundingRect() {
}
NAN_METHOD(Contour::MinAreaRect() {
HandleScope scope;
NAN_METHOD(Contour::MinAreaRect) {
NanScope();
Contour *self = ObjectWrap::Unwrap<Contour>(args.This());
int pos = args[0]->NumberValue();
@ -202,8 +211,8 @@ NAN_METHOD(Contour::MinAreaRect() {
}
NAN_METHOD(Contour::IsConvex() {
HandleScope scope;
NAN_METHOD(Contour::IsConvex) {
NanScope();
Contour *self = ObjectWrap::Unwrap<Contour>(args.This());
int pos = args[0]->NumberValue();
@ -211,8 +220,8 @@ NAN_METHOD(Contour::IsConvex() {
return scope.Close(Boolean::New(isContourConvex(cv::Mat(self->contours[pos]))));
}
NAN_METHOD(Contour::Moments() {
HandleScope scope;
NAN_METHOD(Contour::Moments) {
NanScope();
Contour *self = ObjectWrap::Unwrap<Contour>(args.This());
int pos = args[0]->NumberValue();
@ -229,4 +238,4 @@ NAN_METHOD(Contour::Moments() {
return scope.Close(res);
}
*/

File diff suppressed because it is too large Load Diff

View File

@ -1,30 +1,29 @@
#include "OpenCV.h"
#include "Matrix.h"
#include <nan.h>
void
OpenCV::Init(Handle<Object> target) {
HandleScope scope;
NanScope();
// Version string.
char out [21];
int n = sprintf(out, "%i.%i", CV_MAJOR_VERSION, CV_MINOR_VERSION);
target->Set(String::NewSymbol("version"), String::New(out, n));
target->Set(NanNew<String>("version"), NanNew<String>(out, n));
NODE_SET_METHOD(target, "readImage", ReadImage);
}
Handle<Value>
OpenCV::ReadImage() {
HandleScope scope;
NAN_METHOD(OpenCV::ReadImage) {
NanEscapableScope();
try{
Local<Object> im_h = Matrix::constructor->GetFunction()->NewInstance();
Local<Object> im_h = NanNew(Matrix::constructor)->GetFunction()->NewInstance();
Matrix *img = ObjectWrap::Unwrap<Matrix>(im_h);
cv::Mat mat;
REQ_FUN_ARG(1, cb);
@ -38,7 +37,7 @@ OpenCV::ReadImage() {
} else if (args[0]->IsString()) {
std::string filename = std::string(*v8::String::AsciiValue(args[0]->ToString()));
std::string filename = std::string(*NanAsciiString(args[0]->ToString()));
mat = cv::imread(filename);
} else if (Buffer::HasInstance(args[0])){
@ -49,7 +48,7 @@ OpenCV::ReadImage() {
mat = cv::imdecode(*mbuf, -1);
if (mat.empty()){
return v8::ThrowException(v8::Exception::TypeError(v8::String::New("Error loading file")));
NanThrowTypeError("Error loading file");
}
}
@ -57,23 +56,21 @@ OpenCV::ReadImage() {
Local<Value> argv[2];
argv[0] = Local<Value>::New(Null());
argv[0] = NanNull();
argv[1] = im_h;
TryCatch try_catch;
cb->Call(Context::GetCurrent()->Global(), 2, argv);
cb->Call(NanGetCurrentContext()->Global(), 2, argv);
if (try_catch.HasCaught()) {
FatalException(try_catch);
}
return Undefined();
NanReturnUndefined();
} catch( cv::Exception& e ){
const char* err_msg = e.what();
return v8::ThrowException(v8::Exception::Error(v8::String::New(err_msg)));
NanThrowError(err_msg);
}
};

View File

@ -16,8 +16,7 @@ using namespace node;
#define REQ_FUN_ARG(I, VAR) \
if (args.Length() <= (I) || !args[I]->IsFunction()) \
return v8::ThrowException(v8::Exception::TypeError( \
String::New("Argument " #I " must be a function"))); \
return NanThrowTypeError("Argument " #I " must be a function"); \
Local<Function> VAR = Local<Function>::Cast(args[I]);
@ -27,13 +26,13 @@ using namespace node;
#define JSFUNC(NAME) \
static NAN_METHOD(NAME);
//static Handle<Value> NAME(const Arguments& args);
#define JSTHROW_TYPE(ERR) \
return v8::ThrowException(v8::Exception::TypeError(v8::String::New(ERR)));
NanThrowTypeError( ERR );
#define JSTHROW(ERR) \
return v8::ThrowException(v8::Exception::Error(v8::String::New(ERR)));
NanThrowError( ERR );
#define INT_FROM_ARGS(NAME, IND) \
@ -51,7 +50,6 @@ class OpenCV: public node::ObjectWrap{
static void Init(Handle<Object> target);
static NAN_METHOD(ReadImage);
//static Handle<Value> ReadImage(const v8::Arguments&);
};

View File

@ -1,12 +1,12 @@
#include "OpenCV.h"
#include "Point.h"
/*#include "Point.h"
#include "Matrix.h"
#include "CascadeClassifierWrap.h"
#include "VideoCaptureWrap.h"
#include "Contours.h"
#include "CamShift.h"
#include "HighGUI.h"
#include "FaceRecognizer.h"
#include "FaceRecognizer.h"*/
extern "C" void
@ -14,7 +14,7 @@ init(Handle<Object> target) {
//HandleScope scope;
NanScope();
OpenCV::Init(target);
Point::Init(target);
/*Point::Init(target);
Matrix::Init(target);
CascadeClassifierWrap::Init(target);
VideoCaptureWrap::Init(target);
@ -24,7 +24,7 @@ init(Handle<Object> target) {
#if CV_MAJOR_VERSION >= 2 && CV_MINOR_VERSION >=4
FaceRecognizerWrap::Init(target);
#endif
#endif*/
};