0dfda394c89876e79bd3ac2e6239836d5e12d8ff
[opencv] / otherlibs / highgui / cvcap_cmu.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 //                        Intel License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
15 //
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
18 //
19 //   * Redistribution's of source code must retain the above copyright notice,
20 //     this list of conditions and the following disclaimer.
21 //
22 //   * Redistribution's in binary form must reproduce the above copyright notice,
23 //     this list of conditions and the following disclaimer in the documentation
24 //     and/or other materials provided with the distribution.
25 //
26 //   * The name of Intel Corporation may not be used to endorse or promote products
27 //     derived from this software without specific prior written permission.
28 //
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
39 //
40 //M*/
41
42 #include "_highgui.h"
43
44 #ifdef WIN32
45
46 /****************** Capturing video from camera via CMU lib *******************/
47
48 #if defined(HAVE_CMU1394)
49
50 // This firewire capability added by Philip Gruebele (pgruebele@cox.net).
51 // For this to work you need to install the CMU firewire DCAM drivers,
52 // located at http://www-2.cs.cmu.edu/~iwan/1394/.
53 #include "1394camera.h"
54
55 typedef struct CvCaptureCAM_CMU
56 {
57     CvCaptureVTable* vtable;
58         int     fps;    // 0-5
59         int     mode;   // 0-7
60         int     format; // 0-2, 7 ?
61     int     index;
62         IplImage * image;
63     IplImage* rgb_frame;
64 }
65 CvCaptureCAM_CMU;
66
67 int icvOpenCAM_CMU(CvCaptureCAM_CMU * capture, int wIndex );
68 int icvSetPropertyCAM_CMU( CvCaptureCAM_CMU* capture, int property_id, double value );
69 void icvCloseCAM_CMU( CvCaptureCAM_CMU* capture );
70 int icvGrabFrameCAM_CMU( CvCaptureCAM_CMU* capture );
71 IplImage* icvRetrieveFrameCAM_CMU( CvCaptureCAM_CMU* capture ); 
72 double icvGetPropertyCAM_CMU( CvCaptureCAM_CMU* capture, int property_id );
73 int icvSetPropertyCAM_CMU( CvCaptureCAM_CMU* capture, int property_id, double value );
74
75 static CvCaptureVTable captureCAM_CMU_vtable =
76 {
77         6,
78         (CvCaptureCloseFunc)icvCloseCAM_CMU,
79         (CvCaptureGrabFrameFunc)icvGrabFrameCAM_CMU,
80         (CvCaptureRetrieveFrameFunc)icvRetrieveFrameCAM_CMU,
81         (CvCaptureGetPropertyFunc)icvGetPropertyCAM_CMU,
82         (CvCaptureSetPropertyFunc)icvSetPropertyCAM_CMU,
83         (CvCaptureGetDescriptionFunc)0
84 };
85
86 // CMU 1394 camera stuff.
87 // This firewire capability added by Philip Gruebele (pgruebele@cox.net) 
88 // and modified by Roman Stanchak (rstanchak@yahoo.com).
89 // For this to work you need to install the CMU firewire DCAM drivers,
90 // located at http://www-2.cs.cmu.edu/~iwan/1394/.
91 #define CMU_MAX_CAMERAS     20
92 int             CMU_numCameras = 0;
93 int             CMU_numActiveCameras = 0;
94 bool            CMU_useCameraFlags[CMU_MAX_CAMERAS];
95 C1394Camera     *CMU_theCamera = 0;
96
97 // stupid defines for mode, format, FPS
98 #define CV_CAP_IEEE1394_FPS_1_875 0
99 #define CV_CAP_IEEE1394_FPS_3_75 1
100 #define CV_CAP_IEEE1394_FPS_7_5 2
101 #define CV_CAP_IEEE1394_FPS_15 3
102 #define CV_CAP_IEEE1394_FPS_30 4
103 #define CV_CAP_IEEE1394_FPS_60 5
104
105 // index by size, color
106 #define CV_CAP_IEEE1394_COLOR_MONO 0
107 #define CV_CAP_IEEE1394_COLOR_MONO16 1
108 #define CV_CAP_IEEE1394_COLOR_YUV444 2
109 #define CV_CAP_IEEE1394_COLOR_YUV422 3
110 #define CV_CAP_IEEE1394_COLOR_YUV411 4
111 #define CV_CAP_IEEE1394_COLOR_RGB 5
112
113 #define CV_CAP_IEEE1394_SIZE_160X120 0
114 #define CV_CAP_IEEE1394_SIZE_320X240 1 
115 #define CV_CAP_IEEE1394_SIZE_640X480 2
116 #define CV_CAP_IEEE1394_SIZE_800X600 3
117 #define CV_CAP_IEEE1394_SIZE_1024X768 4
118 #define CV_CAP_IEEE1394_SIZE_1280X960 5
119 #define CV_CAP_IEEE1394_SIZE_1600X1200 6
120
121 // given color, size, output format
122                                                                           // 1 16  444 422 411 RGB
123 char CV_CAP_IEEE1394_FORMAT[7][6] = { {-1, -1,  0, -1, -1, -1}, // 160x120
124                                       {-1, -1, -1,  0, -1, -1}, // 320x240
125                                                                           { 0,  0, -1,  0,  0,  0}, // 640x480
126                                                                           { 1,  1, -1,  1, -1,  1}, // 800x600
127                                                                           { 1,  1, -1,  1, -1,  1}, // 1024x768
128                                                                           { 2,  2, -1,  2, -1,  2}, // 1280x960
129                                                                           { 2,  2, -1,  2, -1,  2}}; // 1600x1200
130 // given color, size, output corresponding mode 
131 char CV_CAP_IEEE1394_MODE[7][6] =   { {-1, -1,  0, -1, -1, -1}, // 160x120
132                                       {-1, -1, -1,  1, -1, -1}, // 320x240
133                                                                           { 5,  6, -1,  3,  2,  4}, // 640x480
134                                                                           { 2,  6, -1,  0, -1,  1}, // 800x600
135                                                                           { 5,  7, -1,  3, -1,  4}, // 1024x768
136                                                                           { 2,  6, -1,  0, -1,  1}, // 1280x960
137                                                                           { 5,  7, -1,  3, -1,  4}}; // 1600x1200
138 // given format, mode, return COLOR
139 char CV_CAP_IEEE1394_COLOR[2][8] = { { CV_CAP_IEEE1394_COLOR_YUV444, 
140                                                                            CV_CAP_IEEE1394_COLOR_YUV422, 
141                                                                            CV_CAP_IEEE1394_COLOR_YUV411, 
142                                                                            CV_CAP_IEEE1394_COLOR_YUV422,
143                                                                            CV_CAP_IEEE1394_COLOR_RGB,
144                                                                            CV_CAP_IEEE1394_COLOR_MONO, 
145                                                                            CV_CAP_IEEE1394_COLOR_MONO16 },
146                                                                          { CV_CAP_IEEE1394_COLOR_YUV422,
147                                                                            CV_CAP_IEEE1394_COLOR_RGB,
148                                                                            CV_CAP_IEEE1394_COLOR_MONO,
149                                                                            CV_CAP_IEEE1394_COLOR_YUV422,
150                                                                            CV_CAP_IEEE1394_COLOR_RGB,
151                                                                            CV_CAP_IEEE1394_COLOR_MONO,
152                                                                            CV_CAP_IEEE1394_COLOR_MONO16,
153                                                                            CV_CAP_IEEE1394_COLOR_MONO16 } };
154
155 // convert frame rate to suitable enum
156 /*static int icvFrameRateToIndex_CMU(double framerate){
157         if(framerate > 30)       return CV_CAP_IEEE1394_FPS_60;
158         else if(framerate > 15)  return CV_CAP_IEEE1394_FPS_30;
159         else if(framerate > 7.5) return CV_CAP_IEEE1394_FPS_15;
160         else if(framerate > 3.75) return CV_CAP_IEEE1394_FPS_7_5;
161         else if(framerate > 1.875) return CV_CAP_IEEE1394_FPS_3_75;
162         return CV_CAP_IEEE1394_FPS_1_875;
163 }*/
164
165 #if _MSC_VER >= 1200
166 #pragma comment(lib,"1394camera.lib")
167 #endif
168
169 // return the size of the image
170 CvSize icvGetSize_CMU( C1394Camera * cmucam ){
171         //int format = cmucam->GetVideoFormat();
172         //int mode = cmucam->GetVideoMode();
173
174         // irrelvant to depth
175         // if( format > 1 ) format=1;
176         
177         // for YUV, should we return a weirdly sized image?
178         //switch(CV_CAP_IEEE1394_COLOR[format][mode]){
179         //case CV_CAP_IEEE1394_COLOR_MONO16:
180         //      return cvSize(cmucam->m_width*2, cmucam->m_height);
181         //default:
182         //      return cvSize(cmucam->m_width, cmucam->m_height);       
183         //}
184         unsigned long width = 0, height = 0;
185         cmucam->GetVideoFrameDimensions( &width, &height );
186         return cvSize((int)width, (int)height);
187 }
188
189 // return the opencv depth flag corresponding to the camera format
190 int icvGetDepth_CMU( C1394Camera * cmucam ){
191         int format = cmucam->GetVideoFormat();
192         int mode = cmucam->GetVideoMode();
193         
194         // TODO
195         if( format==7 ) {
196                 assert(0);
197         }
198         // irrelvant to depth
199         if( format > 1 ) format=1;
200         
201         if(CV_CAP_IEEE1394_COLOR[format][mode]==CV_CAP_IEEE1394_COLOR_MONO16){
202                 return IPL_DEPTH_16S;
203         }
204         return IPL_DEPTH_8U;
205 }
206
207 // return the number of channels for camera
208 int icvGetNChannels_CMU( C1394Camera * cmucam ){
209
210         int format = cmucam->GetVideoFormat();
211         int mode = cmucam->GetVideoMode();
212         
213         if( format==7 ){
214                 assert(0);
215         }
216         
217         // irrelvant to nchannels 
218         if( format > 1 ) format=1;
219
220         switch(CV_CAP_IEEE1394_COLOR[format][mode]){
221         case CV_CAP_IEEE1394_COLOR_RGB:
222                 return 3;
223         case CV_CAP_IEEE1394_COLOR_MONO:
224         case CV_CAP_IEEE1394_COLOR_MONO16:
225                 return 1;
226         case CV_CAP_IEEE1394_COLOR_YUV422:
227         case CV_CAP_IEEE1394_COLOR_YUV444:
228         case CV_CAP_IEEE1394_COLOR_YUV411:
229                 return 3;
230         default:
231                 ;
232         }
233         return -1;
234 }
235
236 int icvOpenCAM_CMU(CvCaptureCAM_CMU * capture, int index )
237 {
238     // if first time, then allocate all available cameras
239     if( CMU_numCameras == 0 )
240     {
241         CMU_numActiveCameras = 0;
242         CMU_theCamera = new C1394Camera[CMU_MAX_CAMERAS];
243
244         ////////////////////////////////////////////////////////////////////////////////////////////////////////
245         // create all cameras
246         try
247         {
248             // create camera0
249             if( CMU_theCamera[0].CheckLink() != CAM_SUCCESS )
250                 throw 1;
251
252             // we have one pin per camera
253             CMU_numCameras = CMU_theCamera[0].GetNumberCameras();
254
255                         // allocate remaining cameras
256             for(int i = 1; i < CMU_numCameras && i<CMU_MAX_CAMERAS; i++ )
257             {
258                 CMU_useCameraFlags[i] = false;
259                 if (CMU_theCamera[i].CheckLink() != CAM_SUCCESS)
260                     throw 1;
261             }
262         }
263         catch (...)
264         {
265             // free any allocated cameras
266             // ...
267             CMU_numCameras = 0;
268                         return 0;
269         }
270     }
271
272     try
273     {
274                 // pick first unused camera
275         if(index==-1){
276                         for(int i = 0; i < CMU_numCameras; i++ )
277                 {
278                     if( !CMU_useCameraFlags[i] ){
279                                         index = i;
280                         break;
281                                 }
282                 }
283                 }
284
285         // no empty camera found 
286         if (index==-1)
287             throw 1;
288
289         if (CMU_theCamera[index].SelectCamera(index) != CAM_SUCCESS)
290             throw 2;
291
292         if (CMU_theCamera[index].InitCamera() != CAM_SUCCESS)
293             throw 3;
294         
295                 // set initial format -- try to pick best frame rate first, then color, then size
296                 bool found_format = false;
297                 for (int rate=5; rate>=0 && !found_format; rate--){
298                         for (int color=CV_CAP_IEEE1394_COLOR_RGB; color>=0 && !found_format; color--){
299                                 for (int size=CV_CAP_IEEE1394_SIZE_1600X1200; size>=0 && !found_format; size--){
300                                         int format = CV_CAP_IEEE1394_FORMAT[size][color];
301                                         int mode = CV_CAP_IEEE1394_MODE[size][color];
302                                         if (format!=-1 && mode!=-1 &&
303                                                 CMU_theCamera[index].HasVideoFrameRate(format,mode,rate)){
304                                                 CMU_theCamera[index].SetVideoFormat(format);
305                                                 CMU_theCamera[index].SetVideoMode(mode);
306                                                 CMU_theCamera[index].SetVideoFrameRate(rate);
307                                                 found_format = (CMU_theCamera[index].StartImageAcquisition() == CAM_SUCCESS);
308                                         }
309                                 }
310                         }
311                 }
312
313                 // try format 7
314                 if(!found_format){
315                         CMU_theCamera[index].SetVideoFormat(7);
316                         CMU_theCamera[index].SetVideoMode(0);
317                         if(CMU_theCamera[index].StartImageAcquisition() != CAM_SUCCESS){
318                                 // no format found
319                                 throw 9;
320                         }
321                 }
322
323                 // allocate image frame
324                 capture->image = cvCreateImage(icvGetSize_CMU(&(CMU_theCamera[index])), 
325                                                icvGetDepth_CMU(&(CMU_theCamera[index])),
326                                                                            icvGetNChannels_CMU(&(CMU_theCamera[index])));
327                 if (capture->image)
328                 {
329                         // successfully activated camera
330                         capture->index = index; //CMU_numActiveCameras;
331                         CMU_numActiveCameras++;
332                         CMU_useCameraFlags[index] = true;
333                 }
334         }
335         catch ( int )
336         {
337                 return 0;
338         }
339
340         return 1;
341 }
342
343 void icvCloseCAM_CMU( CvCaptureCAM_CMU* capture )
344 {
345         if( capture && capture->image )
346         {
347                 cvReleaseImage( &capture->image );
348                 CMU_theCamera[capture->index].StopImageAcquisition();
349                 CMU_useCameraFlags[capture->index] = false;
350                 CMU_numActiveCameras--;
351
352                 if (!CMU_numActiveCameras)
353                 {
354                         delete[] CMU_theCamera;
355                         CMU_theCamera = 0;
356                         CMU_numCameras = 0;
357                 }
358         }
359 }
360
361
362 int icvGrabFrameCAM_CMU( CvCaptureCAM_CMU* capture )
363 {
364         return capture->image && CMU_theCamera &&
365                 CMU_theCamera[capture->index].AcquireImage() == CAM_SUCCESS;
366 }
367
368 static void swapRedBlue(IplImage * im){
369         uchar * ptr = (uchar *) im->imageData;
370         uchar t;
371         for(int i=0; i<im->height; i++){
372                 ptr = (uchar *) im->imageData+im->widthStep*i;
373                 for(int j=0; j<im->width; j++){
374                         t = ptr[0];
375                         ptr[0] = ptr[2];
376                         ptr[2] = t;
377                         ptr+=3;
378                 }
379         }
380 }
381
382 IplImage* icvRetrieveFrameCAM_CMU( CvCaptureCAM_CMU* capture )
383 {
384         if( capture->image && CMU_theCamera )
385         {
386                 //capture->image->imageData = (char *) CMU_theCamera[capture->index].GetRawData(0);
387                 CMU_theCamera[capture->index].getRGB((unsigned char*)capture->image->imageData,
388                         capture->image->imageSize);
389                 swapRedBlue( capture->image );
390                 return capture->image;
391         }
392         return 0;
393 }
394
395
396 double icvGetPropertyCAM_CMU( CvCaptureCAM_CMU* capture, int property_id )
397 {
398         switch( property_id )
399         {
400                 case CV_CAP_PROP_FRAME_WIDTH:
401                         return capture->image->width;
402                 case CV_CAP_PROP_FRAME_HEIGHT:
403                         return capture->image->height;
404                 case CV_CAP_PROP_FPS:
405                         return CMU_theCamera[capture->index].GetVideoFrameRate();
406                 case CV_CAP_PROP_MODE:
407                         return CMU_theCamera[capture->index].GetVideoMode();
408                 case CV_CAP_PROP_FORMAT:
409                         return CMU_theCamera[capture->index].GetVideoFormat();
410         }
411         return 0;
412 }
413
414 static int icvSetVideoSize( CvCaptureCAM_CMU* capture, int, int) {
415         if (capture==0) return 0;
416         // change to closest size
417         return -1;
418 }
419
420 static int icvSetMode(CvCaptureCAM_CMU * capture, int mode){
421         int format = CMU_theCamera[capture->index].GetVideoFormat();
422         if(mode < 0 || mode > 7){
423                 return -1;
424         }
425         if(CMU_theCamera[capture->index].HasVideoMode(format, mode)){
426                 CMU_theCamera[capture->index].StopImageAcquisition();
427                 CMU_theCamera[capture->index].SetVideoMode(mode);
428                 CMU_theCamera[capture->index].StartImageAcquisition();
429                 return 0;       
430         }
431         return -1;
432 }
433
434 static int icvSetFrameRate(CvCaptureCAM_CMU * capture, int rate){
435         int mode = CMU_theCamera[capture->index].GetVideoMode();
436         int format = CMU_theCamera[capture->index].GetVideoFormat();
437         if(rate < 0 || rate > 5){ 
438                 return -1;
439         }
440         if(CMU_theCamera[capture->index].HasVideoFrameRate(format, mode, rate)){
441                 CMU_theCamera[capture->index].StopImageAcquisition();
442                 CMU_theCamera[capture->index].SetVideoFrameRate(rate);
443                 CMU_theCamera[capture->index].StartImageAcquisition();
444                 return 0;       
445         }
446         return -1;
447 }
448
449 static int icvSetFormat(CvCaptureCAM_CMU * capture, int format){
450         if(format < 0 || format > 2){ 
451                 return -1;
452         }
453         if(CMU_theCamera[capture->index].HasVideoFormat(format)){
454                 CMU_theCamera[capture->index].StopImageAcquisition();
455                 CMU_theCamera[capture->index].SetVideoFormat(format);
456                 CMU_theCamera[capture->index].StartImageAcquisition();
457                 return 0;       
458         }
459         return -1;
460 }
461
462 int icvSetPropertyCAM_CMU( CvCaptureCAM_CMU* capture, int property_id, double value ){
463         int retval = -1;
464         int ival= cvRound(value);
465         switch (property_id) {
466                 case CV_CAP_PROP_FRAME_WIDTH:
467                 case CV_CAP_PROP_FRAME_HEIGHT:
468                         {
469                         int width, height;
470                         if (property_id == CV_CAP_PROP_FRAME_WIDTH) {
471                                 width = ival;
472                                 height = width*3/4;
473                         }
474                         else {
475                                 height = ival;
476                                 width = height*4/3;
477                         }
478                         retval = icvSetVideoSize(capture, width, height);
479                         }
480                         break;
481                 case CV_CAP_PROP_FPS:
482                         retval =icvSetFrameRate(capture, ival);
483                         break;
484                 case CV_CAP_PROP_MODE:
485                         retval =icvSetMode(capture, ival);
486                         break;
487                 case CV_CAP_PROP_FORMAT:
488                         retval =icvSetFormat(capture, ival);
489                         break;
490         }
491
492         // resize image if its not the right size anymore
493         CvSize size = icvGetSize_CMU( &(CMU_theCamera[capture->index]) );
494         int depth = icvGetDepth_CMU( &(CMU_theCamera[capture->index]) );
495         int nch = icvGetNChannels_CMU( &(CMU_theCamera[capture->index]) );
496         if(size.width  != capture->image->width ||
497            size.height != capture->image->height ||
498            depth != capture->image->depth ||
499            nch   != capture->image->nChannels )
500         {
501                 cvReleaseImage(&capture->image);
502                 capture->image = cvCreateImage(size, depth, nch);
503         }
504         return retval;
505 }
506
507 CvCapture * cvCaptureFromCAM_CMU (int index)
508 {
509         CvCaptureCAM_CMU* capture = (CvCaptureCAM_CMU*)cvAlloc( sizeof(*capture));
510         memset( capture, 0, sizeof(*capture));
511         capture->vtable = &captureCAM_CMU_vtable;
512
513         if( icvOpenCAM_CMU( capture, index ))
514                 return (CvCapture*)capture;
515
516         cvReleaseCapture( (CvCapture**)&capture );
517         return 0;
518 }
519
520 #endif // CMU
521 #endif // WIN32
522