Merge pull request #1263 from abidrahmank:pyCLAHE_24
[profile/ivi/opencv.git] / modules / superres / src / input_array_utility.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
43 #include "precomp.hpp"
44
45 using namespace std;
46 using namespace cv;
47 using namespace cv::gpu;
48
49 Mat cv::superres::arrGetMat(InputArray arr, Mat& buf)
50 {
51     switch (arr.kind())
52     {
53     case _InputArray::GPU_MAT:
54         arr.getGpuMat().download(buf);
55         return buf;
56
57     case _InputArray::OPENGL_BUFFER:
58         arr.getOGlBuffer().copyTo(buf);
59         return buf;
60
61     case _InputArray::OPENGL_TEXTURE:
62         arr.getOGlTexture2D().copyTo(buf);
63         return buf;
64
65     default:
66         return arr.getMat();
67     }
68 }
69
70 GpuMat cv::superres::arrGetGpuMat(InputArray arr, GpuMat& buf)
71 {
72     switch (arr.kind())
73     {
74     case _InputArray::GPU_MAT:
75         return arr.getGpuMat();
76
77     case _InputArray::OPENGL_BUFFER:
78         arr.getOGlBuffer().copyTo(buf);
79         return buf;
80
81     case _InputArray::OPENGL_TEXTURE:
82         arr.getOGlTexture2D().copyTo(buf);
83         return buf;
84
85     default:
86         buf.upload(arr.getMat());
87         return buf;
88     }
89 }
90
91 namespace
92 {
93     void mat2mat(InputArray src, OutputArray dst)
94     {
95         src.getMat().copyTo(dst);
96     }
97     void arr2buf(InputArray src, OutputArray dst)
98     {
99         dst.getOGlBufferRef().copyFrom(src);
100     }
101     void arr2tex(InputArray src, OutputArray dst)
102     {
103         dst.getOGlTexture2D().copyFrom(src);
104     }
105     void mat2gpu(InputArray src, OutputArray dst)
106     {
107         dst.getGpuMatRef().upload(src.getMat());
108     }
109     void buf2arr(InputArray src, OutputArray dst)
110     {
111         src.getOGlBuffer().copyTo(dst);
112     }
113     void tex2arr(InputArray src, OutputArray dst)
114     {
115         src.getOGlTexture2D().copyTo(dst);
116     }
117     void gpu2mat(InputArray src, OutputArray dst)
118     {
119         GpuMat d = src.getGpuMat();
120         dst.create(d.size(), d.type());
121         Mat m = dst.getMat();
122         d.download(m);
123     }
124     void gpu2gpu(InputArray src, OutputArray dst)
125     {
126         src.getGpuMat().copyTo(dst.getGpuMatRef());
127     }
128 #ifdef HAVE_OPENCV_OCL
129     void ocl2mat(InputArray src, OutputArray dst)
130     {
131         dst.getMatRef() = (Mat)ocl::getOclMatRef(src);
132     }
133     void mat2ocl(InputArray src, OutputArray dst)
134     {
135         Mat m = src.getMat();
136         ocl::getOclMatRef(dst) = (ocl::oclMat)m;
137     }
138     void ocl2ocl(InputArray src, OutputArray dst)
139     {
140         ocl::getOclMatRef(src).copyTo(ocl::getOclMatRef(dst));
141     }
142 #else
143     void ocl2mat(InputArray, OutputArray)
144     {
145         CV_Error(CV_StsNotImplemented, "The called functionality is disabled for current build or platform");;
146     }
147     void mat2ocl(InputArray, OutputArray)
148     {
149         CV_Error(CV_StsNotImplemented, "The called functionality is disabled for current build or platform");;
150     }
151     void ocl2ocl(InputArray, OutputArray)
152     {
153         CV_Error(CV_StsNotImplemented, "The called functionality is disabled for current build or platform");
154     }
155 #endif
156 }
157
158 void cv::superres::arrCopy(InputArray src, OutputArray dst)
159 {
160     typedef void (*func_t)(InputArray src, OutputArray dst);
161     static const func_t funcs[11][11] =
162     {
163         {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
164         {0, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, arr2buf, arr2tex, mat2gpu, mat2ocl},
165         {0, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, arr2buf, arr2tex, mat2gpu, mat2ocl},
166         {0, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, arr2buf, arr2tex, mat2gpu, mat2ocl},
167         {0, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, arr2buf, arr2tex, mat2gpu, mat2ocl},
168         {0, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, arr2buf, arr2tex, mat2gpu, mat2ocl},
169         {0, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, arr2buf, arr2tex, mat2gpu, mat2ocl},
170         {0, buf2arr, buf2arr, buf2arr, buf2arr, buf2arr, buf2arr, buf2arr, buf2arr, buf2arr, 0      },
171         {0, tex2arr, tex2arr, tex2arr, tex2arr, tex2arr, tex2arr, tex2arr, tex2arr, tex2arr, 0      },
172         {0, gpu2mat, gpu2mat, gpu2mat, gpu2mat, gpu2mat, gpu2mat, arr2buf, arr2tex, gpu2gpu, 0      },
173         {0, ocl2mat, ocl2mat, ocl2mat, ocl2mat, ocl2mat, ocl2mat, 0,       0,       0,       ocl2ocl}
174     };
175
176     const int src_kind = src.kind() >> _InputArray::KIND_SHIFT;
177     const int dst_kind = dst.kind() >> _InputArray::KIND_SHIFT;
178
179     CV_DbgAssert( src_kind >= 0 && src_kind < 11 );
180     CV_DbgAssert( dst_kind >= 0 && dst_kind < 11 );
181
182     const func_t func = funcs[src_kind][dst_kind];
183     CV_DbgAssert( func != 0 );
184
185     func(src, dst);
186 }
187
188 namespace
189 {
190     void convertToCn(InputArray src, OutputArray dst, int cn)
191     {
192         CV_Assert( src.channels() == 1 || src.channels() == 3 || src.channels() == 4 );
193         CV_Assert( cn == 1 || cn == 3 || cn == 4 );
194
195         static const int codes[5][5] =
196         {
197             {-1, -1, -1, -1, -1},
198             {-1, -1, -1, COLOR_GRAY2BGR, COLOR_GRAY2BGRA},
199             {-1, -1, -1, -1, -1},
200             {-1, COLOR_BGR2GRAY, -1, -1, COLOR_BGR2BGRA},
201             {-1, COLOR_BGRA2GRAY, -1, COLOR_BGRA2BGR, -1},
202         };
203
204         const int code = codes[src.channels()][cn];
205         CV_DbgAssert( code >= 0 );
206
207         switch (src.kind())
208         {
209         case _InputArray::GPU_MAT:
210             #ifdef HAVE_OPENCV_GPU
211                 gpu::cvtColor(src.getGpuMat(), dst.getGpuMatRef(), code, cn);
212             #else
213                 CV_Error(CV_StsNotImplemented, "The called functionality is disabled for current build or platform");
214             #endif
215             break;
216
217         default:
218             cvtColor(src, dst, code, cn);
219             break;
220         }
221     }
222     void convertToDepth(InputArray src, OutputArray dst, int depth)
223     {
224         CV_Assert( src.depth() <= CV_64F );
225         CV_Assert( depth == CV_8U || depth == CV_32F );
226
227         static const double maxVals[] =
228         {
229             numeric_limits<uchar>::max(),
230             numeric_limits<schar>::max(),
231             numeric_limits<ushort>::max(),
232             numeric_limits<short>::max(),
233             numeric_limits<int>::max(),
234             1.0,
235             1.0,
236         };
237
238         const double scale = maxVals[depth] / maxVals[src.depth()];
239
240         switch (src.kind())
241         {
242         case _InputArray::GPU_MAT:
243             src.getGpuMat().convertTo(dst.getGpuMatRef(), depth, scale);
244             break;
245
246         default:
247             src.getMat().convertTo(dst, depth, scale);
248             break;
249         }
250     }
251 }
252
253 Mat cv::superres::convertToType(const Mat& src, int type, Mat& buf0, Mat& buf1)
254 {
255     if (src.type() == type)
256         return src;
257
258     const int depth = CV_MAT_DEPTH(type);
259     const int cn = CV_MAT_CN(type);
260
261     if (src.depth() == depth)
262     {
263         convertToCn(src, buf0, cn);
264         return buf0;
265     }
266
267     if (src.channels() == cn)
268     {
269         convertToDepth(src, buf1, depth);
270         return buf1;
271     }
272
273     convertToCn(src, buf0, cn);
274     convertToDepth(buf0, buf1, depth);
275     return buf1;
276 }
277
278 GpuMat cv::superres::convertToType(const GpuMat& src, int type, GpuMat& buf0, GpuMat& buf1)
279 {
280     if (src.type() == type)
281         return src;
282
283     const int depth = CV_MAT_DEPTH(type);
284     const int cn = CV_MAT_CN(type);
285
286     if (src.depth() == depth)
287     {
288         convertToCn(src, buf0, cn);
289         return buf0;
290     }
291
292     if (src.channels() == cn)
293     {
294         convertToDepth(src, buf1, depth);
295         return buf1;
296     }
297
298     convertToCn(src, buf0, cn);
299     convertToDepth(buf0, buf1, depth);
300     return buf1;
301 }
302 #ifdef HAVE_OPENCV_OCL
303 namespace
304 {
305     // TODO(pengx17): remove these overloaded functions until IntputArray fully supports oclMat
306     void convertToCn(const ocl::oclMat& src, ocl::oclMat& dst, int cn)
307     {
308         CV_Assert( src.channels() == 1 || src.channels() == 3 || src.channels() == 4 );
309         CV_Assert( cn == 1 || cn == 3 || cn == 4 );
310
311         static const int codes[5][5] =
312         {
313             {-1, -1, -1, -1, -1},
314             {-1, -1, -1, COLOR_GRAY2BGR, COLOR_GRAY2BGRA},
315             {-1, -1, -1, -1, -1},
316             {-1, COLOR_BGR2GRAY, -1, -1, COLOR_BGR2BGRA},
317             {-1, COLOR_BGRA2GRAY, -1, COLOR_BGRA2BGR, -1},
318         };
319
320         const int code = codes[src.channels()][cn];
321         CV_DbgAssert( code >= 0 );
322
323         ocl::cvtColor(src, dst, code, cn);
324     }
325     void convertToDepth(const ocl::oclMat& src, ocl::oclMat& dst, int depth)
326     {
327         CV_Assert( src.depth() <= CV_64F );
328         CV_Assert( depth == CV_8U || depth == CV_32F );
329
330         static const double maxVals[] =
331         {
332             std::numeric_limits<uchar>::max(),
333             std::numeric_limits<schar>::max(),
334             std::numeric_limits<ushort>::max(),
335             std::numeric_limits<short>::max(),
336             std::numeric_limits<int>::max(),
337             1.0,
338             1.0,
339         };
340         const double scale = maxVals[depth] / maxVals[src.depth()];
341         src.convertTo(dst, depth, scale);
342     }
343 }
344 ocl::oclMat cv::superres::convertToType(const ocl::oclMat& src, int type, ocl::oclMat& buf0, ocl::oclMat& buf1)
345 {
346     if (src.type() == type)
347         return src;
348
349     const int depth = CV_MAT_DEPTH(type);
350     const int cn = CV_MAT_CN(type);
351
352     if (src.depth() == depth)
353     {
354         convertToCn(src, buf0, cn);
355         return buf0;
356     }
357
358     if (src.channels() == cn)
359     {
360         convertToDepth(src, buf1, depth);
361         return buf1;
362     }
363
364     convertToCn(src, buf0, cn);
365     convertToDepth(buf0, buf1, depth);
366     return buf1;
367 }
368 #endif