CLAHE Python bindings
[profile/ivi/opencv.git] / modules / superres / src / frame_source.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 //                           License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
16 //
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
19 //
20 //   * Redistribution's of source code must retain the above copyright notice,
21 //     this list of conditions and the following disclaimer.
22 //
23 //   * Redistribution's in binary form must reproduce the above copyright notice,
24 //     this list of conditions and the following disclaimer in the documentation
25 //     and/or other materials provided with the distribution.
26 //
27 //   * The name of the copyright holders may not be used to endorse or promote products
28 //     derived from this software without specific prior written permission.
29 //
30 // This software is provided by the copyright holders and contributors "as is" and
31 // any express or implied warranties, including, but not limited to, the implied
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
34 // indirect, incidental, special, exemplary, or consequential damages
35 // (including, but not limited to, procurement of substitute goods or services;
36 // loss of use, data, or profits; or business interruption) however caused
37 // and on any theory of liability, whether in contract, strict liability,
38 // or tort (including negligence or otherwise) arising in any way out of
39 // the use of this software, even if advised of the possibility of such damage.
40 //
41 //M*/
42 #include "precomp.hpp"
43
44 using namespace std;
45 using namespace cv;
46 using namespace cv::gpu;
47 using namespace cv::superres;
48 using namespace cv::superres::detail;
49
50 cv::superres::FrameSource::~FrameSource()
51 {
52 }
53
54 //////////////////////////////////////////////////////
55 // EmptyFrameSource
56
57 namespace
58 {
59     class EmptyFrameSource : public FrameSource
60     {
61     public:
62         void nextFrame(OutputArray frame);
63         void reset();
64     };
65
66     void EmptyFrameSource::nextFrame(OutputArray frame)
67     {
68         frame.release();
69     }
70
71     void EmptyFrameSource::reset()
72     {
73     }
74 }
75
76 Ptr<FrameSource> cv::superres::createFrameSource_Empty()
77 {
78     return new EmptyFrameSource;
79 }
80
81 //////////////////////////////////////////////////////
82 // VideoFrameSource & CameraFrameSource
83
84 #ifndef HAVE_OPENCV_HIGHGUI
85
86 Ptr<FrameSource> cv::superres::createFrameSource_Video(const string& fileName)
87 {
88     (void) fileName;
89     CV_Error(CV_StsNotImplemented, "The called functionality is disabled for current build or platform");
90     return Ptr<FrameSource>();
91 }
92
93 Ptr<FrameSource> cv::superres::createFrameSource_Camera(int deviceId)
94 {
95     (void) deviceId;
96     CV_Error(CV_StsNotImplemented, "The called functionality is disabled for current build or platform");
97     return Ptr<FrameSource>();
98 }
99
100 #else // HAVE_OPENCV_HIGHGUI
101
102 namespace
103 {
104     class CaptureFrameSource : public FrameSource
105     {
106     public:
107         void nextFrame(OutputArray frame);
108
109     protected:
110         VideoCapture vc_;
111
112     private:
113         Mat frame_;
114     };
115
116     void CaptureFrameSource::nextFrame(OutputArray _frame)
117     {
118         if (_frame.kind() == _InputArray::MAT)
119         {
120             vc_ >> _frame.getMatRef();
121         }
122         else
123         {
124             vc_ >> frame_;
125             arrCopy(frame_, _frame);
126         }
127     }
128
129     class VideoFrameSource : public CaptureFrameSource
130     {
131     public:
132         VideoFrameSource(const string& fileName);
133
134         void reset();
135
136     private:
137         string fileName_;
138     };
139
140     VideoFrameSource::VideoFrameSource(const string& fileName) : fileName_(fileName)
141     {
142         reset();
143     }
144
145     void VideoFrameSource::reset()
146     {
147         vc_.release();
148         vc_.open(fileName_);
149         CV_Assert( vc_.isOpened() );
150     }
151
152     class CameraFrameSource : public CaptureFrameSource
153     {
154     public:
155         CameraFrameSource(int deviceId);
156
157         void reset();
158
159     private:
160         int deviceId_;
161     };
162
163     CameraFrameSource::CameraFrameSource(int deviceId) : deviceId_(deviceId)
164     {
165         reset();
166     }
167
168     void CameraFrameSource::reset()
169     {
170         vc_.release();
171         vc_.open(deviceId_);
172         CV_Assert( vc_.isOpened() );
173     }
174 }
175
176 Ptr<FrameSource> cv::superres::createFrameSource_Video(const string& fileName)
177 {
178     return new VideoFrameSource(fileName);
179 }
180
181 Ptr<FrameSource> cv::superres::createFrameSource_Camera(int deviceId)
182 {
183     return new CameraFrameSource(deviceId);
184 }
185
186 #endif // HAVE_OPENCV_HIGHGUI
187
188 //////////////////////////////////////////////////////
189 // VideoFrameSource_GPU
190
191 #ifndef HAVE_OPENCV_GPU
192
193 Ptr<FrameSource> cv::superres::createFrameSource_Video_GPU(const string& fileName)
194 {
195     (void) fileName;
196     CV_Error(CV_StsNotImplemented, "The called functionality is disabled for current build or platform");
197     return Ptr<FrameSource>();
198 }
199
200 #else // HAVE_OPENCV_GPU
201
202 namespace
203 {
204     class VideoFrameSource_GPU : public FrameSource
205     {
206     public:
207         VideoFrameSource_GPU(const string& fileName);
208
209         void nextFrame(OutputArray frame);
210         void reset();
211
212     private:
213         string fileName_;
214         VideoReader_GPU reader_;
215         GpuMat frame_;
216     };
217
218     VideoFrameSource_GPU::VideoFrameSource_GPU(const string& fileName) : fileName_(fileName)
219     {
220         reset();
221     }
222
223     void VideoFrameSource_GPU::nextFrame(OutputArray _frame)
224     {
225         if (_frame.kind() == _InputArray::GPU_MAT)
226         {
227             bool res = reader_.read(_frame.getGpuMatRef());
228             if (!res)
229                 _frame.release();
230         }
231         else
232         {
233             bool res = reader_.read(frame_);
234             if (!res)
235                 _frame.release();
236             else
237                 arrCopy(frame_, _frame);
238         }
239     }
240
241     void VideoFrameSource_GPU::reset()
242     {
243         reader_.close();
244         reader_.open(fileName_);
245         CV_Assert( reader_.isOpened() );
246     }
247 }
248
249 Ptr<FrameSource> cv::superres::createFrameSource_Video_GPU(const string& fileName)
250 {
251     return new VideoFrameSource(fileName);
252 }
253
254 #endif // HAVE_OPENCV_GPU