From 0e46c20929c9ac5dfc222938eb9dea0ef8941710 Mon Sep 17 00:00:00 2001 From: ser1zw Date: Thu, 30 Dec 2010 03:39:15 +0900 Subject: [PATCH] Ruby 1.9.x fixes --- .gitignore | 3 ++- README.rdoc | 7 ++----- ext/cvmat.cpp | 40 ++++++++++++++++++++-------------------- ext/cvseq.cpp | 8 ++++---- ext/cvtermcriteria.h | 2 +- ext/cvvideowriter.cpp | 8 ++++---- ext/opencv.h | 35 +++++++++++++++++++++++++++++++---- ext/pointset.cpp | 10 +++++----- ext/trackbar.cpp | 2 +- 9 files changed, 70 insertions(+), 45 deletions(-) diff --git a/.gitignore b/.gitignore index ee11562f..d3fedfdd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ .DS_Store examples/data *.o +*.so Makefile mkmf.log -opencv.bundle \ No newline at end of file +opencv.bundle diff --git a/README.rdoc b/README.rdoc index fa57b975..5fcc6c6c 100644 --- a/README.rdoc +++ b/README.rdoc @@ -33,7 +33,7 @@ This also contains the data for the default cascades. You can install by cloning this repository: - git clone git://github.com/jeffrafter/ruby-opencv.git + git clone git://github.com/ser1zw/ruby-opencv.git Then inside the ruby-opencv folder run: @@ -45,8 +45,6 @@ Then inside the ruby-opencv folder run: Once installed it is possible to show images via GUI Window. - require "rubygems" - gem "opencv" require "opencv" 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 subfolder called data. #!/usr/bin/env ruby - require "rubygems" require "opencv" if ARGV.length < 2 @@ -82,7 +79,7 @@ subfolder called data. == REQUIREMENTS: -* OpenCV 2.0 or later. +* OpenCV 2.0 or 2.1 http://opencv.willowgarage.com/wiki/ * ffcall (optional) http://www.haible.de/bruno/packages-ffcall.html diff --git a/ext/cvmat.cpp b/ext/cvmat.cpp index d2a1280c..a155f037 100644 --- a/ext/cvmat.cpp +++ b/ext/cvmat.cpp @@ -437,7 +437,7 @@ rb_method_missing(int argc, VALUE *argv, VALUE self) const char *to_str = "\\Ato_(\\w+)"; VALUE name, args, str[3], method; rb_scan_args(argc, argv, "1*", &name, &args); - if (RARRAY(args)->len != 0) + if (RARRAY_LEN(args) != 0) return rb_call_super(argc, argv); if(rb_reg_match(rb_reg_new(to_str, strlen(to_str), 0), rb_funcall(name, rb_intern("to_s"), 0)) == Qnil) return rb_call_super(argc, argv); @@ -452,7 +452,7 @@ rb_method_missing(int argc, VALUE *argv, VALUE self) VALUE name, args, method; rb_scan_args(argc, argv, "1*", &name, &args); method = rb_funcall(name, rb_intern("to_s"), 0); - if (RARRAY(args)->len != 0 || !rb_respond_to(rb_module_opencv(), rb_intern(StringValuePtr(method)))) + if (RARRAY_LEN(args) != 0 || !rb_respond_to(rb_module_opencv(), rb_intern(StringValuePtr(method)))) return rb_call_super(argc, argv); return rb_funcall(rb_module_opencv(), rb_intern(StringValuePtr(method)), 1, self); } @@ -886,26 +886,26 @@ rb_sub_rect(VALUE self, VALUE args) CvRect area; CvPoint topleft; CvSize size; - switch(RARRAY(args)->len) { + switch(RARRAY_LEN(args)) { case 1: - area = VALUE_TO_CVRECT(RARRAY(args)->ptr[0]); + area = VALUE_TO_CVRECT(RARRAY_PTR(args)[0]); break; case 2: - topleft = VALUE_TO_CVPOINT(RARRAY(args)->ptr[0]); - size = VALUE_TO_CVSIZE(RARRAY(args)->ptr[1]); + topleft = VALUE_TO_CVPOINT(RARRAY_PTR(args)[0]); + size = VALUE_TO_CVSIZE(RARRAY_PTR(args)[1]); area.x = topleft.x; area.y = topleft.y; area.width = size.width; area.height = size.height; break; case 4: - area.x = NUM2INT(RARRAY(args)->ptr[0]); - area.y = NUM2INT(RARRAY(args)->ptr[1]); - area.width = NUM2INT(RARRAY(args)->ptr[2]); - area.height = NUM2INT(RARRAY(args)->ptr[3]); + area.x = NUM2INT(RARRAY_PTR(args)[0]); + area.y = NUM2INT(RARRAY_PTR(args)[1]); + area.width = NUM2INT(RARRAY_PTR(args)[2]); + area.height = NUM2INT(RARRAY_PTR(args)[3]); break; default: - rb_raise(rb_eArgError, "wrong number of arguments (%d of 1 or 2 or 4)", RARRAY(args)->len); + rb_raise(rb_eArgError, "wrong number of arguments (%d of 1 or 2 or 4)", RARRAY_LEN(args)); } return DEPEND_OBJECT(rb_klass, cvGetSubRect(CVARR(self), CVALLOC(CvMat), area), @@ -978,7 +978,7 @@ rb_slice_height(VALUE self, VALUE num) VALUE rb_row(VALUE self, VALUE args) { - int len = RARRAY(args)->len; + int len = RARRAY_LEN(args); if (len < 1) {rb_raise(rb_eArgError, "wrong number of argument.(more than 1)");} VALUE ary = rb_ary_new2(len); for (int i = 0; i < len; i++) { @@ -990,7 +990,7 @@ rb_row(VALUE self, VALUE args) rb_ary_store(ary, i, DEPEND_OBJECT(rb_klass, cvGetRows(CVARR(self), CVALLOC(CvMat), slice.start_index, slice.end_index), self)); } } - return RARRAY(ary)->len > 1 ? ary : rb_ary_entry(ary, 0); + return RARRAY_LEN(ary) > 1 ? ary : rb_ary_entry(ary, 0); } /* @@ -1004,7 +1004,7 @@ rb_row(VALUE self, VALUE args) VALUE rb_col(VALUE self, VALUE args) { - int len = RARRAY(args)->len; + int len = RARRAY_LEN(args); if (len < 1) {rb_raise(rb_eArgError, "wrong number of argument.(more than 1)");} VALUE ary = rb_ary_new2(len); for (int i = 0; i < len; i++) { @@ -1016,7 +1016,7 @@ rb_col(VALUE self, VALUE args) rb_ary_store(ary, i, DEPEND_OBJECT(rb_klass, cvGetCols(CVARR(self), CVALLOC(CvMat), slice.start_index, slice.end_index), self)); } } - return RARRAY(ary)->len > 1 ? ary : rb_ary_entry(ary, 0); + return RARRAY_LEN(ary) > 1 ? ary : rb_ary_entry(ary, 0); } /* @@ -1134,11 +1134,11 @@ VALUE rb_aref(VALUE self, VALUE args) { int index[CV_MAX_DIM]; - for (int i = 0; i < RARRAY(args)->len; i++) { + for (int i = 0; i < RARRAY_LEN(args); i++) { index[i] = NUM2INT(rb_ary_entry(args, i)); } CvScalar scalar = cvScalarAll(0); - switch(RARRAY(args)->len) { + switch(RARRAY_LEN(args)) { case 1: scalar = cvGet1D(CVARR(self), index[0]); break; @@ -1166,10 +1166,10 @@ rb_aset(VALUE self, VALUE args) { CvScalar scalar = VALUE_TO_CVSCALAR(rb_ary_pop(args)); int index[CV_MAX_DIM]; - for (int i = 0; i < RARRAY(args)->len; i++) { + for (int i = 0; i < RARRAY_LEN(args); i++) { index[i] = NUM2INT(rb_ary_entry(args, i)); } - switch(RARRAY(args)->len) { + switch(RARRAY_LEN(args)) { case 1: cvSet1D(CVARR(self), index[0], scalar); break; @@ -1466,7 +1466,7 @@ VALUE rb_merge(VALUE klass, VALUE args) { VALUE object, dest; - int len = RARRAY(args)->len; + int len = RARRAY_LEN(args); if (!(len > 0) || len > CV_CN_MAX) { rb_raise(rb_eArgError, "wrong number of argument (%d for 1..4)", len); } diff --git a/ext/cvseq.cpp b/ext/cvseq.cpp index be6f22dd..41cd94d6 100644 --- a/ext/cvseq.cpp +++ b/ext/cvseq.cpp @@ -343,8 +343,8 @@ rb_push(VALUE self, VALUE args) CvSeq *seq = CVSEQ(self); VALUE klass = seqblock_class(seq), object; void *buffer = 0; - for(int i = 0; i < RARRAY(args)->len; i++){ - object = RARRAY(args)->ptr[i]; + for(int i = 0; i < RARRAY_LEN(args); i++){ + object = RARRAY_PTR(args)[i]; if(CLASS_OF(object) == klass){ cvSeqPush(seq, DATA_PTR(object)); }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) CvSeq *seq = CVSEQ(self); VALUE klass = seqblock_class(seq), object; void *buffer = 0; - for(int i = 0; i < RARRAY(args)->len; i++){ - object = RARRAY(args)->ptr[i]; + for(int i = 0; i < RARRAY_LEN(args); i++){ + object = RARRAY_PTR(args)[i]; if(CLASS_OF(object) == klass){ cvSeqPushFront(seq, DATA_PTR(object)); }else if(rb_obj_is_kind_of(object, rb_klass) && CLASS_OF(object) == klass){ diff --git a/ext/cvtermcriteria.h b/ext/cvtermcriteria.h index 16415ce1..cda3112a 100644 --- a/ext/cvtermcriteria.h +++ b/ext/cvtermcriteria.h @@ -57,7 +57,7 @@ VALUE_TO_CVTERMCRITERIA(VALUE object) case T_FLOAT: return cvTermCriteria(CV_TERMCRIT_EPS, 0, NUM2DBL(object)); case T_ARRAY: - if (RARRAY(object)->len == 2) { + if (RARRAY_LEN(object) == 2) { return cvTermCriteria(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS, NUM2INT(rb_ary_entry(object, 0)), NUM2DBL(rb_ary_entry(object, 1))); diff --git a/ext/cvvideowriter.cpp b/ext/cvvideowriter.cpp index dad0df16..56520621 100644 --- a/ext/cvvideowriter.cpp +++ b/ext/cvvideowriter.cpp @@ -60,17 +60,17 @@ rb_initialize(int argc, VALUE *argv, VALUE self) char codec[4] = {' ', ' ', ' ', ' '}; int codec_number; Check_Type(filename, T_STRING); - if (RSTRING(filename)->len == 0) + if (RSTRING_LEN(filename) == 0) rb_raise(rb_eArgError, "argument 1 (file name) dose not given"); if (NIL_P(fourcc)) codec_number = -1; else { Check_Type(fourcc, T_STRING); - if (RSTRING(fourcc)->len > 4) + if (RSTRING_LEN(fourcc) > 4) rb_raise(rb_eStandardError, "argument 2 (fourcc) should be specific 4-character. (i.e \"PIM1\",\"MJPG\")"); else { - for (int i = 0; i < RSTRING(fourcc)->len; i++) - codec[i] = RSTRING(fourcc)->ptr[i]; + for (int i = 0; i < RSTRING_LEN(fourcc); i++) + codec[i] = RSTRING_PTR(fourcc)[i]; codec_number = CV_FOURCC(codec[0], codec[1], codec[2], codec[3]); } } diff --git a/ext/opencv.h b/ext/opencv.h index 4e2d0779..66044c94 100644 --- a/ext/opencv.h +++ b/ext/opencv.h @@ -15,7 +15,11 @@ /* include headers */ #include +#ifdef HAVE_RUBY_VERSION_H +#include +#else #include +#endif #ifdef RUBY_WIN32_H #ifdef write @@ -28,7 +32,12 @@ #endif extern "C"{ +#ifdef HAVE_RUBY_ST_H +#include +#else #include +#endif + #ifdef HAVE_CALLBACK_H #include // callhack.h is ffcall header #endif @@ -126,6 +135,24 @@ extern "C"{ #define maxint(a,b) ({int _a = (a), _b = (b); _a > _b ? _a : _b; }) +// wrapper for <= 1.8 +#ifndef RARRAY_LEN +#define RARRAY_LEN(arg) (RARRAY(arg)->len) +#endif + +#ifndef RARRAY_PTR +#define RARRAY_PTR(arg) (RARRAY(arg)->ptr) +#endif + +#ifndef RSTRING_LEN +#define RSTRING_LEN(arg) (RSTRING(arg)->len) +#endif + +#ifndef RSTRING_PTR +#define RSTRING_PTR(arg) (RSTRING(arg)->ptr) +#endif + + // OpenCV module __NAMESPACE_BEGIN_OPENCV @@ -306,7 +333,7 @@ __NAMESPACE_END_OPENCV inline VALUE extract_options_from_args_bang(VALUE ary) { - 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(); + 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(); } /* @@ -316,7 +343,7 @@ assert_valid_keys(VALUE keys, VALUE valid_keys) VALUE unknown_keys = rb_funcall(keys, rb_intern("-"), 1, rb_funcall(valid_keys, rb_intern("flatten"), 0)); if (NUM2INT(rb_funcall(unknown_keys, rb_intern("empty?"), 0)) != 0){ rb_raise(rb_eArgError, "Unknown key(s): %s", - RSTRING(rb_funcall(unknown_keys, rb_intern("join"), 1, rb_str_new2(", ")))->ptr); + RSTRING_PTR(rb_funcall(unknown_keys, rb_intern("join"), 1, rb_str_new2(", ")))); } return Qnil; } @@ -330,9 +357,9 @@ assert_valid_keys(VALUE options, int n, ...){ va_start(valid_keys, n); for (int i = 0; i < n; i++) rb_ary_delete(unknown_keys, ID2SYM(rb_intern(va_arg(valid_keys, char*)))); - if (RARRAY(unknown_keys)->len > 0) + if (RARRAY_LEN(unknown_keys) > 0) rb_raise(rb_eArgError, "Unknown key(s): %s", - RSTRING(rb_funcall(unknown_keys, rb_intern("join"), 1, rb_str_new2(", ")))->ptr); + RSTRING_PTR(rb_funcall(unknown_keys, rb_intern("join"), 1, rb_str_new2(", ")))); va_end(valid_keys); } diff --git a/ext/pointset.cpp b/ext/pointset.cpp index e8072826..9e61051c 100644 --- a/ext/pointset.cpp +++ b/ext/pointset.cpp @@ -190,12 +190,12 @@ CVPOINTS_FROM_POINT_SET(VALUE object, CvPoint **pointset) /* to do */ rb_raise(rb_eNotImpError, "CvMat to CvSeq conversion not implemented."); }else if(rb_obj_is_kind_of(object, rb_cArray)){ - *pointset = (CvPoint*)cvAlloc(RARRAY(object)->len * sizeof(CvPoint)); - for(int i = 0; i < RARRAY(object)->len; i++){ + *pointset = (CvPoint*)cvAlloc(RARRAY_LEN(object) * sizeof(CvPoint)); + for(int i = 0; i < RARRAY_LEN(object); i++){ (*pointset)[i].x = NUM2INT(rb_funcall(rb_ary_entry(object, i), rb_intern("x"), 0)); (*pointset)[i].y = NUM2INT(rb_funcall(rb_ary_entry(object, i), rb_intern("y"), 0)); } - return RARRAY(object)->len; + return RARRAY_LEN(object); }else{ rb_raise(rb_eTypeError, "Can't convert CvSeq(PointSet)."); } @@ -220,10 +220,10 @@ VALUE_TO_POINT_SET(VALUE object) rb_raise(rb_eNotImpError, "CvMat to CvSeq conversion not implemented."); }else if(rb_obj_is_kind_of(object, rb_cArray)){ //pointset = cCvSeq::new_sequence(cCvSeq::rb_class(), ) - length = RARRAY(object)->len; + length = RARRAY_LEN(object); storage = cCvMemStorage::new_object(); seq = cvCreateSeq(CV_SEQ_POINT_SET, sizeof(CvSeq), sizeof(CvPoint), CVMEMSTORAGE(storage)); - for(int i = 0; i < RARRAY(object)->len; i++){ + for(int i = 0; i < RARRAY_LEN(object); i++){ p32.x = NUM2DBL(rb_funcall(rb_ary_entry(object, i), rb_intern("x"), 0)); p32.y = NUM2DBL(rb_funcall(rb_ary_entry(object, i), rb_intern("y"), 0)); cvSeqPush(seq, &p32); diff --git a/ext/trackbar.cpp b/ext/trackbar.cpp index 4d4e2eb9..15e1534d 100644 --- a/ext/trackbar.cpp +++ b/ext/trackbar.cpp @@ -76,7 +76,7 @@ VALUE rb_initialize(int argc, VALUE *argv, VALUE self){ if(NIL_P(block)){rb_raise(rb_eArgError, "block not given.");} Check_Type(name, T_STRING); Trackbar *trackbar = TRACKBAR(self); - trackbar->name = strcpy(ALLOC_N(char, RSTRING(name)->len), StringValueCStr(name)); + trackbar->name = strcpy(ALLOC_N(char, RSTRING_LEN(name)), StringValueCStr(name)); trackbar->maxval = NUM2INT(maxval); trackbar->val = IF_INT(val, 0); trackbar->block = block;