Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/superres/src/input_array_utility.cpp
16354 views
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
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
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
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
{
101
void mat2mat(InputArray src, OutputArray dst)
102
{
103
src.getMat().copyTo(dst);
104
}
105
void arr2buf(InputArray src, OutputArray dst)
106
{
107
dst.getOGlBufferRef().copyFrom(src);
108
}
109
void mat2gpu(InputArray src, OutputArray dst)
110
{
111
dst.getGpuMatRef().upload(src.getMat());
112
}
113
void buf2arr(InputArray src, OutputArray dst)
114
{
115
src.getOGlBuffer().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
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
{
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
201
void convertToDepth(InputArray src, OutputArray dst, int depth)
202
{
203
const int sdepth = src.depth();
204
CV_Assert( sdepth <= CV_64F );
205
CV_Assert( depth == CV_8U || depth == CV_32F );
206
207
static const double maxVals[CV_64F + 1] =
208
{
209
(double)std::numeric_limits<uchar>::max(),
210
(double)std::numeric_limits<schar>::max(),
211
(double)std::numeric_limits<ushort>::max(),
212
(double)std::numeric_limits<short>::max(),
213
(double)std::numeric_limits<int>::max(),
214
1.0,
215
1.0,
216
};
217
218
const double scale = maxVals[depth] / maxVals[sdepth];
219
220
switch (src.kind())
221
{
222
case _InputArray::CUDA_GPU_MAT:
223
src.getGpuMat().convertTo(dst.getGpuMatRef(), depth, scale);
224
break;
225
226
case _InputArray::UMAT:
227
src.getUMat().convertTo(dst, depth, scale);
228
break;
229
230
default:
231
src.getMat().convertTo(dst, depth, scale);
232
break;
233
}
234
}
235
}
236
237
Mat cv::superres::convertToType(const Mat& src, int type, Mat& buf0, Mat& buf1)
238
{
239
CV_INSTRUMENT_REGION();
240
241
if (src.type() == type)
242
return src;
243
244
const int depth = CV_MAT_DEPTH(type);
245
const int cn = CV_MAT_CN(type);
246
247
if (src.depth() == depth)
248
{
249
convertToCn(src, buf0, cn);
250
return buf0;
251
}
252
253
if (src.channels() == cn)
254
{
255
convertToDepth(src, buf1, depth);
256
return buf1;
257
}
258
259
convertToCn(src, buf0, cn);
260
convertToDepth(buf0, buf1, depth);
261
return buf1;
262
}
263
264
UMat cv::superres::convertToType(const UMat& src, int type, UMat& buf0, UMat& buf1)
265
{
266
CV_INSTRUMENT_REGION();
267
268
if (src.type() == type)
269
return src;
270
271
const int depth = CV_MAT_DEPTH(type);
272
const int cn = CV_MAT_CN(type);
273
274
if (src.depth() == depth)
275
{
276
convertToCn(src, buf0, cn);
277
return buf0;
278
}
279
280
if (src.channels() == cn)
281
{
282
convertToDepth(src, buf1, depth);
283
return buf1;
284
}
285
286
convertToCn(src, buf0, cn);
287
convertToDepth(buf0, buf1, depth);
288
return buf1;
289
}
290
291
GpuMat cv::superres::convertToType(const GpuMat& src, int type, GpuMat& buf0, GpuMat& buf1)
292
{
293
if (src.type() == type)
294
return src;
295
296
const int depth = CV_MAT_DEPTH(type);
297
const int cn = CV_MAT_CN(type);
298
299
if (src.depth() == depth)
300
{
301
convertToCn(src, buf0, cn);
302
return buf0;
303
}
304
305
if (src.channels() == cn)
306
{
307
convertToDepth(src, buf1, depth);
308
return buf1;
309
}
310
311
convertToCn(src, buf0, cn);
312
convertToDepth(buf0, buf1, depth);
313
return buf1;
314
}
315
316