CLAHE Python bindings
[profile/ivi/opencv.git] / modules / highgui / src / cap.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 "precomp.hpp"
43
44 #if defined _M_X64 && defined _MSC_VER && !defined CV_ICC
45 #pragma optimize("",off)
46 #pragma warning(disable: 4748)
47 #endif
48
49 namespace cv
50 {
51
52 template<> void Ptr<CvCapture>::delete_obj()
53 { cvReleaseCapture(&obj); }
54
55 template<> void Ptr<CvVideoWriter>::delete_obj()
56 { cvReleaseVideoWriter(&obj); }
57
58 }
59
60 /************************* Reading AVIs & Camera data **************************/
61
62 CV_IMPL void cvReleaseCapture( CvCapture** pcapture )
63 {
64     if( pcapture && *pcapture )
65     {
66         delete *pcapture;
67         *pcapture = 0;
68     }
69 }
70
71 CV_IMPL IplImage* cvQueryFrame( CvCapture* capture )
72 {
73     if(!capture)
74         return 0;
75     if(!capture->grabFrame())
76         return 0;
77     return capture->retrieveFrame(0);
78 }
79
80
81 CV_IMPL int cvGrabFrame( CvCapture* capture )
82 {
83     return capture ? capture->grabFrame() : 0;
84 }
85
86 CV_IMPL IplImage* cvRetrieveFrame( CvCapture* capture, int idx )
87 {
88     return capture ? capture->retrieveFrame(idx) : 0;
89 }
90
91 CV_IMPL double cvGetCaptureProperty( CvCapture* capture, int id )
92 {
93     return capture ? capture->getProperty(id) : 0;
94 }
95
96 CV_IMPL int cvSetCaptureProperty( CvCapture* capture, int id, double value )
97 {
98     return capture ? capture->setProperty(id, value) : 0;
99 }
100
101 CV_IMPL int cvGetCaptureDomain( CvCapture* capture)
102 {
103     return capture ? capture->getCaptureDomain() : 0;
104 }
105
106
107 /**
108  * Camera dispatching method: index is the camera number.
109  * If given an index from 0 to 99, it tries to find the first
110  * API that can access a given camera index.
111  * Add multiples of 100 to select an API.
112  */
113 CV_IMPL CvCapture * cvCreateCameraCapture (int index)
114 {
115     int  domains[] =
116     {
117 #ifdef HAVE_DSHOW
118         CV_CAP_DSHOW,
119 #endif
120 #if 1
121         CV_CAP_IEEE1394,   // identical to CV_CAP_DC1394
122 #endif
123 #ifdef HAVE_TYZX
124         CV_CAP_STEREO,
125 #endif
126 #ifdef HAVE_PVAPI
127         CV_CAP_PVAPI,
128 #endif
129 #if 1
130         CV_CAP_VFW,        // identical to CV_CAP_V4L
131 #endif
132 #ifdef HAVE_MIL
133         CV_CAP_MIL,
134 #endif
135 #ifdef HAVE_QUICKTIME
136         CV_CAP_QT,
137 #endif
138 #ifdef HAVE_UNICAP
139         CV_CAP_UNICAP,
140 #endif
141 #ifdef HAVE_OPENNI
142         CV_CAP_OPENNI,
143 #endif
144 #ifdef HAVE_ANDROID_NATIVE_CAMERA
145         CV_CAP_ANDROID,
146 #endif
147 #ifdef HAVE_XIMEA
148         CV_CAP_XIAPI,
149 #endif
150 #ifdef HAVE_AVFOUNDATION
151         CV_CAP_AVFOUNDATION,
152 #endif
153 #ifdef HAVE_GIGE_API
154         CV_CAP_GIGANETIX,
155 #endif
156         -1
157     };
158
159     // interpret preferred interface (0 = autodetect)
160     int pref = (index / 100) * 100;
161     if (pref)
162     {
163         domains[0]=pref;
164         index %= 100;
165         domains[1]=-1;
166     }
167
168     // try every possibly installed camera API
169     for (int i = 0; domains[i] >= 0; i++)
170     {
171 #if defined(HAVE_DSHOW)        || \
172     defined(HAVE_MSMF)         || \
173     defined(HAVE_TYZX)         || \
174     defined(HAVE_VFW)          || \
175     defined(HAVE_LIBV4L)       || \
176     defined(HAVE_CAMV4L)       || \
177     defined(HAVE_CAMV4L2)      || \
178     defined(HAVE_VIDEOIO)      || \
179     defined(HAVE_GSTREAMER)    || \
180     defined(HAVE_DC1394_2)     || \
181     defined(HAVE_DC1394)       || \
182     defined(HAVE_CMU1394)      || \
183     defined(HAVE_MIL)          || \
184     defined(HAVE_QUICKTIME)    || \
185     defined(HAVE_UNICAP)       || \
186     defined(HAVE_PVAPI)        || \
187     defined(HAVE_OPENNI)       || \
188     defined(HAVE_XIMEA)        || \
189     defined(HAVE_AVFOUNDATION) || \
190     defined(HAVE_ANDROID_NATIVE_CAMERA) || \
191     defined(HAVE_GIGE_API) || \
192     (0)
193         // local variable to memorize the captured device
194         CvCapture *capture;
195 #endif
196
197         switch (domains[i])
198         {
199 #ifdef HAVE_MSMF
200         case CV_CAP_MSMF:
201              capture = cvCreateCameraCapture_MSMF (index);
202              if (capture)
203                  return capture;
204             break;
205 #endif
206 #ifdef HAVE_DSHOW
207         case CV_CAP_DSHOW:
208              capture = cvCreateCameraCapture_DShow (index);
209              if (capture)
210                  return capture;
211             break;
212 #endif
213
214 #ifdef HAVE_TYZX
215         case CV_CAP_STEREO:
216             capture = cvCreateCameraCapture_TYZX (index);
217             if (capture)
218                 return capture;
219             break;
220 #endif
221
222         case CV_CAP_VFW:
223 #ifdef HAVE_VFW
224             capture = cvCreateCameraCapture_VFW (index);
225             if (capture)
226                 return capture;
227 #endif
228
229 #if defined HAVE_LIBV4L || defined HAVE_CAMV4L || defined HAVE_CAMV4L2 || defined HAVE_VIDEOIO
230             capture = cvCreateCameraCapture_V4L (index);
231             if (capture)
232                 return capture;
233 #endif
234
235 #ifdef HAVE_GSTREAMER
236             capture = cvCreateCapture_GStreamer(CV_CAP_GSTREAMER_V4L2, 0);
237             if (capture)
238                 return capture;
239             capture = cvCreateCapture_GStreamer(CV_CAP_GSTREAMER_V4L, 0);
240             if (capture)
241                 return capture;
242 #endif
243             break; //CV_CAP_VFW
244
245         case CV_CAP_FIREWIRE:
246 #ifdef HAVE_DC1394_2
247             capture = cvCreateCameraCapture_DC1394_2 (index);
248             if (capture)
249                 return capture;
250 #endif
251
252 #ifdef HAVE_DC1394
253             capture = cvCreateCameraCapture_DC1394 (index);
254             if (capture)
255                 return capture;
256 #endif
257
258 #ifdef HAVE_CMU1394
259             capture = cvCreateCameraCapture_CMU (index);
260             if (capture)
261                 return capture;
262 #endif
263
264 #if defined(HAVE_GSTREAMER) && 0
265             //Re-enable again when gstreamer 1394 support will land in the backend code
266             capture = cvCreateCapture_GStreamer(CV_CAP_GSTREAMER_1394, 0);
267             if (capture)
268                 return capture;
269 #endif
270             break; //CV_CAP_FIREWIRE
271
272 #ifdef HAVE_MIL
273         case CV_CAP_MIL:
274             capture = cvCreateCameraCapture_MIL (index);
275             if (capture)
276                 return capture;
277             break;
278 #endif
279
280 #ifdef HAVE_QUICKTIME
281         case CV_CAP_QT:
282             capture = cvCreateCameraCapture_QT (index);
283             if (capture)
284                 return capture;
285             break;
286 #endif
287
288 #ifdef HAVE_UNICAP
289         case CV_CAP_UNICAP:
290             capture = cvCreateCameraCapture_Unicap (index);
291             if (capture)
292                 return capture;
293         break;
294 #endif
295
296 #ifdef HAVE_PVAPI
297         case CV_CAP_PVAPI:
298             capture = cvCreateCameraCapture_PvAPI (index);
299             if (capture)
300                 return capture;
301         break;
302 #endif
303
304 #ifdef HAVE_OPENNI
305         case CV_CAP_OPENNI:
306             capture = cvCreateCameraCapture_OpenNI (index);
307             if (capture)
308                 return capture;
309         break;
310 #endif
311
312 #ifdef HAVE_ANDROID_NATIVE_CAMERA
313         case CV_CAP_ANDROID:
314             capture = cvCreateCameraCapture_Android (index);
315             if (capture)
316                 return capture;
317         break;
318 #endif
319
320 #ifdef HAVE_XIMEA
321         case CV_CAP_XIAPI:
322             capture = cvCreateCameraCapture_XIMEA (index);
323             if (capture)
324                 return capture;
325         break;
326 #endif
327
328 #ifdef HAVE_AVFOUNDATION
329         case CV_CAP_AVFOUNDATION:
330             capture = cvCreateCameraCapture_AVFoundation (index);
331             if (capture)
332                 return capture;
333         break;
334 #endif
335
336 #ifdef HAVE_GIGE_API
337         case CV_CAP_GIGANETIX:
338             capture = cvCreateCameraCapture_Giganetix (index);
339             if (capture)
340                 return capture;
341         break; // CV_CAP_GIGANETIX
342 #endif
343         }
344     }
345
346     // failed open a camera
347     return 0;
348 }
349
350 /**
351  * Videoreader dispatching method: it tries to find the first
352  * API that can access a given filename.
353  */
354 CV_IMPL CvCapture * cvCreateFileCapture (const char * filename)
355 {
356     CvCapture * result = 0;
357
358     if (! result)
359         result = cvCreateFileCapture_FFMPEG_proxy (filename);
360
361 #ifdef HAVE_XINE
362     if (! result)
363         result = cvCreateFileCapture_XINE (filename);
364 #endif
365
366 #ifdef HAVE_GSTREAMER
367     if (! result)
368         result = cvCreateCapture_GStreamer (CV_CAP_GSTREAMER_FILE, filename);
369 #endif
370
371 #ifdef HAVE_QUICKTIME
372     if (! result)
373         result = cvCreateFileCapture_QT (filename);
374 #endif
375
376 #ifdef HAVE_AVFOUNDATION
377     if (! result)
378         result = cvCreateFileCapture_AVFoundation (filename);
379 #endif
380
381 #ifdef HAVE_OPENNI
382     if (! result)
383         result = cvCreateFileCapture_OpenNI (filename);
384 #endif
385
386     if (! result)
387         result = cvCreateFileCapture_Images (filename);
388
389     return result;
390 }
391
392 /**
393  * Videowriter dispatching method: it tries to find the first
394  * API that can write a given stream.
395  */
396 CV_IMPL CvVideoWriter* cvCreateVideoWriter( const char* filename, int fourcc,
397                                             double fps, CvSize frameSize, int is_color )
398 {
399     //CV_FUNCNAME( "cvCreateVideoWriter" );
400
401     CvVideoWriter *result = 0;
402
403     if(!fourcc || !fps)
404         result = cvCreateVideoWriter_Images(filename);
405
406     if(!result)
407         result = cvCreateVideoWriter_FFMPEG_proxy (filename, fourcc, fps, frameSize, is_color);
408
409 /*  #ifdef HAVE_XINE
410     if(!result)
411         result = cvCreateVideoWriter_XINE(filename, fourcc, fps, frameSize, is_color);
412     #endif
413 */
414 #ifdef HAVE_AVFOUNDATION
415     if (! result)
416         result = cvCreateVideoWriter_AVFoundation(filename, fourcc, fps, frameSize, is_color);
417 #endif
418
419 #ifdef HAVE_QUICKTIME
420     if(!result)
421         result = cvCreateVideoWriter_QT(filename, fourcc, fps, frameSize, is_color);
422 #endif
423
424 #ifdef HAVE_GSTREAMER
425     if (! result)
426         result = cvCreateVideoWriter_GStreamer(filename, fourcc, fps, frameSize, is_color);
427 #endif
428
429     if(!result)
430         result = cvCreateVideoWriter_Images(filename);
431
432     return result;
433 }
434
435 CV_IMPL int cvWriteFrame( CvVideoWriter* writer, const IplImage* image )
436 {
437     return writer ? writer->writeFrame(image) : 0;
438 }
439
440 CV_IMPL void cvReleaseVideoWriter( CvVideoWriter** pwriter )
441 {
442     if( pwriter && *pwriter )
443     {
444         delete *pwriter;
445         *pwriter = 0;
446     }
447 }
448
449 namespace cv
450 {
451
452 VideoCapture::VideoCapture()
453 {}
454
455 VideoCapture::VideoCapture(const string& filename)
456 {
457     open(filename);
458 }
459
460 VideoCapture::VideoCapture(int device)
461 {
462     open(device);
463 }
464
465 VideoCapture::~VideoCapture()
466 {
467     cap.release();
468 }
469
470 bool VideoCapture::open(const string& filename)
471 {
472     if (!isOpened())
473     cap = cvCreateFileCapture(filename.c_str());
474     return isOpened();
475 }
476
477 bool VideoCapture::open(int device)
478 {
479     if (!isOpened())
480     cap = cvCreateCameraCapture(device);
481     return isOpened();
482 }
483
484 bool VideoCapture::isOpened() const { return !cap.empty(); }
485
486 void VideoCapture::release()
487 {
488     cap.release();
489 }
490
491 bool VideoCapture::grab()
492 {
493     return cvGrabFrame(cap) != 0;
494 }
495
496 bool VideoCapture::retrieve(Mat& image, int channel)
497 {
498     IplImage* _img = cvRetrieveFrame(cap, channel);
499     if( !_img )
500     {
501         image.release();
502         return false;
503     }
504     if(_img->origin == IPL_ORIGIN_TL)
505         image = Mat(_img);
506     else
507     {
508         Mat temp(_img);
509         flip(temp, image, 0);
510     }
511     return true;
512 }
513
514 bool VideoCapture::read(Mat& image)
515 {
516     if(grab())
517         retrieve(image);
518     else
519         image.release();
520     return !image.empty();
521 }
522
523 VideoCapture& VideoCapture::operator >> (Mat& image)
524 {
525     read(image);
526     return *this;
527 }
528
529 bool VideoCapture::set(int propId, double value)
530 {
531     return cvSetCaptureProperty(cap, propId, value) != 0;
532 }
533
534 double VideoCapture::get(int propId)
535 {
536     return cvGetCaptureProperty(cap, propId);
537 }
538
539 VideoWriter::VideoWriter()
540 {}
541
542 VideoWriter::VideoWriter(const string& filename, int fourcc, double fps, Size frameSize, bool isColor)
543 {
544     open(filename, fourcc, fps, frameSize, isColor);
545 }
546
547 void VideoWriter::release()
548 {
549     writer.release();
550 }
551
552 VideoWriter::~VideoWriter()
553 {
554     release();
555 }
556
557 bool VideoWriter::open(const string& filename, int fourcc, double fps, Size frameSize, bool isColor)
558 {
559     writer = cvCreateVideoWriter(filename.c_str(), fourcc, fps, frameSize, isColor);
560     return isOpened();
561 }
562
563 bool VideoWriter::isOpened() const
564 {
565     return !writer.empty();
566 }
567
568 void VideoWriter::write(const Mat& image)
569 {
570     IplImage _img = image;
571     cvWriteFrame(writer, &_img);
572 }
573
574 VideoWriter& VideoWriter::operator << (const Mat& image)
575 {
576     write(image);
577     return *this;
578 }
579
580 }