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

Commit

Permalink
Ruby 1.9.x fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ser1zw committed Dec 29, 2010
1 parent 7f823c4 commit 0e46c20
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 45 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.DS_Store
examples/data
*.o
*.so
Makefile
mkmf.log
opencv.bundle
opencv.bundle
7 changes: 2 additions & 5 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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")
Expand All @@ -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
Expand All @@ -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
Expand Down
40 changes: 20 additions & 20 deletions ext/cvmat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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++) {
Expand All @@ -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);
}

/*
Expand All @@ -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++) {
Expand All @@ -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);
}

/*
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
8 changes: 4 additions & 4 deletions ext/cvseq.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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){
Expand Down
2 changes: 1 addition & 1 deletion ext/cvtermcriteria.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
Expand Down
8 changes: 4 additions & 4 deletions ext/cvvideowriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}
Expand Down
35 changes: 31 additions & 4 deletions ext/opencv.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@

/* include headers */
#include <ruby.h>
#ifdef HAVE_RUBY_VERSION_H
#include <ruby/version.h>
#else
#include <version.h>
#endif

#ifdef RUBY_WIN32_H
#ifdef write
Expand All @@ -28,7 +32,12 @@
#endif

extern "C"{
#ifdef HAVE_RUBY_ST_H
#include <ruby/st.h>
#else
#include <st.h>
#endif

#ifdef HAVE_CALLBACK_H
#include <callback.h> // callhack.h is ffcall header
#endif
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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();
}

/*
Expand All @@ -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;
}
Expand All @@ -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);
}
Expand Down
10 changes: 5 additions & 5 deletions ext/pointset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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).");
}
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion ext/trackbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 0e46c20

Please sign in to comment.