Skip to content

Commit

Permalink
Fix code formatting for native code closes peterbraden#295
Browse files Browse the repository at this point in the history
  • Loading branch information
Anshul Jain committed Sep 15, 2015
1 parent 97b8963 commit 7c426bf
Show file tree
Hide file tree
Showing 33 changed files with 3,206 additions and 2,973 deletions.
267 changes: 267 additions & 0 deletions js_code_style.xml

Large diffs are not rendered by default.

166 changes: 166 additions & 0 deletions native_code_style.xml

Large diffs are not rendered by default.

78 changes: 33 additions & 45 deletions src/BackgroundSubtractor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,35 @@
#include <iostream>
#include <nan.h>

#if CV_MAJOR_VERSION >= 2 && CV_MINOR_VERSION >=4
#if ((CV_MAJOR_VERSION >= 2) && (CV_MINOR_VERSION >=4))

Persistent<FunctionTemplate> BackgroundSubtractorWrap::constructor;

void
BackgroundSubtractorWrap::Init(Handle<Object> target) {
NanScope();

// Constructor
Local<FunctionTemplate> ctor = NanNew<FunctionTemplate>(BackgroundSubtractorWrap::New);
NanAssignPersistent(constructor, ctor);
ctor->InstanceTemplate()->SetInternalFieldCount(1);
ctor->SetClassName(NanNew("BackgroundSubtractor"));
void BackgroundSubtractorWrap::Init(Handle<Object> target) {
NanScope();

NODE_SET_METHOD(ctor, "createMOG", CreateMOG);
NODE_SET_PROTOTYPE_METHOD(ctor, "applyMOG", ApplyMOG);
// Constructor
Local<FunctionTemplate> ctor = NanNew<FunctionTemplate>(BackgroundSubtractorWrap::New);
NanAssignPersistent(constructor, ctor);
ctor->InstanceTemplate()->SetInternalFieldCount(1);
ctor->SetClassName(NanNew("BackgroundSubtractor"));

target->Set(NanNew("BackgroundSubtractor"), ctor->GetFunction());
NODE_SET_METHOD(ctor, "createMOG", CreateMOG);
NODE_SET_PROTOTYPE_METHOD(ctor, "applyMOG", ApplyMOG);

};
target->Set(NanNew("BackgroundSubtractor"), ctor->GetFunction());
}

NAN_METHOD(BackgroundSubtractorWrap::New) {
NanScope();

if (args.This()->InternalFieldCount() == 0)
if (args.This()->InternalFieldCount() == 0) {
JSTHROW_TYPE("Cannot Instantiate without new")
}

//Create MOG by default
// Create MOG by default
cv::Ptr<cv::BackgroundSubtractor> bg;
BackgroundSubtractorWrap *pt = new BackgroundSubtractorWrap(bg);

pt->Wrap(args.This());

NanReturnValue(args.This());
Expand Down Expand Up @@ -61,79 +59,69 @@ NAN_METHOD(BackgroundSubtractorWrap::CreateMOG) {

pt->Wrap(n);
NanReturnValue( n );
};
}

//Fetch foreground mask
// Fetch foreground mask
NAN_METHOD(BackgroundSubtractorWrap::ApplyMOG) {

SETUP_FUNCTION(BackgroundSubtractorWrap)

SETUP_FUNCTION(BackgroundSubtractorWrap);
REQ_FUN_ARG(1, cb);

Local<Value> argv[2];

if(args.Length() == 0){
if (args.Length() == 0) {
argv[0] = NanNew("Input image missing");
argv[1] = NanNull();
cb->Call(NanGetCurrentContext()->Global(), 2, argv);
NanReturnUndefined();
}

try{

Local<Object> fgMask = NanNew(Matrix::constructor)->GetFunction()->NewInstance();
try {
Local<Object> fgMask =
NanNew(Matrix::constructor)->GetFunction()->NewInstance();
Matrix *img = ObjectWrap::Unwrap<Matrix>(fgMask);


cv::Mat mat;

if(Buffer::HasInstance(args[0])){
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_64FC1, buf);
mat = cv::imdecode(*mbuf, -1);
//mbuf->release();
}
else{
} else {
Matrix *_img = ObjectWrap::Unwrap<Matrix>(args[0]->ToObject());
mat = (_img->mat).clone();
}

if (mat.empty()){
if (mat.empty()) {
return NanThrowTypeError("Error loading file");
}

cv::Mat _fgMask;
self->subtractor->operator()(mat, _fgMask);

img->mat = _fgMask;

mat.release();

argv[0] = NanNull();
argv[1] = fgMask;

TryCatch try_catch;

cb->Call(NanGetCurrentContext()->Global(), 2, argv);

if (try_catch.HasCaught()) {
FatalException(try_catch);
}

NanReturnUndefined();
}
catch( cv::Exception& e ){
FatalException(try_catch);
}
NanReturnUndefined();
} catch (cv::Exception& e) {
const char* err_msg = e.what();
NanThrowError(err_msg);
NanReturnUndefined();
}
}

};

BackgroundSubtractorWrap::BackgroundSubtractorWrap(cv::Ptr<cv::BackgroundSubtractor> _subtractor){
BackgroundSubtractorWrap::BackgroundSubtractorWrap(
cv::Ptr<cv::BackgroundSubtractor> _subtractor) {
subtractor = _subtractor;
};
}

#endif

18 changes: 9 additions & 9 deletions src/BackgroundSubtractor.h
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
#include "OpenCV.h"

#if CV_MAJOR_VERSION >= 2 && CV_MINOR_VERSION >=4
#if ((CV_MAJOR_VERSION >= 2) && (CV_MINOR_VERSION >=4))

#include <opencv2/video/background_segm.hpp>

class BackgroundSubtractorWrap: public node::ObjectWrap {
public:
cv::Ptr<cv::BackgroundSubtractor> subtractor;
public:
cv::Ptr<cv::BackgroundSubtractor> subtractor;

static Persistent<FunctionTemplate> constructor;
static void Init(Handle<Object> target);
static NAN_METHOD(New);
static Persistent<FunctionTemplate> constructor;
static void Init(Handle<Object> target);
static NAN_METHOD(New);

BackgroundSubtractorWrap(cv::Ptr<cv::BackgroundSubtractor> bg);
BackgroundSubtractorWrap(cv::Ptr<cv::BackgroundSubtractor> bg);

static NAN_METHOD(CreateMOG);
static NAN_METHOD(ApplyMOG);
static NAN_METHOD(CreateMOG);
static NAN_METHOD(ApplyMOG);
};

#endif
Loading

0 comments on commit 7c426bf

Please sign in to comment.