]> rtime.felk.cvut.cz Git - frescor/frsh.git/blob - demo/camera/show_video_v4l_capture.c
Remove "Setup" header from tests
[frescor/frsh.git] / demo / camera / show_video_v4l_capture.c
1 /*==========================================================================
2   $Id$
3   show_video_v4l_capture.c: Functions for capturing video using V4L.
4   Written by Naoyuki Ichimura, AIST, 2001.
5 ==========================================================================*/
6
7 #include "show_video_v4l.h"
8 #include "extract_skin_color.h"
9
10 #define PREFIX_IMAGE_FILE       "video_image"   
11
12 /*=== Local global variables ===*/
13 static unsigned char disp_image[IMAGE_WIDTH_DS*IMAGE_HEIGHT_DS*RGB];/* pointer to image buffer */
14
15 static int fd;                  /* file descriptor for frame buffer */
16 static int exit_flag;           /* flag to exit program */
17 static int frame_no=1;          /* frame number */
18
19 static struct video_mbuf vm;    /* structure for frame buffer */
20 static struct video_mmap vmap;  /* structure for memory space for frame buffer */
21
22 /*=== global variable ===*/
23 extern int extract_skin_color;  /* global variable for enabling image processing */
24
25 void ShowVideoCaptureImage()
26 {
27
28         //static char image_prefix[1024]=PREFIX_IMAGE_FILE;                     /* Prefix of output image file */
29     static unsigned char image[IMAGE_WIDTH_DS*IMAGE_HEIGHT_DS*RGB];     /* Captured image */
30     static unsigned char skin_map[IMAGE_WIDTH_DS*IMAGE_HEIGHT_DS];      /* Skin color map */
31
32 /*======= Wait capture for current frame ====================*/
33     CaptureV4LDoubleBufferingCaptureWait( fd , &vmap );
34
35 /*======= Set data to image array ===========================*/
36     CaptureV4LSetImageDownSamplingForOpenGL( vmap , vm , DOWN_SAMPLING_RATE , image , disp_image );
37
38 /*======= Begin capture for next frame ======================*/
39     if( CaptureV4LDoubleBufferingCaptureNextFrame( fd , &vmap ) == -1 ) {
40         fprintf( stderr , "COuld not capture next frame.\n" );
41         exit(-1);
42     }
43
44 /*======= Display image =====================================*/
45     ShowVideoDisplayImage();
46
47 /*======= Extract skin color by simple thresholding =========*/
48     if( extract_skin_color ) {
49         ExtractSkinColorSimpleThresholding( image , IMAGE_HEIGHT_DS , IMAGE_WIDTH_DS , skin_map );
50         ShowSkinRegion( skin_map , IMAGE_HEIGHT_DS , IMAGE_WIDTH_DS );
51     }
52
53 #if 0
54 /*======= Save image ========================================*/
55     ShowVideoSavePPMImage( image , IMAGE_HEIGHT_DS , IMAGE_WIDTH_DS , frame_no , image_prefix );
56 #endif
57
58 /*======= Swap Buffer: show graphics ========================*/
59     glutSwapBuffers();
60
61 /*======= Increment frame number ============================*/
62     frame_no++;
63
64 /*======= Check exit flag ===================================*/
65     if( exit_flag ) {
66         exit(0);
67     }
68
69 }
70
71 int ShowVideoInitCaptureDevice( char *device_name , int channel_no )
72 {
73
74     struct video_capability vcap;               /* structure for video capability */
75     struct video_channel vch[MAX_NO_CHANNEL];   /* structure for video channel */ 
76     struct video_picture vp;
77
78 /*======= Open video device ================================*/
79     if( ( fd = CaptureV4LOpen( device_name ) ) == -1 ) {
80         fprintf( stderr, "Could not open device %s.\n" , device_name );
81         exit(-1);
82     }
83
84 /*======= Get information of device =========================*/
85     /*=== Get device capabilities ===*/
86     if( CaptureV4LGetDeviceCapability( fd , &vcap ) == -1 ) {
87         fprintf( stderr , "Could not get capabilities of video device.\n" );
88         exit(-1);
89     }
90
91     /*=== Get channel information ===*/
92     if( CaptureV4LGetChannelInfo( fd , vch , vcap.channels ) == -1 ) {
93         fprintf( stderr , "Could not get channel information of video device.\n" );
94         exit(-1);
95     }
96
97     /*=== Get memory map information ===*/
98     if( CaptureV4LGetMemoryMapInfo( fd , &vm ) == -1 ) {
99         fprintf( stderr , "Could not get memory map information.\n" );
100         exit(-1);
101     }
102
103 /*======= Show picture information =========================*/
104     CaptureV4LGetPictureInfo( fd , &vp );
105     CaptureV4LDisplayPictureInfo( vp );
106
107 /*======= Select channel ===================================*/
108     if( CaptureV4LSelectChannel( fd , vch , channel_no ) == -1 ) {
109         fprintf( stderr , "Could not select channel.\n" );
110         exit(-1);
111     }
112
113 /*======= Mapping frame buffer ==============================*/
114     if( CaptureV4LMemoryMapping( fd , vm ) == -1 ) {
115         fprintf( stdout , "Could not map frame buffer.\n" );
116         exit(-1);
117     }
118
119 /*======= Set image size and formate ========================*/
120     vmap.width = CAPTURE_IMAGE_WIDTH;
121     vmap.height = CAPTURE_IMAGE_HEIGHT;
122     vmap.format = VIDEO_PALETTE_RGB24;
123     
124 /*======= Begin Capture =====================================*/
125     CaptureV4LDoubleBufferingInitCapture( fd , &vmap );
126
127     return 0;
128
129 }
130
131 void ShowVideoDisplayImage()
132 {
133
134     glRasterPos2i( 0 , 0 );
135     glDrawPixels( IMAGE_WIDTH_DS , IMAGE_HEIGHT_DS , GL_BGR , GL_UNSIGNED_BYTE , disp_image );
136     glFlush();
137
138 }
139
140 void ShowVideoMouseCheck( int button , int status , int x , int y )
141 {
142
143     if( button == GLUT_LEFT_BUTTON && status == GLUT_DOWN ) {
144         exit_flag = 1;
145     }
146
147 }
148
149 void ShowVideoSavePPMImage( unsigned char *image , int iheight , int iwidth , int frame_no , char *image_prefix )
150 {
151
152     char file_name[1024];
153
154     FILE *fp;
155
156     sprintf( file_name , "%s_%d.ppm" , image_prefix , frame_no );
157     if( ( fp = fopen( file_name , "w" ) ) == NULL ) {
158         fprintf( stderr , "Could not open image file: %s\n" , file_name );
159         exit(1);
160     }
161     fprintf( fp , "P6 %d %d 255\n" , iwidth , iheight );
162     fwrite( image , iheight*iwidth*RGB , 1 , fp );
163     fclose(fp);
164
165
166
167
168
169
170
171