commit 403a62dbba276e6577d5833fb6f04c36b6edb082 Author: Peter Braden Date: Sat Jan 14 14:58:54 2012 -0800 OpenCV Bindings - initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ce93513 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +build +.lock-wscript diff --git a/TODO b/TODO new file mode 100644 index 0000000..a5c0ec2 --- /dev/null +++ b/TODO @@ -0,0 +1,2 @@ +- src/OpenCV.c + - Test to see if bindings are working diff --git a/examples/mona.jpg b/examples/mona.jpg new file mode 100644 index 0000000..ced88ac Binary files /dev/null and b/examples/mona.jpg differ diff --git a/lib/bindings.js b/lib/bindings.js new file mode 100644 index 0000000..37cd759 --- /dev/null +++ b/lib/bindings.js @@ -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; +}} \ No newline at end of file diff --git a/lib/opencv.js b/lib/opencv.js new file mode 100644 index 0000000..5d3c668 --- /dev/null +++ b/lib/opencv.js @@ -0,0 +1,5 @@ +var opencv = require('./bindings') + , Point = opencv.Point + , Image = opencv.Image + +var oc = exports = module.exports = opencv; \ No newline at end of file diff --git a/smoke.sh b/smoke.sh new file mode 100755 index 0000000..c1ad035 --- /dev/null +++ b/smoke.sh @@ -0,0 +1,4 @@ +#!/bin/bash +node-waf configure && node-waf build && echo '-- Compiled OK -- + +' && node smoketest \ No newline at end of file diff --git a/smoketest.js b/smoketest.js new file mode 100644 index 0000000..ee2f142 --- /dev/null +++ b/smoketest.js @@ -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")) \ No newline at end of file diff --git a/src/Image.cc b/src/Image.cc new file mode 100644 index 0000000..7b88d5e --- /dev/null +++ b/src/Image.cc @@ -0,0 +1,57 @@ +#include "Image.h" +#include "OpenCV.h" + +v8::Persistent Image::constructor; + +void +Image::Init(Handle target) { + HandleScope scope; + + // Constructor + constructor = Persistent::New(FunctionTemplate::New(Image::New)); + constructor->InstanceTemplate()->SetInternalFieldCount(1); + constructor->SetClassName(String::NewSymbol("Image")); + + // Prototype + Local 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 +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); +}; + diff --git a/src/Image.h b/src/Image.h new file mode 100644 index 0000000..0ecf993 --- /dev/null +++ b/src/Image.h @@ -0,0 +1,16 @@ +#include "OpenCV.h" + +class Image: public node::ObjectWrap { + public: + IplImage* image; + static Persistent constructor; + static void Init(Handle target); + static Handle New(const Arguments &args); + + Image(int width, int height); + Image(v8::Value* src); + + + static Handle GetWidth(Local prop, const AccessorInfo &info); + static Handle GetHeight(Local prop, const AccessorInfo &info); +}; \ No newline at end of file diff --git a/src/OpenCV.cc b/src/OpenCV.cc new file mode 100644 index 0000000..862774b --- /dev/null +++ b/src/OpenCV.cc @@ -0,0 +1,8 @@ +#include "OpenCV.h" + +void +OpenCV::Init(Handle target) { + HandleScope scope; + + target->Set(String::NewSymbol("version"), String::New("1")); +} \ No newline at end of file diff --git a/src/OpenCV.h b/src/OpenCV.h new file mode 100644 index 0000000..a404142 --- /dev/null +++ b/src/OpenCV.h @@ -0,0 +1,21 @@ +#ifndef __NODE_OPENCV_H__ +#define __NODE_OPENCV_H__ + +#include +#include +#include +#include +#include + +using namespace v8; +using namespace node; + + +class OpenCV: public node::ObjectWrap{ + public: + static void Init(Handle target); +}; + + +#endif + diff --git a/src/Point.cc b/src/Point.cc new file mode 100644 index 0000000..aa14db9 --- /dev/null +++ b/src/Point.cc @@ -0,0 +1,59 @@ +#include "Point.h" +#include "OpenCV.h" + +v8::Persistent Point::constructor; + + +void +Point::Init(Handle target) { + HandleScope scope; + + // Constructor + constructor = Persistent::New(FunctionTemplate::New(Point::New)); + constructor->InstanceTemplate()->SetInternalFieldCount(1); + constructor->SetClassName(String::NewSymbol("Point")); + + // Prototype + Local 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 +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 +Point::GetX(Local prop, const AccessorInfo &info) { + HandleScope scope; + Point *pt = ObjectWrap::Unwrap(info.This()); + return scope.Close(Number::New(pt->point.x)); +} + +Handle +Point::GetY(Local prop, const AccessorInfo &info) { + HandleScope scope; + Point *pt = ObjectWrap::Unwrap(info.This()); + return scope.Close(Number::New(pt->point.y)); +} + + +Point::Point(double x, double y): ObjectWrap() { + point = cvPoint2D32f(x, y); +} \ No newline at end of file diff --git a/src/Point.h b/src/Point.h new file mode 100644 index 0000000..ac2616b --- /dev/null +++ b/src/Point.h @@ -0,0 +1,15 @@ +// Template class for 2D points + +#include "OpenCV.h" + +class Point: public node::ObjectWrap { + public: + CvPoint2D32f point; + static Persistent constructor; + static void Init(Handle target); + static Handle New(const Arguments &args); + Point(double x, double y); + + static Handle GetX(Local prop, const AccessorInfo &info); + static Handle GetY(Local prop, const AccessorInfo &info); +}; \ No newline at end of file diff --git a/src/init.cc b/src/init.cc new file mode 100644 index 0000000..e0c7c38 --- /dev/null +++ b/src/init.cc @@ -0,0 +1,11 @@ +#include "OpenCV.h" +#include "Point.h" +#include "Image.h" + +extern "C" void +init(Handle target) { + HandleScope scope; + OpenCV::Init(target); + Point::Init(target); + Image::Init(target); +} \ No newline at end of file diff --git a/wscript b/wscript new file mode 100644 index 0000000..5498ed7 --- /dev/null +++ b/wscript @@ -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'] \ No newline at end of file