CLAHE Python bindings
[profile/ivi/opencv.git] / modules / ocl / include / opencv2 / ocl / matrix_operations.hpp
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) 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.
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 oclMaterials 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 #ifndef __OPENCV_GPU_MATRIX_OPERATIONS_HPP__
45 #define __OPENCV_GPU_MATRIX_OPERATIONS_HPP__
46
47 namespace cv
48 {
49
50     namespace ocl
51     {
52
53         enum
54         {
55             MAT_ADD = 1,
56             MAT_SUB,
57             MAT_MUL,
58             MAT_DIV,
59             MAT_NOT,
60             MAT_AND,
61             MAT_OR,
62             MAT_XOR
63         };
64
65         class CV_EXPORTS oclMatExpr
66         {
67             public:
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;
73
74             protected:
75                 oclMat a, b;
76                 int op;
77         };
78         ////////////////////////////////////////////////////////////////////////
79         //////////////////////////////// oclMat ////////////////////////////////
80         ////////////////////////////////////////////////////////////////////////
81
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) {}
83
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)
85         {
86             if( _rows > 0 && _cols > 0 )
87                 create( _rows, _cols, _type );
88         }
89
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)
91         {
92             if( _size.height > 0 && _size.width > 0 )
93                 create( _size.height, _size.width, _type );
94         }
95
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)
98         {
99             if(_rows > 0 && _cols > 0)
100             {
101                 create(_rows, _cols, _type);
102                 *this = _s;
103             }
104         }
105
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)
108         {
109             if( _size.height > 0 && _size.width > 0 )
110             {
111                 create( _size.height, _size.width, _type );
112                 *this = _s;
113             }
114         }
115
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)
119         {
120             if( refcount )
121                 CV_XADD(refcount, 1);
122         }
123
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)
127         {
128             cv::Mat m(_rows, _cols, _type, _data, _step);
129             upload(m);
130             //size_t minstep = cols * elemSize();
131             //if( step == Mat::AUTO_STEP )
132             //{
133             //    step = minstep;
134             //    flags |= Mat::CONTINUOUS_FLAG;
135             //}
136             //else
137             //{
138             //    if( rows == 1 ) step = minstep;
139             //    CV_DbgAssert( step >= minstep );
140             //    flags |= step == minstep ? Mat::CONTINUOUS_FLAG : 0;
141             //}
142             //dataend += step * (rows - 1) + minstep;
143         }
144
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)
149         {
150             cv::Mat m(_size, _type, _data, _step);
151             upload(m);
152             //size_t minstep = cols * elemSize();
153             //if( step == Mat::AUTO_STEP )
154             //{
155             //    step = minstep;
156             //    flags |= Mat::CONTINUOUS_FLAG;
157             //}
158             //else
159             //{
160             //    if( rows == 1 ) step = minstep;
161             //    CV_DbgAssert( step >= minstep );
162             //    flags |= step == minstep ? Mat::CONTINUOUS_FLAG : 0;
163             //}
164             //dataend += step * (rows - 1) + minstep;
165         }
166
167
168         inline oclMat::oclMat(const oclMat &m, const Range &rRange, const Range &cRange)
169         {
170             flags = m.flags;
171             step = m.step;
172             refcount = m.refcount;
173             data = m.data;
174             datastart = m.datastart;
175             dataend = m.dataend;
176             wholerows = m.wholerows;
177             wholecols = m.wholecols;
178             offset = m.offset;
179             if( rRange == Range::all() )
180                 rows = m.rows;
181             else
182             {
183                 CV_Assert( 0 <= rRange.start && rRange.start <= rRange.end && rRange.end <= m.rows );
184                 rows = rRange.size();
185                 offset += step * rRange.start;
186             }
187
188             if( cRange == Range::all() )
189                 cols = m.cols;
190             else
191             {
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;
196             }
197
198             if( rows == 1 )
199                 flags |= Mat::CONTINUOUS_FLAG;
200
201             if( refcount )
202                 CV_XADD(refcount, 1);
203             if( rows <= 0 || cols <= 0 )
204                 rows = cols = 0;
205         }
206
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)
211         {
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.cols &&
215                        0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows );
216             if( refcount )
217                 CV_XADD(refcount, 1);
218             if( rows <= 0 || cols <= 0 )
219                 rows = cols = 0;
220         }
221
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)
224         {
225             //clCxt = Context::getContext();
226             upload(m);
227         }
228
229         inline oclMat::~oclMat()
230         {
231             release();
232         }
233
234         inline oclMat &oclMat::operator = (const oclMat &m)
235         {
236             if( this != &m )
237             {
238                 if( m.refcount )
239                     CV_XADD(m.refcount, 1);
240                 release();
241                 clCxt = m.clCxt;
242                 flags = m.flags;
243                 rows = m.rows;
244                 cols = m.cols;
245                 step = m.step;
246                 data = m.data;
247                 datastart = m.datastart;
248                 dataend = m.dataend;
249                 offset = m.offset;
250                 wholerows = m.wholerows;
251                 wholecols = m.wholecols;
252                 refcount = m.refcount;
253             }
254             return *this;
255         }
256
257         inline oclMat &oclMat::operator = (const Mat &m)
258         {
259             //clCxt = Context::getContext();
260             upload(m);
261             return *this;
262         }
263
264         inline oclMat& oclMat::operator = (const oclMatExpr& expr)
265         {
266             expr.assign(*this);
267             return *this;
268         }
269
270         /* Fixme! To be supported in OpenCL later. */
271 #if 0
272         template <class T> inline oclMat::operator DevMem2D_<T>() const
273         {
274             return DevMem2D_<T>(rows, cols, (T *)data, step);
275         }
276         template <class T> inline oclMat::operator PtrStep_<T>() const
277         {
278             return PtrStep_<T>(static_cast< DevMem2D_<T> >(*this));
279         }
280 #endif
281
282         //CPP: void oclMat::upload(const Mat& m);
283
284         inline oclMat::operator Mat() const
285         {
286             Mat m;
287             download(m);
288             return m;
289         }
290
291         //CPP void oclMat::download(cv::Mat& m) const;
292
293         inline oclMat oclMat::row(int y) const
294         {
295             return oclMat(*this, Range(y, y + 1), Range::all());
296         }
297         inline oclMat oclMat::col(int x) const
298         {
299             return oclMat(*this, Range::all(), Range(x, x + 1));
300         }
301         inline oclMat oclMat::rowRange(int startrow, int endrow) const
302         {
303             return oclMat(*this, Range(startrow, endrow), Range::all());
304         }
305         inline oclMat oclMat::rowRange(const Range &r) const
306         {
307             return oclMat(*this, r, Range::all());
308         }
309         inline oclMat oclMat::colRange(int startcol, int endcol) const
310         {
311             return oclMat(*this, Range::all(), Range(startcol, endcol));
312         }
313         inline oclMat oclMat::colRange(const Range &r) const
314         {
315             return oclMat(*this, Range::all(), r);
316         }
317
318         inline oclMat oclMat::clone() const
319         {
320             oclMat m;
321             copyTo(m);
322             return m;
323         }
324
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;
328
329         inline void oclMat::assignTo( oclMat &m, int mtype ) const
330         {
331             if( mtype < 0 )
332                 m = *this;
333             else
334                 convertTo(m, mtype);
335         }
336
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)
341         {
342             create(_size.height, _size.width, _type);
343         }
344         //CPP void oclMat::create(int _rows, int _cols, int _type);
345         //CPP void oclMat::release();
346
347         inline void oclMat::swap(oclMat &b)
348         {
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 );
361         }
362
363         inline void oclMat::locateROI( Size &wholeSize, Point &ofs ) const
364         {
365             size_t esz = elemSize();//, minstep;
366             //ptrdiff_t delta1 = offset;//, delta2 = dataend - datastart;
367             CV_DbgAssert( step > 0 );
368             if( offset == 0 )
369                 ofs.x = ofs.y = 0;
370             else
371             {
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 );
375             }
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;
383         }
384
385         inline oclMat &oclMat::adjustROI( int dtop, int dbottom, int dleft, int dright )
386         {
387             Size wholeSize;
388             Point ofs;
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;
394             rows = row2 - row1;
395             cols = col2 - col1;
396             if( esz * cols == step || rows == 1 )
397                 flags |= Mat::CONTINUOUS_FLAG;
398             else
399                 flags &= ~Mat::CONTINUOUS_FLAG;
400             return *this;
401         }
402
403         inline oclMat oclMat::operator()( Range rRange, Range cRange ) const
404         {
405             return oclMat(*this, rRange, cRange);
406         }
407         inline oclMat oclMat::operator()( const Rect &roi ) const
408         {
409             return oclMat(*this, roi);
410         }
411
412         inline bool oclMat::isContinuous() const
413         {
414             return (flags & Mat::CONTINUOUS_FLAG) != 0;
415         }
416         inline size_t oclMat::elemSize() const
417         {
418             return CV_ELEM_SIZE((CV_MAKE_TYPE(type(), oclchannels())));
419         }
420         inline size_t oclMat::elemSize1() const
421         {
422             return CV_ELEM_SIZE1(flags);
423         }
424         inline int oclMat::type() const
425         {
426             return CV_MAT_TYPE(flags);
427         }
428         inline int oclMat::ocltype() const
429         {
430             return CV_MAKE_TYPE(depth(), oclchannels());
431         }
432         inline int oclMat::depth() const
433         {
434             return CV_MAT_DEPTH(flags);
435         }
436         inline int oclMat::channels() const
437         {
438             return CV_MAT_CN(flags);
439         }
440         inline int oclMat::oclchannels() const
441         {
442             return (CV_MAT_CN(flags)) == 3 ? 4 : (CV_MAT_CN(flags));
443         }
444         inline size_t oclMat::step1() const
445         {
446             return step / elemSize1();
447         }
448         inline Size oclMat::size() const
449         {
450             return Size(cols, rows);
451         }
452         inline bool oclMat::empty() const
453         {
454             return data == 0;
455         }
456
457
458
459         inline uchar *oclMat::ptr(int y)
460         {
461             CV_DbgAssert( (unsigned)y < (unsigned)rows );
462             CV_Error(CV_GpuNotSupported, "This function hasn't been supported yet.\n");
463             return data + step * y;
464         }
465
466         inline const uchar *oclMat::ptr(int y) const
467         {
468             CV_DbgAssert( (unsigned)y < (unsigned)rows );
469             CV_Error(CV_GpuNotSupported, "This function hasn't been supported yet.\n");
470             return data + step * y;
471         }
472
473         template<typename _Tp> inline _Tp *oclMat::ptr(int y)
474         {
475             CV_DbgAssert( (unsigned)y < (unsigned)rows );
476             CV_Error(CV_GpuNotSupported, "This function hasn't been supported yet.\n");
477             return (_Tp *)(data + step * y);
478         }
479
480         template<typename _Tp> inline const _Tp *oclMat::ptr(int y) const
481         {
482             CV_DbgAssert( (unsigned)y < (unsigned)rows );
483             CV_Error(CV_GpuNotSupported, "This function hasn't been supported yet.\n");
484             return (const _Tp *)(data + step * y);
485         }
486
487         inline oclMat oclMat::t() const
488         {
489             oclMat tmp;
490             transpose(*this, tmp);
491             return tmp;
492         }
493
494         static inline void swap( oclMat &a, oclMat &b )
495         {
496             a.swap(b);
497         }
498
499         inline void ensureSizeIsEnough(int rows, int cols, int type, oclMat &m)
500         {
501             if (m.type() == type && m.rows >= rows && m.cols >= cols)
502                 m = m(Rect(0, 0, cols, rows));
503             else
504                 m.create(rows, cols, type);
505         }
506
507         inline void ensureSizeIsEnough(Size size, int type, oclMat &m)
508         {
509             ensureSizeIsEnough(size.height, size.width, type, m);
510         }
511
512
513     } /* end of namespace ocl */
514
515 } /* end of namespace cv */
516
517 #endif /* __OPENCV_GPU_MATRIX_OPERATIONS_HPP__ */