hornetseye-opencv 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +1 -0
- data/COPYING +679 -0
- data/README.md +5 -0
- data/Rakefile +183 -0
- data/ext/cvmat.cc +96 -0
- data/ext/cvmat.hh +28 -0
- data/ext/error.hh +50 -0
- data/ext/init.cc +43 -0
- data/ext/node.cc +103 -0
- data/ext/node.hh +28 -0
- data/ext/rubyinc.hh +54 -0
- data/ext/rubytools.hh +33 -0
- data/ext/rubytools.tcc +32 -0
- data/lib/hornetseye-opencv/cvmat.rb +42 -0
- data/lib/hornetseye-opencv/node.rb +39 -0
- data/lib/hornetseye_opencv_ext.rb +18 -0
- metadata +120 -0
data/README.md
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,183 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'date'
|
3
|
+
require 'rake/clean'
|
4
|
+
require 'rake/testtask'
|
5
|
+
require 'rake/packagetask'
|
6
|
+
require 'rake/loaders/makefile'
|
7
|
+
require 'rbconfig'
|
8
|
+
|
9
|
+
PKG_NAME = 'hornetseye-opencv'
|
10
|
+
PKG_VERSION = '0.1.0'
|
11
|
+
CXX = ENV[ 'CXX' ] || 'g++'
|
12
|
+
STRIP = ENV[ 'STRIP' ] || 'strip'
|
13
|
+
RB_FILES = FileList[ 'lib/**/*.rb' ]
|
14
|
+
CC_FILES = FileList[ 'ext/*.cc' ]
|
15
|
+
HH_FILES = FileList[ 'ext/*.hh' ] + FileList[ 'ext/*.tcc' ]
|
16
|
+
TC_FILES = FileList[ 'test/tc_*.rb' ]
|
17
|
+
TS_FILES = FileList[ 'test/ts_*.rb' ]
|
18
|
+
SO_FILE = "ext/#{PKG_NAME.tr '\-', '_'}.so"
|
19
|
+
PKG_FILES = [ 'Rakefile', 'README.md', 'COPYING', '.document' ] +
|
20
|
+
RB_FILES + CC_FILES + HH_FILES + TS_FILES + TC_FILES
|
21
|
+
BIN_FILES = [ 'README.md', 'COPYING', '.document', SO_FILE ] +
|
22
|
+
RB_FILES + TS_FILES + TC_FILES
|
23
|
+
SUMMARY = %q{OpenCV integration for Hornetseye}
|
24
|
+
DESCRIPTION = %q{This Ruby extension provides conversions from Hornetseye::MultiArray to OpenCV::CvMat and vice versa.}
|
25
|
+
AUTHOR = %q{Jan Wedekind}
|
26
|
+
EMAIL = %q{jan@wedesoft.de}
|
27
|
+
HOMEPAGE = %q{http://wedesoft.github.com/hornetseye-opencv/}
|
28
|
+
|
29
|
+
OBJ = CC_FILES.ext 'o'
|
30
|
+
$CXXFLAGS = ENV[ 'CXXFLAGS' ] || ''
|
31
|
+
$CXXFLAGS = "#{$CXXFLAGS} -fPIC -DNDEBUG"
|
32
|
+
if RbConfig::CONFIG[ 'rubyhdrdir' ]
|
33
|
+
$CXXFLAGS = "#{$CXXFLAGS} -I#{RbConfig::CONFIG[ 'rubyhdrdir' ]} " +
|
34
|
+
"-I#{RbConfig::CONFIG[ 'rubyhdrdir' ]}/#{RbConfig::CONFIG[ 'arch' ]}"
|
35
|
+
else
|
36
|
+
$CXXFLAGS = "#{$CXXFLAGS} -I#{RbConfig::CONFIG[ 'archdir' ]}"
|
37
|
+
end
|
38
|
+
$LIBRUBYARG = RbConfig::CONFIG[ 'LIBRUBYARG' ]
|
39
|
+
$SITELIBDIR = RbConfig::CONFIG[ 'sitelibdir' ]
|
40
|
+
$SITEARCHDIR = RbConfig::CONFIG[ 'sitearchdir' ]
|
41
|
+
|
42
|
+
task :default => :all
|
43
|
+
|
44
|
+
desc 'Compile Ruby extension (default)'
|
45
|
+
task :all => [ SO_FILE ]
|
46
|
+
|
47
|
+
file SO_FILE => OBJ do |t|
|
48
|
+
sh "#{CXX} -shared -o #{t.name} #{OBJ} -lcv -lhighgui -lcvaux -lcxcore -lml #{$LIBRUBYARG}"
|
49
|
+
# sh "#{STRIP} --strip-all #{t.name}"
|
50
|
+
end
|
51
|
+
|
52
|
+
task :test => [ SO_FILE ]
|
53
|
+
|
54
|
+
desc 'Install Ruby extension'
|
55
|
+
task :install => :all do
|
56
|
+
verbose true do
|
57
|
+
for f in RB_FILES do
|
58
|
+
FileUtils.mkdir_p "#{$SITELIBDIR}/#{File.dirname f.gsub( /^lib\//, '' )}"
|
59
|
+
FileUtils.cp_r f, "#{$SITELIBDIR}/#{f.gsub( /^lib\//, '' )}"
|
60
|
+
end
|
61
|
+
FileUtils.mkdir_p $SITEARCHDIR
|
62
|
+
FileUtils.cp SO_FILE, "#{$SITEARCHDIR}/#{File.basename SO_FILE}"
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
desc 'Uninstall Ruby extension'
|
67
|
+
task :uninstall do
|
68
|
+
verbose true do
|
69
|
+
for f in RB_FILES do
|
70
|
+
FileUtils.rm_f "#{$SITELIBDIR}/#{f.gsub /^lib\//, ''}"
|
71
|
+
end
|
72
|
+
FileUtils.rm_f "#{$SITEARCHDIR}/#{File.basename SO_FILE}"
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
Rake::TestTask.new do |t|
|
77
|
+
t.libs << 'ext'
|
78
|
+
t.test_files = TC_FILES
|
79
|
+
end
|
80
|
+
|
81
|
+
begin
|
82
|
+
require 'yard'
|
83
|
+
YARD::Rake::YardocTask.new :yard do |y|
|
84
|
+
y.options << '--no-private'
|
85
|
+
y.files << FileList[ 'lib/**/*.rb' ]
|
86
|
+
end
|
87
|
+
rescue LoadError
|
88
|
+
STDERR.puts 'Please install \'yard\' if you want to generate documentation'
|
89
|
+
end
|
90
|
+
|
91
|
+
Rake::PackageTask.new PKG_NAME, PKG_VERSION do |p|
|
92
|
+
p.need_tar = true
|
93
|
+
p.package_files = PKG_FILES
|
94
|
+
end
|
95
|
+
|
96
|
+
begin
|
97
|
+
require 'rubygems'
|
98
|
+
require 'rubygems/builder'
|
99
|
+
$SPEC = Gem::Specification.new do |s|
|
100
|
+
s.name = PKG_NAME
|
101
|
+
s.version = PKG_VERSION
|
102
|
+
s.platform = Gem::Platform::RUBY
|
103
|
+
s.date = Date.today.to_s
|
104
|
+
s.summary = SUMMARY
|
105
|
+
s.description = DESCRIPTION
|
106
|
+
s.author = AUTHOR
|
107
|
+
s.email = EMAIL
|
108
|
+
s.homepage = HOMEPAGE
|
109
|
+
s.files = PKG_FILES
|
110
|
+
s.test_files = TC_FILES
|
111
|
+
s.require_paths = [ 'lib', 'ext' ]
|
112
|
+
s.rubyforge_project = %q{hornetseye}
|
113
|
+
s.extensions = %w{Rakefile}
|
114
|
+
s.has_rdoc = 'yard'
|
115
|
+
s.extra_rdoc_files = []
|
116
|
+
s.rdoc_options = %w{--no-private}
|
117
|
+
s.add_dependency %<malloc>, [ '~> 1.1' ]
|
118
|
+
s.add_dependency %<multiarray>, [ '~> 0.10' ]
|
119
|
+
s.add_development_dependency %q{rake}
|
120
|
+
end
|
121
|
+
GEM_SOURCE = "#{PKG_NAME}-#{PKG_VERSION}.gem"
|
122
|
+
$BINSPEC = Gem::Specification.new do |s|
|
123
|
+
s.name = PKG_NAME
|
124
|
+
s.version = PKG_VERSION
|
125
|
+
s.platform = Gem::Platform::CURRENT
|
126
|
+
s.date = Date.today.to_s
|
127
|
+
s.summary = SUMMARY
|
128
|
+
s.description = DESCRIPTION
|
129
|
+
s.author = AUTHOR
|
130
|
+
s.email = EMAIL
|
131
|
+
s.homepage = HOMEPAGE
|
132
|
+
s.files = BIN_FILES
|
133
|
+
s.test_files = TC_FILES
|
134
|
+
s.require_paths = [ 'lib', 'ext' ]
|
135
|
+
s.rubyforge_project = %q{hornetseye}
|
136
|
+
s.has_rdoc = 'yard'
|
137
|
+
s.extra_rdoc_files = []
|
138
|
+
s.rdoc_options = %w{--no-private}
|
139
|
+
s.add_dependency %<malloc>, [ '~> 1.1' ]
|
140
|
+
s.add_dependency %<multiarray>, [ '~> 0.10' ]
|
141
|
+
end
|
142
|
+
GEM_BINARY = "#{PKG_NAME}-#{PKG_VERSION}-#{$BINSPEC.platform}.gem"
|
143
|
+
desc "Build the gem file #{GEM_SOURCE}"
|
144
|
+
task :gem => [ "pkg/#{GEM_SOURCE}" ]
|
145
|
+
file "pkg/#{GEM_SOURCE}" => [ 'pkg' ] + $SPEC.files do
|
146
|
+
when_writing 'Creating GEM' do
|
147
|
+
Gem::Builder.new( $SPEC ).build
|
148
|
+
verbose true do
|
149
|
+
FileUtils.mv GEM_SOURCE, "pkg/#{GEM_SOURCE}"
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
153
|
+
desc "Build the gem file #{GEM_BINARY}"
|
154
|
+
task :gem_binary => [ "pkg/#{GEM_BINARY}" ]
|
155
|
+
file "pkg/#{GEM_BINARY}" => [ 'pkg' ] + $BINSPEC.files do
|
156
|
+
when_writing 'Creating binary GEM' do
|
157
|
+
Gem::Builder.new( $BINSPEC ).build
|
158
|
+
verbose true do
|
159
|
+
FileUtils.mv GEM_BINARY, "pkg/#{GEM_BINARY}"
|
160
|
+
end
|
161
|
+
end
|
162
|
+
end
|
163
|
+
rescue LoadError
|
164
|
+
STDERR.puts 'Please install \'rubygems\' if you want to create Gem packages'
|
165
|
+
end
|
166
|
+
|
167
|
+
rule '.o' => '.cc' do |t|
|
168
|
+
sh "#{CXX} #{$CXXFLAGS} -c -o #{t.name} #{t.source}"
|
169
|
+
end
|
170
|
+
|
171
|
+
file ".depends.mf" do |t|
|
172
|
+
sh "g++ -MM #{$CXXFLAGS} #{CC_FILES.join ' '} | " +
|
173
|
+
"sed -e :a -e N -e 's/\\n/\\$/g' -e ta | " +
|
174
|
+
"sed -e 's/ *\\\\\\$ */ /g' -e 's/\\$/\\n/g' | sed -e 's/^/ext\\//' > #{t.name}"
|
175
|
+
end
|
176
|
+
CC_FILES.each do |t|
|
177
|
+
file t.ext(".o") => t
|
178
|
+
end
|
179
|
+
import ".depends.mf"
|
180
|
+
|
181
|
+
CLEAN.include 'ext/*.o'
|
182
|
+
CLOBBER.include SO_FILE, 'doc', '.yardoc', '.depends.mf'
|
183
|
+
|
data/ext/cvmat.cc
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
/* HornetsEye - Computer Vision with Ruby
|
2
|
+
Copyright (C) 2006, 2007, 2008, 2009 Jan Wedekind
|
3
|
+
|
4
|
+
This program is free software: you can redistribute it and/or modify
|
5
|
+
it under the terms of the GNU General Public License as published by
|
6
|
+
the Free Software Foundation, either version 3 of the License, or
|
7
|
+
(at your option) any later version.
|
8
|
+
|
9
|
+
This program is distributed in the hope that it will be useful,
|
10
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12
|
+
GNU General Public License for more details.
|
13
|
+
|
14
|
+
You should have received a copy of the GNU General Public License
|
15
|
+
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
16
|
+
#include <algorithm>
|
17
|
+
#include <opencv/cxcore.h>
|
18
|
+
#include "cvmat.hh"
|
19
|
+
#include "error.hh"
|
20
|
+
#include "rubytools.hh"
|
21
|
+
|
22
|
+
using namespace std;
|
23
|
+
|
24
|
+
VALUE CvMatExt::cRubyClass = Qnil;
|
25
|
+
|
26
|
+
VALUE CvMatExt::registerRubyClass( VALUE module )
|
27
|
+
{
|
28
|
+
cRubyClass = rb_define_class_under( module, "CvMat", rb_cObject );
|
29
|
+
rb_define_method( cRubyClass, "to_multiarray",
|
30
|
+
RUBY_METHOD_FUNC( wrapToMultiArray ), 0 );
|
31
|
+
return cRubyClass;
|
32
|
+
}
|
33
|
+
|
34
|
+
void CvMatExt::deleteRubyObject( void *ptr )
|
35
|
+
{
|
36
|
+
CvMat *mat = (CvMat *)ptr;
|
37
|
+
cvReleaseMat( &mat );
|
38
|
+
}
|
39
|
+
|
40
|
+
VALUE CvMatExt::wrapToMultiArray( VALUE rbSelf )
|
41
|
+
{
|
42
|
+
VALUE rbRetVal = Qnil;
|
43
|
+
try {
|
44
|
+
VALUE mHornetseye = rb_define_module( "Hornetseye" );
|
45
|
+
CvMat *cvMat;
|
46
|
+
dataGetStruct( rbSelf, cRubyClass, CvMat, cvMat );
|
47
|
+
int width = cvMat->width;
|
48
|
+
int height = cvMat->height;
|
49
|
+
int channels = CV_MAT_CN( cvMat->type );
|
50
|
+
VALUE rbTypecode;
|
51
|
+
switch ( CV_MAT_DEPTH( cvMat->type ) ) {
|
52
|
+
case CV_8U:
|
53
|
+
rbTypecode = rb_const_get( mHornetseye, rb_intern( "UBYTE" ) );
|
54
|
+
break;
|
55
|
+
case CV_8S:
|
56
|
+
rbTypecode = rb_const_get( mHornetseye, rb_intern( "BYTE" ) );
|
57
|
+
break;
|
58
|
+
case CV_16U:
|
59
|
+
rbTypecode = rb_const_get( mHornetseye, rb_intern( "USINT" ) );
|
60
|
+
break;
|
61
|
+
case CV_16S:
|
62
|
+
rbTypecode = rb_const_get( mHornetseye, rb_intern( "SINT" ) );
|
63
|
+
break;
|
64
|
+
case CV_32S:
|
65
|
+
rbTypecode = rb_const_get( mHornetseye, rb_intern( "INT" ) );
|
66
|
+
break;
|
67
|
+
case CV_32F:
|
68
|
+
rbTypecode = rb_const_get( mHornetseye, rb_intern( "SFLOAT" ) );
|
69
|
+
break;
|
70
|
+
case CV_64F:
|
71
|
+
rbTypecode = rb_const_get( mHornetseye, rb_intern( "DFLOAT" ) );
|
72
|
+
break;
|
73
|
+
default:
|
74
|
+
ERRORMACRO( false, Error, ,
|
75
|
+
"Encountered unknown type number "
|
76
|
+
<< CV_MAT_DEPTH( cvMat->type )
|
77
|
+
<< " while trying to convert to MultiArray" );
|
78
|
+
};
|
79
|
+
VALUE rbTypeSize = rb_funcall( rbTypecode, rb_intern( "storage_size" ), 0 );
|
80
|
+
int typesize = NUM2INT( rbTypeSize );
|
81
|
+
int size = width * height * channels * typesize;
|
82
|
+
VALUE cMalloc = rb_define_class_under( mHornetseye, "Malloc", rb_cObject );
|
83
|
+
VALUE rbDest = rb_funcall( cMalloc, rb_intern( "new" ), 1, INT2NUM( size ) );
|
84
|
+
char *dest; Data_Get_Struct( rbDest, char, dest );
|
85
|
+
copy( cvMat->data.ptr, cvMat->data.ptr + size, dest );
|
86
|
+
VALUE rbMultiArray = rb_funcall( mHornetseye, rb_intern( "MultiArray" ), 4,
|
87
|
+
rbTypecode, INT2NUM( channels ),
|
88
|
+
INT2NUM( width ), INT2NUM( height ) );
|
89
|
+
VALUE rbImport = rb_funcall( rbMultiArray, rb_intern( "new" ), 1, rbDest );
|
90
|
+
rbRetVal = rbImport;
|
91
|
+
} catch( std::exception &e ) {
|
92
|
+
rb_raise( rb_eRuntimeError, "%s", e.what() );
|
93
|
+
};
|
94
|
+
return rbRetVal;
|
95
|
+
}
|
96
|
+
|
data/ext/cvmat.hh
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
/* HornetsEye - Computer Vision with Ruby
|
2
|
+
Copyright (C) 2006, 2007, 2008, 2009 Jan Wedekind
|
3
|
+
|
4
|
+
This program is free software: you can redistribute it and/or modify
|
5
|
+
it under the terms of the GNU General Public License as published by
|
6
|
+
the Free Software Foundation, either version 3 of the License, or
|
7
|
+
(at your option) any later version.
|
8
|
+
|
9
|
+
This program is distributed in the hope that it will be useful,
|
10
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12
|
+
GNU General Public License for more details.
|
13
|
+
|
14
|
+
You should have received a copy of the GNU General Public License
|
15
|
+
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
16
|
+
#ifndef HORNETSEYE_CVMAT_HH
|
17
|
+
#define HORNETSEYE_CVMAT_HH
|
18
|
+
#include "rubyinc.hh"
|
19
|
+
|
20
|
+
class CvMatExt {
|
21
|
+
public:
|
22
|
+
static VALUE cRubyClass;
|
23
|
+
static VALUE registerRubyClass( VALUE module );
|
24
|
+
static void deleteRubyObject( void *ptr );
|
25
|
+
static VALUE wrapToMultiArray( VALUE rbSelf );
|
26
|
+
};
|
27
|
+
|
28
|
+
#endif
|
data/ext/error.hh
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
/* HornetsEye - Computer Vision with Ruby
|
2
|
+
Copyright (C) 2006, 2007, 2008, 2009, 2010 Jan Wedekind
|
3
|
+
|
4
|
+
This program is free software: you can redistribute it and/or modify
|
5
|
+
it under the terms of the GNU General Public License as published by
|
6
|
+
the Free Software Foundation, either version 3 of the License, or
|
7
|
+
(at your option) any later version.
|
8
|
+
|
9
|
+
This program is distributed in the hope that it will be useful,
|
10
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12
|
+
GNU General Public License for more details.
|
13
|
+
|
14
|
+
You should have received a copy of the GNU General Public License
|
15
|
+
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
16
|
+
#ifndef ERROR_HH
|
17
|
+
#define ERROR_HH
|
18
|
+
|
19
|
+
#include <exception>
|
20
|
+
#include <sstream>
|
21
|
+
|
22
|
+
class Error: public std::exception
|
23
|
+
{
|
24
|
+
public:
|
25
|
+
Error(void) {}
|
26
|
+
Error( Error &e ): std::exception( e )
|
27
|
+
{ m_message << e.m_message.str(); }
|
28
|
+
virtual ~Error(void) throw() {}
|
29
|
+
template< typename T >
|
30
|
+
std::ostream &operator<<( const T &t )
|
31
|
+
{ m_message << t; return m_message; }
|
32
|
+
std::ostream &operator<<( std::ostream& (*__pf)(std::ostream&) )
|
33
|
+
{ (*__pf)( m_message ); return m_message; }
|
34
|
+
virtual const char* what(void) const throw() {
|
35
|
+
temp = m_message.str();
|
36
|
+
return temp.c_str();
|
37
|
+
}
|
38
|
+
protected:
|
39
|
+
std::ostringstream m_message;
|
40
|
+
mutable std::string temp;
|
41
|
+
};
|
42
|
+
|
43
|
+
#define ERRORMACRO( condition, class, params, message ) \
|
44
|
+
if ( !( condition ) ) { \
|
45
|
+
class _e params; \
|
46
|
+
_e << message; \
|
47
|
+
throw _e; \
|
48
|
+
};
|
49
|
+
|
50
|
+
#endif
|
data/ext/init.cc
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
/* HornetsEye - Computer Vision with Ruby
|
2
|
+
Copyright (C) 2006, 2007, 2008, 2009, 2010 Jan Wedekind
|
3
|
+
|
4
|
+
This program is free software: you can redistribute it and/or modify
|
5
|
+
it under the terms of the GNU General Public License as published by
|
6
|
+
the Free Software Foundation, either version 3 of the License, or
|
7
|
+
(at your option) any later version.
|
8
|
+
|
9
|
+
This program is distributed in the hope that it will be useful,
|
10
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12
|
+
GNU General Public License for more details.
|
13
|
+
|
14
|
+
You should have received a copy of the GNU General Public License
|
15
|
+
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
16
|
+
#include "rubyinc.hh"
|
17
|
+
#include "cvmat.hh"
|
18
|
+
#include "node.hh"
|
19
|
+
|
20
|
+
#ifdef WIN32
|
21
|
+
#define DLLEXPORT __declspec(dllexport)
|
22
|
+
#define DLLLOCAL
|
23
|
+
#else
|
24
|
+
#define DLLEXPORT __attribute__ ((visibility("default")))
|
25
|
+
#define DLLLOCAL __attribute__ ((visibility("hidden")))
|
26
|
+
#endif
|
27
|
+
|
28
|
+
extern "C" DLLEXPORT void Init_hornetseye_opencv(void);
|
29
|
+
|
30
|
+
extern "C" {
|
31
|
+
|
32
|
+
void Init_hornetseye_opencv(void)
|
33
|
+
{
|
34
|
+
rb_require( "opencv" );
|
35
|
+
rb_require( "multiarray" );
|
36
|
+
VALUE mOpenCV = rb_define_module( "OpenCV" );
|
37
|
+
VALUE mHornetseye = rb_define_module( "Hornetseye" );
|
38
|
+
CvMatExt::registerRubyClass( mOpenCV );
|
39
|
+
Node::registerRubyClass( mHornetseye );
|
40
|
+
rb_require( "hornetseye_opencv_ext.rb" );
|
41
|
+
}
|
42
|
+
|
43
|
+
}
|
data/ext/node.cc
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
/* HornetsEye - Computer Vision with Ruby
|
2
|
+
Copyright (C) 2006, 2007, 2008, 2009, 2010 Jan Wedekind
|
3
|
+
|
4
|
+
This program is free software: you can redistribute it and/or modify
|
5
|
+
it under the terms of the GNU General Public License as published by
|
6
|
+
the Free Software Foundation, either version 3 of the License, or
|
7
|
+
(at your option) any later version.
|
8
|
+
|
9
|
+
This program is distributed in the hope that it will be useful,
|
10
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12
|
+
GNU General Public License for more details.
|
13
|
+
|
14
|
+
You should have received a copy of the GNU General Public License
|
15
|
+
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
16
|
+
#include <algorithm>
|
17
|
+
#include <boost/shared_array.hpp>
|
18
|
+
#include <opencv/cxcore.h>
|
19
|
+
#include "cvmat.hh"
|
20
|
+
#include "error.hh"
|
21
|
+
#include "rubytools.hh"
|
22
|
+
#include "node.hh"
|
23
|
+
|
24
|
+
using namespace boost;
|
25
|
+
using namespace std;
|
26
|
+
|
27
|
+
VALUE Node::mModule = Qnil;
|
28
|
+
|
29
|
+
VALUE Node::cRubyClass = Qnil;
|
30
|
+
|
31
|
+
VALUE Node::registerRubyClass( VALUE module )
|
32
|
+
{
|
33
|
+
mModule = module;
|
34
|
+
cRubyClass = rb_define_class_under( module, "Node", rb_cObject );
|
35
|
+
rb_define_method( cRubyClass, "to_cvmat", RUBY_METHOD_FUNC( wrapToCvMat ), 0 );
|
36
|
+
return cRubyClass;
|
37
|
+
}
|
38
|
+
|
39
|
+
VALUE Node::wrapToCvMat( VALUE rbSelf )
|
40
|
+
{
|
41
|
+
VALUE rbRetVal = Qnil;
|
42
|
+
try {
|
43
|
+
VALUE rbMalloc = rb_funcall( rbSelf, rb_intern( "memory" ), 0 );
|
44
|
+
VALUE rbTypecode = rb_funcall( rbSelf, rb_intern( "typecode" ), 0 );
|
45
|
+
VALUE rbShape = rb_funcall( rbSelf, rb_intern( "shape" ), 0 );
|
46
|
+
Check_Type( rbShape, T_ARRAY );
|
47
|
+
int rank = RARRAY_LEN(rbShape);
|
48
|
+
ERRORMACRO( rank == 3, Error, ,
|
49
|
+
"Array needs to have three dimensions for conversion to "
|
50
|
+
"CvMat (but had " << rank << " dimension(s))" );
|
51
|
+
int
|
52
|
+
channels = NUM2INT( RARRAY_PTR(rbShape)[0] ),
|
53
|
+
width = NUM2INT( RARRAY_PTR(rbShape)[1] ),
|
54
|
+
height = NUM2INT( RARRAY_PTR(rbShape)[2] );
|
55
|
+
int type = -1;
|
56
|
+
if ( rb_funcall( rbTypecode, rb_intern( "==" ), 1,
|
57
|
+
rb_const_get( mModule, rb_intern( "UBYTE" ) ) ) ==
|
58
|
+
Qtrue )
|
59
|
+
type = CV_8U;
|
60
|
+
else if ( rb_funcall( rbTypecode, rb_intern( "==" ), 1,
|
61
|
+
rb_const_get( mModule, rb_intern( "BYTE" ) ) ) ==
|
62
|
+
Qtrue )
|
63
|
+
type = CV_8S;
|
64
|
+
else if ( rb_funcall( rbTypecode, rb_intern( "==" ), 1,
|
65
|
+
rb_const_get( mModule, rb_intern( "USINT" ) ) ) ==
|
66
|
+
Qtrue )
|
67
|
+
type = CV_16U;
|
68
|
+
else if ( rb_funcall( rbTypecode, rb_intern( "==" ), 1,
|
69
|
+
rb_const_get( mModule, rb_intern( "SINT" ) ) ) ==
|
70
|
+
Qtrue )
|
71
|
+
type = CV_16S;
|
72
|
+
else if ( rb_funcall( rbTypecode, rb_intern( "==" ), 1,
|
73
|
+
rb_const_get( mModule, rb_intern( "INT" ) ) ) ==
|
74
|
+
Qtrue )
|
75
|
+
type = CV_32S;
|
76
|
+
else if ( rb_funcall( rbTypecode, rb_intern( "==" ), 1,
|
77
|
+
rb_const_get( mModule, rb_intern( "SFLOAT" ) ) ) ==
|
78
|
+
Qtrue )
|
79
|
+
type = CV_32F;
|
80
|
+
else if ( rb_funcall( rbTypecode, rb_intern( "==" ), 1,
|
81
|
+
rb_const_get( mModule, rb_intern( "DFLOAT" ) ) ) ==
|
82
|
+
Qtrue )
|
83
|
+
type = CV_64F;
|
84
|
+
else {
|
85
|
+
ERRORMACRO( false, Error, ,
|
86
|
+
"Conversion to CvMat only supports UBYTE, BYTE, USINT, "
|
87
|
+
"SINT, INT, SFLOAT, DFLOAT as basic types" );
|
88
|
+
};
|
89
|
+
VALUE rbTypeSize = rb_funcall( rbSelf, rb_intern( "storage_size" ), 0 );
|
90
|
+
int typesize = NUM2INT( rbTypeSize );
|
91
|
+
VALUE cMalloc = rb_define_class_under( mModule, "Malloc", rb_cObject );
|
92
|
+
unsigned char *source;
|
93
|
+
dataGetStruct( rbMalloc, cMalloc, unsigned char, source );
|
94
|
+
CvMat *cvMat = cvCreateMat( height, width, CV_MAKETYPE( type, channels ) );
|
95
|
+
rbRetVal =
|
96
|
+
Data_Wrap_Struct( CvMatExt::cRubyClass, 0, CvMatExt::deleteRubyObject, cvMat );
|
97
|
+
copy( source, source + typesize, cvMat->data.ptr );
|
98
|
+
} catch ( std::exception &e ) {
|
99
|
+
rb_raise( rb_eRuntimeError, "%s", e.what() );
|
100
|
+
};
|
101
|
+
return rbRetVal;
|
102
|
+
}
|
103
|
+
|