From d3ecd00428b73392e6c2bc19ab25825eb8612c05 Mon Sep 17 00:00:00 2001 From: ser1zw Date: Thu, 30 Jun 2011 14:00:26 +0900 Subject: [PATCH] added type check to some CvMat methods --- ext/opencv/cvmat.cpp | 4 +++- test/test_cvmat.rb | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/ext/opencv/cvmat.cpp b/ext/opencv/cvmat.cpp index 017bd08..c0fbd4d 100644 --- a/ext/opencv/cvmat.cpp +++ b/ext/opencv/cvmat.cpp @@ -1786,9 +1786,11 @@ rb_mul(int argc, VALUE *argv, VALUE self) if (rb_scan_args(argc, argv, "11", &val, &scale) < 2) scale = rb_float_new(1.0); dest = new_mat_kind_object(cvGetSize(CVARR(self)), self); + if (rb_obj_is_kind_of(val, rb_klass)) { cvMul(CVARR(self), CVARR(val), CVARR(dest), NUM2DBL(scale)); - }else{ + } + else { CvScalar scl = VALUE_TO_CVSCALAR(val); VALUE mat = new_object(cvGetSize(CVARR(self)), cvGetElemType(CVARR(self))); cvSet(CVARR(mat), scl); diff --git a/test/test_cvmat.rb b/test/test_cvmat.rb index 9a9b71d..b31df74 100755 --- a/test/test_cvmat.rb +++ b/test/test_cvmat.rb @@ -992,6 +992,10 @@ class TestCvMat < OpenCVTestCase assert_cvscalar_equal(CvScalar.new(0, 0, 0, 0), m4[j, i]) } } + + assert_raise(TypeError) { + m0.convert_scale(DUMMY_OBJ) + } end def test_convert_scale_abs @@ -1018,6 +1022,10 @@ class TestCvMat < OpenCVTestCase assert_cvscalar_equal(m0[j, i], m4[j, i]) } } + + assert_raise(TypeError) { + m0.convert_scale(DUMMY_OBJ) + } end def test_add @@ -1105,6 +1113,13 @@ class TestCvMat < OpenCVTestCase n += 1 } } + + assert_raise(TypeError) { + m1.add(DUMMY_OBJ) + } + assert_raise(TypeError) { + m1.add(CvScalar.new(1), DUMMY_OBJ) + } end def test_sub @@ -1192,6 +1207,13 @@ class TestCvMat < OpenCVTestCase n += 1 } } + + assert_raise(TypeError) { + m1.sub(DUMMY_OBJ) + } + assert_raise(TypeError) { + m1.sub(CvScalar.new(1), DUMMY_OBJ) + } end def test_mul @@ -1236,6 +1258,13 @@ class TestCvMat < OpenCVTestCase n = (c + 1) * scale CvScalar.new(n * 0.1, n * 0.2, n * 0.3, n * 0.4) } + + assert_raise(TypeError) { + m1.mul(DUMMY_OBJ) + } + assert_raise(TypeError) { + m1.mul(m2, DUMMY_OBJ) + } end def test_mat_mul @@ -1280,6 +1309,13 @@ class TestCvMat < OpenCVTestCase assert_in_delta(17, m[2, 1][0], 0.001) assert_in_delta(20.1, m[2, 2][0], 0.001) } + + assert_raise(TypeError) { + m0.mat_mul(DUMMY_OBJ) + } + assert_raise(TypeError) { + m0.mat_mul(m1, DUMMY_OBJ) + } end def test_div