From 3d45dd1146c5196c00ebc0c47857780f592a4ba5 Mon Sep 17 00:00:00 2001 From: ser1zw Date: Sun, 3 Jul 2011 17:19:39 +0900 Subject: [PATCH] added type check to some CvMat methods (4) --- ext/opencv/cvfont.h | 8 ++ ext/opencv/cvmat.cpp | 36 +++++---- ext/opencv/cvmat.h | 2 +- test/test_cvmat_drawing.rb | 151 +++++++++++++++++++++++++++++++++++-- 4 files changed, 176 insertions(+), 21 deletions(-) diff --git a/ext/opencv/cvfont.h b/ext/opencv/cvfont.h index 429d279..50e12e5 100644 --- a/ext/opencv/cvfont.h +++ b/ext/opencv/cvfont.h @@ -52,6 +52,14 @@ CVFONT(VALUE object) return ptr; } +inline CvFont* +CVFONT_WITH_CHECK(VALUE object) +{ + if (!rb_obj_is_kind_of(object, cCvFont::rb_class())) + raise_typeerror(object, cCvFont::rb_class()); + return CVFONT(object); +} + __NAMESPACE_END_OPENCV #endif // RUBY_OPENCV_CVFONT_H diff --git a/ext/opencv/cvmat.cpp b/ext/opencv/cvmat.cpp index d16625b..2254060 100644 --- a/ext/opencv/cvmat.cpp +++ b/ext/opencv/cvmat.cpp @@ -2874,17 +2874,19 @@ rb_fill_poly_bang(int argc, VALUE *argv, VALUE self) int num_polygons; int *num_points; CvPoint **p; + rb_scan_args(argc, argv, "11", &polygons, &drawing_option); - // TODO: Check type of argument + Check_Type(polygons, T_ARRAY); drawing_option = DRAWING_OPTION(drawing_option); num_polygons = RARRAY_LEN(polygons); num_points = ALLOCA_N(int, num_polygons); + p = ALLOCA_N(CvPoint*, num_polygons); - for (j = 0; j < num_polygons; j++) { + for (j = 0; j < num_polygons; ++j) { points = rb_ary_entry(polygons, j); num_points[j] = RARRAY_LEN(points); p[j] = ALLOCA_N(CvPoint, num_points[j]); - for (i = 0; i < num_points[j]; i++) { + for (i = 0; i < num_points[j]; ++i) { p[j][i] = VALUE_TO_CVPOINT(rb_ary_entry(points, i)); } } @@ -2944,12 +2946,13 @@ rb_fill_convex_poly_bang(int argc, VALUE *argv, VALUE self) VALUE points, drawing_option; int i, num_points; CvPoint *p; + rb_scan_args(argc, argv, "11", &points, &drawing_option); - // TODO: Check type of argument + Check_Type(points, T_ARRAY); drawing_option = DRAWING_OPTION(drawing_option); num_points = RARRAY_LEN(points); p = ALLOCA_N(CvPoint, num_points); - for (i = 0; i < num_points; i++) + for (i = 0; i < num_points; ++i) p[i] = VALUE_TO_CVPOINT(rb_ary_entry(points, i)); cvFillConvexPoly(CVARR(self), @@ -3011,17 +3014,20 @@ rb_poly_line_bang(int argc, VALUE *argv, VALUE self) int num_polygons; int *num_points; CvPoint **p; + rb_scan_args(argc, argv, "11", &polygons, &drawing_option); - // TODO: Check type of argument + Check_Type(polygons, T_ARRAY); drawing_option = DRAWING_OPTION(drawing_option); num_polygons = RARRAY_LEN(polygons); num_points = ALLOCA_N(int, num_polygons); p = ALLOCA_N(CvPoint*, num_polygons); - for (j = 0; j < num_polygons; j++) { + + for (j = 0; j < num_polygons; ++j) { points = rb_ary_entry(polygons, j); + Check_Type(points, T_ARRAY); num_points[j] = RARRAY_LEN(points); p[j] = ALLOCA_N(CvPoint, num_points[j]); - for (i = 0; i < num_points[j]; i++) { + for (i = 0; i < num_points[j]; ++i) { p[j][i] = VALUE_TO_CVPOINT(rb_ary_entry(points, i)); } } @@ -3048,23 +3054,25 @@ rb_poly_line_bang(int argc, VALUE *argv, VALUE self) * font should be CvFont object. */ VALUE -rb_put_text(int argc, VALUE *argv, VALUE self) +rb_put_text(int argc, VALUE* argv, VALUE self) { return rb_put_text_bang(argc, argv, rb_clone(self)); } /* * call-seq: - * put_text!(str, point ,font[,color]) -> self + * put_text!(str, point, font[,color]) -> self * * Draws text string. Return self. */ VALUE -rb_put_text_bang(int argc, VALUE *argv, VALUE self) +rb_put_text_bang(int argc, VALUE* argv, VALUE self) { - VALUE text, point, font, color; - rb_scan_args(argc, argv, "22", &text, &point, &font, &color); - cvPutText(CVARR(self), StringValueCStr(text), VALUE_TO_CVPOINT(point), CVFONT(font), *CVSCALAR(color)); + VALUE _text, _point, _font, _color; + rb_scan_args(argc, argv, "31", &_text, &_point, &_font, &_color); + CvScalar color = NIL_P(_color) ? CV_RGB(0, 0, 0) : VALUE_TO_CVSCALAR(_color); + cvPutText(CVARR(self), StringValueCStr(_text), VALUE_TO_CVPOINT(_point), + CVFONT_WITH_CHECK(_font), color); return self; } diff --git a/ext/opencv/cvmat.h b/ext/opencv/cvmat.h index 436eab8..dee8f06 100644 --- a/ext/opencv/cvmat.h +++ b/ext/opencv/cvmat.h @@ -160,9 +160,9 @@ VALUE rb_fill_convex_poly(int argc, VALUE *argv, VALUE self); VALUE rb_fill_convex_poly_bang(int argc, VALUE *argv, VALUE self); VALUE rb_poly_line(int argc, VALUE *argv, VALUE self); VALUE rb_poly_line_bang(int argc, VALUE *argv, VALUE self); - VALUE rb_put_text(int argc, VALUE *argv, VALUE self); VALUE rb_put_text_bang(int argc, VALUE *argv, VALUE self); + /* cv function */ VALUE rb_sobel(int argc, VALUE *argv, VALUE self); VALUE rb_laplace(int argc, VALUE *argv, VALUE self); diff --git a/test/test_cvmat_drawing.rb b/test/test_cvmat_drawing.rb index 3083dd3..fe48542 100755 --- a/test/test_cvmat_drawing.rb +++ b/test/test_cvmat_drawing.rb @@ -30,9 +30,23 @@ class TestCvMat_drawing < OpenCVTestCase :color => CvColor::Red, :thickness => 3, :line_type => :aa) m1.line!(CvPoint.new(1, 0), CvPoint.new(m0.width - 1, m0.height - 1), :color => CvColor::Blue, :thickness => 1, :line_type => :aa) - - # Uncomment the following line to view the image + + # Uncomment the following line to show the image # snap(['Line: Blue, thickness = 1', m1], ['Line: Red, thickness = 3', m2]) + + assert_raise(TypeError) { + m0.line(DUMMY_OBJ, CvPoint.new(1, 0)) + } + assert_raise(TypeError) { + m0.line(CvPoint.new(1, 0), DUMMY_OBJ) + } + assert_raise(TypeError) { + m0.line(CvPoint.new(1, 0), CvPoint.new(1, 1), :color => DUMMY_OBJ) + } + # assert_raise(CvError) { + # m0.line(CvPoint.new(1, 0), CvPoint.new(1, 1), :thickness => DUMMY_OBJ) + # } + m0.line(CvPoint.new(1, 0), CvPoint.new(1, 1), :line_type => DUMMY_OBJ) end def test_rectangle @@ -43,8 +57,22 @@ class TestCvMat_drawing < OpenCVTestCase m1.rectangle!(CvPoint.new(20, 20), CvPoint.new(m0.width - 20, m0.height - 20), :color => CvColor::Blue, :thickness => 1, :line_type => :aa) - # Uncomment the following line to view the image + # Uncomment the following line to show the image # snap(['Rectangle: Blue, thickness = 1', m1], ['Rectangle: Red, thickness = 3', m2]) + + assert_raise(TypeError) { + m0.line(DUMMY_OBJ, CvPoint.new(1, 0)) + } + assert_raise(TypeError) { + m0.rectangle(CvPoint.new(1, 0), DUMMY_OBJ) + } + assert_raise(TypeError) { + m0.rectangle(CvPoint.new(1, 0), CvPoint.new(1, 1), :color => DUMMY_OBJ) + } + # assert_raise(CvError) { + # m0.rectangle(CvPoint.new(1, 0), CvPoint.new(1, 1), :thickness => DUMMY_OBJ) + # } + m0.rectangle(CvPoint.new(1, 0), CvPoint.new(1, 1), :line_type => DUMMY_OBJ) end def test_circle @@ -54,8 +82,23 @@ class TestCvMat_drawing < OpenCVTestCase :color => CvColor::Red, :thickness => 3, :line_type => :aa) m1.circle!(CvPoint.new(m0.width / 2, m0.height / 2), 80, :color => CvColor::Blue, :thickness => 1, :line_type => :aa) - # Uncomment the following line to view the image + + # Uncomment the following line to show the image # snap(['Circle: Blue, thickness = 1', m1], ['Circle: Red, thickness = 3', m2]) + + assert_raise(TypeError) { + m0.circle(DUMMY_OBJ, 10) + } + assert_raise(TypeError) { + m0.circle(CvPoint.new(1, 0), DUMMY_OBJ) + } + assert_raise(TypeError) { + m0.circle(CvPoint.new(1, 0), 10, :color => DUMMY_OBJ) + } + # assert_raise(CvError) { + # m0.circle(CvPoint.new(1, 0), 10, :thickness => DUMMY_OBJ) + # } + m0.circle(CvPoint.new(1, 0), 10, :line_type => DUMMY_OBJ) end def test_ellipse @@ -66,8 +109,34 @@ class TestCvMat_drawing < OpenCVTestCase m1.ellipse!(CvPoint.new(m0.width / 2, m0.height / 2), CvSize.new(100, 60), 30, 0, 360, :color => CvColor::Blue, :thickness => 1, :line_type => :aa) - # Uncomment the following line to view the image + # Uncomment the following line to show the image # snap(['Ellipse: Blue, thickness = 1', m1], ['Ellipse: Red, thickness = 3', m2]) + + assert_raise(TypeError) { + m1.ellipse(DUMMY_OBJ, CvSize.new(100, 60), 30, 0, 360) + } + assert_raise(TypeError) { + m1.ellipse(CvPoint.new(m0.width / 2, m0.height / 2), DUMMY_OBJ, 30, 0, 360) + } + assert_raise(TypeError) { + m1.ellipse(CvPoint.new(m0.width / 2, m0.height / 2), CvSize.new(100, 60), DUMMY_OBJ, 0, 360) + } + assert_raise(TypeError) { + m1.ellipse(CvPoint.new(m0.width / 2, m0.height / 2), CvSize.new(100, 60), 30, DUMMY_OBJ, 360) + } + assert_raise(TypeError) { + m1.ellipse(CvPoint.new(m0.width / 2, m0.height / 2), CvSize.new(100, 60), 30, 0, DUMMY_OBJ) + } + assert_raise(TypeError) { + m1.ellipse(CvPoint.new(m0.width / 2, m0.height / 2), CvSize.new(100, 60), 30, 0, 360, + :color => DUMMY_OBJ) + } + # assert_raise(CvError) { + # m1.ellipse(CvPoint.new(m0.width / 2, m0.height / 2), CvSize.new(100, 60), 30, 0, 360, + # :thickness => DUMMY_OBJ) + # } + m1.ellipse(CvPoint.new(m0.width / 2, m0.height / 2), CvSize.new(100, 60), 30, 0, 360, + :line_type => DUMMY_OBJ) end def test_ellipse_box @@ -77,8 +146,19 @@ class TestCvMat_drawing < OpenCVTestCase m2 = m0.ellipse_box(box, :color => CvColor::Red, :thickness => 3, :line_type => :aa) m1.ellipse_box!(box, :color => CvColor::Blue, :thickness => 1, :line_type => :aa) - # Uncomment the following line to view the image + # Uncomment the following line to show the image # snap(['Ellipse box: Blue, thickness = 1', m1], ['Ellipse box: Red, thickness = 3', m2]) + + assert_raise(TypeError) { + m1.ellipse_box(DUMMY_OBJ) + } + assert_raise(TypeError) { + m1.ellipse_box(box, :color => DUMMY_OBJ) + } + # assert_raise(CvError) { + # m1.ellipse_box(box, :thickness => DUMMY_OBJ) + # } + m1.ellipse_box(box, :line_type => DUMMY_OBJ) end def test_fill_poly @@ -93,6 +173,20 @@ class TestCvMat_drawing < OpenCVTestCase # Uncomment the following line to view the image # snap(['Fill poly: Blue', m1], ['Fill poly: Red', m2]) + + assert_raise(TypeError) { + m1.fill_poly(DUMMY_OBJ) + } + assert_raise(TypeError) { + m1.fill_poly([DUMMY_OBJ, DUMMY_OBJ]) + } + assert_raise(TypeError) { + m1.fill_poly(pt, :color => DUMMY_OBJ) + } + # assert_raise(CvError) { + # m1.fill_poly(pt, :thickness => DUMMY_OBJ) + # } + m1.fill_poly(pt, :line_type => DUMMY_OBJ) end def test_fill_convex_poly @@ -105,6 +199,21 @@ class TestCvMat_drawing < OpenCVTestCase # Uncomment the following line to view the image # snap(['Fill convex poly: Blue', m1], ['Fill convex poly: Red', m2]) + + + assert_raise(TypeError) { + m1.fill_convex_poly(DUMMY_OBJ) + } + assert_raise(TypeError) { + m1.fill_convex_poly([DUMMY_OBJ, DUMMY_OBJ]) + } + assert_raise(TypeError) { + m1.fill_convex_poly(pt, :color => DUMMY_OBJ) + } + # assert_raise(CvError) { + # m1.fill_convex_poly(pt, :thickness => DUMMY_OBJ) + # } + m1.fill_convex_poly(pt, :line_type => DUMMY_OBJ) end def test_poly_line @@ -118,6 +227,23 @@ class TestCvMat_drawing < OpenCVTestCase # Uncomment the following line to view the image # snap(['Fill poly line: Blue, thickness = 1', m1], ['Fill poly line: Red, thickness = 3', m2]) + + assert_raise(TypeError) { + m1.poly_line(DUMMY_OBJ) + } + assert_raise(TypeError) { + m1.poly_line([DUMMY_OBJ, DUMMY_OBJ]) + } + assert_raise(TypeError) { + m1.poly_line([[DUMMY_OBJ, DUMMY_OBJ], [DUMMY_OBJ, DUMMY_OBJ]]) + } + assert_raise(TypeError) { + m1.poly_line(pt, :color => DUMMY_OBJ) + } + # assert_raise(CvError) { + # m1.poly_line(pt, :thickness => DUMMY_OBJ) + # } + m1.poly_line(pt, :line_type => DUMMY_OBJ) end def test_put_text @@ -130,6 +256,19 @@ class TestCvMat_drawing < OpenCVTestCase # Uncomment the following lines to view the image # snap(['Put text: Blue, thickness = 1', m1], ['Put text: Red, thickness = 3', m2]) + + assert_raise(TypeError) { + m0.put_text(DUMMY_OBJ, CvPoint.new(60, 90), font) + } + assert_raise(TypeError) { + m0.put_text('test', DUMMY_OBJ, font) + } + assert_raise(TypeError) { + m0.put_text('test', CvPoint.new(60, 90), DUMMY_OBJ) + } + assert_raise(TypeError) { + m0.put_text('test', CvPoint.new(60, 90), font, DUMMY_OBJ) + } end end