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) 2010-2012, Institute Of Software Chinese Academy Of Science, all rights reserved.
14 // Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.
15 // Copyright (C) 2010-2012, Multicoreware, Inc., 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 oclMaterials 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 #ifndef __OPENCV_OCL_MATRIX_OPERATIONS_HPP__
45 #define __OPENCV_OCL_MATRIX_OPERATIONS_HPP__
65 class CV_EXPORTS oclMatExpr
68 oclMatExpr() : a(oclMat()), b(oclMat()), op(0) {}
69 oclMatExpr(const oclMat& _a, const oclMat& _b, int _op)
70 : a(_a), b(_b), op(_op) {}
71 operator oclMat() const;
72 void assign(oclMat& m) const;
78 ////////////////////////////////////////////////////////////////////////
79 //////////////////////////////// oclMat ////////////////////////////////
80 ////////////////////////////////////////////////////////////////////////
82 inline oclMat::oclMat() : flags(0), rows(0), cols(0), step(0), data(0), refcount(0), datastart(0), dataend(0), offset(0), wholerows(0), wholecols(0) {}
84 inline oclMat::oclMat(int _rows, int _cols, int _type) : flags(0), rows(0), cols(0), step(0), data(0), refcount(0), datastart(0), dataend(0), offset(0), wholerows(0), wholecols(0)
86 if( _rows > 0 && _cols > 0 )
87 create( _rows, _cols, _type );
90 inline oclMat::oclMat(Size _size, int _type) : flags(0), rows(0), cols(0), step(0), data(0), refcount(0), datastart(0), dataend(0), offset(0), wholerows(0), wholecols(0)
92 if( _size.height > 0 && _size.width > 0 )
93 create( _size.height, _size.width, _type );
96 inline oclMat::oclMat(int _rows, int _cols, int _type, const Scalar &_s)
97 : flags(0), rows(0), cols(0), step(0), data(0), refcount(0), datastart(0), dataend(0), offset(0), wholerows(0), wholecols(0)
99 if(_rows > 0 && _cols > 0)
101 create(_rows, _cols, _type);
106 inline oclMat::oclMat(Size _size, int _type, const Scalar &_s)
107 : flags(0), rows(0), cols(0), step(0), data(0), refcount(0), datastart(0), dataend(0), offset(0), wholerows(0), wholecols(0)
109 if( _size.height > 0 && _size.width > 0 )
111 create( _size.height, _size.width, _type );
116 inline oclMat::oclMat(const oclMat &m)
117 : flags(m.flags), rows(m.rows), cols(m.cols), step(m.step), data(m.data),
118 refcount(m.refcount), datastart(m.datastart), dataend(m.dataend), clCxt(m.clCxt), offset(m.offset), wholerows(m.wholerows), wholecols(m.wholecols)
121 CV_XADD(refcount, 1);
124 inline oclMat::oclMat(int _rows, int _cols, int _type, void *_data, size_t _step)
125 : flags(0), rows(0), cols(0), step(0), data(0), refcount(0),
126 datastart(0), dataend(0), offset(0), wholerows(0), wholecols(0)
128 cv::Mat m(_rows, _cols, _type, _data, _step);
130 //size_t minstep = cols * elemSize();
131 //if( step == Mat::AUTO_STEP )
134 // flags |= Mat::CONTINUOUS_FLAG;
138 // if( rows == 1 ) step = minstep;
139 // CV_DbgAssert( step >= minstep );
140 // flags |= step == minstep ? Mat::CONTINUOUS_FLAG : 0;
142 //dataend += step * (rows - 1) + minstep;
145 inline oclMat::oclMat(Size _size, int _type, void *_data, size_t _step)
146 : flags(0), rows(0), cols(0),
147 step(0), data(0), refcount(0),
148 datastart(0), dataend(0), offset(0), wholerows(0), wholecols(0)
150 cv::Mat m(_size, _type, _data, _step);
152 //size_t minstep = cols * elemSize();
153 //if( step == Mat::AUTO_STEP )
156 // flags |= Mat::CONTINUOUS_FLAG;
160 // if( rows == 1 ) step = minstep;
161 // CV_DbgAssert( step >= minstep );
162 // flags |= step == minstep ? Mat::CONTINUOUS_FLAG : 0;
164 //dataend += step * (rows - 1) + minstep;
168 inline oclMat::oclMat(const oclMat &m, const Range &rRange, const Range &cRange)
172 refcount = m.refcount;
174 datastart = m.datastart;
176 wholerows = m.wholerows;
177 wholecols = m.wholecols;
179 if( rRange == Range::all() )
183 CV_Assert( 0 <= rRange.start && rRange.start <= rRange.end && rRange.end <= m.rows );
184 rows = rRange.size();
185 offset += step * rRange.start;
188 if( cRange == Range::all() )
192 CV_Assert( 0 <= cRange.start && cRange.start <= cRange.end && cRange.end <= m.cols );
193 cols = cRange.size();
194 offset += cRange.start * elemSize();
195 flags &= cols < m.cols ? ~Mat::CONTINUOUS_FLAG : -1;
199 flags |= Mat::CONTINUOUS_FLAG;
202 CV_XADD(refcount, 1);
203 if( rows <= 0 || cols <= 0 )
207 inline oclMat::oclMat(const oclMat &m, const Rect &roi)
208 : flags(m.flags), rows(roi.height), cols(roi.width),
209 step(m.step), data(m.data), refcount(m.refcount),
210 datastart(m.datastart), dataend(m.dataend), clCxt(m.clCxt), offset(m.offset), wholerows(m.wholerows), wholecols(m.wholecols)
212 flags &= roi.width < m.cols ? ~Mat::CONTINUOUS_FLAG : -1;
213 offset += roi.y * step + roi.x * elemSize();
214 CV_Assert( 0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.wholecols &&
215 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.wholerows );
217 CV_XADD(refcount, 1);
218 if( rows <= 0 || cols <= 0 )
222 inline oclMat::oclMat(const Mat &m)
223 : flags(0), rows(0), cols(0), step(0), data(0), refcount(0), datastart(0), dataend(0) , offset(0), wholerows(0), wholecols(0)
225 //clCxt = Context::getContext();
229 inline oclMat::~oclMat()
234 inline oclMat &oclMat::operator = (const oclMat &m)
239 CV_XADD(m.refcount, 1);
247 datastart = m.datastart;
250 wholerows = m.wholerows;
251 wholecols = m.wholecols;
252 refcount = m.refcount;
257 inline oclMat &oclMat::operator = (const Mat &m)
259 //clCxt = Context::getContext();
264 inline oclMat& oclMat::operator = (const oclMatExpr& expr)
270 /* Fixme! To be supported in OpenCL later. */
272 template <class T> inline oclMat::operator DevMem2D_<T>() const
274 return DevMem2D_<T>(rows, cols, (T *)data, step);
276 template <class T> inline oclMat::operator PtrStep_<T>() const
278 return PtrStep_<T>(static_cast< DevMem2D_<T> >(*this));
282 //CPP: void oclMat::upload(const Mat& m);
284 inline oclMat::operator Mat() const
291 //CPP void oclMat::download(cv::Mat& m) const;
293 inline oclMat oclMat::row(int y) const
295 return oclMat(*this, Range(y, y + 1), Range::all());
297 inline oclMat oclMat::col(int x) const
299 return oclMat(*this, Range::all(), Range(x, x + 1));
301 inline oclMat oclMat::rowRange(int startrow, int endrow) const
303 return oclMat(*this, Range(startrow, endrow), Range::all());
305 inline oclMat oclMat::rowRange(const Range &r) const
307 return oclMat(*this, r, Range::all());
309 inline oclMat oclMat::colRange(int startcol, int endcol) const
311 return oclMat(*this, Range::all(), Range(startcol, endcol));
313 inline oclMat oclMat::colRange(const Range &r) const
315 return oclMat(*this, Range::all(), r);
318 inline oclMat oclMat::clone() const
325 //CPP void oclMat::copyTo( oclMat& m ) const;
326 //CPP void oclMat::copyTo( oclMat& m, const oclMat& mask ) const;
327 //CPP void oclMat::convertTo( oclMat& m, int rtype, double alpha=1, double beta=0 ) const;
329 inline void oclMat::assignTo( oclMat &m, int mtype ) const
337 //CPP oclMat& oclMat::operator = (const Scalar& s);
338 //CPP oclMat& oclMat::setTo(const Scalar& s, const oclMat& mask=oclMat());
339 //CPP oclMat oclMat::reshape(int _cn, int _rows=0) const;
340 inline void oclMat::create(Size _size, int _type)
342 create(_size.height, _size.width, _type);
344 //CPP void oclMat::create(int _rows, int _cols, int _type);
345 //CPP void oclMat::release();
347 inline void oclMat::swap(oclMat &b)
349 std::swap( flags, b.flags );
350 std::swap( rows, b.rows );
351 std::swap( cols, b.cols );
352 std::swap( step, b.step );
353 std::swap( data, b.data );
354 std::swap( datastart, b.datastart );
355 std::swap( dataend, b.dataend );
356 std::swap( refcount, b.refcount );
357 std::swap( offset, b.offset );
358 std::swap( clCxt, b.clCxt );
359 std::swap( wholerows, b.wholerows );
360 std::swap( wholecols, b.wholecols );
363 inline void oclMat::locateROI( Size &wholeSize, Point &ofs ) const
365 size_t esz = elemSize();//, minstep;
366 //ptrdiff_t delta1 = offset;//, delta2 = dataend - datastart;
367 CV_DbgAssert( step > 0 );
372 ofs.y = (int)(offset / step);
373 ofs.x = (int)((offset - step * ofs.y) / esz);
374 //CV_DbgAssert( data == datastart + ofs.y*step + ofs.x*esz );
376 //minstep = (ofs.x + cols)*esz;
377 //wholeSize.height = (int)((delta2 - minstep)/step + 1);
378 //wholeSize.height = std::max(wholeSize.height, ofs.y + rows);
379 //wholeSize.width = (int)((delta2 - step*(wholeSize.height-1))/esz);
380 //wholeSize.width = std::max(wholeSize.width, ofs.x + cols);
381 wholeSize.height = wholerows;
382 wholeSize.width = wholecols;
385 inline oclMat &oclMat::adjustROI( int dtop, int dbottom, int dleft, int dright )
389 size_t esz = elemSize();
390 locateROI( wholeSize, ofs );
391 int row1 = std::max(ofs.y - dtop, 0), row2 = std::min(ofs.y + rows + dbottom, wholeSize.height);
392 int col1 = std::max(ofs.x - dleft, 0), col2 = std::min(ofs.x + cols + dright, wholeSize.width);
393 offset += (row1 - ofs.y) * step + (col1 - ofs.x) * esz;
396 if( esz * cols == step || rows == 1 )
397 flags |= Mat::CONTINUOUS_FLAG;
399 flags &= ~Mat::CONTINUOUS_FLAG;
403 inline oclMat oclMat::operator()( Range rRange, Range cRange ) const
405 return oclMat(*this, rRange, cRange);
407 inline oclMat oclMat::operator()( const Rect &roi ) const
409 return oclMat(*this, roi);
412 inline bool oclMat::isContinuous() const
414 return (flags & Mat::CONTINUOUS_FLAG) != 0;
416 inline size_t oclMat::elemSize() const
418 return CV_ELEM_SIZE((CV_MAKE_TYPE(type(), oclchannels())));
420 inline size_t oclMat::elemSize1() const
422 return CV_ELEM_SIZE1(flags);
424 inline int oclMat::type() const
426 return CV_MAT_TYPE(flags);
428 inline int oclMat::ocltype() const
430 return CV_MAKE_TYPE(depth(), oclchannels());
432 inline int oclMat::depth() const
434 return CV_MAT_DEPTH(flags);
436 inline int oclMat::channels() const
438 return CV_MAT_CN(flags);
440 inline int oclMat::oclchannels() const
442 return (CV_MAT_CN(flags)) == 3 ? 4 : (CV_MAT_CN(flags));
444 inline size_t oclMat::step1() const
446 return step / elemSize1();
448 inline Size oclMat::size() const
450 return Size(cols, rows);
452 inline bool oclMat::empty() const
459 inline uchar *oclMat::ptr(int y)
461 CV_DbgAssert( (unsigned)y < (unsigned)rows );
462 CV_Error(Error::GpuNotSupported, "This function hasn't been supported yet.\n");
463 return data + step * y;
466 inline const uchar *oclMat::ptr(int y) const
468 CV_DbgAssert( (unsigned)y < (unsigned)rows );
469 CV_Error(Error::GpuNotSupported, "This function hasn't been supported yet.\n");
470 return data + step * y;
473 template<typename _Tp> inline _Tp *oclMat::ptr(int y)
475 CV_DbgAssert( (unsigned)y < (unsigned)rows );
476 CV_Error(Error::GpuNotSupported, "This function hasn't been supported yet.\n");
477 return (_Tp *)(data + step * y);
480 template<typename _Tp> inline const _Tp *oclMat::ptr(int y) const
482 CV_DbgAssert( (unsigned)y < (unsigned)rows );
483 CV_Error(Error::GpuNotSupported, "This function hasn't been supported yet.\n");
484 return (const _Tp *)(data + step * y);
487 inline oclMat oclMat::t() const
490 transpose(*this, tmp);
494 static inline void swap( oclMat &a, oclMat &b )
499 inline void ensureSizeIsEnough(int rows, int cols, int type, oclMat &m)
501 if (m.type() == type && m.rows >= rows && m.cols >= cols)
502 m = m(Rect(0, 0, cols, rows));
504 m.create(rows, cols, type);
507 inline void ensureSizeIsEnough(Size size, int type, oclMat &m)
509 ensureSizeIsEnough(size.height, size.width, type, m);
513 } /* end of namespace ocl */
515 } /* end of namespace cv */
517 #endif /* __OPENCV_OCL_MATRIX_OPERATIONS_HPP__ */