Merge pull request #1263 from abidrahmank:pyCLAHE_24
[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 if(_frame.kind() == _InputArray::GPU_MAT)
123         {
124             vc_ >> frame_;
125             arrCopy(frame_, _frame);
126         }
127         else if(_frame.kind() == _InputArray::OCL_MAT)
128         {
129             vc_ >> frame_;
130             if(!frame_.empty())
131             {
132                 arrCopy(frame_, _frame);
133             }
134         }
135         else
136         {
137             //should never get here
138         }
139     }
140
141     class VideoFrameSource : public CaptureFrameSource
142     {
143     public:
144         VideoFrameSource(const string& fileName);
145
146         void reset();
147
148     private:
149         string fileName_;
150     };
151
152     VideoFrameSource::VideoFrameSource(const string& fileName) : fileName_(fileName)
153     {
154         reset();
155     }
156
157     void VideoFrameSource::reset()
158     {
159         vc_.release();
160         vc_.open(fileName_);
161         CV_Assert( vc_.isOpened() );
162     }
163
164     class CameraFrameSource : public CaptureFrameSource
165     {
166     public:
167         CameraFrameSource(int deviceId);
168
169         void reset();
170
171     private:
172         int deviceId_;
173     };
174
175     CameraFrameSource::CameraFrameSource(int deviceId) : deviceId_(deviceId)
176     {
177         reset();
178     }
179
180     void CameraFrameSource::reset()
181     {
182         vc_.release();
183         vc_.open(deviceId_);
184         CV_Assert( vc_.isOpened() );
185     }
186 }
187
188 Ptr<FrameSource> cv::superres::createFrameSource_Video(const string& fileName)
189 {
190     return new VideoFrameSource(fileName);
191 }
192
193 Ptr<FrameSource> cv::superres::createFrameSource_Camera(int deviceId)
194 {
195     return new CameraFrameSource(deviceId);
196 }
197
198 #endif // HAVE_OPENCV_HIGHGUI
199
200 //////////////////////////////////////////////////////
201 // VideoFrameSource_GPU
202
203 #ifndef HAVE_OPENCV_GPU
204
205 Ptr<FrameSource> cv::superres::createFrameSource_Video_GPU(const string& fileName)
206 {
207     (void) fileName;
208     CV_Error(CV_StsNotImplemented, "The called functionality is disabled for current build or platform");
209     return Ptr<FrameSource>();
210 }
211
212 #else // HAVE_OPENCV_GPU
213
214 namespace
215 {
216     class VideoFrameSource_GPU : public FrameSource
217     {
218     public:
219         VideoFrameSource_GPU(const string& fileName);
220
221         void nextFrame(OutputArray frame);
222         void reset();
223
224     private:
225         string fileName_;
226         VideoReader_GPU reader_;
227         GpuMat frame_;
228     };
229
230     VideoFrameSource_GPU::VideoFrameSource_GPU(const string& fileName) : fileName_(fileName)
231     {
232         reset();
233     }
234
235     void VideoFrameSource_GPU::nextFrame(OutputArray _frame)
236     {
237         if (_frame.kind() == _InputArray::GPU_MAT)
238         {
239             bool res = reader_.read(_frame.getGpuMatRef());
240             if (!res)
241                 _frame.release();
242         }
243         else
244         {
245             bool res = reader_.read(frame_);
246             if (!res)
247                 _frame.release();
248             else
249                 arrCopy(frame_, _frame);
250         }
251     }
252
253     void VideoFrameSource_GPU::reset()
254     {
255         reader_.close();
256         reader_.open(fileName_);
257         CV_Assert( reader_.isOpened() );
258     }
259 }
260
261 Ptr<FrameSource> cv::superres::createFrameSource_Video_GPU(const string& fileName)
262 {
263     return new VideoFrameSource(fileName);
264 }
265
266 #endif // HAVE_OPENCV_GPU