OpenCV Bindings - initial commit

This commit is contained in:
Peter Braden 2012-01-14 14:58:54 -08:00
commit 403a62dbba
15 changed files with 243 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
build
.lock-wscript

2
TODO Normal file
View File

@ -0,0 +1,2 @@
- src/OpenCV.c
- Test to see if bindings are working

BIN
examples/mona.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.3 MiB

10
lib/bindings.js Normal file
View File

@ -0,0 +1,10 @@
module.exports = require('../build/Release/opencv.node');
try {
module.exports = require('../build/Release/opencv.node');
} catch (e) { try {
module.exports = require('../build/default/opencv.node');
} catch (e) {
throw e;
}}

5
lib/opencv.js Normal file
View File

@ -0,0 +1,5 @@
var opencv = require('./bindings')
, Point = opencv.Point
, Image = opencv.Image
var oc = exports = module.exports = opencv;

4
smoke.sh Executable file
View File

@ -0,0 +1,4 @@
#!/bin/bash
node-waf configure && node-waf build && echo '-- Compiled OK --
' && node smoketest

14
smoketest.js Normal file
View File

@ -0,0 +1,14 @@
var opencv = require('./lib/opencv')
, assert = require('assert')
assert.ok(!!opencv)
assert.ok(!!opencv.Point)
assert.ok(!!new opencv.Point(1, 2))
assert.equal(new opencv.Point(1, 2).x, 1)
assert.equal(new opencv.Point(1, 2).y, 2)
assert.equal(Math.round(new opencv.Point(1.1, 2).x * 100), 110)
assert.equal(Math.round(new opencv.Point(1.2, 2.75).y *100), 275)
console.log(opencv.Image("./examples/mona.jpg"))

57
src/Image.cc Normal file
View File

@ -0,0 +1,57 @@
#include "Image.h"
#include "OpenCV.h"
v8::Persistent<FunctionTemplate> Image::constructor;
void
Image::Init(Handle<Object> target) {
HandleScope scope;
// Constructor
constructor = Persistent<FunctionTemplate>::New(FunctionTemplate::New(Image::New));
constructor->InstanceTemplate()->SetInternalFieldCount(1);
constructor->SetClassName(String::NewSymbol("Image"));
// Prototype
Local<ObjectTemplate> proto = constructor->PrototypeTemplate();
/*proto->SetAccessor(String::NewSymbol("source"), GetSource, SetSource);
proto->SetAccessor(String::NewSymbol("complete"), GetComplete);
proto->SetAccessor(String::NewSymbol("width"), GetWidth);
proto->SetAccessor(String::NewSymbol("height"), GetHeight);
proto->SetAccessor(String::NewSymbol("onload"), GetOnload, SetOnload);
proto->SetAccessor(String::NewSymbol("onerror"), GetOnerror, SetOnerror);*/
target->Set(String::NewSymbol("Image"), constructor->GetFunction());
};
Handle<Value>
Image::New(const Arguments &args) {
Image *img;
int width, height;
if (args[0]->IsNumber() && args[1]->IsNumber()){
width = args[0]->Uint32Value();
height = args[1]->Uint32Value();
img = new Image(width, height);
} else {
img = new Image(*args[0]);
}
img->Wrap(args.This());
return args.This();
};
Image::Image(int width, int height): ObjectWrap() {
};
Image::Image(v8::Value* fileName): ObjectWrap() {
char * fname = fileName->ToString()->Utf8Value();
image = cvLoadImage(fname);
};

16
src/Image.h Normal file
View File

@ -0,0 +1,16 @@
#include "OpenCV.h"
class Image: public node::ObjectWrap {
public:
IplImage* image;
static Persistent<FunctionTemplate> constructor;
static void Init(Handle<Object> target);
static Handle<Value> New(const Arguments &args);
Image(int width, int height);
Image(v8::Value* src);
static Handle<Value> GetWidth(Local<String> prop, const AccessorInfo &info);
static Handle<Value> GetHeight(Local<String> prop, const AccessorInfo &info);
};

8
src/OpenCV.cc Normal file
View File

@ -0,0 +1,8 @@
#include "OpenCV.h"
void
OpenCV::Init(Handle<Object> target) {
HandleScope scope;
target->Set(String::NewSymbol("version"), String::New("1"));
}

21
src/OpenCV.h Normal file
View File

