Skip to content
This repository was archived by the owner on Dec 3, 2021. It is now read-only.

Commit 0e46c20

Browse files
committed
Ruby 1.9.x fixes
1 parent 7f823c4 commit 0e46c20

9 files changed

+70
-45
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
.DS_Store
22
examples/data
33
*.o
4+
*.so
45
Makefile
56
mkmf.log
6-
opencv.bundle
7+
opencv.bundle

README.rdoc

+2-5
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ This also contains the data for the default cascades.
3333

3434
You can install by cloning this repository:
3535

36-
git clone git://github.com/jeffrafter/ruby-opencv.git
36+
git clone git://github.com/ser1zw/ruby-opencv.git
3737

3838
Then inside the ruby-opencv folder run:
3939

@@ -45,8 +45,6 @@ Then inside the ruby-opencv folder run:
4545

4646
Once installed it is possible to show images via GUI Window.
4747

48-
require "rubygems"
49-
gem "opencv"
5048
require "opencv"
5149

5250
image = OpenCV::IplImage.load("sample.jpg")
@@ -63,7 +61,6 @@ In order for this to work you must copy the OpenCV haarcascades data into a
6361
subfolder called data.
6462

6563
#!/usr/bin/env ruby
66-
require "rubygems"
6764
require "opencv"
6865

6966
if ARGV.length < 2
@@ -82,7 +79,7 @@ subfolder called data.
8279

8380
== REQUIREMENTS:
8481

85-
* OpenCV 2.0 or later.
82+
* OpenCV 2.0 or 2.1
8683
http://opencv.willowgarage.com/wiki/
8784
* ffcall (optional)
8885
http://www.haible.de/bruno/packages-ffcall.html

ext/cvmat.cpp

