Merge remote-tracking branch 'upstream/3.4' into merge-3.4
[platform/upstream/opencv.git] / modules / imgproc / test / test_drawing.cpp
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
5 //  By downloading, copying, installing or using the software you agree to this license.
6 //  If you do not agree to this license, do not download, install,
7 //  copy or use the software.
8 //
9 //
10 //                          License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
16 //
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
19 //
20 //   * Redistribution's of source code must retain the above copyright notice,
21 //     this list of conditions and the following disclaimer.
22 //
23 //   * Redistribution's in binary form must reproduce the above copyright notice,
24 //     this list of conditions and the following disclaimer in the documentation
25 //     and/or other materials provided with the distribution.
26 //
27 //   * The name of the copyright holders may not be used to endorse or promote products
28 //     derived from this software without specific prior written permission.
29 //
30 // This software is provided by the copyright holders and contributors "as is" and
31 // any express or implied warranties, including, but not limited to, the implied
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
34 // indirect, incidental, special, exemplary, or consequential damages
35 // (including, but not limited to, procurement of substitute goods or services;
36 // loss of use, data, or profits; or business interruption) however caused
37 // and on any theory of liability, whether in contract, strict liability,
38 // or tort (including negligence or otherwise) arising in any way out of
39 // the use of this software, even if advised of the possibility of such damage.
40 //
41 //M*/
42
43 #include "test_precomp.hpp"
44
45 namespace opencv_test { namespace {
46
47 //#define DRAW_TEST_IMAGE
48
49 class CV_DrawingTest : public cvtest::BaseTest
50 {
51 public:
52     CV_DrawingTest(){}
53 protected:
54     void run( int );
55     virtual void draw( Mat& img ) = 0;
56     virtual int checkLineIterator( Mat& img) = 0;
57     virtual int checkLineVirtualIterator() = 0;
58 };
59
60 void CV_DrawingTest::run( int )
61 {
62     Mat testImg, valImg;
63     const string fname = "../highgui/drawing/image.png";
64     string path = ts->get_data_path(), filename;
65     filename = path + fname;
66
67     draw( testImg );
68
69     valImg = imread( filename );
70     if( valImg.empty() )
71     {
72         //imwrite( filename, testImg );
73         ts->printf( ts->LOG, "test image can not be read");
74 #ifdef HAVE_PNG
75         ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_TEST_DATA);
76 #else
77         ts->printf( ts->LOG, "PNG image support is not available");
78         ts->set_failed_test_info(cvtest::TS::OK);
79 #endif
80         return;
81     }
82     else
83     {
84         // image should match exactly
85         float err = (float)cvtest::norm( testImg, valImg, NORM_L1 );
86         float Eps = 1;
87         if( err > Eps)
88         {
89             ts->printf( ts->LOG, "NORM_L1 between testImg and valImg is equal %f (larger than %f)\n", err, Eps );
90             ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY);
91         }
92         else
93         {
94             ts->set_failed_test_info(checkLineIterator( testImg ));
95         }
96     }
97     ts->set_failed_test_info(checkLineVirtualIterator());
98     ts->set_failed_test_info(cvtest::TS::OK);
99 }
100
101 class CV_DrawingTest_CPP : public CV_DrawingTest
102 {
103 public:
104     CV_DrawingTest_CPP() {}
105 protected:
106     virtual void draw( Mat& img );
107     virtual int checkLineIterator( Mat& img);
108     virtual int checkLineVirtualIterator();
109 };
110
111 void CV_DrawingTest_CPP::draw( Mat& img )
112 {
113     Size imgSize( 600, 400 );
114     img.create( imgSize, CV_8UC3 );
115
116     vector<Point> polyline(4);
117     polyline[0] = Point(0, 0);
118     polyline[1] = Point(imgSize.width, 0);
119     polyline[2] = Point(imgSize.width, imgSize.height);
120     polyline[3] = Point(0, imgSize.height);
121     const Point* pts = &polyline[0];
122     int n = (int)polyline.size();
123     fillPoly( img, &pts, &n, 1, Scalar::all(255) );
124
125     Point p1(1,1), p2(3,3);
126     if( clipLine(Rect(0,0,imgSize.width,imgSize.height), p1, p2) && clipLine(imgSize, p1, p2) )
127         circle( img, Point(300,100), 40, Scalar(0,0,255), 3 ); // draw
128
129     p2 = Point(3,imgSize.height+1000);
130     if( clipLine(Rect(0,0,imgSize.width,imgSize.height), p1, p2) && clipLine(imgSize, p1, p2) )
131         circle( img, Point(500,300), 50, cvColorToScalar(255,CV_8UC3), 5, 8, 1 ); // draw
132
133     p1 = Point(imgSize.width,1), p2 = Point(imgSize.width,3);
134     if( clipLine(Rect(0,0,imgSize.width,imgSize.height), p1, p2) && clipLine(imgSize, p1, p2) )
135         circle( img, Point(390,100), 10, Scalar(0,0,255), 3 ); // not draw
136
137     p1 = Point(imgSize.width-1,1), p2 = Point(imgSize.width,3);
138     if( clipLine(Rect(0,0,imgSize.width,imgSize.height), p1, p2) && clipLine(imgSize, p1, p2) )
139         ellipse( img, Point(390,100), Size(20,30), 60, 0, 220.0, Scalar(0,200,0), 4 ); //draw
140
141     ellipse( img, RotatedRect(Point(100,200),Size(200,100),160), Scalar(200,200,255), 5 );
142
143     polyline.clear();
144     ellipse2Poly( Point(430,180), Size(100,150), 30, 0, 150, 20, polyline );
145     pts = &polyline[0];
146     n = (int)polyline.size();
147     polylines( img, &pts, &n, 1, false, Scalar(0,0,150), 4, CV_AA );
148     n = 0;
149     for( vector<Point>::const_iterator it = polyline.begin(); n < (int)polyline.size()-1; ++it, n++ )
150     {
151         line( img, *it, *(it+1), Scalar(50,250,100));
152     }
153
154     polyline.clear();
155     ellipse2Poly( Point(500,300), Size(50,80), 0, 0, 180, 10, polyline );
156     pts = &polyline[0];
157     n = (int)polyline.size();
158     polylines( img, &pts, &n, 1, true, Scalar(100,200,100), 20 );
159     fillConvexPoly( img, pts, n, Scalar(0, 80, 0) );
160
161     polyline.resize(8);
162     // external rectengular
163     polyline[0] = Point(0, 0);
164     polyline[1] = Point(80, 0);
165     polyline[2] = Point(80, 80);
166     polyline[3] = Point(0, 80);
167     // internal rectangular
168     polyline[4] = Point(20, 20);
169     polyline[5] = Point(60, 20);
170     polyline[6] = Point(60, 60);
171     polyline[7] = Point(20, 60);
172     const Point* ppts[] = {&polyline[0], &polyline[0]+4};
173     int pn[] = {4, 4};
174     fillPoly( img, ppts, pn, 2, Scalar(100, 100, 0), 8, 0, Point(500, 20) );
175
176     rectangle( img, Point(0, 300), Point(50, 398), Scalar(0,0,255) );
177
178     string text1 = "OpenCV";
179     int baseline = 0, thickness = 3, fontFace = FONT_HERSHEY_SCRIPT_SIMPLEX;
180     float fontScale = 2;
181     Size textSize = getTextSize( text1, fontFace, fontScale, thickness, &baseline);
182     baseline += thickness;
183     Point textOrg((img.cols - textSize.width)/2, (img.rows + textSize.height)/2);
184     rectangle(img, textOrg + Point(0, baseline), textOrg + Point(textSize.width, -textSize.height), Scalar(0,0,255));
185     line(img, textOrg + Point(0, thickness), textOrg + Point(textSize.width, thickness), Scalar(0, 0, 255));
186     putText(img, text1, textOrg, fontFace, fontScale, Scalar(150,0,150), thickness, 8);
187
188     string text2 = "abcdefghijklmnopqrstuvwxyz1234567890";
189     Scalar color(200,0,0);
190     fontScale = 0.5, thickness = 1;
191     int dist = 5;
192
193     textSize = getTextSize( text2, FONT_HERSHEY_SIMPLEX, fontScale, thickness, &baseline);
194     textOrg = Point(5,5)+Point(0,textSize.height+dist);
195     putText(img, text2, textOrg, FONT_HERSHEY_SIMPLEX, fontScale, color, thickness, CV_AA);
196
197     fontScale = 1;
198     textSize = getTextSize( text2, FONT_HERSHEY_PLAIN, fontScale, thickness, &baseline);
199     textOrg += Point(0,textSize.height+dist);
200     putText(img, text2, textOrg, FONT_HERSHEY_PLAIN, fontScale, color, thickness, CV_AA);
201
202     fontScale = 0.5;
203     textSize = getTextSize( text2, FONT_HERSHEY_DUPLEX, fontScale, thickness, &baseline);
204     textOrg += Point(0,textSize.height+dist);
205     putText(img, text2, textOrg, FONT_HERSHEY_DUPLEX, fontScale, color, thickness, CV_AA);
206
207     textSize = getTextSize( text2, FONT_HERSHEY_COMPLEX, fontScale, thickness, &baseline);
208     textOrg += Point(0,textSize.height+dist);
209     putText(img, text2, textOrg, FONT_HERSHEY_COMPLEX, fontScale, color, thickness, CV_AA);
210
211     textSize = getTextSize( text2, FONT_HERSHEY_TRIPLEX, fontScale, thickness, &baseline);
212     textOrg += Point(0,textSize.height+dist);
213     putText(img, text2, textOrg, FONT_HERSHEY_TRIPLEX, fontScale, color, thickness, CV_AA);
214
215     fontScale = 1;
216     textSize = getTextSize( text2, FONT_HERSHEY_COMPLEX_SMALL, fontScale, thickness, &baseline);
217     textOrg += Point(0,180) + Point(0,textSize.height+dist);
218     putText(img, text2, textOrg, FONT_HERSHEY_COMPLEX_SMALL, fontScale, color, thickness, CV_AA);
219
220     textSize = getTextSize( text2, FONT_HERSHEY_SCRIPT_SIMPLEX, fontScale, thickness, &baseline);
221     textOrg += Point(0,textSize.height+dist);
222     putText(img, text2, textOrg, FONT_HERSHEY_SCRIPT_SIMPLEX, fontScale, color, thickness, CV_AA);
223
224     textSize = getTextSize( text2, FONT_HERSHEY_SCRIPT_COMPLEX, fontScale, thickness, &baseline);
225     textOrg += Point(0,textSize.height+dist);
226     putText(img, text2, textOrg, FONT_HERSHEY_SCRIPT_COMPLEX, fontScale, color, thickness, CV_AA);
227
228     dist = 15, fontScale = 0.5;
229     textSize = getTextSize( text2, FONT_ITALIC, fontScale, thickness, &baseline);
230     textOrg += Point(0,textSize.height+dist);
231     putText(img, text2, textOrg, FONT_ITALIC, fontScale, color, thickness, CV_AA);
232 }
233
234 int CV_DrawingTest_CPP::checkLineIterator( Mat& img )
235 {
236     LineIterator it( img, Point(0,300), Point(1000, 300) );
237     for(int i = 0; i < it.count; ++it, i++ )
238     {
239         Vec3b v = (Vec3b)(*(*it)) - img.at<Vec3b>(300,i);
240         float err = (float)cvtest::norm( v, NORM_L2 );
241         if( err != 0 )
242         {
243             ts->printf( ts->LOG, "LineIterator works incorrect" );
244             ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_OUTPUT);
245         }
246     }
247     ts->set_failed_test_info(cvtest::TS::OK);
248     return 0;
249 }
250
251 int CV_DrawingTest_CPP::checkLineVirtualIterator(  )
252 {
253     RNG randomGenerator(1);
254     for (size_t test = 0; test < 10000; ++test)
255     {
256         int width = randomGenerator.uniform(0, 512+1);
257         int height = randomGenerator.uniform(0, 512+1);
258         int x1 = randomGenerator.uniform(-512, 1024+1);
259         int y1 = randomGenerator.uniform(-512, 1024+1);
260         int x2 = randomGenerator.uniform(-512, 1024+1);
261         int y2 = randomGenerator.uniform(-512, 1024+1);
262         int x3 = randomGenerator.uniform(-512, 1024+1);
263         int y3 = randomGenerator.uniform(-512, 1024+1);
264         int channels = randomGenerator.uniform(1, 3+1);
265         Mat m(cv::Size(width, height), CV_MAKETYPE(8U, channels));
266         Point p1(x1, y1);
267         Point p2(x2, y2);
268         Point offset(x3, y3);
269         LineIterator it( m, p1, p2 );
270         LineIterator vit(Rect(offset.x, offset.y, width, height), p1 + offset, p2 + offset);
271         if (it.count != vit.count)
272         {
273            ts->printf( ts->LOG, "virtual LineIterator works incorrectly" );
274            ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_OUTPUT);
275            break;
276         }
277         else
278         {
279             for(int i = 0; i < it.count; ++it, ++vit, i++ )
280             {
281                 Point pIt = it.pos();
282                 Point pVit = vit.pos() - offset;
283                 if (pIt != pVit)
284                 {
285                     ts->printf( ts->LOG, "virtual LineIterator works incorrectly" );
286                     ts->set_failed_test_info(cvtest::TS::FAIL_INVALID_OUTPUT);
287                     break;
288                 }
289             }
290         }
291     }
292     ts->set_failed_test_info(cvtest::TS::OK);
293     return 0;
294 }
295
296 class CV_DrawingTest_Far : public CV_DrawingTest_CPP
297 {
298 public:
299     CV_DrawingTest_Far() {}
300 protected:
301     virtual void draw(Mat& img);
302 };
303
304 void CV_DrawingTest_Far::draw(Mat& img)
305 {
306     Size imgSize(32768 + 600, 400);
307     img.create(imgSize, CV_8UC3);
308
309     vector<Point> polyline(4);
310     polyline[0] = Point(32768 + 0, 0);
311     polyline[1] = Point(imgSize.width, 0);
312     polyline[2] = Point(imgSize.width, imgSize.height);
313     polyline[3] = Point(32768 + 0, imgSize.height);
314     const Point* pts = &polyline[0];
315     int n = (int)polyline.size();
316     fillPoly(img, &pts, &n, 1, Scalar::all(255));
317
318     Point p1(32768 + 1, 1), p2(32768 + 3, 3);
319     if (clipLine(Rect(32768 + 0, 0, imgSize.width, imgSize.height), p1, p2) && clipLine(imgSize, p1, p2))
320         circle(img, Point(32768 + 300, 100), 40, Scalar(0, 0, 255), 3); // draw
321
322     p2 = Point(32768 + 3, imgSize.height + 1000);
323     if (clipLine(Rect(32768 + 0, 0, imgSize.width, imgSize.height), p1, p2) && clipLine(imgSize, p1, p2))
324         circle(img, Point(65536 + 500, 300), 50, cvColorToScalar(255, CV_8UC3), 5, 8, 1); // draw
325
326     p1 = Point(imgSize.width, 1), p2 = Point(imgSize.width, 3);
327     if (clipLine(Rect(32768 + 0, 0, imgSize.width, imgSize.height), p1, p2) && clipLine(imgSize, p1, p2))
328         circle(img, Point(32768 + 390, 100), 10, Scalar(0, 0, 255), 3); // not draw
329
330     p1 = Point(imgSize.width - 1, 1), p2 = Point(imgSize.width, 3);
331     if (clipLine(Rect(32768 + 0, 0, imgSize.width, imgSize.height), p1, p2) && clipLine(imgSize, p1, p2))
332         ellipse(img, Point(32768 + 390, 100), Size(20, 30), 60, 0, 220.0, Scalar(0, 200, 0), 4); //draw
333
334     ellipse(img, RotatedRect(Point(32768 + 100, 200), Size(200, 100), 160), Scalar(200, 200, 255), 5);
335
336     polyline.clear();
337     ellipse2Poly(Point(32768 + 430, 180), Size(100, 150), 30, 0, 150, 20, polyline);
338     pts = &polyline[0];
339     n = (int)polyline.size();
340     polylines(img, &pts, &n, 1, false, Scalar(0, 0, 150), 4, CV_AA);
341     n = 0;
342     for (vector<Point>::const_iterator it = polyline.begin(); n < (int)polyline.size() - 1; ++it, n++)
343     {
344         line(img, *it, *(it + 1), Scalar(50, 250, 100));
345     }
346
347     polyline.clear();
348     ellipse2Poly(Point(32768 + 500, 300), Size(50, 80), 0, 0, 180, 10, polyline);
349     pts = &polyline[0];
350     n = (int)polyline.size();
351     polylines(img, &pts, &n, 1, true, Scalar(100, 200, 100), 20);
352     fillConvexPoly(img, pts, n, Scalar(0, 80, 0));
353
354     polyline.resize(8);
355     // external rectengular
356     polyline[0] = Point(32768 + 0, 0);
357     polyline[1] = Point(32768 + 80, 0);
358     polyline[2] = Point(32768 + 80, 80);
359     polyline[3] = Point(32768 + 0, 80);
360     // internal rectangular
361     polyline[4] = Point(32768 + 20, 20);
362     polyline[5] = Point(32768 + 60, 20);
363     polyline[6] = Point(32768 + 60, 60);
364     polyline[7] = Point(32768 + 20, 60);
365     const Point* ppts[] = { &polyline[0], &polyline[0] + 4 };
366     int pn[] = { 4, 4 };
367     fillPoly(img, ppts, pn, 2, Scalar(100, 100, 0), 8, 0, Point(500, 20));
368
369     rectangle(img, Point(32768 + 0, 300), Point(32768 + 50, 398), Scalar(0, 0, 255));
370
371     string text1 = "OpenCV";
372     int baseline = 0, thickness = 3, fontFace = FONT_HERSHEY_SCRIPT_SIMPLEX;
373     float fontScale = 2;
374     Size textSize = getTextSize(text1, fontFace, fontScale, thickness, &baseline);
375     baseline += thickness;
376     Point textOrg((32768 + img.cols - textSize.width) / 2, (img.rows + textSize.height) / 2);
377     rectangle(img, textOrg + Point(0, baseline), textOrg + Point(textSize.width, -textSize.height), Scalar(0, 0, 255));
378     line(img, textOrg + Point(0, thickness), textOrg + Point(textSize.width, thickness), Scalar(0, 0, 255));
379     putText(img, text1, textOrg, fontFace, fontScale, Scalar(150, 0, 150), thickness, 8);
380
381     string text2 = "abcdefghijklmnopqrstuvwxyz1234567890";
382     Scalar color(200, 0, 0);
383     fontScale = 0.5, thickness = 1;
384     int dist = 5;
385
386     textSize = getTextSize(text2, FONT_HERSHEY_SIMPLEX, fontScale, thickness, &baseline);
387     textOrg = Point(32768 + 5, 5) + Point(0, textSize.height + dist);
388     putText(img, text2, textOrg, FONT_HERSHEY_SIMPLEX, fontScale, color, thickness, CV_AA);
389
390     fontScale = 1;
391     textSize = getTextSize(text2, FONT_HERSHEY_PLAIN, fontScale, thickness, &baseline);
392     textOrg += Point(0, textSize.height + dist);
393     putText(img, text2, textOrg, FONT_HERSHEY_PLAIN, fontScale, color, thickness, CV_AA);
394
395     fontScale = 0.5;
396     textSize = getTextSize(text2, FONT_HERSHEY_DUPLEX, fontScale, thickness, &baseline);
397     textOrg += Point(0, textSize.height + dist);
398     putText(img, text2, textOrg, FONT_HERSHEY_DUPLEX, fontScale, color, thickness, CV_AA);
399
400     textSize = getTextSize(text2, FONT_HERSHEY_COMPLEX, fontScale, thickness, &baseline);
401     textOrg += Point(0, textSize.height + dist);
402     putText(img, text2, textOrg, FONT_HERSHEY_COMPLEX, fontScale, color, thickness, CV_AA);
403
404     textSize = getTextSize(text2, FONT_HERSHEY_TRIPLEX, fontScale, thickness, &baseline);
405     textOrg += Point(0, textSize.height + dist);
406     putText(img, text2, textOrg, FONT_HERSHEY_TRIPLEX, fontScale, color, thickness, CV_AA);
407
408     fontScale = 1;
409     textSize = getTextSize(text2, FONT_HERSHEY_COMPLEX_SMALL, fontScale, thickness, &baseline);
410     textOrg += Point(0, 180) + Point(0, textSize.height + dist);
411     putText(img, text2, textOrg, FONT_HERSHEY_COMPLEX_SMALL, fontScale, color, thickness, CV_AA);
412
413     textSize = getTextSize(text2, FONT_HERSHEY_SCRIPT_SIMPLEX, fontScale, thickness, &baseline);
414     textOrg += Point(0, textSize.height + dist);
415     putText(img, text2, textOrg, FONT_HERSHEY_SCRIPT_SIMPLEX, fontScale, color, thickness, CV_AA);
416
417     textSize = getTextSize(text2, FONT_HERSHEY_SCRIPT_COMPLEX, fontScale, thickness, &baseline);
418     textOrg += Point(0, textSize.height + dist);
419     putText(img, text2, textOrg, FONT_HERSHEY_SCRIPT_COMPLEX, fontScale, color, thickness, CV_AA);
420
421     dist = 15, fontScale = 0.5;
422     textSize = getTextSize(text2, FONT_ITALIC, fontScale, thickness, &baseline);
423     textOrg += Point(0, textSize.height + dist);
424     putText(img, text2, textOrg, FONT_ITALIC, fontScale, color, thickness, CV_AA);
425
426     img = img(Rect(32768, 0, 600, 400)).clone();
427 }
428
429 TEST(Drawing,    cpp_regression) { CV_DrawingTest_CPP test; test.safe_run(); }
430 TEST(Drawing,    far_regression) { CV_DrawingTest_Far test; test.safe_run(); }
431
432 class CV_FillConvexPolyTest : public cvtest::BaseTest
433 {
434 public:
435     CV_FillConvexPolyTest() {}
436     ~CV_FillConvexPolyTest() {}
437 protected:
438     void run(int)
439     {
440         vector<Point> line1;
441         vector<Point> line2;
442
443         line1.push_back(Point(1, 1));
444         line1.push_back(Point(5, 1));
445         line1.push_back(Point(5, 8));
446         line1.push_back(Point(1, 8));
447
448         line2.push_back(Point(2, 2));
449         line2.push_back(Point(10, 2));
450         line2.push_back(Point(10, 16));
451         line2.push_back(Point(2, 16));
452
453         Mat gray0(10,10,CV_8U, Scalar(0));
454         fillConvexPoly(gray0, line1, Scalar(255), 8, 0);
455         int nz1 = countNonZero(gray0);
456
457         fillConvexPoly(gray0, line2, Scalar(0), 8, 1);
458         int nz2 = countNonZero(gray0)/255;
459
460         CV_Assert( nz1 == 40 && nz2 == 0 );
461     }
462 };
463
464 TEST(Drawing, fillconvexpoly_clipping) { CV_FillConvexPolyTest test; test.safe_run(); }
465
466 class CV_DrawingTest_UTF8 : public cvtest::BaseTest
467 {
468 public:
469     CV_DrawingTest_UTF8() {}
470     ~CV_DrawingTest_UTF8() {}
471 protected:
472     void run(int)
473     {
474         vector<string> lines;
475         lines.push_back("abcdefghijklmnopqrstuvwxyz1234567890");
476         // cyrillic letters small
477         lines.push_back("\xD0\xB0\xD0\xB1\xD0\xB2\xD0\xB3\xD0\xB4\xD0\xB5\xD1\x91\xD0\xB6\xD0\xB7"
478                         "\xD0\xB8\xD0\xB9\xD0\xBA\xD0\xBB\xD0\xBC\xD0\xBD\xD0\xBE\xD0\xBF\xD1\x80"
479                         "\xD1\x81\xD1\x82\xD1\x83\xD1\x84\xD1\x85\xD1\x86\xD1\x87\xD1\x88\xD1\x89"
480                         "\xD1\x8A\xD1\x8B\xD1\x8C\xD1\x8D\xD1\x8E\xD1\x8F");
481         // cyrillic letters capital
482         lines.push_back("\xD0\x90\xD0\x91\xD0\x92\xD0\x93\xD0\x94\xD0\x95\xD0\x81\xD0\x96\xD0\x97"
483                         "\xD0\x98\xD0\x99\xD0\x9A\xD0\x9B\xD0\x9C\xD0\x9D\xD0\x9E\xD0\x9F\xD0\xA0"
484                         "\xD0\xA1\xD0\xA2\xD0\xA3\xD0\xA4\xD0\xA5\xD0\xA6\xD0\xA7\xD0\xA8\xD0\xA9"
485                         "\xD0\xAA\xD0\xAB\xD0\xAC\xD0\xAD\xD0\xAE\xD0\xAF");
486         // bounds
487         lines.push_back("-\xD0\x80-\xD0\x8E-\xD0\x8F-");
488         lines.push_back("-\xD1\x90-\xD1\x91-\xD1\xBF-");
489         // bad utf8
490         lines.push_back("-\x81-\x82-\x83-");
491         lines.push_back("--\xF0--");
492         lines.push_back("-\xF0");
493
494         vector<int> fonts;
495         fonts.push_back(FONT_HERSHEY_SIMPLEX);
496         fonts.push_back(FONT_HERSHEY_PLAIN);
497         fonts.push_back(FONT_HERSHEY_DUPLEX);
498         fonts.push_back(FONT_HERSHEY_COMPLEX);
499         fonts.push_back(FONT_HERSHEY_TRIPLEX);
500         fonts.push_back(FONT_HERSHEY_COMPLEX_SMALL);
501         fonts.push_back(FONT_HERSHEY_SCRIPT_SIMPLEX);
502         fonts.push_back(FONT_HERSHEY_SCRIPT_COMPLEX);
503
504         vector<Mat> results;
505         Size bigSize(0, 0);
506         for (vector<int>::const_iterator font = fonts.begin(); font != fonts.end(); ++font)
507         {
508             for (int italic = 0; italic <= FONT_ITALIC; italic += FONT_ITALIC)
509             {
510                 for (vector<string>::const_iterator line = lines.begin(); line != lines.end(); ++line)
511                 {
512                     const float fontScale = 1;
513                     const int thickness = 1;
514                     const Scalar color(20,20,20);
515                     int baseline = 0;
516
517                     Size textSize = getTextSize(*line, *font | italic, fontScale, thickness, &baseline);
518                     Point textOrg(0, textSize.height + 2);
519                     Mat img(textSize + Size(0, baseline), CV_8UC3, Scalar(255, 255, 255));
520                     putText(img, *line, textOrg, *font | italic, fontScale, color, thickness, CV_AA);
521
522                     results.push_back(img);
523                     bigSize.width = max(bigSize.width, img.size().width);
524                     bigSize.height += img.size().height + 1;
525                 }
526             }
527         }
528
529         int shift = 0;
530         Mat result(bigSize, CV_8UC3, Scalar(100, 100, 100));
531         for (vector<Mat>::const_iterator img = results.begin(); img != results.end(); ++img)
532         {
533             Rect roi(Point(0, shift), img->size());
534             Mat sub(result, roi);
535             img->copyTo(sub);
536             shift += img->size().height + 1;
537         }
538         if (cvtest::debugLevel > 0)
539             imwrite("all_fonts.png", result);
540     }
541 };
542
543 TEST(Drawing, utf8_support) { CV_DrawingTest_UTF8 test; test.safe_run(); }
544
545
546 TEST(Drawing, _914)
547 {
548     const int rows = 256;
549     const int cols = 256;
550
551     Mat img(rows, cols, CV_8UC1, Scalar(255));
552
553     line(img, Point(0, 10), Point(255, 10), Scalar(0), 2, 4);
554     line(img, Point(-5, 20), Point(260, 20), Scalar(0), 2, 4);
555     line(img, Point(10, 0), Point(10, 255), Scalar(0), 2, 4);
556
557     double x0 = 0.0/pow(2.0, -2.0);
558     double x1 = 255.0/pow(2.0, -2.0);
559     double y = 30.5/pow(2.0, -2.0);
560
561     line(img, Point(int(x0), int(y)), Point(int(x1), int(y)), Scalar(0), 2, 4, 2);
562
563     int pixelsDrawn = rows*cols - countNonZero(img);
564     ASSERT_EQ( (3*rows + cols)*3 - 3*9, pixelsDrawn);
565 }
566
567 TEST(Drawing, polylines_empty)
568 {
569     Mat img(100, 100, CV_8UC1, Scalar(0));
570     vector<Point> pts; // empty
571     polylines(img, pts, false, Scalar(255));
572     int cnt = countNonZero(img);
573     ASSERT_EQ(cnt, 0);
574 }
575
576 TEST(Drawing, polylines)
577 {
578     Mat img(100, 100, CV_8UC1, Scalar(0));
579     vector<Point> pts;
580     pts.push_back(Point(0, 0));
581     pts.push_back(Point(20, 0));
582     polylines(img, pts, false, Scalar(255));
583     int cnt = countNonZero(img);
584     ASSERT_EQ(cnt, 21);
585 }
586
587 TEST(Drawing, longline)
588 {
589     Mat mat = Mat::zeros(256, 256, CV_8UC1);
590
591     line(mat, cv::Point(34, 204), cv::Point(46400, 47400), cv::Scalar(255), 3);
592     EXPECT_EQ(310, cv::countNonZero(mat));
593
594     Point pt[6];
595     pt[0].x = 32;
596     pt[0].y = 204;
597     pt[1].x = 34;
598     pt[1].y = 202;
599     pt[2].x = 87;
600     pt[2].y = 255;
601     pt[3].x = 82;
602     pt[3].y = 255;
603     pt[4].x = 37;
604     pt[4].y = 210;
605     pt[5].x = 37;
606     pt[5].y = 209;
607     fillConvexPoly(mat, pt, 6, cv::Scalar(0));
608
609     EXPECT_EQ(0, cv::countNonZero(mat));
610 }
611
612
613 TEST(Drawing, putText_no_garbage)
614 {
615     Size sz(640, 480);
616     Mat mat = Mat::zeros(sz, CV_8UC1);
617
618     mat = Scalar::all(0);
619     putText(mat, "029", Point(10, 350), 0, 10, Scalar(128), 15);
620
621     EXPECT_EQ(0, cv::countNonZero(mat(Rect(0, 0,           10, sz.height))));
622     EXPECT_EQ(0, cv::countNonZero(mat(Rect(sz.width-10, 0, 10, sz.height))));
623     EXPECT_EQ(0, cv::countNonZero(mat(Rect(205, 0,         10, sz.height))));
624     EXPECT_EQ(0, cv::countNonZero(mat(Rect(405, 0,         10, sz.height))));
625 }
626
627
628 TEST(Drawing, line)
629 {
630     Mat mat = Mat::zeros(Size(100,100), CV_8UC1);
631
632     ASSERT_THROW(line(mat, Point(1,1),Point(99,99),Scalar(255),0), cv::Exception);
633 }
634
635 TEST(Drawing, regression_16308)
636 {
637     Mat_<uchar> img(Size(100, 100), (uchar)0);
638     circle(img, Point(50, 50), 50, 255, 1, LINE_AA);
639     EXPECT_NE(0, (int)img.at<uchar>(0, 50));
640     EXPECT_NE(0, (int)img.at<uchar>(50, 0));
641     EXPECT_NE(0, (int)img.at<uchar>(50, 99));
642     EXPECT_NE(0, (int)img.at<uchar>(99, 50));
643 }
644
645 TEST(Drawing, fillpoly_circle)
646 {
647     Mat img_c(640, 480, CV_8UC3, Scalar::all(0));
648     Mat img_fp = img_c.clone(), img_fcp = img_c.clone(), img_fp3 = img_c.clone();
649
650     Point center1(img_c.cols/2, img_c.rows/2);
651     Point center2(img_c.cols/10, img_c.rows*3/4);
652     Point center3 = Point(img_c.cols, img_c.rows) - center2;
653     int radius = img_c.rows/4;
654     int radius_small = img_c.cols/15;
655     Scalar color(0, 0, 255);
656
657     circle(img_c, center1, radius, color, -1);
658
659     // check that circle, fillConvexPoly and fillPoly
660     // give almost the same result then asked to draw a single circle
661     vector<Point> vtx;
662     ellipse2Poly(center1, Size(radius, radius), 0, 0, 360, 1, vtx);
663     fillConvexPoly(img_fcp, vtx, color);
664     fillPoly(img_fp, vtx, color);
665     double diff_fp = cv::norm(img_c, img_fp, NORM_L1)/(255*radius*2*CV_PI);
666     double diff_fcp = cv::norm(img_c, img_fcp, NORM_L1)/(255*radius*2*CV_PI);
667     EXPECT_LT(diff_fp, 1.);
668     EXPECT_LT(diff_fcp, 1.);
669
670     // check that fillPoly can draw 3 disjoint circles at once
671     circle(img_c, center2, radius_small, color, -1);
672     circle(img_c, center3, radius_small, color, -1);
673
674     vector<vector<Point> > vtx3(3);
675     vtx3[0] = vtx;
676     ellipse2Poly(center2, Size(radius_small, radius_small), 0, 0, 360, 1, vtx3[1]);
677     ellipse2Poly(center3, Size(radius_small, radius_small), 0, 0, 360, 1, vtx3[2]);
678     fillPoly(img_fp3, vtx3, color);
679     double diff_fp3 = cv::norm(img_c, img_fp3, NORM_L1)/(255*(radius+radius_small*2)*2*CV_PI);
680     EXPECT_LT(diff_fp3, 1.);
681 }
682
683 }} // namespace