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