From 67de94e7115188791474c341a001b6b48240678f Mon Sep 17 00:00:00 2001 From: ser1zw Date: Sat, 21 May 2011 20:43:16 +0900 Subject: [PATCH] tested CvSlice --- ext/opencv/cvslice.cpp | 63 +++++++++++++++++++++++++++++++++--------- ext/opencv/cvslice.h | 15 +++++++--- test/test_cvslice.rb | 31 +++++++++++++++++++++ 3 files changed, 92 insertions(+), 17 deletions(-) create mode 100755 test/test_cvslice.rb diff --git a/ext/opencv/cvslice.cpp b/ext/opencv/cvslice.cpp index 40e70bd..1369d0b 100644 --- a/ext/opencv/cvslice.cpp +++ b/ext/opencv/cvslice.cpp @@ -41,20 +41,11 @@ define_ruby_class() VALUE opencv = rb_module_opencv(); rb_klass = rb_define_class_under(opencv, "CvSlice", rb_cObject); rb_define_alloc_func(rb_klass, rb_allocate); - rb_define_singleton_method(rb_klass, "compatible?", RUBY_METHOD_FUNC(rb_compatible_q), 1); rb_define_private_method(rb_klass, "initialize", RUBY_METHOD_FUNC(rb_initialize), 2); -} - -/* - * call-seq: - * compatible?(obj) - * - * same Object#kind_of(Range) - */ -VALUE -rb_compatible_q(VALUE klass, VALUE object) -{ - return rb_obj_is_kind_of(object, rb_cRange); + rb_define_method(rb_klass, "start_index", RUBY_METHOD_FUNC(rb_start_index_aref), 0); + rb_define_method(rb_klass, "end_index", RUBY_METHOD_FUNC(rb_end_index_aref), 0); + rb_define_method(rb_klass, "start_index=", RUBY_METHOD_FUNC(rb_start_index_aset), 1); + rb_define_method(rb_klass, "end_index=", RUBY_METHOD_FUNC(rb_end_index_aset), 1); } VALUE @@ -78,5 +69,51 @@ rb_initialize(VALUE self, VALUE start, VALUE end) return self; } +/* + * call-seq: + * start_index + * + */ +VALUE +rb_start_index_aref(VALUE self) +{ + return INT2NUM(CVSLICE(self)->start_index); +} + +/* + * call-seq: + * end_index + * + */ +VALUE +rb_end_index_aref(VALUE self) +{ + return INT2NUM(CVSLICE(self)->end_index); +} + +/* + * call-seq: + * start_index = index + * + */ +VALUE +rb_start_index_aset(VALUE self, VALUE index) +{ + CVSLICE(self)->start_index = FIX2INT(index); + return self; +} + +/* + * call-seq: + * end_index = index + * + */ +VALUE +rb_end_index_aset(VALUE self, VALUE index) +{ + CVSLICE(self)->end_index = FIX2INT(index); + return self; +} + __NAMESPACE_END_CVSLICE __NAMESPACE_END_OPENCV diff --git a/ext/opencv/cvslice.h b/ext/opencv/cvslice.h index fd7a64b..d57c381 100644 --- a/ext/opencv/cvslice.h +++ b/ext/opencv/cvslice.h @@ -22,10 +22,12 @@ VALUE rb_class(); void define_ruby_class(); -VALUE rb_compatible_q(VALUE klass, VALUE object); - VALUE rb_allocate(VALUE klass); VALUE rb_initialize(VALUE self, VALUE start, VALUE end); +VALUE rb_start_index_aref(VALUE self); +VALUE rb_end_index_aref(VALUE self); +VALUE rb_start_index_aset(VALUE self, VALUE index); +VALUE rb_end_index_aset(VALUE self, VALUE index); __NAMESPACE_END_CVSLICE @@ -40,10 +42,15 @@ CVSLICE(VALUE object) inline CvSlice VALUE_TO_CVSLICE(VALUE object) { - if(cCvSlice::rb_compatible_q(cCvSlice::rb_class(), object)){ + if (rb_obj_is_kind_of(object, cCvSlice::rb_class())) { + CvSlice* ptr = CVSLICE(object); + return *ptr; + } + else if (rb_obj_is_kind_of(object, rb_cRange)) { return cvSlice(NUM2INT(rb_funcall(object, rb_intern("begin"), 0)), rb_funcall(object, rb_intern("exclude_end?"), 0) ? NUM2INT(rb_funcall(object, rb_intern("end"), 0)) : NUM2INT(rb_funcall(object, rb_intern("end"), 0)) - 1); - }else{ + } + else { rb_raise(rb_eTypeError, "require %s or compatible object.", rb_class2name(cCvSlice::rb_class())); } } diff --git a/test/test_cvslice.rb b/test/test_cvslice.rb new file mode 100755 index 0000000..ad5d6ab --- /dev/null +++ b/test/test_cvslice.rb @@ -0,0 +1,31 @@ +#!/usr/bin/env ruby +# -*- mode: ruby; coding: utf-8-unix -*- +require 'test/unit' +require 'opencv' +require File.expand_path(File.dirname(__FILE__)) + '/helper' + +include OpenCV + +# Tests for OpenCV::CvSlice +class TestCvSlice < OpenCVTestCase + def setup + @slice = CvSlice.new(2, 4) + end + + def test_initialize + assert_equal(CvSlice, @slice.class) + end + + def test_start_index + assert_equal(2, @slice.start_index) + @slice.start_index = 3 + assert_equal(3, @slice.start_index) + end + + def test_end_index + assert_equal(4, @slice.end_index) + @slice.end_index = 5 + assert_equal(5, @slice.end_index) + end +end +