+20-20
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ rb_method_missing(int argc, VALUE *argv, VALUE self)
437437
const char *to_str = "\\Ato_(\\w+)";
438438
VALUE name, args, str[3], method;
439439
rb_scan_args(argc, argv, "1*", &name, &args);
440-
if (RARRAY(args)->len != 0)
440+
if (RARRAY_LEN(args) != 0)
441441
return rb_call_super(argc, argv);
442442
if(rb_reg_match(rb_reg_new(to_str, strlen(to_str), 0), rb_funcall(name, rb_intern("to_s"), 0)) == Qnil)
443443
return rb_call_super(argc, argv);
@@ -452,7 +452,7 @@ rb_method_missing(int argc, VALUE *argv, VALUE self)
452452
VALUE name, args, method;
453453
rb_scan_args(argc, argv, "1*", &name, &args);
454454
method = rb_funcall(name, rb_intern("to_s"), 0);
455-
if (RARRAY(args)->len != 0 || !rb_respond_to(rb_module_opencv(), rb_intern(StringValuePtr(method))))
455+
if (RARRAY_LEN(args) != 0 || !rb_respond_to(rb_module_opencv(), rb_intern(StringValuePtr(method))))
456456
return rb_call_super(argc, argv);
457457
return rb_funcall(rb_module_opencv(), rb_intern(StringValuePtr(method)), 1, self);
458458
}
@@ -886,26 +886,26 @@ rb_sub_rect(VALUE self, VALUE args)
886886
CvRect area;
887887
CvPoint topleft;
888888
CvSize size;
889-
switch(RARRAY(args)->len) {
889+
switch(RARRAY_LEN(args)) {
890890
case 1:
891-
area = VALUE_TO_CVRECT(RARRAY(args)->ptr[0]);
891+
area = VALUE_TO_CVRECT(RARRAY_PTR(args)[0]);
892892
break;
893893
case 2:
894-
topleft = VALUE_TO_CVPOINT(RARRAY(args)->ptr[0]);
895-
size = VALUE_TO_CVSIZE(RARRAY(args)->ptr[1]);
894+
topleft = VALUE_TO_CVPOINT(RARRAY_PTR(args)[0]);
895+
size = VALUE_TO_CVSIZE(RARRAY_PTR(args)[1]);
896896
area.x = topleft.x;
897897
area.y = topleft.y;
898898
area.width = size.width;
899899
area.height = size.height;
900900
break;
901901
case 4:
902-
area.x = NUM2INT(RARRAY(args)->ptr[0]);
903-
area.y = NUM2INT(RARRAY(args)->ptr[1]);
904-
area.width = NUM2INT(RARRAY(args)->ptr[2]);
905-
area.height = NUM2INT(RARRAY(args)->ptr[3]);
902+
area.x = NUM2INT(RARRAY_PTR(args)[0]);
903+
area.y = NUM2INT(RARRAY_PTR(args)[1]);
904+
area.width = NUM2INT(RARRAY_PTR(args)[2]);
905+
area.height = NUM2INT(RARRAY_PTR(args)[3]);
906906
break;
907907
default:
908-
rb_raise(rb_eArgError, "wrong number of arguments (%d of 1 or 2 or 4)", RARRAY(args)->len);
908+
rb_raise(rb_eArgError, "wrong number of arguments (%d of 1 or 2 or 4)", RARRAY_LEN(args));
909909
}
910910
return DEPEND_OBJECT(rb_klass,
911911
cvGetSubRect(CVARR(self), CVALLOC(CvMat), area),
@@ -978,7 +978,7 @@ rb_slice_height(VALUE self, VALUE num)
978978
VALUE
979979
rb_row(VALUE self, VALUE args)
980980
{
981-
int len = RARRAY(args)->len;
981+
int len = RARRAY_LEN(args);
982982
if (len < 1) {rb_raise(rb_eArgError, "wrong number of argument.(more than 1)");}
983983
VALUE ary = rb_ary_new2(len);
984984
for (int i = 0; i < len; i++) {
@@ -990,7 +990,7 @@ rb_row(VALUE self, VALUE args)
990990
rb_ary_store(ary, i, DEPEND_OBJECT(rb_klass, cvGetRows(CVARR(self), CVALLOC(CvMat), slice.start_index, slice.end_index), self));
991991
}
992992
}
993-
return RARRAY(ary)->len > 1 ? ary : rb_ary_entry(ary, 0);
993+
return RARRAY_LEN(ary) > 1 ? ary : rb_ary_entry(ary, 0);
994994
}
995995

996996
/*
@@ -1004,7 +1004,7 @@ rb_row(VALUE self, VALUE args)
10041004
VALUE
10051005
rb_col(VALUE self, VALUE args)
10061006
{
1007-
int len = RARRAY(args)->len;
1007+
int len = RARRAY_LEN(args);
10081008
if (len < 1) {rb_raise(rb_eArgError, "wrong number of argument.(more than 1)");}
10091009
VALUE ary = rb_ary_new2(len);
10101010
for (int i = 0; i < len; i++) {
@@ -1016,7 +1016,7 @@ rb_col(VALUE self, VALUE args)
10161016
rb_ary_store(ary, i, DEPEND_OBJECT(rb_klass, cvGetCols(CVARR(self), CVALLOC(CvMat), slice.start_index, slice.end_index), self));
10171017
}
10181018
}
1019-
return RARRAY(ary)->len > 1 ? ary : rb_ary_entry(ary, 0);
1019+
return RARRAY_LEN(ary) > 1 ? ary : rb_ary_entry(ary, 0);
10201020
}
10211021

10221022
/*
@@ -1134,11 +1134,11 @@ VALUE
11341134
rb_aref(VALUE self, VALUE args)
11351135
{
11361136
int index[CV_MAX_DIM];
1137-
for (int i = 0; i < RARRAY(args)->len; i++) {
1137+
for (int i = 0; i < RARRAY_LEN(args); i++) {
11381138
index[i] = NUM2INT(rb_ary_entry(args, i));
11391139
}
11401140
CvScalar scalar = cvScalarAll(0);
1141-
switch(RARRAY(args)->len) {
1141+
switch(RARRAY_LEN(args)) {
11421142
case 1:
11431143
scalar = cvGet1D(CVARR(self), index[0]);
11441144
break;
@@ -1166,10 +1166,10 @@ rb_aset(VALUE self, VALUE args)
11661166
{
11671167
CvScalar scalar = VALUE_TO_CVSCALAR(rb_ary_pop(args));
11681168
int index[CV_MAX_DIM];
1169-
for (int i = 0; i < RARRAY(args)->len; i++) {
1169+
for (int i = 0; i < RARRAY_LEN(args); i++) {
11701170
index[i] = NUM2INT(rb_ary_entry(args, i));
11711171
}
1172-
switch(RARRAY(args)->len) {
1172+
switch(RARRAY_LEN(args)) {
11731173
case 1:
11741174
cvSet1D(CVARR(self), index[0], scalar);
11751175
break;
@@ -1466,7 +1466,7 @@ VALUE
14661466
rb_merge(VALUE klass, VALUE args)
14671467
{
14681468
VALUE object, dest;
1469-
int len = RARRAY(args)->len;
1469+
int len = RARRAY_LEN(args);
14701470
if (!(len > 0) || len > CV_CN_MAX) {
14711471
rb_raise(rb_eArgError, "wrong number of argument (%d for 1..4)", len);
14721472
}

ext/cvseq.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -343,8 +343,8 @@ rb_push(VALUE self, VALUE args)
343343
CvSeq *seq = CVSEQ(self);
344344
VALUE klass = seqblock_class(seq), object;
345345
void *buffer = 0;
346-
for(int i = 0; i < RARRAY(args)->len; i++){
347-
object = RARRAY(args)->ptr[i];
346+
for(int i = 0; i < RARRAY_LEN(args); i++){
347+
object = RARRAY_PTR(args)[i];
348348
if(CLASS_OF(object) == klass){
349349
cvSeqPush(seq, DATA_PTR(object));
350350
}else if(rb_obj_is_kind_of(object, rb_klass) && CLASS_OF(object) == klass){ // object is CvSeq
@@ -403,8 +403,8 @@ rb_unshift(VALUE self, VALUE args)
403403
CvSeq *seq = CVSEQ(self);
404404
VALUE klass = seqblock_class(seq), object;
405405
void *buffer = 0;
406-
for(int i = 0; i < RARRAY(args)->len; i++){
407-
object = RARRAY(args)->ptr[i];
406+
for(int i = 0; i < RARRAY_LEN(args); i++){
407+
object = RARRAY_PTR(args)[i];
408408
if(CLASS_OF(object) == klass){
409409
cvSeqPushFront(seq, DATA_PTR(object));
410410
}else if(rb_obj_is_kind_of(object, rb_klass) && CLASS_OF(object) == klass){

ext/cvtermcriteria.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ VALUE_TO_CVTERMCRITERIA(VALUE object)
5757
case T_FLOAT:
5858
return cvTermCriteria(CV_TERMCRIT_EPS, 0, NUM2DBL(object));
5959
case T_ARRAY:
60-
if (RARRAY(object)->len == 2) {
60+
if (RARRAY_LEN(object) == 2) {
6161
return cvTermCriteria(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS,
6262
NUM2INT(rb_ary_entry(object, 0)),
6363
NUM2DBL(rb_ary_entry(object, 1)));

ext/cvvideowriter.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,17 @@ rb_initialize(int argc, VALUE *argv, VALUE self)
6060
char codec[4] = {' ', ' ', ' ', ' '};
6161
int codec_number;
6262
Check_Type(filename, T_STRING);
63-
if (RSTRING(filename)->len == 0)
63+
if (RSTRING_LEN(filename) == 0)
6464
rb_raise(rb_eArgError, "argument 1 (file name) dose not given");
6565
if (NIL_P(fourcc))
6666
codec_number = -1;
6767
else {
6868
Check_Type(fourcc, T_STRING);
69-
if (RSTRING(fourcc)->len > 4)
69+
if (RSTRING_LEN(fourcc) > 4)
7070
rb_raise(rb_eStandardError, "argument 2 (fourcc) should be specific 4-character. (i.e \"PIM1\",\"MJPG\")");
7171
else {
72-
for (int i = 0; i < RSTRING(fourcc)->len; i++)
73-
codec[i] = RSTRING(fourcc)->ptr[i];
72+
for (int i = 0; i < RSTRING_LEN(fourcc); i++)
73+
codec[i] = RSTRING_PTR(fourcc)[i];
7474
codec_number = CV_FOURCC(codec[0], codec[1], codec[2], codec[3]);
7575
}
7676
}

ext/opencv.h

+31-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@
1515

1616
/* include headers */
1717
#include <ruby.h>
18+
#ifdef HAVE_RUBY_VERSION_H
19+
#include <ruby/version.h>
20+
#else
1821
#include <version.h>
22+
#endif
1923

2024
#ifdef RUBY_WIN32_H
2125
#ifdef write
@@ -28,7 +32,12 @@
2832
#endif
2933

3034
extern "C"{
35+
#ifdef HAVE_RUBY_ST_H
36+
#include <ruby/st.h>
37+
#else
3138
#include <st.h>
39+
#endif
40+
3241
#ifdef HAVE_CALLBACK_H
3342
#include <callback.h> // callhack.h is ffcall header
3443
#endif
@@ -126,6 +135,24 @@ extern "C"{
126135

127136
#define maxint(a,b) ({int _a = (a), _b = (b); _a > _b ? _a : _b; })
128137

138+
// wrapper for <= 1.8
139+
#ifndef RARRAY_LEN
140+
#define RARRAY_LEN(arg) (RARRAY(arg)->len)
141+
#endif
142+
143+
#ifndef RARRAY_PTR
144+
#define RARRAY_PTR(arg) (RARRAY(arg)->ptr)
145+
#endif
146+
147+
#ifndef RSTRING_LEN
148+
#define RSTRING_LEN(arg) (RSTRING(arg)->len)
149+
#endif
150+
151+
#ifndef RSTRING_PTR
152+
#define RSTRING_PTR(arg) (RSTRING(arg)->ptr)
153+
#endif
154+
155+
129156
// OpenCV module
130157
__NAMESPACE_BEGIN_OPENCV
131158

@@ -306,7 +333,7 @@ __NAMESPACE_END_OPENCV
306333
inline VALUE
307334
extract_options_from_args_bang(VALUE ary)
308335
{
309-
return (RARRAY(ary)->len > 0 && rb_obj_is_kind_of(RARRAY(ary)->ptr[RARRAY(ary)->len -1], rb_cHash)) ? rb_ary_pop(ary) : rb_hash_new();
336+
return (RARRAY_LEN(ary) > 0 && rb_obj_is_kind_of(RARRAY_PTR(ary)[RARRAY_LEN(ary) -1], rb_cHash)) ? rb_ary_pop(ary) : rb_hash_new();
310337
}
311338

312339
/*
@@ -316,7 +343,7 @@ assert_valid_keys(VALUE keys, VALUE valid_keys)
316343
VALUE unknown_keys = rb_funcall(keys, rb_intern("-"), 1, rb_funcall(valid_keys, rb_intern("flatten"), 0));
317344
if (NUM2INT(rb_funcall(unknown_keys, rb_intern("empty?"), 0)) != 0){
318345
rb_raise(rb_eArgError, "Unknown key(s): %s",
319-
RSTRING(rb_funcall(unknown_keys, rb_intern("join"), 1, rb_str_new2(", ")))->ptr);
346+
RSTRING_PTR(rb_funcall(unknown_keys, rb_intern("join"), 1, rb_str_new2(", "))));
320347
}
321348
return Qnil;
322349
}
@@ -330,9 +357,9 @@ assert_valid_keys(VALUE options, int n, ...){
330357
va_start(valid_keys, n);
331358
for (int i = 0; i < n; i++)
332359
rb_ary_delete(unknown_keys, ID2SYM(rb_intern(va_arg(valid_keys, char*))));
333-
if (RARRAY(unknown_keys)->len > 0)
360+
if (RARRAY_LEN(unknown_keys) > 0)
334361
rb_raise(rb_eArgError, "Unknown key(s): %s",
335-
RSTRING(rb_funcall(unknown_keys, rb_intern("join"), 1, rb_str_new2(", ")))->ptr);
362+
RSTRING_PTR(rb_funcall(unknown_keys, rb_intern("join"), 1, rb_str_new2(", "))));
336363
va_end(valid_keys);
337364
}
338365

ext/pointset.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -190,12 +190,12 @@ CVPOINTS_FROM_POINT_SET(VALUE object, CvPoint **pointset)
190190
/* to do */
191191
rb_raise(rb_eNotImpError, "CvMat to CvSeq conversion not implemented.");
192192
}else if(rb_obj_is_kind_of(object, rb_cArray)){
193-
*pointset = (CvPoint*)cvAlloc(RARRAY(object)->len * sizeof(CvPoint));
194-
for(int i = 0; i < RARRAY(object)->len; i++){
193+
*pointset = (CvPoint*)cvAlloc(RARRAY_LEN(object) * sizeof(CvPoint));
194+
for(int i = 0; i < RARRAY_LEN(object); i++){
195195
(*pointset)[i].x = NUM2INT(rb_funcall(rb_ary_entry(object, i), rb_intern("x"), 0));
196196
(*pointset)[i].y = NUM2INT(rb_funcall(rb_ary_entry(object, i), rb_intern("y"), 0));
197197
}
198-
return RARRAY(object)->len;
198+
return RARRAY_LEN(object);
199199
}else{
200200
rb_raise(rb_eTypeError, "Can't convert CvSeq(PointSet).");
201201
}
@@ -220,10 +220,10 @@ VALUE_TO_POINT_SET(VALUE object)
220220
rb_raise(rb_eNotImpError, "CvMat to CvSeq conversion not implemented.");
221221
}else if(rb_obj_is_kind_of(object, rb_cArray)){
222222
//pointset = cCvSeq::new_sequence(cCvSeq::rb_class(), )
223-
length = RARRAY(object)->len;
223+
length = RARRAY_LEN(object);
224224
storage = cCvMemStorage::new_object();
225225
seq = cvCreateSeq(CV_SEQ_POINT_SET, sizeof(CvSeq), sizeof(CvPoint), CVMEMSTORAGE(storage));
226-
for(int i = 0; i < RARRAY(object)->len; i++){
226+
for(int i = 0; i < RARRAY_LEN(object); i++){
227227
p32.x = NUM2DBL(rb_funcall(rb_ary_entry(object, i), rb_intern("x"), 0));
228228
p32.y = NUM2DBL(rb_funcall(rb_ary_entry(object, i), rb_intern("y"), 0));
229229
cvSeqPush(seq, &p32);

ext/trackbar.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ VALUE rb_initialize(int argc, VALUE *argv, VALUE self){
7676
if(NIL_P(block)){rb_raise(rb_eArgError, "block not given.");}
7777
Check_Type(name, T_STRING);
7878
Trackbar *trackbar = TRACKBAR(self);
79-
trackbar->name = strcpy(ALLOC_N(char, RSTRING(name)->len), StringValueCStr(name));
79+
trackbar->name = strcpy(ALLOC_N(char, RSTRING_LEN(name)), StringValueCStr(name));
8080
trackbar->maxval = NUM2INT(maxval);
8181
trackbar->val = IF_INT(val, 0);
8282
trackbar->block = block;

0 commit comments

Comments
 (0)