Update to 2.0.0 tree from current Fremantle build
[opencv] / samples / c / drawing.c
1 #ifdef _CH_
2 #pragma package <opencv>
3 #endif
4
5 #define CV_NO_BACKWARD_COMPATIBILITY
6
7 #ifndef _EiC
8 #include "cv.h"
9 #include "highgui.h"
10 #include <stdlib.h>
11 #include <stdio.h>
12 #endif
13
14 #define NUMBER 100
15 #define DELAY 5
16 char wndname[] = "Drawing Demo";
17
18 CvScalar random_color(CvRNG* rng)
19 {
20     int icolor = cvRandInt(rng);
21     return CV_RGB(icolor&255, (icolor>>8)&255, (icolor>>16)&255);
22 }
23
24 int main( int argc, char** argv )
25 {
26     int line_type = CV_AA; // change it to 8 to see non-antialiased graphics
27     int i;
28     CvPoint pt1,pt2;
29     double angle;
30     CvSize sz;
31     CvPoint  ptt[6];
32     CvPoint* pt[2];
33     int  arr[2];
34     CvFont font;
35     CvRNG rng;
36     int width = 1000, height = 700;
37     int width3 = width*3, height3 = height*3;
38     CvSize text_size;
39     int ymin = 0;
40     // Load the source image
41     IplImage* image = cvCreateImage( cvSize(width,height), 8, 3 );
42     IplImage* image2;
43
44     // Create a window
45     cvNamedWindow(wndname, 1 );
46     cvZero( image );
47     cvShowImage(wndname,image);
48     cvWaitKey(DELAY);
49
50     rng = cvRNG((unsigned)-1);
51     pt[0] = &(ptt[0]);
52     pt[1] = &(ptt[3]);
53
54     arr[0] = 3;
55     arr[1] = 3;
56
57     for (i = 0; i< NUMBER; i++)
58     {
59         pt1.x=cvRandInt(&rng) % width3 - width;
60         pt1.y=cvRandInt(&rng) % height3 - height;
61         pt2.x=cvRandInt(&rng) % width3 - width;
62         pt2.y=cvRandInt(&rng) % height3 - height;
63
64         cvLine( image, pt1, pt2, random_color(&rng), cvRandInt(&rng)%10, line_type, 0 );
65         cvShowImage(wndname,image);
66         if(cvWaitKey(DELAY) >= 0) return 0;
67     }
68
69     for (i = 0; i< NUMBER; i++)
70     {
71         pt1.x=cvRandInt(&rng) % width3 - width;
72         pt1.y=cvRandInt(&rng) % height3 - height;
73         pt2.x=cvRandInt(&rng) % width3 - width;
74         pt2.y=cvRandInt(&rng) % height3 - height;
75
76         cvRectangle( image,pt1, pt2, random_color(&rng), cvRandInt(&rng)%10-1, line_type, 0 );
77         cvShowImage(wndname,image);
78         if(cvWaitKey(DELAY) >= 0) return 0;
79     }
80
81     for (i = 0; i< NUMBER; i++)
82     {
83         pt1.x=cvRandInt(&rng) % width3 - width;
84         pt1.y=cvRandInt(&rng) % height3 - height;
85         sz.width =cvRandInt(&rng)%200;
86         sz.height=cvRandInt(&rng)%200;
87         angle = (cvRandInt(&rng)%1000)*0.180;
88
89         cvEllipse( image, pt1, sz, angle, angle - 100, angle + 200,
90                    random_color(&rng), cvRandInt(&rng)%10-1, line_type, 0 );
91         cvShowImage(wndname,image);
92         if(cvWaitKey(DELAY) >= 0) return 0;
93     }
94
95     for (i = 0; i< NUMBER; i++)
96     {
97         pt[0][0].x=cvRandInt(&rng) % width3 - width;
98         pt[0][0].y=cvRandInt(&rng) % height3 - height;
99         pt[0][1].x=cvRandInt(&rng) % width3 - width;
100         pt[0][1].y=cvRandInt(&rng) % height3 - height;
101         pt[0][2].x=cvRandInt(&rng) % width3 - width;
102         pt[0][2].y=cvRandInt(&rng) % height3 - height;
103         pt[1][0].x=cvRandInt(&rng) % width3 - width;
104         pt[1][0].y=cvRandInt(&rng) % height3 - height;
105         pt[1][1].x=cvRandInt(&rng) % width3 - width;
106         pt[1][1].y=cvRandInt(&rng) % height3 - height;
107         pt[1][2].x=cvRandInt(&rng) % width3 - width;
108         pt[1][2].y=cvRandInt(&rng) % height3 - height;
109
110         cvPolyLine( image, pt, arr, 2, 1, random_color(&rng), cvRandInt(&rng)%10, line_type, 0 );
111         cvShowImage(wndname,image);
112         if(cvWaitKey(DELAY) >= 0) return 0;
113     }
114
115     for (i = 0; i< NUMBER; i++)
116     {
117         pt[0][0].x=cvRandInt(&rng) % width3 - width;
118         pt[0][0].y=cvRandInt(&rng) % height3 - height;
119         pt[0][1].x=cvRandInt(&rng) % width3 - width;
120         pt[0][1].y=cvRandInt(&rng) % height3 - height;
121         pt[0][2].x=cvRandInt(&rng) % width3 - width;
122         pt[0][2].y=cvRandInt(&rng) % height3 - height;
123         pt[1][0].x=cvRandInt(&rng) % width3 - width;
124         pt[1][0].y=cvRandInt(&rng) % height3 - height;
125         pt[1][1].x=cvRandInt(&rng) % width3 - width;
126         pt[1][1].y=cvRandInt(&rng) % height3 - height;
127         pt[1][2].x=cvRandInt(&rng) % width3 - width;
128         pt[1][2].y=cvRandInt(&rng) % height3 - height;
129
130         cvFillPoly( image, pt, arr, 2, random_color(&rng), line_type, 0 );
131         cvShowImage(wndname,image);
132         if(cvWaitKey(DELAY) >= 0) return 0;
133     }
134
135     for (i = 0; i< NUMBER; i++)
136     {
137         pt1.x=cvRandInt(&rng) % width3 - width;
138         pt1.y=cvRandInt(&rng) % height3 - height;
139
140         cvCircle( image, pt1, cvRandInt(&rng)%300, random_color(&rng),
141                   cvRandInt(&rng)%10-1, line_type, 0 );
142         cvShowImage(wndname,image);
143         if(cvWaitKey(DELAY) >= 0) return 0;
144     }
145
146     for (i = 1; i< NUMBER; i++)
147     {
148         pt1.x=cvRandInt(&rng) % width3 - width;
149         pt1.y=cvRandInt(&rng) % height3 - height;
150
151         cvInitFont( &font, cvRandInt(&rng) % 8,
152                     (cvRandInt(&rng)%100)*0.05+0.1, (cvRandInt(&rng)%100)*0.05+0.1,
153                     (cvRandInt(&rng)%5)*0.1, cvRound(cvRandInt(&rng)%10), line_type );
154
155         cvPutText( image, "Testing text rendering!", pt1, &font, random_color(&rng));
156         cvShowImage(wndname,image);
157         if(cvWaitKey(DELAY) >= 0) return 0;
158     }
159
160     cvInitFont( &font, CV_FONT_HERSHEY_COMPLEX, 3, 3, 0.0, 5, line_type );
161
162     cvGetTextSize( "OpenCV forever!", &font, &text_size, &ymin );
163
164     pt1.x = (width - text_size.width)/2;
165     pt1.y = (height + text_size.height)/2;
166     image2 = cvCloneImage(image);
167
168     for( i = 0; i < 255; i++ )
169     {
170         cvSubS( image2, cvScalarAll(i), image, 0 );
171         cvPutText( image, "OpenCV forever!", pt1, &font, CV_RGB(255,i,i));
172         cvShowImage(wndname,image);
173         if(cvWaitKey(DELAY) >= 0) return 0;
174     }
175
176     // Wait for a key stroke; the same function arranges events processing
177     cvWaitKey(0);
178     cvReleaseImage(&image);
179     cvReleaseImage(&image2);
180     cvDestroyWindow(wndname);
181
182     return 0;
183 }
184
185 #ifdef _EiC
186 main(1,"drawing.c");
187 #endif