Merge pull request #1263 from abidrahmank:pyCLAHE_24
[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 #ifdef HAVE_MSMF
121         CV_CAP_MSMF,
122 #endif
123 #if 1
124         CV_CAP_IEEE1394,   // identical to CV_CAP_DC1394
125 #endif
126 #ifdef HAVE_TYZX
127         CV_CAP_STEREO,
128 #endif
129 #ifdef HAVE_PVAPI
130         CV_CAP_PVAPI,
131 #endif
132 #if 1
133         CV_CAP_VFW,        // identical to CV_CAP_V4L
134 #endif
135 #ifdef HAVE_MIL
136         CV_CAP_MIL,
137 #endif
138 #ifdef HAVE_QUICKTIME
139         CV_CAP_QT,
140 #endif
141 #ifdef HAVE_UNICAP
142         CV_CAP_UNICAP,
143 #endif
144 #ifdef HAVE_OPENNI
145         CV_CAP_OPENNI,
146 #endif
147 #ifdef HAVE_ANDROID_NATIVE_CAMERA
148         CV_CAP_ANDROID,
149 #endif
150 #ifdef HAVE_XIMEA
151         CV_CAP_XIAPI,
152 #endif
153 #ifdef HAVE_AVFOUNDATION
154         CV_CAP_AVFOUNDATION,
155 #endif
156 #ifdef HAVE_GIGE_API
157         CV_CAP_GIGANETIX,
158 #endif
159         -1
160     };
161
162     // interpret preferred interface (0 = autodetect)
163     int pref = (index / 100) * 100;
164     if (pref)
165     {
166         domains[0]=pref;
167         index %= 100;
168         domains[1]=-1;
169     }
170
171     // try every possibly installed camera API
172     for (int i = 0; domains[i] >= 0; i++)
173     {
174 #if defined(HAVE_DSHOW)        || \
175     defined(HAVE_MSMF)         || \
176     defined(HAVE_TYZX)         || \
177     defined(HAVE_VFW)          || \
178     defined(HAVE_LIBV4L)       || \
179     defined(HAVE_CAMV4L)       || \
180     defined(HAVE_CAMV4L2)      || \
181     defined(HAVE_VIDEOIO)      || \
182     defined(HAVE_GSTREAMER)    || \
183     defined(HAVE_DC1394_2)     || \
184     defined(HAVE_DC1394)       || \
185     defined(HAVE_CMU1394)      || \
186     defined(HAVE_MIL)          || \
187     defined(HAVE_QUICKTIME)    || \
188     defined(HAVE_UNICAP)       || \
189     defined(HAVE_PVAPI)        || \
190     defined(HAVE_OPENNI)       || \
191     defined(HAVE_XIMEA)        || \
192     defined(HAVE_AVFOUNDATION) || \
193     defined(HAVE_ANDROID_NATIVE_CAMERA) || \
194     defined(HAVE_GIGE_API) || \
195     (0)
196         // local variable to memorize the captured device
197         CvCapture *capture;
198 #endif
199
200         switch (domains[i])
201         {
202 #ifdef HAVE_DSHOW
203         case CV_CAP_DSHOW:
204              capture = cvCreateCameraCapture_DShow (index);
205              if (capture)
206                  return capture;
207             break;
208 #endif
209 #ifdef HAVE_MSMF
210         case CV_CAP_MSMF:
211              capture = cvCreateCameraCapture_MSMF (index);
212              if (capture)
213                  return capture;
214             break;
215 #endif
216 #ifdef HAVE_TYZX
217         case CV_CAP_STEREO:
218             capture = cvCreateCameraCapture_TYZX (index);
219             if (capture)
220                 return capture;
221             break;
222 #endif
223         case CV_CAP_VFW:
224 #ifdef HAVE_VFW
225             capture = cvCreateCameraCapture_VFW (index);
226             if (capture)
227                 return capture;
228 #endif
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_VFW
362     if (! result)
363         result = cvCreateFileCapture_VFW (filename);
364 #endif
365
366 #ifdef HAVE_MSMF
367     if (! result)
368         result = cvCreateFileCapture_MSMF (filename);
369 #endif
370
371 #ifdef HAVE_XINE
372     if (! result)
373         result = cvCreateFileCapture_XINE (filename);
374 #endif
375
376 #ifdef HAVE_GSTREAMER
377     if (! result)
378         result = cvCreateCapture_GStreamer (CV_CAP_GSTREAMER_FILE, filename);
379 #endif
380
381 #ifdef HAVE_QUICKTIME
382     if (! result)
383         result = cvCreateFileCapture_QT (filename);
384 #endif
385
386 #ifdef HAVE_AVFOUNDATION
387     if (! result)
388         result = cvCreateFileCapture_AVFoundation (filename);
389 #endif
390
391 #ifdef HAVE_OPENNI
392     if (! result)
393         result = cvCreateFileCapture_OpenNI (filename);
394 #endif
395
396     if (! result)
397         result = cvCreateFileCapture_Images (filename);
398
399     return result;
400 }
401
402 /**
403  * Videowriter dispatching method: it tries to find the first
404  * API that can write a given stream.
405  */
406 CV_IMPL CvVideoWriter* cvCreateVideoWriter( const char* filename, int fourcc,
407                                             double fps, CvSize frameSize, int is_color )
408 {
409     //CV_FUNCNAME( "cvCreateVideoWriter" );
410
411     CvVideoWriter *result = 0;
412
413     if(!fourcc || !fps)
414         result = cvCreateVideoWriter_Images(filename);
415
416     if(!result)
417         result = cvCreateVideoWriter_FFMPEG_proxy (filename, fourcc, fps, frameSize, is_color);
418
419 #ifdef HAVE_VFW
420     if(!result)
421         result = cvCreateVideoWriter_VFW(filename, fourcc, fps, frameSize, is_color);
422 #endif
423
424 #ifdef HAVE_MSMF
425     if (!result)
426         result = cvCreateVideoWriter_MSMF(filename, fourcc, fps, frameSize, is_color);
427 #endif
428
429 /*  #ifdef HAVE_XINE
430     if(!result)
431         result = cvCreateVideoWriter_XINE(filename, fourcc, fps, frameSize, is_color);
432     #endif
433 */
434 #ifdef HAVE_AVFOUNDATION
435     if (! result)
436         result = cvCreateVideoWriter_AVFoundation(filename, fourcc, fps, frameSize, is_color);
437 #endif
438
439 #ifdef HAVE_QUICKTIME
440     if(!result)
441         result = cvCreateVideoWriter_QT(filename, fourcc, fps, frameSize, is_color);
442 #endif
443
444 #ifdef HAVE_GSTREAMER
445     if (! result)
446         result = cvCreateVideoWriter_GStreamer(filename, fourcc, fps, frameSize, is_color);
447 #endif
448
449     if(!result)
450         result = cvCreateVideoWriter_Images(filename);
451
452     return result;
453 }
454
455 CV_IMPL int cvWriteFrame( CvVideoWriter* writer, const IplImage* image )
456 {
457     return writer ? writer->writeFrame(image) : 0;
458 }
459
460 CV_IMPL void cvReleaseVideoWriter( CvVideoWriter** pwriter )
461 {
462     if( pwriter && *pwriter )
463     {
464         delete *pwriter;
465         *pwriter = 0;
466     }
467 }
468
469 namespace cv
470 {
471
472 VideoCapture::VideoCapture()
473 {}
474
475 VideoCapture::VideoCapture(const string& filename)
476 {
477     open(filename);
478 }
479
480 VideoCapture::VideoCapture(int device)
481 {
482     open(device);
483 }
484
485 VideoCapture::~VideoCapture()
486 {
487     cap.release();
488 }
489
490 bool VideoCapture::open(const string& filename)
491 {
492     if (isOpened()) release();
493     cap = cvCreateFileCapture(filename.c_str());
494     return isOpened();
495 }
496
497 bool VideoCapture::open(int device)
498 {
499     if (isOpened()) release();
500     cap = cvCreateCameraCapture(device);
501     return isOpened();
502 }
503
504 bool VideoCapture::isOpened() const { return !cap.empty(); }
505
506 void VideoCapture::release()
507 {
508     cap.release();
509 }
510
511 bool VideoCapture::grab()
512 {
513     return cvGrabFrame(cap) != 0;
514 }
515
516 bool VideoCapture::retrieve(Mat& image, int channel)
517 {
518     IplImage* _img = cvRetrieveFrame(cap, channel);
519     if( !_img )
520     {
521         image.release();
522         return false;
523     }
524     if(_img->origin == IPL_ORIGIN_TL)
525         image = Mat(_img);
526     else
527     {
528         Mat temp(_img);
529         flip(temp, image, 0);
530     }
531     return true;
532 }
533
534 bool VideoCapture::read(Mat& image)
535 {
536     if(grab())
537         retrieve(image);
538     else
539         image.release();
540     return !image.empty();
541 }
542
543 VideoCapture& VideoCapture::operator >> (Mat& image)
544 {
545     read(image);
546     return *this;
547 }
548
549 bool VideoCapture::set(int propId, double value)
550 {
551     return cvSetCaptureProperty(cap, propId, value) != 0;
552 }
553
554 double VideoCapture::get(int propId)
555 {
556     return cvGetCaptureProperty(cap, propId);
557 }
558
559 VideoWriter::VideoWriter()
560 {}
561
562 VideoWriter::VideoWriter(const string& filename, int fourcc, double fps, Size frameSize, bool isColor)
563 {
564     open(filename, fourcc, fps, frameSize, isColor);
565 }
566
567 void VideoWriter::release()
568 {
569     writer.release();
570 }
571
572 VideoWriter::~VideoWriter()
573 {
574     release();
575 }
576
577 bool VideoWriter::open(const string& filename, int fourcc, double fps, Size frameSize, bool isColor)
578 {
579     writer = cvCreateVideoWriter(filename.c_str(), fourcc, fps, frameSize, isColor);
580     return isOpened();
581 }
582
583 bool VideoWriter::isOpened() const
584 {
585     return !writer.empty();
586 }
587
588 void VideoWriter::write(const Mat& image)
589 {
590     IplImage _img = image;
591     cvWriteFrame(writer, &_img);
592 }
593
594 VideoWriter& VideoWriter::operator << (const Mat& image)
595 {
596     write(image);
597     return *this;
598 }
599
600 }