Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/core/src/cuda_gpu_mat.cpp
16337 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
// Copyright (C) 2013, OpenCV Foundation, all rights reserved.
16
// Third party copyrights are property of their respective owners.
17
//
18
// Redistribution and use in source and binary forms, with or without modification,
19
// are permitted provided that the following conditions are met:
20
//
21
// * Redistribution's of source code must retain the above copyright notice,
22
// this list of conditions and the following disclaimer.
23
//
24
// * Redistribution's in binary form must reproduce the above copyright notice,
25
// this list of conditions and the following disclaimer in the documentation
26
// and/or other materials provided with the distribution.
27
//
28
// * The name of the copyright holders may not be used to endorse or promote products
29
// derived from this software without specific prior written permission.
30
//
31
// This software is provided by the copyright holders and contributors "as is" and
32
// any express or implied warranties, including, but not limited to, the implied
33
// warranties of merchantability and fitness for a particular purpose are disclaimed.
34
// In no event shall the Intel Corporation or contributors be liable for any direct,
35
// indirect, incidental, special, exemplary, or consequential damages
36
// (including, but not limited to, procurement of substitute goods or services;
37
// loss of use, data, or profits; or business interruption) however caused
38
// and on any theory of liability, whether in contract, strict liability,
39
// or tort (including negligence or otherwise) arising in any way out of
40
// the use of this software, even if advised of the possibility of such damage.
41
//
42
//M*/
43
44
#include "precomp.hpp"
45
46
using namespace cv;
47
using namespace cv::cuda;
48
49
void cv::cuda::GpuMat::updateContinuityFlag()
50
{
51
int sz[] = { rows, cols };
52
size_t steps[] = { step, elemSize() };
53
flags = cv::updateContinuityFlag(flags, 2, sz, steps);
54
}
55
56
cv::cuda::GpuMat::GpuMat(int rows_, int cols_, int type_, void* data_, size_t step_) :
57
flags(Mat::MAGIC_VAL + (type_ & Mat::TYPE_MASK)), rows(rows_), cols(cols_),
58
step(step_), data((uchar*)data_), refcount(0),
59
datastart((uchar*)data_), dataend((const uchar*)data_),
60
allocator(defaultAllocator())
61
{
62
size_t minstep = cols * elemSize();
63
64
if (step == Mat::AUTO_STEP)
65
{
66
step = minstep;
67
}
68
else
69
{
70
if (rows == 1)
71
step = minstep;
72
73
CV_DbgAssert( step >= minstep );
74
}
75
76
dataend += step * (rows - 1) + minstep;
77
updateContinuityFlag();
78
}
79
80
cv::cuda::GpuMat::GpuMat(Size size_, int type_, void* data_, size_t step_) :
81
flags(Mat::MAGIC_VAL + (type_ & Mat::TYPE_MASK)), rows(size_.height), cols(size_.width),
82
step(step_), data((uchar*)data_), refcount(0),
83
datastart((uchar*)data_), dataend((const uchar*)data_),
84
allocator(defaultAllocator())
85
{
86
size_t minstep = cols * elemSize();
87
88
if (step == Mat::AUTO_STEP)
89
{
90
step = minstep;
91
}
92
else
93
{
94
if (rows == 1)
95
step = minstep;
96
97
CV_DbgAssert( step >= minstep );
98
}
99
100
dataend += step * (rows - 1) + minstep;
101
updateContinuityFlag();
102
}
103
104
cv::cuda::GpuMat::GpuMat(const GpuMat& m, Range rowRange_, Range colRange_)
105
{
106
flags = m.flags;
107
step = m.step; refcount = m.refcount;
108
data = m.data; datastart = m.datastart; dataend = m.dataend;
109
allocator = m.allocator;
110
111
if (rowRange_ == Range::all())
112
{
113
rows = m.rows;
114
}
115
else
116
{
117
CV_Assert( 0 <= rowRange_.start && rowRange_.start <= rowRange_.end && rowRange_.end <= m.rows );
118
119
rows = rowRange_.size();
120
data += step*rowRange_.start;
121
}
122
123
if (colRange_ == Range::all())
124
{
125
cols = m.cols;
126
}
127
else
128
{
129
CV_Assert( 0 <= colRange_.start && colRange_.start <= colRange_.end && colRange_.end <= m.cols );
130
131
cols = colRange_.size();
132
data += colRange_.start*elemSize();
133
}
134
135
if (refcount)
136
CV_XADD(refcount, 1);
137
138
if (rows <= 0 || cols <= 0)
139
rows = cols = 0;
140
141
updateContinuityFlag();
142
}
143
144
cv::cuda::GpuMat::GpuMat(const GpuMat& m, Rect roi) :
145
flags(m.flags), rows(roi.height), cols(roi.width),
146
step(m.step), data(m.data + roi.y*step), refcount(m.refcount),
147
datastart(m.datastart), dataend(m.dataend),
148
allocator(m.allocator)
149
{
150
data += roi.x * elemSize();
151
152
CV_Assert( 0 <= roi.x && 0 <= roi.width &&
153
roi.x + roi.width <= m.cols &&
154
0 <= roi.y && 0 <= roi.height &&
155
roi.y + roi.height <= m.rows );
156
157
if (refcount)
158
CV_XADD(refcount, 1);
159
160
if (rows <= 0 || cols <= 0)
161
rows = cols = 0;
162
updateContinuityFlag();
163
}
164
165
GpuMat cv::cuda::GpuMat::reshape(int new_cn, int new_rows) const
166
{
167
GpuMat hdr = *this;
168
169
int cn = channels();
170
if (new_cn == 0)
171
new_cn = cn;
172
173
int total_width = cols * cn;
174
175
if ((new_cn > total_width || total_width % new_cn != 0) && new_rows == 0)
176
new_rows = rows * total_width / new_cn;
177
178
if (new_rows != 0 && new_rows != rows)
179
{
180
int total_size = total_width * rows;
181
182
if (!isContinuous())
183
CV_Error(cv::Error::BadStep, "The matrix is not continuous, thus its number of rows can not be changed");
184
185
if ((unsigned)new_rows > (unsigned)total_size)
186
CV_Error(cv::Error::StsOutOfRange, "Bad new number of rows");
187
188
total_width = total_size / new_rows;
189
190
if (total_width * new_rows != total_size)
191
CV_Error(cv::Error::StsBadArg, "The total number of matrix elements is not divisible by the new number of rows");
192
193
hdr.rows = new_rows;
194
hdr.step = total_width * elemSize1();
195
}
196
197
int new_width = total_width / new_cn;
198
199
if (new_width * new_cn != total_width)
200
CV_Error(cv::Error::BadNumChannels, "The total width is not divisible by the new number of channels");
201
202
hdr.cols = new_width;
203
hdr.flags = (hdr.flags & ~CV_MAT_CN_MASK) | ((new_cn - 1) << CV_CN_SHIFT);
204
205
return hdr;
206
}
207
208
void cv::cuda::GpuMat::locateROI(Size& wholeSize, Point& ofs) const
209
{
210
CV_DbgAssert( step > 0 );
211
212
size_t esz = elemSize();
213
ptrdiff_t delta1 = data - datastart;
214
ptrdiff_t delta2 = dataend - datastart;
215
216
if (delta1 == 0)
217
{
218
ofs.x = ofs.y = 0;
219
}
220
else
221
{
222
ofs.y = static_cast<int>(delta1 / step);
223
ofs.x = static_cast<int>((delta1 - step * ofs.y) / esz);
224
225
CV_DbgAssert( data == datastart + ofs.y * step + ofs.x * esz );
226
}
227
228
size_t minstep = (ofs.x + cols) * esz;
229
230
wholeSize.height = std::max(static_cast<int>((delta2 - minstep) / step + 1), ofs.y + rows);
231
wholeSize.width = std::max(static_cast<int>((delta2 - step * (wholeSize.height - 1)) / esz), ofs.x + cols);
232
}
233
234
GpuMat& cv::cuda::GpuMat::adjustROI(int dtop, int dbottom, int dleft, int dright)
235
{
236
Size wholeSize;
237
Point ofs;
238
locateROI(wholeSize, ofs);
239
240
size_t esz = elemSize();
241
242
int row1 = std::max(ofs.y - dtop, 0);
243
int row2 = std::min(ofs.y + rows + dbottom, wholeSize.height);
244
245
int col1 = std::max(ofs.x - dleft, 0);
246
int col2 = std::min(ofs.x + cols + dright, wholeSize.width);
247
248
data += (row1 - ofs.y) * step + (col1 - ofs.x) * esz;
249
rows = row2 - row1;
250
cols = col2 - col1;
251
252
updateContinuityFlag();
253
return *this;
254
}
255
256
namespace
257
{
258
template <class ObjType>
259
void createContinuousImpl(int rows, int cols, int type, ObjType& obj)
260
{
261
const int area = rows * cols;
262
263
if (obj.empty() || obj.type() != type || !obj.isContinuous() || obj.size().area() != area)
264
obj.create(1, area, type);
265
266
obj = obj.reshape(obj.channels(), rows);
267
}
268
}
269
270
void cv::cuda::createContinuous(int rows, int cols, int type, OutputArray arr)
271
{
272
switch (arr.kind())
273
{
274
case _InputArray::MAT:
275
::createContinuousImpl(rows, cols, type, arr.getMatRef());
276
break;
277
278
case _InputArray::CUDA_GPU_MAT:
279
::createContinuousImpl(rows, cols, type, arr.getGpuMatRef());
280
break;
281
282
case _InputArray::CUDA_HOST_MEM:
283
::createContinuousImpl(rows, cols, type, arr.getHostMemRef());
284
break;
285
286
default:
287
arr.create(rows, cols, type);
288
}
289
}
290
291
namespace
292
{
293
template <class ObjType>
294
void ensureSizeIsEnoughImpl(int rows, int cols, int type, ObjType& obj)
295
{
296
if (obj.empty() || obj.type() != type || obj.data != obj.datastart)
297
{
298
obj.create(rows, cols, type);
299
}
300
else
301
{
302
const size_t esz = obj.elemSize();
303
const ptrdiff_t delta2 = obj.dataend - obj.datastart;
304
305
const size_t minstep = obj.cols * esz;
306
307
Size wholeSize;
308
wholeSize.height = std::max(static_cast<int>((delta2 - minstep) / static_cast<size_t>(obj.step) + 1), obj.rows);
309
wholeSize.width = std::max(static_cast<int>((delta2 - static_cast<size_t>(obj.step) * (wholeSize.height - 1)) / esz), obj.cols);
310
311
if (wholeSize.height < rows || wholeSize.width < cols)
312
{
313
obj.create(rows, cols, type);
314
}
315
else
316
{
317
obj.cols = cols;
318
obj.rows = rows;
319
}
320
}
321
}
322
}
323
324
void cv::cuda::ensureSizeIsEnough(int rows, int cols, int type, OutputArray arr)
325
{
326
switch (arr.kind())
327
{
328
case _InputArray::MAT:
329
::ensureSizeIsEnoughImpl(rows, cols, type, arr.getMatRef());
330
break;
331
332
case _InputArray::CUDA_GPU_MAT:
333
::ensureSizeIsEnoughImpl(rows, cols, type, arr.getGpuMatRef());
334
break;
335
336
case _InputArray::CUDA_HOST_MEM:
337
::ensureSizeIsEnoughImpl(rows, cols, type, arr.getHostMemRef());
338
break;
339
340
default:
341
arr.create(rows, cols, type);
342
}
343
}
344
345
GpuMat cv::cuda::getInputMat(InputArray _src, Stream& stream)
346
{
347
#ifndef HAVE_CUDA
348
CV_UNUSED(_src);
349
CV_UNUSED(stream);
350
throw_no_cuda();
351
#else
352
GpuMat src;
353
if (_src.kind() == _InputArray::CUDA_GPU_MAT)
354
{
355
src = _src.getGpuMat();
356
}
357
else if (!_src.empty())
358
{
359
BufferPool pool(stream);
360
src = pool.getBuffer(_src.size(), _src.type());
361
src.upload(_src, stream);
362
}
363
return src;
364
#endif
365
}
366
367
GpuMat cv::cuda::getOutputMat(OutputArray _dst, int rows, int cols, int type, Stream& stream)
368
{
369
#ifndef HAVE_CUDA
370
CV_UNUSED(_dst);
371
CV_UNUSED(rows);
372
CV_UNUSED(cols);
373
CV_UNUSED(type);
374
CV_UNUSED(stream);
375
throw_no_cuda();
376
#else
377
GpuMat dst;
378
if (_dst.kind() == _InputArray::CUDA_GPU_MAT)
379
{
380
_dst.create(rows, cols, type);
381
dst = _dst.getGpuMat();
382
}
383
else
384
{
385
BufferPool pool(stream);
386
dst = pool.getBuffer(rows, cols, type);
387
}
388
return dst;
389
#endif
390
}
391
392
void cv::cuda::syncOutput(const GpuMat& dst, OutputArray _dst, Stream& stream)
393
{
394
#ifndef HAVE_CUDA
395
CV_UNUSED(dst);
396
CV_UNUSED(_dst);
397
CV_UNUSED(stream);
398
throw_no_cuda();
399
#else
400
if (_dst.kind() != _InputArray::CUDA_GPU_MAT)
401
{
402
if (stream)
403
dst.download(_dst, stream);
404
else
405
dst.download(_dst);
406
}
407
#endif
408
}
409
410
#ifndef HAVE_CUDA
411
412
GpuMat::Allocator* cv::cuda::GpuMat::defaultAllocator()
413
{
414
return 0;
415
}
416
417
void cv::cuda::GpuMat::setDefaultAllocator(Allocator* allocator)
418
{
419
CV_UNUSED(allocator);
420
throw_no_cuda();
421
}
422
423
void cv::cuda::GpuMat::create(int _rows, int _cols, int _type)
424
{
425
CV_UNUSED(_rows);
426
CV_UNUSED(_cols);
427
CV_UNUSED(_type);
428
throw_no_cuda();
429
}
430
431
void cv::cuda::GpuMat::release()
432
{
433
}
434
435
void cv::cuda::GpuMat::upload(InputArray arr)
436
{
437
CV_UNUSED(arr);
438
throw_no_cuda();
439
}
440
441
void cv::cuda::GpuMat::upload(InputArray arr, Stream& _stream)
442
{
443
CV_UNUSED(arr);
444
CV_UNUSED(_stream);
445
throw_no_cuda();
446
}
447
448
void cv::cuda::GpuMat::download(OutputArray _dst) const
449
{
450
CV_UNUSED(_dst);
451
throw_no_cuda();
452
}
453
454
void cv::cuda::GpuMat::download(OutputArray _dst, Stream& _stream) const
455
{
456
CV_UNUSED(_dst);
457
CV_UNUSED(_stream);
458
throw_no_cuda();
459
}
460
461
void cv::cuda::GpuMat::copyTo(OutputArray _dst) const
462
{
463
CV_UNUSED(_dst);
464
throw_no_cuda();
465
}
466
467
void cv::cuda::GpuMat::copyTo(OutputArray _dst, Stream& _stream) const
468
{
469
CV_UNUSED(_dst);
470
CV_UNUSED(_stream);
471
throw_no_cuda();
472
}
473
474
void cv::cuda::GpuMat::copyTo(OutputArray _dst, InputArray _mask, Stream& _stream) const
475
{
476
CV_UNUSED(_dst);
477
CV_UNUSED(_mask);
478
CV_UNUSED(_stream);
479
throw_no_cuda();
480
}
481
482
GpuMat& cv::cuda::GpuMat::setTo(Scalar s, Stream& _stream)
483
{
484
CV_UNUSED(s);
485
CV_UNUSED(_stream);
486
throw_no_cuda();
487
}
488
489
GpuMat& cv::cuda::GpuMat::setTo(Scalar s, InputArray _mask, Stream& _stream)
490
{
491
CV_UNUSED(s);
492
CV_UNUSED(_mask);
493
CV_UNUSED(_stream);
494
throw_no_cuda();
495
}
496
497
void cv::cuda::GpuMat::convertTo(OutputArray _dst, int rtype, Stream& _stream) const
498
{
499
CV_UNUSED(_dst);
500
CV_UNUSED(rtype);
501
CV_UNUSED(_stream);
502
throw_no_cuda();
503
}
504
505
void cv::cuda::GpuMat::convertTo(OutputArray _dst, int rtype, double alpha, double beta, Stream& _stream) const
506
{
507
CV_UNUSED(_dst);
508
CV_UNUSED(rtype);
509
CV_UNUSED(alpha);
510
CV_UNUSED(beta);
511
CV_UNUSED(_stream);
512
throw_no_cuda();
513
}
514
515
#endif
516
517