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 cv;
46 using namespace cv::cuda;
47 
arrGetMat(InputArray arr,Mat & buf)48 Mat cv::superres::arrGetMat(InputArray arr, Mat& buf)
49 {
50     switch (arr.kind())
51     {
52     case _InputArray::CUDA_GPU_MAT:
53         arr.getGpuMat().download(buf);
54         return buf;
55 
56     case _InputArray::OPENGL_BUFFER:
57         arr.getOGlBuffer().copyTo(buf);
58         return buf;
59 
60     default:
61         return arr.getMat();
62     }
63 }
64 
arrGetUMat(InputArray arr,UMat & buf)65 UMat cv::superres::arrGetUMat(InputArray arr, UMat& buf)
66 {
67     switch (arr.kind())
68     {
69     case _InputArray::CUDA_GPU_MAT:
70         arr.getGpuMat().download(buf);
71         return buf;
72 
73     case _InputArray::OPENGL_BUFFER:
74         arr.getOGlBuffer().copyTo(buf);
75         return buf;
76 
77     default:
78         return arr.getUMat();
79     }
80 }
81 
arrGetGpuMat(InputArray arr,GpuMat & buf)82 GpuMat cv::superres::arrGetGpuMat(InputArray arr, GpuMat& buf)
83 {
84     switch (arr.kind())
85     {
86     case _InputArray::CUDA_GPU_MAT:
87         return arr.getGpuMat();
88 
89     case _InputArray::OPENGL_BUFFER:
90         arr.getOGlBuffer().copyTo(buf);
91         return buf;
92 
93     default:
94         buf.upload(arr.getMat());
95         return buf;
96     }
97 }
98 
99 namespace
100 {
mat2mat(InputArray src,OutputArray dst)101     void mat2mat(InputArray src, OutputArray dst)
102     {
103         src.getMat().copyTo(dst);
104     }
arr2buf(InputArray src,OutputArray dst)105     void arr2buf(InputArray src, OutputArray dst)
106     {
107         dst.getOGlBufferRef().copyFrom(src);
108     }
mat2gpu(InputArray src,OutputArray dst)109     void mat2gpu(InputArray src, OutputArray dst)
110     {
111         dst.getGpuMatRef().upload(src.getMat());
112     }
buf2arr(InputArray src,OutputArray dst)113     void buf2arr(InputArray src, OutputArray dst)
114     {
115         src.getOGlBuffer().copyTo(dst);
116     }
gpu2mat(InputArray src,OutputArray dst)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     }
gpu2gpu(InputArray src,OutputArray dst)124     void gpu2gpu(InputArray src, OutputArray dst)
125     {
126         src.getGpuMat().copyTo(dst.getGpuMatRef());
127     }
128 }
129 
arrCopy(InputArray src,OutputArray dst)130 void cv::superres::arrCopy(InputArray src, OutputArray dst)
131 {
132     if (dst.isUMat() || src.isUMat())
133     {
134         src.copyTo(dst);
135         return;
136     }
137 
138     typedef void (*func_t)(InputArray src, OutputArray dst);
139     static const func_t funcs[10][10] =
140     {
141         { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
142         { 0, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, arr2buf, 0, mat2gpu },
143         { 0, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, arr2buf, 0, mat2gpu },
144         { 0, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, arr2buf, 0, mat2gpu },
145         { 0, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, arr2buf, 0, mat2gpu },
146         { 0, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, arr2buf, 0, mat2gpu },
147         { 0, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, arr2buf, 0, mat2gpu },
148         { 0, buf2arr, buf2arr, buf2arr, buf2arr, buf2arr, buf2arr, buf2arr, 0, buf2arr },
149         { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
150         { 0, gpu2mat, gpu2mat, gpu2mat, gpu2mat, gpu2mat, gpu2mat, arr2buf, 0 , gpu2gpu },
151     };
152 
153     const int src_kind = src.kind() >> _InputArray::KIND_SHIFT;
154     const int dst_kind = dst.kind() >> _InputArray::KIND_SHIFT;
155 
156     CV_Assert( src_kind >= 0 && src_kind < 10 );
157     CV_Assert( dst_kind >= 0 && dst_kind < 10 );
158 
159     const func_t func = funcs[src_kind][dst_kind];
160     CV_Assert( func != 0 );
161 
162     func(src, dst);
163 }
164 
165 namespace
166 {
convertToCn(InputArray src,OutputArray dst,int cn)167     void convertToCn(InputArray src, OutputArray dst, int cn)
168     {
169         int scn = src.channels();
170         CV_Assert( scn == 1 || scn == 3 || scn == 4 );
171         CV_Assert( cn == 1 || cn == 3 || cn == 4 );
172 
173         static const int codes[5][5] =
174         {
175             { -1, -1, -1, -1, -1 },
176             { -1, -1, -1, COLOR_GRAY2BGR, COLOR_GRAY2BGRA },
177             { -1, -1, -1, -1, -1 },
178             { -1, COLOR_BGR2GRAY, -1, -1, COLOR_BGR2BGRA },
179             { -1, COLOR_BGRA2GRAY, -1, COLOR_BGRA2BGR, -1 }
180         };
181 
182         const int code = codes[scn][cn];
183         CV_Assert( code >= 0 );
184 
185         switch (src.kind())
186         {
187         case _InputArray::CUDA_GPU_MAT:
188             #ifdef HAVE_OPENCV_CUDAIMGPROC
189                 cuda::cvtColor(src.getGpuMat(), dst.getGpuMatRef(), code, cn);
190             #else
191                 CV_Error(cv::Error::StsNotImplemented, "The called functionality is disabled for current build or platform");
192             #endif
193             break;
194 
195         default:
196             cv::cvtColor(src, dst, code, cn);
197             break;
198         }
199     }
200 
convertToDepth(InputArray src,OutputArray dst,int depth)201     void convertToDepth(InputArray src, OutputArray dst, int depth)
202     {
203         CV_Assert( src.depth() <= CV_64F );
204         CV_Assert( depth == CV_8U || depth == CV_32F );
205 
206         static const double maxVals[] =
207         {
208             std::numeric_limits<uchar>::max(),
209             std::numeric_limits<schar>::max(),
210             std::numeric_limits<ushort>::max(),
211             std::numeric_limits<short>::max(),
212             std::numeric_limits<int>::max(),
213             1.0,
214             1.0,
215         };
216 
217         const double scale = maxVals[depth] / maxVals[src.depth()];
218 
219         switch (src.kind())
220         {
221         case _InputArray::CUDA_GPU_MAT:
222             src.getGpuMat().convertTo(dst.getGpuMatRef(), depth, scale);
223             break;
224 
225         case _InputArray::UMAT:
226             src.getUMat().convertTo(dst, depth, scale);
227             break;
228 
229         default:
230             src.getMat().convertTo(dst, depth, scale);
231             break;
232         }
233     }
234 }
235 
convertToType(const Mat & src,int type,Mat & buf0,Mat & buf1)236 Mat cv::superres::convertToType(const Mat& src, int type, Mat& buf0, Mat& buf1)
237 {
238     if (src.type() == type)
239         return src;
240 
241     const int depth = CV_MAT_DEPTH(type);
242     const int cn = CV_MAT_CN(type);
243 
244     if (src.depth() == depth)
245     {
246         convertToCn(src, buf0, cn);
247         return buf0;
248     }
249 
250     if (src.channels() == cn)
251     {
252         convertToDepth(src, buf1, depth);
253         return buf1;
254     }
255 
256     convertToCn(src, buf0, cn);
257     convertToDepth(buf0, buf1, depth);
258     return buf1;
259 }
260 
convertToType(const UMat & src,int type,UMat & buf0,UMat & buf1)261 UMat cv::superres::convertToType(const UMat& src, int type, UMat& buf0, UMat& buf1)
262 {
263     if (src.type() == type)
264         return src;
265 
266     const int depth = CV_MAT_DEPTH(type);
267     const int cn = CV_MAT_CN(type);
268 
269     if (src.depth() == depth)
270     {
271         convertToCn(src, buf0, cn);
272         return buf0;
273     }
274 
275     if (src.channels() == cn)
276     {
277         convertToDepth(src, buf1, depth);
278         return buf1;
279     }
280 
281     convertToCn(src, buf0, cn);
282     convertToDepth(buf0, buf1, depth);
283     return buf1;
284 }
285 
convertToType(const GpuMat & src,int type,GpuMat & buf0,GpuMat & buf1)286 GpuMat cv::superres::convertToType(const GpuMat& src, int type, GpuMat& buf0, GpuMat& buf1)
287 {
288     if (src.type() == type)
289         return src;
290 
291     const int depth = CV_MAT_DEPTH(type);
292     const int cn = CV_MAT_CN(type);
293 
294     if (src.depth() == depth)
295     {
296         convertToCn(src, buf0, cn);
297         return buf0;
298     }
299 
300     if (src.channels() == cn)
301     {
302         convertToDepth(src, buf1, depth);
303         return buf1;
304     }
305 
306     convertToCn(src, buf0, cn);
307     convertToDepth(buf0, buf1, depth);
308     return buf1;
309 }
310