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
// Copyright (C) 2013, OpenCV Foundation, all rights reserved.
16
// Third party copyrights are property of their respective owners.
18
// Redistribution and use in source and binary forms, with or without modification,
19
// are permitted provided that the following conditions are met:
21
// * Redistribution's of source code must retain the above copyright notice,
22
// this list of conditions and the following disclaimer.
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.
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.
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.
44
#include "precomp.hpp"
47
using namespace cv::cuda;
49
cv::cuda::GpuMat::GpuMat(int rows_, int cols_, int type_, void* data_, size_t step_) :
50
flags(Mat::MAGIC_VAL + (type_ & Mat::TYPE_MASK)), rows(rows_), cols(cols_),
51
step(step_), data((uchar*)data_), refcount(0),
52
datastart((uchar*)data_), dataend((const uchar*)data_),
53
allocator(defaultAllocator())
55
size_t minstep = cols * elemSize();
57
if (step == Mat::AUTO_STEP)
60
flags |= Mat::CONTINUOUS_FLAG;
67
CV_DbgAssert( step >= minstep );
69
flags |= step == minstep ? Mat::CONTINUOUS_FLAG : 0;
72
dataend += step * (rows - 1) + minstep;
75
cv::cuda::GpuMat::GpuMat(Size size_, int type_, void* data_, size_t step_) :
76
flags(Mat::MAGIC_VAL + (type_ & Mat::TYPE_MASK)), rows(size_.height), cols(size_.width),
77
step(step_), data((uchar*)data_), refcount(0),
78
datastart((uchar*)data_), dataend((const uchar*)data_),
79
allocator(defaultAllocator())
81
size_t minstep = cols * elemSize();
83
if (step == Mat::AUTO_STEP)
86
flags |= Mat::CONTINUOUS_FLAG;
93
CV_DbgAssert( step >= minstep );
95
flags |= step == minstep ? Mat::CONTINUOUS_FLAG : 0;
98
dataend += step * (rows - 1) + minstep;
101
cv::cuda::GpuMat::GpuMat(const GpuMat& m, Range rowRange_, Range colRange_)
104
step = m.step; refcount = m.refcount;
105
data = m.data; datastart = m.datastart; dataend = m.dataend;
106
allocator = m.allocator;
108
if (rowRange_ == Range::all())
114
CV_Assert( 0 <= rowRange_.start && rowRange_.start <= rowRange_.end && rowRange_.end <= m.rows );
116
rows = rowRange_.size();
117
data += step*rowRange_.start;
120
if (colRange_ == Range::all())
126
CV_Assert( 0 <= colRange_.start && colRange_.start <= colRange_.end && colRange_.end <= m.cols );
128
cols = colRange_.size();
129
data += colRange_.start*elemSize();
130
flags &= cols < m.cols ? ~Mat::CONTINUOUS_FLAG : -1;
134
flags |= Mat::CONTINUOUS_FLAG;
137
CV_XADD(refcount, 1);
139
if (rows <= 0 || cols <= 0)
143
cv::cuda::GpuMat::GpuMat(const GpuMat& m, Rect roi) :
144
flags(m.flags), rows(roi.height), cols(roi.width),
145
step(m.step), data(m.data + roi.y*step), refcount(m.refcount),
146
datastart(m.datastart), dataend(m.dataend),
147
allocator(m.allocator)
149
flags &= roi.width < m.cols ? ~Mat::CONTINUOUS_FLAG : -1;
150
data += roi.x * elemSize();
152
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 );
155
CV_XADD(refcount, 1);
157
if (rows <= 0 || cols <= 0)
161
GpuMat cv::cuda::GpuMat::reshape(int new_cn, int new_rows) const
169
int total_width = cols * cn;
171
if ((new_cn > total_width || total_width % new_cn != 0) && new_rows == 0)
172
new_rows = rows * total_width / new_cn;
174
if (new_rows != 0 && new_rows != rows)
176
int total_size = total_width * rows;
179
CV_Error(cv::Error::BadStep, "The matrix is not continuous, thus its number of rows can not be changed");
181
if ((unsigned)new_rows > (unsigned)total_size)
182
CV_Error(cv::Error::StsOutOfRange, "Bad new number of rows");
184
total_width = total_size / new_rows;
186
if (total_width * new_rows != total_size)
187
CV_Error(cv::Error::StsBadArg, "The total number of matrix elements is not divisible by the new number of rows");
190
hdr.step = total_width * elemSize1();
193
int new_width = total_width / new_cn;
195
if (new_width * new_cn != total_width)
196
CV_Error(cv::Error::BadNumChannels, "The total width is not divisible by the new number of channels");
198
hdr.cols = new_width;
199
hdr.flags = (hdr.flags & ~CV_MAT_CN_MASK) | ((new_cn - 1) << CV_CN_SHIFT);
204
void cv::cuda::GpuMat::locateROI(Size& wholeSize, Point& ofs) const
206
CV_DbgAssert( step > 0 );
208
size_t esz = elemSize();
209
ptrdiff_t delta1 = data - datastart;
210
ptrdiff_t delta2 = dataend - datastart;
218
ofs.y = static_cast<int>(delta1 / step);
219
ofs.x = static_cast<int>((delta1 - step * ofs.y) / esz);
221
CV_DbgAssert( data == datastart + ofs.y * step + ofs.x * esz );
224
size_t minstep = (ofs.x + cols) * esz;
226
wholeSize.height = std::max(static_cast<int>((delta2 - minstep) / step + 1), ofs.y + rows);
227
wholeSize.width = std::max(static_cast<int>((delta2 - step * (wholeSize.height - 1)) / esz), ofs.x + cols);
230
GpuMat& cv::cuda::GpuMat::adjustROI(int dtop, int dbottom, int dleft, int dright)
234
locateROI(wholeSize, ofs);
236
size_t esz = elemSize();
238
int row1 = std::max(ofs.y - dtop, 0);
239
int row2 = std::min(ofs.y + rows + dbottom, wholeSize.height);
241
int col1 = std::max(ofs.x - dleft, 0);
242
int col2 = std::min(ofs.x + cols + dright, wholeSize.width);
244
data += (row1 - ofs.y) * step + (col1 - ofs.x) * esz;
248
if (esz * cols == step || rows == 1)
249
flags |= Mat::CONTINUOUS_FLAG;
251
flags &= ~Mat::CONTINUOUS_FLAG;
258
template <class ObjType>
259
void createContinuousImpl(int rows, int cols, int type, ObjType& obj)
261
const int area = rows * cols;
263
if (obj.empty() || obj.type() != type || !obj.isContinuous() || obj.size().area() != area)
264
obj.create(1, area, type);
266
obj = obj.reshape(obj.channels(), rows);
270
void cv::cuda::createContinuous(int rows, int cols, int type, OutputArray arr)
274
case _InputArray::MAT:
275
::createContinuousImpl(rows, cols, type, arr.getMatRef());
278
case _InputArray::CUDA_GPU_MAT:
279
::createContinuousImpl(rows, cols, type, arr.getGpuMatRef());
282
case _InputArray::CUDA_HOST_MEM:
283
::createContinuousImpl(rows, cols, type, arr.getHostMemRef());
287
arr.create(rows, cols, type);
293
template <class ObjType>
294
void ensureSizeIsEnoughImpl(int rows, int cols, int type, ObjType& obj)
296
if (obj.empty() || obj.type() != type || obj.data != obj.datastart)
298
obj.create(rows, cols, type);
302
const size_t esz = obj.elemSize();
303
const ptrdiff_t delta2 = obj.dataend - obj.datastart;
305
const size_t minstep = obj.cols * esz;
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);
311
if (wholeSize.height < rows || wholeSize.width < cols)
313
obj.create(rows, cols, type);
324
void cv::cuda::ensureSizeIsEnough(int rows, int cols, int type, OutputArray arr)
328
case _InputArray::MAT:
329
::ensureSizeIsEnoughImpl(rows, cols, type, arr.getMatRef());
332
case _InputArray::CUDA_GPU_MAT:
333
::ensureSizeIsEnoughImpl(rows, cols, type, arr.getGpuMatRef());
336
case _InputArray::CUDA_HOST_MEM:
337
::ensureSizeIsEnoughImpl(rows, cols, type, arr.getHostMemRef());
341
arr.create(rows, cols, type);
345
GpuMat cv::cuda::getInputMat(InputArray _src, Stream& stream)
354
if (_src.kind() == _InputArray::CUDA_GPU_MAT)
356
src = _src.getGpuMat();
358
else if (!_src.empty())
360
BufferPool pool(stream);
361
src = pool.getBuffer(_src.size(), _src.type());
362
src.upload(_src, stream);
369
GpuMat cv::cuda::getOutputMat(OutputArray _dst, int rows, int cols, int type, Stream& stream)
381
if (_dst.kind() == _InputArray::CUDA_GPU_MAT)
383
_dst.create(rows, cols, type);
384
dst = _dst.getGpuMat();
388
BufferPool pool(stream);
389
dst = pool.getBuffer(rows, cols, type);
396
void cv::cuda::syncOutput(const GpuMat& dst, OutputArray _dst, Stream& stream)
404
if (_dst.kind() != _InputArray::CUDA_GPU_MAT)
407
dst.download(_dst, stream);
416
GpuMat::Allocator* cv::cuda::GpuMat::defaultAllocator()
421
void cv::cuda::GpuMat::setDefaultAllocator(Allocator* allocator)
427
void cv::cuda::GpuMat::create(int _rows, int _cols, int _type)
435
void cv::cuda::GpuMat::release()
439
void cv::cuda::GpuMat::upload(InputArray arr)
445
void cv::cuda::GpuMat::upload(InputArray arr, Stream& _stream)
452
void cv::cuda::GpuMat::download(OutputArray _dst) const
458
void cv::cuda::GpuMat::download(OutputArray _dst, Stream& _stream) const
465
void cv::cuda::GpuMat::copyTo(OutputArray _dst) const
471
void cv::cuda::GpuMat::copyTo(OutputArray _dst, Stream& _stream) const
478
void cv::cuda::GpuMat::copyTo(OutputArray _dst, InputArray _mask, Stream& _stream) const
486
GpuMat& cv::cuda::GpuMat::setTo(Scalar s, Stream& _stream)
494
GpuMat& cv::cuda::GpuMat::setTo(Scalar s, InputArray _mask, Stream& _stream)
503
void cv::cuda::GpuMat::convertTo(OutputArray _dst, int rtype, Stream& _stream) const
511
void cv::cuda::GpuMat::convertTo(OutputArray _dst, int rtype, double alpha, double beta, Stream& _stream) const