CLAHE Python bindings
[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 }
129
130 void cv::superres::arrCopy(InputArray src, OutputArray dst)
131 {
132     typedef void (*func_t)(InputArray src, OutputArray dst);
133     static const func_t funcs[10][10] =
134     {
135         {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
136         {0, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, arr2buf, arr2tex, mat2gpu},
137         {0, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, arr2buf, arr2tex, mat2gpu},
138         {0, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, arr2buf, arr2tex, mat2gpu},
139         {0, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, arr2buf, arr2tex, mat2gpu},
140         {0, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, arr2buf, arr2tex, mat2gpu},
141         {0, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, arr2buf, arr2tex, mat2gpu},
142         {0, buf2arr, buf2arr, buf2arr, buf2arr, buf2arr, buf2arr, buf2arr, buf2arr, buf2arr},
143         {0, tex2arr, tex2arr, tex2arr, tex2arr, tex2arr, tex2arr, tex2arr, tex2arr, tex2arr},
144         {0, gpu2mat, gpu2mat, gpu2mat, gpu2mat, gpu2mat, gpu2mat, arr2buf, arr2tex, gpu2gpu}
145     };
146
147     const int src_kind = src.kind() >> _InputArray::KIND_SHIFT;
148     const int dst_kind = dst.kind() >> _InputArray::KIND_SHIFT;
149
150     CV_DbgAssert( src_kind >= 0 && src_kind < 10 );
151     CV_DbgAssert( dst_kind >= 0 && dst_kind < 10 );
152
153     const func_t func = funcs[src_kind][dst_kind];
154     CV_DbgAssert( func != 0 );
155
156     func(src, dst);
157 }
158
159 namespace
160 {
161     void convertToCn(InputArray src, OutputArray dst, int cn)
162     {
163         CV_Assert( src.channels() == 1 || src.channels() == 3 || src.channels() == 4 );
164         CV_Assert( cn == 1 || cn == 3 || cn == 4 );
165
166         static const int codes[5][5] =
167         {
168             {-1, -1, -1, -1, -1},
169             {-1, -1, -1, COLOR_GRAY2BGR, COLOR_GRAY2BGRA},
170             {-1, -1, -1, -1, -1},
171             {-1, COLOR_BGR2GRAY, -1, -1, COLOR_BGR2BGRA},
172             {-1, COLOR_BGRA2GRAY, -1, COLOR_BGRA2BGR, -1},
173         };
174
175         const int code = codes[src.channels()][cn];
176         CV_DbgAssert( code >= 0 );
177
178         switch (src.kind())
179         {
180         case _InputArray::GPU_MAT:
181             #ifdef HAVE_OPENCV_GPU
182                 gpu::cvtColor(src.getGpuMat(), dst.getGpuMatRef(), code, cn);
183             #else
184                 CV_Error(CV_StsNotImplemented, "The called functionality is disabled for current build or platform");
185             #endif
186             break;
187
188         default:
189             cvtColor(src, dst, code, cn);
190             break;
191         }
192     }
193
194     void convertToDepth(InputArray src, OutputArray dst, int depth)
195     {
196         CV_Assert( src.depth() <= CV_64F );
197         CV_Assert( depth == CV_8U || depth == CV_32F );
198
199         static const double maxVals[] =
200         {
201             numeric_limits<uchar>::max(),
202             numeric_limits<schar>::max(),
203             numeric_limits<ushort>::max(),
204             numeric_limits<short>::max(),
205             numeric_limits<int>::max(),
206             1.0,
207             1.0,
208         };
209
210         const double scale = maxVals[depth] / maxVals[src.depth()];
211
212         switch (src.kind())
213         {
214         case _InputArray::GPU_MAT:
215             src.getGpuMat().convertTo(dst.getGpuMatRef(), depth, scale);
216             break;
217
218         default:
219             src.getMat().convertTo(dst, depth, scale);
220             break;
221         }
222     }
223 }
224
225 Mat cv::superres::convertToType(const Mat& src, int type, Mat& buf0, Mat& buf1)
226 {
227     if (src.type() == type)
228         return src;
229
230     const int depth = CV_MAT_DEPTH(type);
231     const int cn = CV_MAT_CN(type);
232
233     if (src.depth() == depth)
234     {
235         convertToCn(src, buf0, cn);
236         return buf0;
237     }
238
239     if (src.channels() == cn)
240     {
241         convertToDepth(src, buf1, depth);
242         return buf1;
243     }
244
245     convertToCn(src, buf0, cn);
246     convertToDepth(buf0, buf1, depth);
247     return buf1;
248 }
249
250 GpuMat cv::superres::convertToType(const GpuMat& src, int type, GpuMat& buf0, GpuMat& buf1)
251 {
252     if (src.type() == type)
253         return src;
254
255     const int depth = CV_MAT_DEPTH(type);
256     const int cn = CV_MAT_CN(type);
257
258     if (src.depth() == depth)
259     {
260         convertToCn(src, buf0, cn);
261         return buf0;
262     }
263
264     if (src.channels() == cn)
265     {
266         convertToDepth(src, buf1, depth);
267         return buf1;
268     }
269
270     convertToCn(src, buf0, cn);
271     convertToDepth(buf0, buf1, depth);
272     return buf1;
273 }