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