1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
11 // For Open Source Computer Vision Library
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.
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
20 // * Redistribution's of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
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.
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.
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.
43 #include "precomp.hpp"
47 using namespace cv::gpu;
49 Mat cv::superres::arrGetMat(InputArray arr, Mat& buf)
53 case _InputArray::GPU_MAT:
54 arr.getGpuMat().download(buf);
57 case _InputArray::OPENGL_BUFFER:
58 arr.getOGlBuffer().copyTo(buf);
61 case _InputArray::OPENGL_TEXTURE:
62 arr.getOGlTexture2D().copyTo(buf);
70 GpuMat cv::superres::arrGetGpuMat(InputArray arr, GpuMat& buf)
74 case _InputArray::GPU_MAT:
75 return arr.getGpuMat();
77 case _InputArray::OPENGL_BUFFER:
78 arr.getOGlBuffer().copyTo(buf);
81 case _InputArray::OPENGL_TEXTURE:
82 arr.getOGlTexture2D().copyTo(buf);
86 buf.upload(arr.getMat());
93 void mat2mat(InputArray src, OutputArray dst)
95 src.getMat().copyTo(dst);
97 void arr2buf(InputArray src, OutputArray dst)
99 dst.getOGlBufferRef().copyFrom(src);
101 void arr2tex(InputArray src, OutputArray dst)
103 dst.getOGlTexture2D().copyFrom(src);
105 void mat2gpu(InputArray src, OutputArray dst)
107 dst.getGpuMatRef().upload(src.getMat());
109 void buf2arr(InputArray src, OutputArray dst)
111 src.getOGlBuffer().copyTo(dst);
113 void tex2arr(InputArray src, OutputArray dst)
115 src.getOGlTexture2D().copyTo(dst);
117 void gpu2mat(InputArray src, OutputArray dst)
119 GpuMat d = src.getGpuMat();
120 dst.create(d.size(), d.type());
121 Mat m = dst.getMat();
124 void gpu2gpu(InputArray src, OutputArray dst)
126 src.getGpuMat().copyTo(dst.getGpuMatRef());
128 #ifdef HAVE_OPENCV_OCL
129 void ocl2mat(InputArray src, OutputArray dst)
131 dst.getMatRef() = (Mat)ocl::getOclMatRef(src);
133 void mat2ocl(InputArray src, OutputArray dst)
135 Mat m = src.getMat();
136 ocl::getOclMatRef(dst) = (ocl::oclMat)m;
138 void ocl2ocl(InputArray src, OutputArray dst)
140 ocl::getOclMatRef(src).copyTo(ocl::getOclMatRef(dst));
143 void ocl2mat(InputArray, OutputArray)
145 CV_Error(CV_StsNotImplemented, "The called functionality is disabled for current build or platform");;
147 void mat2ocl(InputArray, OutputArray)
149 CV_Error(CV_StsNotImplemented, "The called functionality is disabled for current build or platform");;
151 void ocl2ocl(InputArray, OutputArray)
153 CV_Error(CV_StsNotImplemented, "The called functionality is disabled for current build or platform");
158 void cv::superres::arrCopy(InputArray src, OutputArray dst)
160 typedef void (*func_t)(InputArray src, OutputArray dst);
161 static const func_t funcs[11][11] =
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}
176 const int src_kind = src.kind() >> _InputArray::KIND_SHIFT;
177 const int dst_kind = dst.kind() >> _InputArray::KIND_SHIFT;
179 CV_DbgAssert( src_kind >= 0 && src_kind < 11 );
180 CV_DbgAssert( dst_kind >= 0 && dst_kind < 11 );
182 const func_t func = funcs[src_kind][dst_kind];
183 CV_DbgAssert( func != 0 );
190 void convertToCn(InputArray src, OutputArray dst, int cn)
192 CV_Assert( src.channels() == 1 || src.channels() == 3 || src.channels() == 4 );
193 CV_Assert( cn == 1 || cn == 3 || cn == 4 );
195 static const int codes[5][5] =
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},
204 const int code = codes[src.channels()][cn];
205 CV_DbgAssert( code >= 0 );
209 case _InputArray::GPU_MAT:
210 #ifdef HAVE_OPENCV_GPU
211 gpu::cvtColor(src.getGpuMat(), dst.getGpuMatRef(), code, cn);
213 CV_Error(CV_StsNotImplemented, "The called functionality is disabled for current build or platform");
218 cvtColor(src, dst, code, cn);
222 void convertToDepth(InputArray src, OutputArray dst, int depth)
224 CV_Assert( src.depth() <= CV_64F );
225 CV_Assert( depth == CV_8U || depth == CV_32F );
227 static const double maxVals[] =
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(),
238 const double scale = maxVals[depth] / maxVals[src.depth()];
242 case _InputArray::GPU_MAT:
243 src.getGpuMat().convertTo(dst.getGpuMatRef(), depth, scale);
247 src.getMat().convertTo(dst, depth, scale);
253 Mat cv::superres::convertToType(const Mat& src, int type, Mat& buf0, Mat& buf1)
255 if (src.type() == type)
258 const int depth = CV_MAT_DEPTH(type);
259 const int cn = CV_MAT_CN(type);
261 if (src.depth() == depth)
263 convertToCn(src, buf0, cn);
267 if (src.channels() == cn)
269 convertToDepth(src, buf1, depth);
273 convertToCn(src, buf0, cn);
274 convertToDepth(buf0, buf1, depth);
278 GpuMat cv::superres::convertToType(const GpuMat& src, int type, GpuMat& buf0, GpuMat& buf1)
280 if (src.type() == type)
283 const int depth = CV_MAT_DEPTH(type);
284 const int cn = CV_MAT_CN(type);
286 if (src.depth() == depth)
288 convertToCn(src, buf0, cn);
292 if (src.channels() == cn)
294 convertToDepth(src, buf1, depth);
298 convertToCn(src, buf0, cn);
299 convertToDepth(buf0, buf1, depth);
302 #ifdef HAVE_OPENCV_OCL
305 // TODO(pengx17): remove these overloaded functions until IntputArray fully supports oclMat
306 void convertToCn(const ocl::oclMat& src, ocl::oclMat& dst, int cn)
308 CV_Assert( src.channels() == 1 || src.channels() == 3 || src.channels() == 4 );
309 CV_Assert( cn == 1 || cn == 3 || cn == 4 );
311 static const int codes[5][5] =
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},
320 const int code = codes[src.channels()][cn];
321 CV_DbgAssert( code >= 0 );
323 ocl::cvtColor(src, dst, code, cn);
325 void convertToDepth(const ocl::oclMat& src, ocl::oclMat& dst, int depth)
327 CV_Assert( src.depth() <= CV_64F );
328 CV_Assert( depth == CV_8U || depth == CV_32F );
330 static const double maxVals[] =
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(),
340 const double scale = maxVals[depth] / maxVals[src.depth()];
341 src.convertTo(dst, depth, scale);
344 ocl::oclMat cv::superres::convertToType(const ocl::oclMat& src, int type, ocl::oclMat& buf0, ocl::oclMat& buf1)
346 if (src.type() == type)
349 const int depth = CV_MAT_DEPTH(type);
350 const int cn = CV_MAT_CN(type);
352 if (src.depth() == depth)
354 convertToCn(src, buf0, cn);
358 if (src.channels() == cn)
360 convertToDepth(src, buf1, depth);
364 convertToCn(src, buf0, cn);
365 convertToDepth(buf0, buf1, depth);