@ -0,0 +1,21 @@
#ifndef __NODE_OPENCV_H__
#define __NODE_OPENCV_H__
#include <v8.h>
#include <node.h>
#include <node_object_wrap.h>
#include <node_version.h>
#include <cv.h>
using namespace v8;
using namespace node;
class OpenCV: public node::ObjectWrap{
public:
static void Init(Handle<Object> target);
};
#endif

59
src/Point.cc Normal file
View File

@ -0,0 +1,59 @@
#include "Point.h"
#include "OpenCV.h"
v8::Persistent<FunctionTemplate> Point::constructor;
void
Point::Init(Handle<Object> target) {
HandleScope scope;
// Constructor
constructor = Persistent<FunctionTemplate>::New(FunctionTemplate::New(Point::New));
constructor->InstanceTemplate()->SetInternalFieldCount(1);
constructor->SetClassName(String::NewSymbol("Point"));
// Prototype
Local<ObjectTemplate> proto = constructor->PrototypeTemplate();
proto->SetAccessor(String::NewSymbol("x"), GetX);
proto->SetAccessor(String::NewSymbol("y"), GetY);
/*proto->SetAccessor(String::NewSymbol("source"), GetSource, SetSource);
proto->SetAccessor(String::NewSymbol("complete"), GetComplete);
proto->SetAccessor(String::NewSymbol("width"), GetWidth);
proto->SetAccessor(String::NewSymbol("height"), GetHeight);
proto->SetAccessor(String::NewSymbol("onload"), GetOnload, SetOnload);
proto->SetAccessor(String::NewSymbol("onerror"), GetOnerror, SetOnerror);*/
target->Set(String::NewSymbol("Point"), constructor->GetFunction());
};
Handle<Value>
Point::New(const Arguments &args) {
HandleScope scope;
double x = 0, y = 0;
if (args[0]->IsNumber()) x = args[0]->NumberValue();
if (args[1]->IsNumber()) y = args[1]->NumberValue();
Point *pt = new Point(x, y);
pt->Wrap(args.This());
return args.This();
}
Handle<Value>
Point::GetX(Local<String> prop, const AccessorInfo &info) {
HandleScope scope;
Point *pt = ObjectWrap::Unwrap<Point>(info.This());
return scope.Close(Number::New(pt->point.x));
}
Handle<Value>
Point::GetY(Local<String> prop, const AccessorInfo &info) {
HandleScope scope;
Point *pt = ObjectWrap::Unwrap<Point>(info.This());
return scope.Close(Number::New(pt->point.y));
}
Point::Point(double x, double y): ObjectWrap() {
point = cvPoint2D32f(x, y);
}

15
src/Point.h Normal file
View File

@ -0,0 +1,15 @@
// Template class for 2D points
#include "OpenCV.h"
class Point: public node::ObjectWrap {
public:
CvPoint2D32f point;
static Persistent<FunctionTemplate> constructor;
static void Init(Handle<Object> target);
static Handle<Value> New(const Arguments &args);
Point(double x, double y);
static Handle<Value> GetX(Local<String> prop, const AccessorInfo &info);
static Handle<Value> GetY(Local<String> prop, const AccessorInfo &info);
};

11
src/init.cc Normal file
View File

@ -0,0 +1,11 @@
#include "OpenCV.h"
#include "Point.h"
#include "Image.h"
extern "C" void
init(Handle<Object> target) {
HandleScope scope;
OpenCV::Init(target);
Point::Init(target);
Image::Init(target);
}

19
wscript Normal file
View File

@ -0,0 +1,19 @@
def set_options(opt):
opt.tool_options("compiler_cxx")
def configure(conf):
conf.check_tool('compiler_cxx')
conf.check_tool('node_addon')
#conf.check(lib='opencv_core', libpath=[
# '/lib', '/usr/lib', '/usr/local/lib','/usr/local/include'], uselib_store='OPENCV', mandatory=True)
conf.check_cfg(package='opencv', args='--cflags --libs', uselib_store='OPENCV')
def build(bld):
obj = bld.new_task_gen("cxx", "shlib", "node_addon")
obj.cxxflags = ["-g", "-D_FILE_OFFSET_BITS=64", "-D_LARGEFILE_SOURCE", "-Wall"]
# This is the name of our extension.
obj.target = "opencv"
obj.source = bld.glob('src/*.cc')
obj.uselib = ['OPENCV']