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());
130 void cv::superres::arrCopy(InputArray src, OutputArray dst)
132 typedef void (*func_t)(InputArray src, OutputArray dst);
133 static const func_t funcs[10][10] =
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}
147 const int src_kind = src.kind() >> _InputArray::KIND_SHIFT;
148 const int dst_kind = dst.kind() >> _InputArray::KIND_SHIFT;
150 CV_DbgAssert( src_kind >= 0 && src_kind < 10 );
151 CV_DbgAssert( dst_kind >= 0 && dst_kind < 10 );
153 const func_t func = funcs[src_kind][dst_kind];
154 CV_DbgAssert( func != 0 );
161 void convertToCn(InputArray src, OutputArray dst, int cn)
163 CV_Assert( src.channels() == 1 || src.channels() == 3 || src.channels() == 4 );
164 CV_Assert( cn == 1 || cn == 3 || cn == 4 );
166 static const int codes[5][5] =
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},
175 const int code = codes[src.channels()][cn];
176 CV_DbgAssert( code >= 0 );
180 case _InputArray::GPU_MAT:
181 #ifdef HAVE_OPENCV_GPU
182 gpu::cvtColor(src.getGpuMat(), dst.getGpuMatRef(), code, cn);
184 CV_Error(CV_StsNotImplemented, "The called functionality is disabled for current build or platform");
189 cvtColor(src, dst, code, cn);
194 void convertToDepth(InputArray src, OutputArray dst, int depth)
196 CV_Assert( src.depth() <= CV_64F );
197 CV_Assert( depth == CV_8U || depth == CV_32F );
199 static const double maxVals[] =
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(),
210 const double scale = maxVals[depth] / maxVals[src.depth()];
214 case _InputArray::GPU_MAT:
215 src.getGpuMat().convertTo(dst.getGpuMatRef(), depth, scale);
219 src.getMat().convertTo(dst, depth, scale);
225 Mat cv::superres::convertToType(const Mat& src, int type, Mat& buf0, Mat& buf1)
227 if (src.type() == type)
230 const int depth = CV_MAT_DEPTH(type);
231 const int cn = CV_MAT_CN(type);
233 if (src.depth() == depth)
235 convertToCn(src, buf0, cn);
239 if (src.channels() == cn)
241 convertToDepth(src, buf1, depth);
245 convertToCn(src, buf0, cn);
246 convertToDepth(buf0, buf1, depth);
250 GpuMat cv::superres::convertToType(const GpuMat& src, int type, GpuMat& buf0, GpuMat& buf1)
252 if (src.type() == type)
255 const int depth = CV_MAT_DEPTH(type);
256 const int cn = CV_MAT_CN(type);
258 if (src.depth() == depth)
260 convertToCn(src, buf0, cn);
264 if (src.channels() == cn)
266 convertToDepth(src, buf1, depth);
270 convertToCn(src, buf0, cn);
271 convertToDepth(buf0, buf1, depth);