1
0
Fork 0
mirror of https://github.com/ruby-opencv/ruby-opencv synced 2023-03-27 23:22:12 -04:00

tested CvSlice

This commit is contained in:
ser1zw 2011-05-21 20:43:16 +09:00
parent 9567a93fba
commit 67de94e711
3 changed files with 92 additions and 17 deletions

View file

@ -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?(<i>obj</i>)
*
* 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

View file

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

31
test/test_cvslice.rb Executable file
View file

@ -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