~paparazzi-uav/paparazzi/v5.0-manual

« back to all changes in this revision

Viewing changes to sw/ext/opencv_bebop/opencv/modules/core/include/opencv2/core/mat.inl.hpp

  • Committer: Paparazzi buildbot
  • Date: 2016-05-18 15:00:29 UTC
  • Revision ID: felix.ruess+docbot@gmail.com-20160518150029-e8lgzi5kvb4p7un9
Manual import commit 4b8bbb730080dac23cf816b98908dacfabe2a8ec from v5.0 branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
// Copyright (C) 2015, Itseez Inc., all rights reserved.
 
17
// Third party copyrights are property of their respective owners.
 
18
//
 
19
// Redistribution and use in source and binary forms, with or without modification,
 
20
// are permitted provided that the following conditions are met:
 
21
//
 
22
//   * Redistribution's of source code must retain the above copyright notice,
 
23
//     this list of conditions and the following disclaimer.
 
24
//
 
25
//   * Redistribution's in binary form must reproduce the above copyright notice,
 
26
//     this list of conditions and the following disclaimer in the documentation
 
27
//     and/or other materials provided with the distribution.
 
28
//
 
29
//   * The name of the copyright holders may not be used to endorse or promote products
 
30
//     derived from this software without specific prior written permission.
 
31
//
 
32
// This software is provided by the copyright holders and contributors "as is" and
 
33
// any express or implied warranties, including, but not limited to, the implied
 
34
// warranties of merchantability and fitness for a particular purpose are disclaimed.
 
35
// In no event shall the Intel Corporation or contributors be liable for any direct,
 
36
// indirect, incidental, special, exemplary, or consequential damages
 
37
// (including, but not limited to, procurement of substitute goods or services;
 
38
// loss of use, data, or profits; or business interruption) however caused
 
39
// and on any theory of liability, whether in contract, strict liability,
 
40
// or tort (including negligence or otherwise) arising in any way out of
 
41
// the use of this software, even if advised of the possibility of such damage.
 
42
//
 
43
//M*/
 
44
 
 
45
#ifndef __OPENCV_CORE_MATRIX_OPERATIONS_HPP__
 
46
#define __OPENCV_CORE_MATRIX_OPERATIONS_HPP__
 
47
 
 
48
#ifndef __cplusplus
 
49
#  error mat.inl.hpp header must be compiled as C++
 
50
#endif
 
51
 
 
52
namespace cv
 
53
{
 
54
 
 
55
//! @cond IGNORED
 
56
 
 
57
//////////////////////// Input/Output Arrays ////////////////////////
 
58
 
 
59
inline void _InputArray::init(int _flags, const void* _obj)
 
60
{ flags = _flags; obj = (void*)_obj; }
 
61
 
 
62
inline void _InputArray::init(int _flags, const void* _obj, Size _sz)
 
63
{ flags = _flags; obj = (void*)_obj; sz = _sz; }
 
64
 
 
65
inline void* _InputArray::getObj() const { return obj; }
 
66
inline int _InputArray::getFlags() const { return flags; }
 
67
inline Size _InputArray::getSz() const { return sz; }
 
68
 
 
69
inline _InputArray::_InputArray() { init(NONE, 0); }
 
70
inline _InputArray::_InputArray(int _flags, void* _obj) { init(_flags, _obj); }
 
71
inline _InputArray::_InputArray(const Mat& m) { init(MAT+ACCESS_READ, &m); }
 
72
inline _InputArray::_InputArray(const std::vector<Mat>& vec) { init(STD_VECTOR_MAT+ACCESS_READ, &vec); }
 
73
inline _InputArray::_InputArray(const UMat& m) { init(UMAT+ACCESS_READ, &m); }
 
74
inline _InputArray::_InputArray(const std::vector<UMat>& vec) { init(STD_VECTOR_UMAT+ACCESS_READ, &vec); }
 
75
 
 
76
template<typename _Tp> inline
 
77
_InputArray::_InputArray(const std::vector<_Tp>& vec)
 
78
{ init(FIXED_TYPE + STD_VECTOR + DataType<_Tp>::type + ACCESS_READ, &vec); }
 
79
 
 
80
inline
 
81
_InputArray::_InputArray(const std::vector<bool>& vec)
 
82
{ init(FIXED_TYPE + STD_BOOL_VECTOR + DataType<bool>::type + ACCESS_READ, &vec); }
 
83
 
 
84
template<typename _Tp> inline
 
85
_InputArray::_InputArray(const std::vector<std::vector<_Tp> >& vec)
 
86
{ init(FIXED_TYPE + STD_VECTOR_VECTOR + DataType<_Tp>::type + ACCESS_READ, &vec); }
 
87
 
 
88
template<typename _Tp> inline
 
89
_InputArray::_InputArray(const std::vector<Mat_<_Tp> >& vec)
 
90
{ init(FIXED_TYPE + STD_VECTOR_MAT + DataType<_Tp>::type + ACCESS_READ, &vec); }
 
91
 
 
92
template<typename _Tp, int m, int n> inline
 
93
_InputArray::_InputArray(const Matx<_Tp, m, n>& mtx)
 
94
{ init(FIXED_TYPE + FIXED_SIZE + MATX + DataType<_Tp>::type + ACCESS_READ, &mtx, Size(n, m)); }
 
95
 
 
96
template<typename _Tp> inline
 
97
_InputArray::_InputArray(const _Tp* vec, int n)
 
98
{ init(FIXED_TYPE + FIXED_SIZE + MATX + DataType<_Tp>::type + ACCESS_READ, vec, Size(n, 1)); }
 
99
 
 
100
template<typename _Tp> inline
 
101
_InputArray::_InputArray(const Mat_<_Tp>& m)
 
102
{ init(FIXED_TYPE + MAT + DataType<_Tp>::type + ACCESS_READ, &m); }
 
103
 
 
104
inline _InputArray::_InputArray(const double& val)
 
105
{ init(FIXED_TYPE + FIXED_SIZE + MATX + CV_64F + ACCESS_READ, &val, Size(1,1)); }
 
106
 
 
107
inline _InputArray::_InputArray(const MatExpr& expr)
 
108
{ init(FIXED_TYPE + FIXED_SIZE + EXPR + ACCESS_READ, &expr); }
 
109
 
 
110
inline _InputArray::_InputArray(const cuda::GpuMat& d_mat)
 
111
{ init(CUDA_GPU_MAT + ACCESS_READ, &d_mat); }
 
112
 
 
113
inline _InputArray::_InputArray(const std::vector<cuda::GpuMat>& d_mat)
 
114
{       init(STD_VECTOR_CUDA_GPU_MAT + ACCESS_READ, &d_mat);}
 
115
 
 
116
inline _InputArray::_InputArray(const ogl::Buffer& buf)
 
117
{ init(OPENGL_BUFFER + ACCESS_READ, &buf); }
 
118
 
 
119
inline _InputArray::_InputArray(const cuda::HostMem& cuda_mem)
 
120
{ init(CUDA_HOST_MEM + ACCESS_READ, &cuda_mem); }
 
121
 
 
122
inline _InputArray::~_InputArray() {}
 
123
 
 
124
inline Mat _InputArray::getMat(int i) const
 
125
{
 
126
    if( kind() == MAT && i < 0 )
 
127
        return *(const Mat*)obj;
 
128
    return getMat_(i);
 
129
}
 
130
 
 
131
inline bool _InputArray::isMat() const { return kind() == _InputArray::MAT; }
 
132
inline bool _InputArray::isUMat() const  { return kind() == _InputArray::UMAT; }
 
133
inline bool _InputArray::isMatVector() const { return kind() == _InputArray::STD_VECTOR_MAT; }
 
134
inline bool _InputArray::isUMatVector() const  { return kind() == _InputArray::STD_VECTOR_UMAT; }
 
135
inline bool _InputArray::isMatx() const { return kind() == _InputArray::MATX; }
 
136
inline bool _InputArray::isVector() const { return kind() == _InputArray::STD_VECTOR || kind() == _InputArray::STD_BOOL_VECTOR; }
 
137
inline bool _InputArray::isGpuMatVector() const { return kind() == _InputArray::STD_VECTOR_CUDA_GPU_MAT; }
 
138
 
 
139
////////////////////////////////////////////////////////////////////////////////////////
 
140
 
 
141
inline _OutputArray::_OutputArray() { init(ACCESS_WRITE, 0); }
 
142
inline _OutputArray::_OutputArray(int _flags, void* _obj) { init(_flags|ACCESS_WRITE, _obj); }
 
143
inline _OutputArray::_OutputArray(Mat& m) { init(MAT+ACCESS_WRITE, &m); }
 
144
inline _OutputArray::_OutputArray(std::vector<Mat>& vec) { init(STD_VECTOR_MAT+ACCESS_WRITE, &vec); }
 
145
inline _OutputArray::_OutputArray(UMat& m) { init(UMAT+ACCESS_WRITE, &m); }
 
146
inline _OutputArray::_OutputArray(std::vector<UMat>& vec) { init(STD_VECTOR_UMAT+ACCESS_WRITE, &vec); }
 
147
 
 
148
template<typename _Tp> inline
 
149
_OutputArray::_OutputArray(std::vector<_Tp>& vec)
 
150
{ init(FIXED_TYPE + STD_VECTOR + DataType<_Tp>::type + ACCESS_WRITE, &vec); }
 
151
 
 
152
inline
 
153
_OutputArray::_OutputArray(std::vector<bool>&)
 
154
{ CV_Error(Error::StsUnsupportedFormat, "std::vector<bool> cannot be an output array\n"); }
 
155
 
 
156
template<typename _Tp> inline
 
157
_OutputArray::_OutputArray(std::vector<std::vector<_Tp> >& vec)
 
158
{ init(FIXED_TYPE + STD_VECTOR_VECTOR + DataType<_Tp>::type + ACCESS_WRITE, &vec); }
 
159
 
 
160
template<typename _Tp> inline
 
161
_OutputArray::_OutputArray(std::vector<Mat_<_Tp> >& vec)
 
162
{ init(FIXED_TYPE + STD_VECTOR_MAT + DataType<_Tp>::type + ACCESS_WRITE, &vec); }
 
163
 
 
164
template<typename _Tp> inline
 
165
_OutputArray::_OutputArray(Mat_<_Tp>& m)
 
166
{ init(FIXED_TYPE + MAT + DataType<_Tp>::type + ACCESS_WRITE, &m); }
 
167
 
 
168
template<typename _Tp, int m, int n> inline
 
169
_OutputArray::_OutputArray(Matx<_Tp, m, n>& mtx)
 
170
{ init(FIXED_TYPE + FIXED_SIZE + MATX + DataType<_Tp>::type + ACCESS_WRITE, &mtx, Size(n, m)); }
 
171
 
 
172
template<typename _Tp> inline
 
173
_OutputArray::_OutputArray(_Tp* vec, int n)
 
174
{ init(FIXED_TYPE + FIXED_SIZE + MATX + DataType<_Tp>::type + ACCESS_WRITE, vec, Size(n, 1)); }
 
175
 
 
176
template<typename _Tp> inline
 
177
_OutputArray::_OutputArray(const std::vector<_Tp>& vec)
 
178
{ init(FIXED_TYPE + FIXED_SIZE + STD_VECTOR + DataType<_Tp>::type + ACCESS_WRITE, &vec); }
 
179
 
 
180
template<typename _Tp> inline
 
181
_OutputArray::_OutputArray(const std::vector<std::vector<_Tp> >& vec)
 
182
{ init(FIXED_TYPE + FIXED_SIZE + STD_VECTOR_VECTOR + DataType<_Tp>::type + ACCESS_WRITE, &vec); }
 
183
 
 
184
template<typename _Tp> inline
 
185
_OutputArray::_OutputArray(const std::vector<Mat_<_Tp> >& vec)
 
186
{ init(FIXED_TYPE + FIXED_SIZE + STD_VECTOR_MAT + DataType<_Tp>::type + ACCESS_WRITE, &vec); }
 
187
 
 
188
template<typename _Tp> inline
 
189
_OutputArray::_OutputArray(const Mat_<_Tp>& m)
 
190
{ init(FIXED_TYPE + FIXED_SIZE + MAT + DataType<_Tp>::type + ACCESS_WRITE, &m); }
 
191
 
 
192
template<typename _Tp, int m, int n> inline
 
193
_OutputArray::_OutputArray(const Matx<_Tp, m, n>& mtx)
 
194
{ init(FIXED_TYPE + FIXED_SIZE + MATX + DataType<_Tp>::type + ACCESS_WRITE, &mtx, Size(n, m)); }
 
195
 
 
196
template<typename _Tp> inline
 
197
_OutputArray::_OutputArray(const _Tp* vec, int n)
 
198
{ init(FIXED_TYPE + FIXED_SIZE + MATX + DataType<_Tp>::type + ACCESS_WRITE, vec, Size(n, 1)); }
 
199
 
 
200
inline _OutputArray::_OutputArray(cuda::GpuMat& d_mat)
 
201
{ init(CUDA_GPU_MAT + ACCESS_WRITE, &d_mat); }
 
202
 
 
203
inline _OutputArray::_OutputArray(std::vector<cuda::GpuMat>& d_mat)
 
204
{       init(STD_VECTOR_CUDA_GPU_MAT + ACCESS_WRITE, &d_mat);}
 
205
 
 
206
inline _OutputArray::_OutputArray(ogl::Buffer& buf)
 
207
{ init(OPENGL_BUFFER + ACCESS_WRITE, &buf); }
 
208
 
 
209
inline _OutputArray::_OutputArray(cuda::HostMem& cuda_mem)
 
210
{ init(CUDA_HOST_MEM + ACCESS_WRITE, &cuda_mem); }
 
211
 
 
212
inline _OutputArray::_OutputArray(const Mat& m)
 
213
{ init(FIXED_TYPE + FIXED_SIZE + MAT + ACCESS_WRITE, &m); }
 
214
 
 
215
inline _OutputArray::_OutputArray(const std::vector<Mat>& vec)
 
216
{ init(FIXED_SIZE + STD_VECTOR_MAT + ACCESS_WRITE, &vec); }
 
217
 
 
218
inline _OutputArray::_OutputArray(const UMat& m)
 
219
{ init(FIXED_TYPE + FIXED_SIZE + UMAT + ACCESS_WRITE, &m); }
 
220
 
 
221
inline _OutputArray::_OutputArray(const std::vector<UMat>& vec)
 
222
{ init(FIXED_SIZE + STD_VECTOR_UMAT + ACCESS_WRITE, &vec); }
 
223
 
 
224
inline _OutputArray::_OutputArray(const cuda::GpuMat& d_mat)
 
225
{ init(FIXED_TYPE + FIXED_SIZE + CUDA_GPU_MAT + ACCESS_WRITE, &d_mat); }
 
226
 
 
227
 
 
228
inline _OutputArray::_OutputArray(const ogl::Buffer& buf)
 
229
{ init(FIXED_TYPE + FIXED_SIZE + OPENGL_BUFFER + ACCESS_WRITE, &buf); }
 
230
 
 
231
inline _OutputArray::_OutputArray(const cuda::HostMem& cuda_mem)
 
232
{ init(FIXED_TYPE + FIXED_SIZE + CUDA_HOST_MEM + ACCESS_WRITE, &cuda_mem); }
 
233
 
 
234
///////////////////////////////////////////////////////////////////////////////////////////
 
235
 
 
236
inline _InputOutputArray::_InputOutputArray() { init(ACCESS_RW, 0); }
 
237
inline _InputOutputArray::_InputOutputArray(int _flags, void* _obj) { init(_flags|ACCESS_RW, _obj); }
 
238
inline _InputOutputArray::_InputOutputArray(Mat& m) { init(MAT+ACCESS_RW, &m); }
 
239
inline _InputOutputArray::_InputOutputArray(std::vector<Mat>& vec) { init(STD_VECTOR_MAT+ACCESS_RW, &vec); }
 
240
inline _InputOutputArray::_InputOutputArray(UMat& m) { init(UMAT+ACCESS_RW, &m); }
 
241
inline _InputOutputArray::_InputOutputArray(std::vector<UMat>& vec) { init(STD_VECTOR_UMAT+ACCESS_RW, &vec); }
 
242
 
 
243
template<typename _Tp> inline
 
244
_InputOutputArray::_InputOutputArray(std::vector<_Tp>& vec)
 
245
{ init(FIXED_TYPE + STD_VECTOR + DataType<_Tp>::type + ACCESS_RW, &vec); }
 
246
 
 
247
inline _InputOutputArray::_InputOutputArray(std::vector<bool>&)
 
248
{ CV_Error(Error::StsUnsupportedFormat, "std::vector<bool> cannot be an input/output array\n"); }
 
249
 
 
250
template<typename _Tp> inline
 
251
_InputOutputArray::_InputOutputArray(std::vector<std::vector<_Tp> >& vec)
 
252
{ init(FIXED_TYPE + STD_VECTOR_VECTOR + DataType<_Tp>::type + ACCESS_RW, &vec); }
 
253
 
 
254
template<typename _Tp> inline
 
255
_InputOutputArray::_InputOutputArray(std::vector<Mat_<_Tp> >& vec)
 
256
{ init(FIXED_TYPE + STD_VECTOR_MAT + DataType<_Tp>::type + ACCESS_RW, &vec); }
 
257
 
 
258
template<typename _Tp> inline
 
259
_InputOutputArray::_InputOutputArray(Mat_<_Tp>& m)
 
260
{ init(FIXED_TYPE + MAT + DataType<_Tp>::type + ACCESS_RW, &m); }
 
261
 
 
262
template<typename _Tp, int m, int n> inline
 
263
_InputOutputArray::_InputOutputArray(Matx<_Tp, m, n>& mtx)
 
264
{ init(FIXED_TYPE + FIXED_SIZE + MATX + DataType<_Tp>::type + ACCESS_RW, &mtx, Size(n, m)); }
 
265
 
 
266
template<typename _Tp> inline
 
267
_InputOutputArray::_InputOutputArray(_Tp* vec, int n)
 
268
{ init(FIXED_TYPE + FIXED_SIZE + MATX + DataType<_Tp>::type + ACCESS_RW, vec, Size(n, 1)); }
 
269
 
 
270
template<typename _Tp> inline
 
271
_InputOutputArray::_InputOutputArray(const std::vector<_Tp>& vec)
 
272
{ init(FIXED_TYPE + FIXED_SIZE + STD_VECTOR + DataType<_Tp>::type + ACCESS_RW, &vec); }
 
273
 
 
274
template<typename _Tp> inline
 
275
_InputOutputArray::_InputOutputArray(const std::vector<std::vector<_Tp> >& vec)
 
276
{ init(FIXED_TYPE + FIXED_SIZE + STD_VECTOR_VECTOR + DataType<_Tp>::type + ACCESS_RW, &vec); }
 
277
 
 
278
template<typename _Tp> inline
 
279
_InputOutputArray::_InputOutputArray(const std::vector<Mat_<_Tp> >& vec)
 
280
{ init(FIXED_TYPE + FIXED_SIZE + STD_VECTOR_MAT + DataType<_Tp>::type + ACCESS_RW, &vec); }
 
281
 
 
282
template<typename _Tp> inline
 
283
_InputOutputArray::_InputOutputArray(const Mat_<_Tp>& m)
 
284
{ init(FIXED_TYPE + FIXED_SIZE + MAT + DataType<_Tp>::type + ACCESS_RW, &m); }
 
285
 
 
286
template<typename _Tp, int m, int n> inline
 
287
_InputOutputArray::_InputOutputArray(const Matx<_Tp, m, n>& mtx)
 
288
{ init(FIXED_TYPE + FIXED_SIZE + MATX + DataType<_Tp>::type + ACCESS_RW, &mtx, Size(n, m)); }
 
289
 
 
290
template<typename _Tp> inline
 
291
_InputOutputArray::_InputOutputArray(const _Tp* vec, int n)
 
292
{ init(FIXED_TYPE + FIXED_SIZE + MATX + DataType<_Tp>::type + ACCESS_RW, vec, Size(n, 1)); }
 
293
 
 
294
inline _InputOutputArray::_InputOutputArray(cuda::GpuMat& d_mat)
 
295
{ init(CUDA_GPU_MAT + ACCESS_RW, &d_mat); }
 
296
 
 
297
inline _InputOutputArray::_InputOutputArray(ogl::Buffer& buf)
 
298
{ init(OPENGL_BUFFER + ACCESS_RW, &buf); }
 
299
 
 
300
inline _InputOutputArray::_InputOutputArray(cuda::HostMem& cuda_mem)
 
301
{ init(CUDA_HOST_MEM + ACCESS_RW, &cuda_mem); }
 
302
 
 
303
inline _InputOutputArray::_InputOutputArray(const Mat& m)
 
304
{ init(FIXED_TYPE + FIXED_SIZE + MAT + ACCESS_RW, &m); }
 
305
 
 
306
inline _InputOutputArray::_InputOutputArray(const std::vector<Mat>& vec)
 
307
{ init(FIXED_SIZE + STD_VECTOR_MAT + ACCESS_RW, &vec); }
 
308
 
 
309
inline _InputOutputArray::_InputOutputArray(const UMat& m)
 
310
{ init(FIXED_TYPE + FIXED_SIZE + UMAT + ACCESS_RW, &m); }
 
311
 
 
312
inline _InputOutputArray::_InputOutputArray(const std::vector<UMat>& vec)
 
313
{ init(FIXED_SIZE + STD_VECTOR_UMAT + ACCESS_RW, &vec); }
 
314
 
 
315
inline _InputOutputArray::_InputOutputArray(const cuda::GpuMat& d_mat)
 
316
{ init(FIXED_TYPE + FIXED_SIZE + CUDA_GPU_MAT + ACCESS_RW, &d_mat); }
 
317
inline _InputOutputArray::_InputOutputArray(const std::vector<cuda::GpuMat>& d_mat)
 
318
{       init(FIXED_TYPE + FIXED_SIZE + STD_VECTOR_CUDA_GPU_MAT + ACCESS_RW, &d_mat);}
 
319
 
 
320
inline _InputOutputArray::_InputOutputArray(const ogl::Buffer& buf)
 
321
{ init(FIXED_TYPE + FIXED_SIZE + OPENGL_BUFFER + ACCESS_RW, &buf); }
 
322
 
 
323
inline _InputOutputArray::_InputOutputArray(const cuda::HostMem& cuda_mem)
 
324
{ init(FIXED_TYPE + FIXED_SIZE + CUDA_HOST_MEM + ACCESS_RW, &cuda_mem); }
 
325
 
 
326
//////////////////////////////////////////// Mat //////////////////////////////////////////
 
327
 
 
328
inline
 
329
Mat::Mat()
 
330
    : flags(MAGIC_VAL), dims(0), rows(0), cols(0), data(0), datastart(0), dataend(0),
 
331
      datalimit(0), allocator(0), u(0), size(&rows)
 
332
{}
 
333
 
 
334
inline
 
335
Mat::Mat(int _rows, int _cols, int _type)
 
336
    : flags(MAGIC_VAL), dims(0), rows(0), cols(0), data(0), datastart(0), dataend(0),
 
337
      datalimit(0), allocator(0), u(0), size(&rows)
 
338
{
 
339
    create(_rows, _cols, _type);
 
340
}
 
341
 
 
342
inline
 
343
Mat::Mat(int _rows, int _cols, int _type, const Scalar& _s)
 
344
    : flags(MAGIC_VAL), dims(0), rows(0), cols(0), data(0), datastart(0), dataend(0),
 
345
      datalimit(0), allocator(0), u(0), size(&rows)
 
346
{
 
347
    create(_rows, _cols, _type);
 
348
    *this = _s;
 
349
}
 
350
 
 
351
inline
 
352
Mat::Mat(Size _sz, int _type)
 
353
    : flags(MAGIC_VAL), dims(0), rows(0), cols(0), data(0), datastart(0), dataend(0),
 
354
      datalimit(0), allocator(0), u(0), size(&rows)
 
355
{
 
356
    create( _sz.height, _sz.width, _type );
 
357
}
 
358
 
 
359
inline
 
360
Mat::Mat(Size _sz, int _type, const Scalar& _s)
 
361
    : flags(MAGIC_VAL), dims(0), rows(0), cols(0), data(0), datastart(0), dataend(0),
 
362
      datalimit(0), allocator(0), u(0), size(&rows)
 
363
{
 
364
    create(_sz.height, _sz.width, _type);
 
365
    *this = _s;
 
366
}
 
367
 
 
368
inline
 
369
Mat::Mat(int _dims, const int* _sz, int _type)
 
370
    : flags(MAGIC_VAL), dims(0), rows(0), cols(0), data(0), datastart(0), dataend(0),
 
371
      datalimit(0), allocator(0), u(0), size(&rows)
 
372
{
 
373
    create(_dims, _sz, _type);
 
374
}
 
375
 
 
376
inline
 
377
Mat::Mat(int _dims, const int* _sz, int _type, const Scalar& _s)
 
378
    : flags(MAGIC_VAL), dims(0), rows(0), cols(0), data(0), datastart(0), dataend(0),
 
379
      datalimit(0), allocator(0), u(0), size(&rows)
 
380
{
 
381
    create(_dims, _sz, _type);
 
382
    *this = _s;
 
383
}
 
384
 
 
385
inline
 
386
Mat::Mat(const Mat& m)
 
387
    : flags(m.flags), dims(m.dims), rows(m.rows), cols(m.cols), data(m.data),
 
388
      datastart(m.datastart), dataend(m.dataend), datalimit(m.datalimit), allocator(m.allocator),
 
389
      u(m.u), size(&rows)
 
390
{
 
391
    if( u )
 
392
        CV_XADD(&u->refcount, 1);
 
393
    if( m.dims <= 2 )
 
394
    {
 
395
        step[0] = m.step[0]; step[1] = m.step[1];
 
396
    }
 
397
    else
 
398
    {
 
399
        dims = 0;
 
400
        copySize(m);
 
401
    }
 
402
}
 
403
 
 
404
inline
 
405
Mat::Mat(int _rows, int _cols, int _type, void* _data, size_t _step)
 
406
    : flags(MAGIC_VAL + (_type & TYPE_MASK)), dims(2), rows(_rows), cols(_cols),
 
407
      data((uchar*)_data), datastart((uchar*)_data), dataend(0), datalimit(0),
 
408
      allocator(0), u(0), size(&rows)
 
409
{
 
410
    CV_Assert(total() == 0 || data != NULL);
 
411
 
 
412
    size_t esz = CV_ELEM_SIZE(_type), esz1 = CV_ELEM_SIZE1(_type);
 
413
    size_t minstep = cols * esz;
 
414
    if( _step == AUTO_STEP )
 
415
    {
 
416
        _step = minstep;
 
417
        flags |= CONTINUOUS_FLAG;
 
418
    }
 
419
    else
 
420
    {
 
421
        if( rows == 1 ) _step = minstep;
 
422
        CV_DbgAssert( _step >= minstep );
 
423
 
 
424
        if (_step % esz1 != 0)
 
425
        {
 
426
            CV_Error(Error::BadStep, "Step must be a multiple of esz1");
 
427
        }
 
428
 
 
429
        flags |= _step == minstep ? CONTINUOUS_FLAG : 0;
 
430
    }
 
431
    step[0] = _step;
 
432
    step[1] = esz;
 
433
    datalimit = datastart + _step * rows;
 
434
    dataend = datalimit - _step + minstep;
 
435
}
 
436
 
 
437
inline
 
438
Mat::Mat(Size _sz, int _type, void* _data, size_t _step)
 
439
    : flags(MAGIC_VAL + (_type & TYPE_MASK)), dims(2), rows(_sz.height), cols(_sz.width),
 
440
      data((uchar*)_data), datastart((uchar*)_data), dataend(0), datalimit(0),
 
441
      allocator(0), u(0), size(&rows)
 
442
{
 
443
    CV_Assert(total() == 0 || data != NULL);
 
444
 
 
445
    size_t esz = CV_ELEM_SIZE(_type), esz1 = CV_ELEM_SIZE1(_type);
 
446
    size_t minstep = cols*esz;
 
447
    if( _step == AUTO_STEP )
 
448
    {
 
449
        _step = minstep;
 
450
        flags |= CONTINUOUS_FLAG;
 
451
    }
 
452
    else
 
453
    {
 
454
        if( rows == 1 ) _step = minstep;
 
455
        CV_DbgAssert( _step >= minstep );
 
456
 
 
457
        if (_step % esz1 != 0)
 
458
        {
 
459
            CV_Error(Error::BadStep, "Step must be a multiple of esz1");
 
460
        }
 
461
 
 
462
        flags |= _step == minstep ? CONTINUOUS_FLAG : 0;
 
463
    }
 
464
    step[0] = _step;
 
465
    step[1] = esz;
 
466
    datalimit = datastart + _step*rows;
 
467
    dataend = datalimit - _step + minstep;
 
468
}
 
469
 
 
470
template<typename _Tp> inline
 
471
Mat::Mat(const std::vector<_Tp>& vec, bool copyData)
 
472
    : flags(MAGIC_VAL | DataType<_Tp>::type | CV_MAT_CONT_FLAG), dims(2), rows((int)vec.size()),
 
473
      cols(1), data(0), datastart(0), dataend(0), allocator(0), u(0), size(&rows)
 
474
{
 
475
    if(vec.empty())
 
476
        return;
 
477
    if( !copyData )
 
478
    {
 
479
        step[0] = step[1] = sizeof(_Tp);
 
480
        datastart = data = (uchar*)&vec[0];
 
481
        datalimit = dataend = datastart + rows * step[0];
 
482
    }
 
483
    else
 
484
        Mat((int)vec.size(), 1, DataType<_Tp>::type, (uchar*)&vec[0]).copyTo(*this);
 
485
}
 
486
 
 
487
template<typename _Tp, int n> inline
 
488
Mat::Mat(const Vec<_Tp, n>& vec, bool copyData)
 
489
    : flags(MAGIC_VAL | DataType<_Tp>::type | CV_MAT_CONT_FLAG), dims(2), rows(n), cols(1), data(0),
 
490
      datastart(0), dataend(0), allocator(0), u(0), size(&rows)
 
491
{
 
492
    if( !copyData )
 
493
    {
 
494
        step[0] = step[1] = sizeof(_Tp);
 
495
        datastart = data = (uchar*)vec.val;
 
496
        datalimit = dataend = datastart + rows * step[0];
 
497
    }
 
498
    else
 
499
        Mat(n, 1, DataType<_Tp>::type, (void*)vec.val).copyTo(*this);
 
500
}
 
501
 
 
502
 
 
503
template<typename _Tp, int m, int n> inline
 
504
Mat::Mat(const Matx<_Tp,m,n>& M, bool copyData)
 
505
    : flags(MAGIC_VAL | DataType<_Tp>::type | CV_MAT_CONT_FLAG), dims(2), rows(m), cols(n), data(0),
 
506
      datastart(0), dataend(0), allocator(0), u(0), size(&rows)
 
507
{
 
508
    if( !copyData )
 
509
    {
 
510
        step[0] = cols * sizeof(_Tp);
 
511
        step[1] = sizeof(_Tp);
 
512
        datastart = data = (uchar*)M.val;
 
513
        datalimit = dataend = datastart + rows * step[0];
 
514
    }
 
515
    else
 
516
        Mat(m, n, DataType<_Tp>::type, (uchar*)M.val).copyTo(*this);
 
517
}
 
518
 
 
519
template<typename _Tp> inline
 
520
Mat::Mat(const Point_<_Tp>& pt, bool copyData)
 
521
    : flags(MAGIC_VAL | DataType<_Tp>::type | CV_MAT_CONT_FLAG), dims(2), rows(2), cols(1), data(0),
 
522
      datastart(0), dataend(0), allocator(0), u(0), size(&rows)
 
523
{
 
524
    if( !copyData )
 
525
    {
 
526
        step[0] = step[1] = sizeof(_Tp);
 
527
        datastart = data = (uchar*)&pt.x;
 
528
        datalimit = dataend = datastart + rows * step[0];
 
529
    }
 
530
    else
 
531
    {
 
532
        create(2, 1, DataType<_Tp>::type);
 
533
        ((_Tp*)data)[0] = pt.x;
 
534
        ((_Tp*)data)[1] = pt.y;
 
535
    }
 
536
}
 
537
 
 
538
template<typename _Tp> inline
 
539
Mat::Mat(const Point3_<_Tp>& pt, bool copyData)
 
540
    : flags(MAGIC_VAL | DataType<_Tp>::type | CV_MAT_CONT_FLAG), dims(2), rows(3), cols(1), data(0),
 
541
      datastart(0), dataend(0), allocator(0), u(0), size(&rows)
 
542
{
 
543
    if( !copyData )
 
544
    {
 
545
        step[0] = step[1] = sizeof(_Tp);
 
546
        datastart = data = (uchar*)&pt.x;
 
547
        datalimit = dataend = datastart + rows * step[0];
 
548
    }
 
549
    else
 
550
    {
 
551
        create(3, 1, DataType<_Tp>::type);
 
552
        ((_Tp*)data)[0] = pt.x;
 
553
        ((_Tp*)data)[1] = pt.y;
 
554
        ((_Tp*)data)[2] = pt.z;
 
555
    }
 
556
}
 
557
 
 
558
template<typename _Tp> inline
 
559
Mat::Mat(const MatCommaInitializer_<_Tp>& commaInitializer)
 
560
    : flags(MAGIC_VAL | DataType<_Tp>::type | CV_MAT_CONT_FLAG), dims(0), rows(0), cols(0), data(0),
 
561
      datastart(0), dataend(0), allocator(0), u(0), size(&rows)
 
562
{
 
563
    *this = commaInitializer.operator Mat_<_Tp>();
 
564
}
 
565
 
 
566
inline
 
567
Mat::~Mat()
 
568
{
 
569
    release();
 
570
    if( step.p != step.buf )
 
571
        fastFree(step.p);
 
572
}
 
573
 
 
574
inline
 
575
Mat& Mat::operator = (const Mat& m)
 
576
{
 
577
    if( this != &m )
 
578
    {
 
579
        if( m.u )
 
580
            CV_XADD(&m.u->refcount, 1);
 
581
        release();
 
582
        flags = m.flags;
 
583
        if( dims <= 2 && m.dims <= 2 )
 
584
        {
 
585
            dims = m.dims;
 
586
            rows = m.rows;
 
587
            cols = m.cols;
 
588
            step[0] = m.step[0];
 
589
            step[1] = m.step[1];
 
590
        }
 
591
        else
 
592
            copySize(m);
 
593
        data = m.data;
 
594
        datastart = m.datastart;
 
595
        dataend = m.dataend;
 
596
        datalimit = m.datalimit;
 
597
        allocator = m.allocator;
 
598
        u = m.u;
 
599
    }
 
600
    return *this;
 
601
}
 
602
 
 
603
inline
 
604
Mat Mat::row(int y) const
 
605
{
 
606
    return Mat(*this, Range(y, y + 1), Range::all());
 
607
}
 
608
 
 
609
inline
 
610
Mat Mat::col(int x) const
 
611
{
 
612
    return Mat(*this, Range::all(), Range(x, x + 1));
 
613
}
 
614
 
 
615
inline
 
616
Mat Mat::rowRange(int startrow, int endrow) const
 
617
{
 
618
    return Mat(*this, Range(startrow, endrow), Range::all());
 
619
}
 
620
 
 
621
inline
 
622
Mat Mat::rowRange(const Range& r) const
 
623
{
 
624
    return Mat(*this, r, Range::all());
 
625
}
 
626
 
 
627
inline
 
628
Mat Mat::colRange(int startcol, int endcol) const
 
629
{
 
630
    return Mat(*this, Range::all(), Range(startcol, endcol));
 
631
}
 
632
 
 
633
inline
 
634
Mat Mat::colRange(const Range& r) const
 
635
{
 
636
    return Mat(*this, Range::all(), r);
 
637
}
 
638
 
 
639
inline
 
640
Mat Mat::clone() const
 
641
{
 
642
    Mat m;
 
643
    copyTo(m);
 
644
    return m;
 
645
}
 
646
 
 
647
inline
 
648
void Mat::assignTo( Mat& m, int _type ) const
 
649
{
 
650
    if( _type < 0 )
 
651
        m = *this;
 
652
    else
 
653
        convertTo(m, _type);
 
654
}
 
655
 
 
656
inline
 
657
void Mat::create(int _rows, int _cols, int _type)
 
658
{
 
659
    _type &= TYPE_MASK;
 
660
    if( dims <= 2 && rows == _rows && cols == _cols && type() == _type && data )
 
661
        return;
 
662
    int sz[] = {_rows, _cols};
 
663
    create(2, sz, _type);
 
664
}
 
665
 
 
666
inline
 
667
void Mat::create(Size _sz, int _type)
 
668
{
 
669
    create(_sz.height, _sz.width, _type);
 
670
}
 
671
 
 
672
inline
 
673
void Mat::addref()
 
674
{
 
675
    if( u )
 
676
        CV_XADD(&u->refcount, 1);
 
677
}
 
678
 
 
679
inline void Mat::release()
 
680
{
 
681
    if( u && CV_XADD(&u->refcount, -1) == 1 )
 
682
        deallocate();
 
683
    u = NULL;
 
684
    datastart = dataend = datalimit = data = 0;
 
685
    for(int i = 0; i < dims; i++)
 
686
        size.p[i] = 0;
 
687
}
 
688
 
 
689
inline
 
690
Mat Mat::operator()( Range _rowRange, Range _colRange ) const
 
691
{
 
692
    return Mat(*this, _rowRange, _colRange);
 
693
}
 
694
 
 
695
inline
 
696
Mat Mat::operator()( const Rect& roi ) const
 
697
{
 
698
    return Mat(*this, roi);
 
699
}
 
700
 
 
701
inline
 
702
Mat Mat::operator()(const Range* ranges) const
 
703
{
 
704
    return Mat(*this, ranges);
 
705
}
 
706
 
 
707
inline
 
708
bool Mat::isContinuous() const
 
709
{
 
710
    return (flags & CONTINUOUS_FLAG) != 0;
 
711
}
 
712
 
 
713
inline
 
714
bool Mat::isSubmatrix() const
 
715
{
 
716
    return (flags & SUBMATRIX_FLAG) != 0;
 
717
}
 
718
 
 
719
inline
 
720
size_t Mat::elemSize() const
 
721
{
 
722
    return dims > 0 ? step.p[dims - 1] : 0;
 
723
}
 
724
 
 
725
inline
 
726
size_t Mat::elemSize1() const
 
727
{
 
728
    return CV_ELEM_SIZE1(flags);
 
729
}
 
730
 
 
731
inline
 
732
int Mat::type() const
 
733
{
 
734
    return CV_MAT_TYPE(flags);
 
735
}
 
736
 
 
737
inline
 
738
int Mat::depth() const
 
739
{
 
740
    return CV_MAT_DEPTH(flags);
 
741
}
 
742
 
 
743
inline
 
744
int Mat::channels() const
 
745
{
 
746
    return CV_MAT_CN(flags);
 
747
}
 
748
 
 
749
inline
 
750
size_t Mat::step1(int i) const
 
751
{
 
752
    return step.p[i] / elemSize1();
 
753
}
 
754
 
 
755
inline
 
756
bool Mat::empty() const
 
757
{
 
758
    return data == 0 || total() == 0;
 
759
}
 
760
 
 
761
inline
 
762
size_t Mat::total() const
 
763
{
 
764
    if( dims <= 2 )
 
765
        return (size_t)rows * cols;
 
766
    size_t p = 1;
 
767
    for( int i = 0; i < dims; i++ )
 
768
        p *= size[i];
 
769
    return p;
 
770
}
 
771
 
 
772
inline
 
773
uchar* Mat::ptr(int y)
 
774
{
 
775
    CV_DbgAssert( y == 0 || (data && dims >= 1 && (unsigned)y < (unsigned)size.p[0]) );
 
776
    return data + step.p[0] * y;
 
777
}
 
778
 
 
779
inline
 
780
const uchar* Mat::ptr(int y) const
 
781
{
 
782
    CV_DbgAssert( y == 0 || (data && dims >= 1 && (unsigned)y < (unsigned)size.p[0]) );
 
783
    return data + step.p[0] * y;
 
784
}
 
785
 
 
786
template<typename _Tp> inline
 
787
_Tp* Mat::ptr(int y)
 
788
{
 
789
    CV_DbgAssert( y == 0 || (data && dims >= 1 && (unsigned)y < (unsigned)size.p[0]) );
 
790
    return (_Tp*)(data + step.p[0] * y);
 
791
}
 
792
 
 
793
template<typename _Tp> inline
 
794
const _Tp* Mat::ptr(int y) const
 
795
{
 
796
    CV_DbgAssert( y == 0 || (data && dims >= 1 && data && (unsigned)y < (unsigned)size.p[0]) );
 
797
    return (const _Tp*)(data + step.p[0] * y);
 
798
}
 
799
 
 
800
inline
 
801
uchar* Mat::ptr(int i0, int i1)
 
802
{
 
803
    CV_DbgAssert(dims >= 2);
 
804
    CV_DbgAssert(data);
 
805
    CV_DbgAssert((unsigned)i0 < (unsigned)size.p[0]);
 
806
    CV_DbgAssert((unsigned)i1 < (unsigned)size.p[1]);
 
807
    return data + i0 * step.p[0] + i1 * step.p[1];
 
808
}
 
809
 
 
810
inline
 
811
const uchar* Mat::ptr(int i0, int i1) const
 
812
{
 
813
    CV_DbgAssert(dims >= 2);
 
814
    CV_DbgAssert(data);
 
815
    CV_DbgAssert((unsigned)i0 < (unsigned)size.p[0]);
 
816
    CV_DbgAssert((unsigned)i1 < (unsigned)size.p[1]);
 
817
    return data + i0 * step.p[0] + i1 * step.p[1];
 
818
}
 
819
 
 
820
template<typename _Tp> inline
 
821
_Tp* Mat::ptr(int i0, int i1)
 
822
{
 
823
    CV_DbgAssert(dims >= 2);
 
824
    CV_DbgAssert(data);
 
825
    CV_DbgAssert((unsigned)i0 < (unsigned)size.p[0]);
 
826
    CV_DbgAssert((unsigned)i1 < (unsigned)size.p[1]);
 
827
    return (_Tp*)(data + i0 * step.p[0] + i1 * step.p[1]);
 
828
}
 
829
 
 
830
template<typename _Tp> inline
 
831
const _Tp* Mat::ptr(int i0, int i1) const
 
832
{
 
833
    CV_DbgAssert(dims >= 2);
 
834
    CV_DbgAssert(data);
 
835
    CV_DbgAssert((unsigned)i0 < (unsigned)size.p[0]);
 
836
    CV_DbgAssert((unsigned)i1 < (unsigned)size.p[1]);
 
837
    return (const _Tp*)(data + i0 * step.p[0] + i1 * step.p[1]);
 
838
}
 
839
 
 
840
inline
 
841
uchar* Mat::ptr(int i0, int i1, int i2)
 
842
{
 
843
    CV_DbgAssert(dims >= 3);
 
844
    CV_DbgAssert(data);
 
845
    CV_DbgAssert((unsigned)i0 < (unsigned)size.p[0]);
 
846
    CV_DbgAssert((unsigned)i1 < (unsigned)size.p[1]);
 
847
    CV_DbgAssert((unsigned)i2 < (unsigned)size.p[2]);
 
848
    return data + i0 * step.p[0] + i1 * step.p[1] + i2 * step.p[2];
 
849
}
 
850
 
 
851
inline
 
852
const uchar* Mat::ptr(int i0, int i1, int i2) const
 
853
{
 
854
    CV_DbgAssert(dims >= 3);
 
855
    CV_DbgAssert(data);
 
856
    CV_DbgAssert((unsigned)i0 < (unsigned)size.p[0]);
 
857
    CV_DbgAssert((unsigned)i1 < (unsigned)size.p[1]);
 
858
    CV_DbgAssert((unsigned)i2 < (unsigned)size.p[2]);
 
859
    return data + i0 * step.p[0] + i1 * step.p[1] + i2 * step.p[2];
 
860
}
 
861
 
 
862
template<typename _Tp> inline
 
863
_Tp* Mat::ptr(int i0, int i1, int i2)
 
864
{
 
865
    CV_DbgAssert(dims >= 3);
 
866
    CV_DbgAssert(data);
 
867
    CV_DbgAssert((unsigned)i0 < (unsigned)size.p[0]);
 
868
    CV_DbgAssert((unsigned)i1 < (unsigned)size.p[1]);
 
869
    CV_DbgAssert((unsigned)i2 < (unsigned)size.p[2]);
 
870
    return (_Tp*)(data + i0 * step.p[0] + i1 * step.p[1] + i2 * step.p[2]);
 
871
}
 
872
 
 
873
template<typename _Tp> inline
 
874
const _Tp* Mat::ptr(int i0, int i1, int i2) const
 
875
{
 
876
    CV_DbgAssert(dims >= 3);
 
877
    CV_DbgAssert(data);
 
878
    CV_DbgAssert((unsigned)i0 < (unsigned)size.p[0]);
 
879
    CV_DbgAssert((unsigned)i1 < (unsigned)size.p[1]);
 
880
    CV_DbgAssert((unsigned)i2 < (unsigned)size.p[2]);
 
881
    return (const _Tp*)(data + i0 * step.p[0] + i1 * step.p[1] + i2 * step.p[2]);
 
882
}
 
883
 
 
884
inline
 
885
uchar* Mat::ptr(const int* idx)
 
886
{
 
887
    int i, d = dims;
 
888
    uchar* p = data;
 
889
    CV_DbgAssert( d >= 1 && p );
 
890
    for( i = 0; i < d; i++ )
 
891
    {
 
892
        CV_DbgAssert( (unsigned)idx[i] < (unsigned)size.p[i] );
 
893
        p += idx[i] * step.p[i];
 
894
    }
 
895
    return p;
 
896
}
 
897
 
 
898
inline
 
899
const uchar* Mat::ptr(const int* idx) const
 
900
{
 
901
    int i, d = dims;
 
902
    uchar* p = data;
 
903
    CV_DbgAssert( d >= 1 && p );
 
904
    for( i = 0; i < d; i++ )
 
905
    {
 
906
        CV_DbgAssert( (unsigned)idx[i] < (unsigned)size.p[i] );
 
907
        p += idx[i] * step.p[i];
 
908
    }
 
909
    return p;
 
910
}
 
911
 
 
912
template<typename _Tp> inline
 
913
_Tp& Mat::at(int i0, int i1)
 
914
{
 
915
    CV_DbgAssert(dims <= 2);
 
916
    CV_DbgAssert(data);
 
917
    CV_DbgAssert((unsigned)i0 < (unsigned)size.p[0]);
 
918
    CV_DbgAssert((unsigned)(i1 * DataType<_Tp>::channels) < (unsigned)(size.p[1] * channels()));
 
919
    CV_DbgAssert(CV_ELEM_SIZE1(DataType<_Tp>::depth) == elemSize1());
 
920
    return ((_Tp*)(data + step.p[0] * i0))[i1];
 
921
}
 
922
 
 
923
template<typename _Tp> inline
 
924
const _Tp& Mat::at(int i0, int i1) const
 
925
{
 
926
    CV_DbgAssert(dims <= 2);
 
927
    CV_DbgAssert(data);
 
928
    CV_DbgAssert((unsigned)i0 < (unsigned)size.p[0]);
 
929
    CV_DbgAssert((unsigned)(i1 * DataType<_Tp>::channels) < (unsigned)(size.p[1] * channels()));
 
930
    CV_DbgAssert(CV_ELEM_SIZE1(DataType<_Tp>::depth) == elemSize1());
 
931
    return ((const _Tp*)(data + step.p[0] * i0))[i1];
 
932
}
 
933
 
 
934
template<typename _Tp> inline
 
935
_Tp& Mat::at(Point pt)
 
936
{
 
937
    CV_DbgAssert(dims <= 2);
 
938
    CV_DbgAssert(data);
 
939
    CV_DbgAssert((unsigned)pt.y < (unsigned)size.p[0]);
 
940
    CV_DbgAssert((unsigned)(pt.x * DataType<_Tp>::channels) < (unsigned)(size.p[1] * channels()));
 
941
    CV_DbgAssert(CV_ELEM_SIZE1(DataType<_Tp>::depth) == elemSize1());
 
942
    return ((_Tp*)(data + step.p[0] * pt.y))[pt.x];
 
943
}
 
944
 
 
945
template<typename _Tp> inline
 
946
const _Tp& Mat::at(Point pt) const
 
947
{
 
948
    CV_DbgAssert(dims <= 2);
 
949
    CV_DbgAssert(data);
 
950
    CV_DbgAssert((unsigned)pt.y < (unsigned)size.p[0]);
 
951
    CV_DbgAssert((unsigned)(pt.x * DataType<_Tp>::channels) < (unsigned)(size.p[1] * channels()));
 
952
    CV_DbgAssert(CV_ELEM_SIZE1(DataType<_Tp>::depth) == elemSize1());
 
953
    return ((const _Tp*)(data + step.p[0] * pt.y))[pt.x];
 
954
}
 
955
 
 
956
template<typename _Tp> inline
 
957
_Tp& Mat::at(int i0)
 
958
{
 
959
    CV_DbgAssert(dims <= 2);
 
960
    CV_DbgAssert(data);
 
961
    CV_DbgAssert((unsigned)i0 < (unsigned)(size.p[0] * size.p[1]));
 
962
    CV_DbgAssert(elemSize() == CV_ELEM_SIZE(DataType<_Tp>::type));
 
963
    if( isContinuous() || size.p[0] == 1 )
 
964
        return ((_Tp*)data)[i0];
 
965
    if( size.p[1] == 1 )
 
966
        return *(_Tp*)(data + step.p[0] * i0);
 
967
    int i = i0 / cols, j = i0 - i * cols;
 
968
    return ((_Tp*)(data + step.p[0] * i))[j];
 
969
}
 
970
 
 
971
template<typename _Tp> inline
 
972
const _Tp& Mat::at(int i0) const
 
973
{
 
974
    CV_DbgAssert(dims <= 2);
 
975
    CV_DbgAssert(data);
 
976
    CV_DbgAssert((unsigned)i0 < (unsigned)(size.p[0] * size.p[1]));
 
977
    CV_DbgAssert(elemSize() == CV_ELEM_SIZE(DataType<_Tp>::type));
 
978
    if( isContinuous() || size.p[0] == 1 )
 
979
        return ((const _Tp*)data)[i0];
 
980
    if( size.p[1] == 1 )
 
981
        return *(const _Tp*)(data + step.p[0] * i0);
 
982
    int i = i0 / cols, j = i0 - i * cols;
 
983
    return ((const _Tp*)(data + step.p[0] * i))[j];
 
984
}
 
985
 
 
986
template<typename _Tp> inline
 
987
_Tp& Mat::at(int i0, int i1, int i2)
 
988
{
 
989
    CV_DbgAssert( elemSize() == CV_ELEM_SIZE(DataType<_Tp>::type) );
 
990
    return *(_Tp*)ptr(i0, i1, i2);
 
991
}
 
992
 
 
993
template<typename _Tp> inline
 
994
const _Tp& Mat::at(int i0, int i1, int i2) const
 
995
{
 
996
    CV_DbgAssert( elemSize() == CV_ELEM_SIZE(DataType<_Tp>::type) );
 
997
    return *(const _Tp*)ptr(i0, i1, i2);
 
998
}
 
999
 
 
1000
template<typename _Tp> inline
 
1001
_Tp& Mat::at(const int* idx)
 
1002
{
 
1003
    CV_DbgAssert( elemSize() == CV_ELEM_SIZE(DataType<_Tp>::type) );
 
1004
    return *(_Tp*)ptr(idx);
 
1005
}
 
1006
 
 
1007
template<typename _Tp> inline
 
1008
const _Tp& Mat::at(const int* idx) const
 
1009
{
 
1010
    CV_DbgAssert( elemSize() == CV_ELEM_SIZE(DataType<_Tp>::type) );
 
1011
    return *(const _Tp*)ptr(idx);
 
1012
}
 
1013
 
 
1014
template<typename _Tp, int n> inline
 
1015
_Tp& Mat::at(const Vec<int, n>& idx)
 
1016
{
 
1017
    CV_DbgAssert( elemSize() == CV_ELEM_SIZE(DataType<_Tp>::type) );
 
1018
    return *(_Tp*)ptr(idx.val);
 
1019
}
 
1020
 
 
1021
template<typename _Tp, int n> inline
 
1022
const _Tp& Mat::at(const Vec<int, n>& idx) const
 
1023
{
 
1024
    CV_DbgAssert( elemSize() == CV_ELEM_SIZE(DataType<_Tp>::type) );
 
1025
    return *(const _Tp*)ptr(idx.val);
 
1026
}
 
1027
 
 
1028
template<typename _Tp> inline
 
1029
MatConstIterator_<_Tp> Mat::begin() const
 
1030
{
 
1031
    CV_DbgAssert( elemSize() == sizeof(_Tp) );
 
1032
    return MatConstIterator_<_Tp>((const Mat_<_Tp>*)this);
 
1033
}
 
1034
 
 
1035
template<typename _Tp> inline
 
1036
MatConstIterator_<_Tp> Mat::end() const
 
1037
{
 
1038
    CV_DbgAssert( elemSize() == sizeof(_Tp) );
 
1039
    MatConstIterator_<_Tp> it((const Mat_<_Tp>*)this);
 
1040
    it += total();
 
1041
    return it;
 
1042
}
 
1043
 
 
1044
template<typename _Tp> inline
 
1045
MatIterator_<_Tp> Mat::begin()
 
1046
{
 
1047
    CV_DbgAssert( elemSize() == sizeof(_Tp) );
 
1048
    return MatIterator_<_Tp>((Mat_<_Tp>*)this);
 
1049
}
 
1050
 
 
1051
template<typename _Tp> inline
 
1052
MatIterator_<_Tp> Mat::end()
 
1053
{
 
1054
    CV_DbgAssert( elemSize() == sizeof(_Tp) );
 
1055
    MatIterator_<_Tp> it((Mat_<_Tp>*)this);
 
1056
    it += total();
 
1057
    return it;
 
1058
}
 
1059
 
 
1060
template<typename _Tp, typename Functor> inline
 
1061
void Mat::forEach(const Functor& operation) {
 
1062
    this->forEach_impl<_Tp>(operation);
 
1063
}
 
1064
 
 
1065
template<typename _Tp, typename Functor> inline
 
1066
void Mat::forEach(const Functor& operation) const {
 
1067
    // call as not const
 
1068
    (const_cast<Mat*>(this))->forEach<const _Tp>(operation);
 
1069
}
 
1070
 
 
1071
template<typename _Tp> inline
 
1072
Mat::operator std::vector<_Tp>() const
 
1073
{
 
1074
    std::vector<_Tp> v;
 
1075
    copyTo(v);
 
1076
    return v;
 
1077
}
 
1078
 
 
1079
template<typename _Tp, int n> inline
 
1080
Mat::operator Vec<_Tp, n>() const
 
1081
{
 
1082
    CV_Assert( data && dims <= 2 && (rows == 1 || cols == 1) &&
 
1083
               rows + cols - 1 == n && channels() == 1 );
 
1084
 
 
1085
    if( isContinuous() && type() == DataType<_Tp>::type )
 
1086
        return Vec<_Tp, n>((_Tp*)data);
 
1087
    Vec<_Tp, n> v;
 
1088
    Mat tmp(rows, cols, DataType<_Tp>::type, v.val);
 
1089
    convertTo(tmp, tmp.type());
 
1090
    return v;
 
1091
}
 
1092
 
 
1093
template<typename _Tp, int m, int n> inline
 
1094
Mat::operator Matx<_Tp, m, n>() const
 
1095
{
 
1096
    CV_Assert( data && dims <= 2 && rows == m && cols == n && channels() == 1 );
 
1097
 
 
1098
    if( isContinuous() && type() == DataType<_Tp>::type )
 
1099
        return Matx<_Tp, m, n>((_Tp*)data);
 
1100
    Matx<_Tp, m, n> mtx;
 
1101
    Mat tmp(rows, cols, DataType<_Tp>::type, mtx.val);
 
1102
    convertTo(tmp, tmp.type());
 
1103
    return mtx;
 
1104
}
 
1105
 
 
1106
template<typename _Tp> inline
 
1107
void Mat::push_back(const _Tp& elem)
 
1108
{
 
1109
    if( !data )
 
1110
    {
 
1111
        *this = Mat(1, 1, DataType<_Tp>::type, (void*)&elem).clone();
 
1112
        return;
 
1113
    }
 
1114
    CV_Assert(DataType<_Tp>::type == type() && cols == 1
 
1115
              /* && dims == 2 (cols == 1 implies dims == 2) */);
 
1116
    const uchar* tmp = dataend + step[0];
 
1117
    if( !isSubmatrix() && isContinuous() && tmp <= datalimit )
 
1118
    {
 
1119
        *(_Tp*)(data + (size.p[0]++) * step.p[0]) = elem;
 
1120
        dataend = tmp;
 
1121
    }
 
1122
    else
 
1123
        push_back_(&elem);
 
1124
}
 
1125
 
 
1126
template<typename _Tp> inline
 
1127
void Mat::push_back(const Mat_<_Tp>& m)
 
1128
{
 
1129
    push_back((const Mat&)m);
 
1130
}
 
1131
 
 
1132
template<> inline
 
1133
void Mat::push_back(const MatExpr& expr)
 
1134
{
 
1135
    push_back(static_cast<Mat>(expr));
 
1136
}
 
1137
 
 
1138
#ifdef CV_CXX_MOVE_SEMANTICS
 
1139
 
 
1140
inline
 
1141
Mat::Mat(Mat&& m)
 
1142
    : flags(m.flags), dims(m.dims), rows(m.rows), cols(m.cols), data(m.data),
 
1143
      datastart(m.datastart), dataend(m.dataend), datalimit(m.datalimit), allocator(m.allocator),
 
1144
      u(m.u), size(&rows)
 
1145
{
 
1146
    if (m.dims <= 2)  // move new step/size info
 
1147
    {
 
1148
        step[0] = m.step[0];
 
1149
        step[1] = m.step[1];
 
1150
    }
 
1151
    else
 
1152
    {
 
1153
        CV_DbgAssert(m.step.p != m.step.buf);
 
1154
        step.p = m.step.p;
 
1155
        size.p = m.size.p;
 
1156
        m.step.p = m.step.buf;
 
1157
        m.size.p = &m.rows;
 
1158
    }
 
1159
    m.flags = MAGIC_VAL; m.dims = m.rows = m.cols = 0;
 
1160
    m.data = NULL; m.datastart = NULL; m.dataend = NULL; m.datalimit = NULL;
 
1161
    m.allocator = NULL;
 
1162
    m.u = NULL;
 
1163
}
 
1164
 
 
1165
inline
 
1166
Mat& Mat::operator = (Mat&& m)
 
1167
{
 
1168
    release();
 
1169
    flags = m.flags; dims = m.dims; rows = m.rows; cols = m.cols; data = m.data;
 
1170
    datastart = m.datastart; dataend = m.dataend; datalimit = m.datalimit; allocator = m.allocator;
 
1171
    u = m.u;
 
1172
    if (step.p != step.buf) // release self step/size
 
1173
    {
 
1174
        fastFree(step.p);
 
1175
        step.p = step.buf;
 
1176
        size.p = &rows;
 
1177
    }
 
1178
    if (m.dims <= 2) // move new step/size info
 
1179
    {
 
1180
        step[0] = m.step[0];
 
1181
        step[1] = m.step[1];
 
1182
    }
 
1183
    else
 
1184
    {
 
1185
        CV_DbgAssert(m.step.p != m.step.buf);
 
1186
        step.p = m.step.p;
 
1187
        size.p = m.size.p;
 
1188
        m.step.p = m.step.buf;
 
1189
        m.size.p = &m.rows;
 
1190
    }
 
1191
    m.flags = MAGIC_VAL; m.dims = m.rows = m.cols = 0;
 
1192
    m.data = NULL; m.datastart = NULL; m.dataend = NULL; m.datalimit = NULL;
 
1193
    m.allocator = NULL;
 
1194
    m.u = NULL;
 
1195
    return *this;
 
1196
}
 
1197
 
 
1198
#endif
 
1199
 
 
1200
 
 
1201
///////////////////////////// MatSize ////////////////////////////
 
1202
 
 
1203
inline
 
1204
MatSize::MatSize(int* _p)
 
1205
    : p(_p) {}
 
1206
 
 
1207
inline
 
1208
Size MatSize::operator()() const
 
1209
{
 
1210
    CV_DbgAssert(p[-1] <= 2);
 
1211
    return Size(p[1], p[0]);
 
1212
}
 
1213
 
 
1214
inline
 
1215
const int& MatSize::operator[](int i) const
 
1216
{
 
1217
    return p[i];
 
1218
}
 
1219
 
 
1220
inline
 
1221
int& MatSize::operator[](int i)
 
1222
{
 
1223
    return p[i];
 
1224
}
 
1225
 
 
1226
inline
 
1227
MatSize::operator const int*() const
 
1228
{
 
1229
    return p;
 
1230
}
 
1231
 
 
1232
inline
 
1233
bool MatSize::operator == (const MatSize& sz) const
 
1234
{
 
1235
    int d = p[-1];
 
1236
    int dsz = sz.p[-1];
 
1237
    if( d != dsz )
 
1238
        return false;
 
1239
    if( d == 2 )
 
1240
        return p[0] == sz.p[0] && p[1] == sz.p[1];
 
1241
 
 
1242
    for( int i = 0; i < d; i++ )
 
1243
        if( p[i] != sz.p[i] )
 
1244
            return false;
 
1245
    return true;
 
1246
}
 
1247
 
 
1248
inline
 
1249
bool MatSize::operator != (const MatSize& sz) const
 
1250
{
 
1251
    return !(*this == sz);
 
1252
}
 
1253
 
 
1254
 
 
1255
 
 
1256
///////////////////////////// MatStep ////////////////////////////
 
1257
 
 
1258
inline
 
1259
MatStep::MatStep()
 
1260
{
 
1261
    p = buf; p[0] = p[1] = 0;
 
1262
}
 
1263
 
 
1264
inline
 
1265
MatStep::MatStep(size_t s)
 
1266
{
 
1267
    p = buf; p[0] = s; p[1] = 0;
 
1268
}
 
1269
 
 
1270
inline
 
1271
const size_t& MatStep::operator[](int i) const
 
1272
{
 
1273
    return p[i];
 
1274
}
 
1275
 
 
1276
inline
 
1277
size_t& MatStep::operator[](int i)
 
1278
{
 
1279
    return p[i];
 
1280
}
 
1281
 
 
1282
inline MatStep::operator size_t() const
 
1283
{
 
1284
    CV_DbgAssert( p == buf );
 
1285
    return buf[0];
 
1286
}
 
1287
 
 
1288
inline MatStep& MatStep::operator = (size_t s)
 
1289
{
 
1290
    CV_DbgAssert( p == buf );
 
1291
    buf[0] = s;
 
1292
    return *this;
 
1293
}
 
1294
 
 
1295
 
 
1296
 
 
1297
////////////////////////////// Mat_<_Tp> ////////////////////////////
 
1298
 
 
1299
template<typename _Tp> inline
 
1300
Mat_<_Tp>::Mat_()
 
1301
    : Mat()
 
1302
{
 
1303
    flags = (flags & ~CV_MAT_TYPE_MASK) | DataType<_Tp>::type;
 
1304
}
 
1305
 
 
1306
template<typename _Tp> inline
 
1307
Mat_<_Tp>::Mat_(int _rows, int _cols)
 
1308
    : Mat(_rows, _cols, DataType<_Tp>::type)
 
1309
{
 
1310
}
 
1311
 
 
1312
template<typename _Tp> inline
 
1313
Mat_<_Tp>::Mat_(int _rows, int _cols, const _Tp& value)
 
1314
    : Mat(_rows, _cols, DataType<_Tp>::type)
 
1315
{
 
1316
    *this = value;
 
1317
}
 
1318
 
 
1319
template<typename _Tp> inline
 
1320
Mat_<_Tp>::Mat_(Size _sz)
 
1321
    : Mat(_sz.height, _sz.width, DataType<_Tp>::type)
 
1322
{}
 
1323
 
 
1324
template<typename _Tp> inline
 
1325
Mat_<_Tp>::Mat_(Size _sz, const _Tp& value)
 
1326
    : Mat(_sz.height, _sz.width, DataType<_Tp>::type)
 
1327
{
 
1328
    *this = value;
 
1329
}
 
1330
 
 
1331
template<typename _Tp> inline
 
1332
Mat_<_Tp>::Mat_(int _dims, const int* _sz)
 
1333
    : Mat(_dims, _sz, DataType<_Tp>::type)
 
1334
{}
 
1335
 
 
1336
template<typename _Tp> inline
 
1337
Mat_<_Tp>::Mat_(int _dims, const int* _sz, const _Tp& _s)
 
1338
    : Mat(_dims, _sz, DataType<_Tp>::type, Scalar(_s))
 
1339
{}
 
1340
 
 
1341
template<typename _Tp> inline
 
1342
Mat_<_Tp>::Mat_(const Mat_<_Tp>& m, const Range* ranges)
 
1343
    : Mat(m, ranges)
 
1344
{}
 
1345
 
 
1346
template<typename _Tp> inline
 
1347
Mat_<_Tp>::Mat_(const Mat& m)
 
1348
    : Mat()
 
1349
{
 
1350
    flags = (flags & ~CV_MAT_TYPE_MASK) | DataType<_Tp>::type;
 
1351
    *this = m;
 
1352
}
 
1353
 
 
1354
template<typename _Tp> inline
 
1355
Mat_<_Tp>::Mat_(const Mat_& m)
 
1356
    : Mat(m)
 
1357
{}
 
1358
 
 
1359
template<typename _Tp> inline
 
1360
Mat_<_Tp>::Mat_(int _rows, int _cols, _Tp* _data, size_t steps)
 
1361
    : Mat(_rows, _cols, DataType<_Tp>::type, _data, steps)
 
1362
{}
 
1363
 
 
1364
template<typename _Tp> inline
 
1365
Mat_<_Tp>::Mat_(const Mat_& m, const Range& _rowRange, const Range& _colRange)
 
1366
    : Mat(m, _rowRange, _colRange)
 
1367
{}
 
1368
 
 
1369
template<typename _Tp> inline
 
1370
Mat_<_Tp>::Mat_(const Mat_& m, const Rect& roi)
 
1371
    : Mat(m, roi)
 
1372
{}
 
1373
 
 
1374
template<typename _Tp> template<int n> inline
 
1375
Mat_<_Tp>::Mat_(const Vec<typename DataType<_Tp>::channel_type, n>& vec, bool copyData)
 
1376
    : Mat(n / DataType<_Tp>::channels, 1, DataType<_Tp>::type, (void*)&vec)
 
1377
{
 
1378
    CV_Assert(n%DataType<_Tp>::channels == 0);
 
1379
    if( copyData )
 
1380
        *this = clone();
 
1381
}
 
1382
 
 
1383
template<typename _Tp> template<int m, int n> inline
 
1384
Mat_<_Tp>::Mat_(const Matx<typename DataType<_Tp>::channel_type, m, n>& M, bool copyData)
 
1385
    : Mat(m, n / DataType<_Tp>::channels, DataType<_Tp>::type, (void*)&M)
 
1386
{
 
1387
    CV_Assert(n % DataType<_Tp>::channels == 0);
 
1388
    if( copyData )
 
1389
        *this = clone();
 
1390
}
 
1391
 
 
1392
template<typename _Tp> inline
 
1393
Mat_<_Tp>::Mat_(const Point_<typename DataType<_Tp>::channel_type>& pt, bool copyData)
 
1394
    : Mat(2 / DataType<_Tp>::channels, 1, DataType<_Tp>::type, (void*)&pt)
 
1395
{
 
1396
    CV_Assert(2 % DataType<_Tp>::channels == 0);
 
1397
    if( copyData )
 
1398
        *this = clone();
 
1399
}
 
1400
 
 
1401
template<typename _Tp> inline
 
1402
Mat_<_Tp>::Mat_(const Point3_<typename DataType<_Tp>::channel_type>& pt, bool copyData)
 
1403
    : Mat(3 / DataType<_Tp>::channels, 1, DataType<_Tp>::type, (void*)&pt)
 
1404
{
 
1405
    CV_Assert(3 % DataType<_Tp>::channels == 0);
 
1406
    if( copyData )
 
1407
        *this = clone();
 
1408
}
 
1409
 
 
1410
template<typename _Tp> inline
 
1411
Mat_<_Tp>::Mat_(const MatCommaInitializer_<_Tp>& commaInitializer)
 
1412
    : Mat(commaInitializer)
 
1413
{}
 
1414
 
 
1415
template<typename _Tp> inline
 
1416
Mat_<_Tp>::Mat_(const std::vector<_Tp>& vec, bool copyData)
 
1417
    : Mat(vec, copyData)
 
1418
{}
 
1419
 
 
1420
template<typename _Tp> inline
 
1421
Mat_<_Tp>& Mat_<_Tp>::operator = (const Mat& m)
 
1422
{
 
1423
    if( DataType<_Tp>::type == m.type() )
 
1424
    {
 
1425
        Mat::operator = (m);
 
1426
        return *this;
 
1427
    }
 
1428
    if( DataType<_Tp>::depth == m.depth() )
 
1429
    {
 
1430
        return (*this = m.reshape(DataType<_Tp>::channels, m.dims, 0));
 
1431
    }
 
1432
    CV_DbgAssert(DataType<_Tp>::channels == m.channels());
 
1433
    m.convertTo(*this, type());
 
1434
    return *this;
 
1435
}
 
1436
 
 
1437
template<typename _Tp> inline
 
1438
Mat_<_Tp>& Mat_<_Tp>::operator = (const Mat_& m)
 
1439
{
 
1440
    Mat::operator=(m);
 
1441
    return *this;
 
1442
}
 
1443
 
 
1444
template<typename _Tp> inline
 
1445
Mat_<_Tp>& Mat_<_Tp>::operator = (const _Tp& s)
 
1446
{
 
1447
    typedef typename DataType<_Tp>::vec_type VT;
 
1448
    Mat::operator=(Scalar((const VT&)s));
 
1449
    return *this;
 
1450
}
 
1451
 
 
1452
template<typename _Tp> inline
 
1453
void Mat_<_Tp>::create(int _rows, int _cols)
 
1454
{
 
1455
    Mat::create(_rows, _cols, DataType<_Tp>::type);
 
1456
}
 
1457
 
 
1458
template<typename _Tp> inline
 
1459
void Mat_<_Tp>::create(Size _sz)
 
1460
{
 
1461
    Mat::create(_sz, DataType<_Tp>::type);
 
1462
}
 
1463
 
 
1464
template<typename _Tp> inline
 
1465
void Mat_<_Tp>::create(int _dims, const int* _sz)
 
1466
{
 
1467
    Mat::create(_dims, _sz, DataType<_Tp>::type);
 
1468
}
 
1469
 
 
1470
template<typename _Tp> inline
 
1471
Mat_<_Tp> Mat_<_Tp>::cross(const Mat_& m) const
 
1472
{
 
1473
    return Mat_<_Tp>(Mat::cross(m));
 
1474
}
 
1475
 
 
1476
template<typename _Tp> template<typename T2> inline
 
1477
Mat_<_Tp>::operator Mat_<T2>() const
 
1478
{
 
1479
    return Mat_<T2>(*this);
 
1480
}
 
1481
 
 
1482
template<typename _Tp> inline
 
1483
Mat_<_Tp> Mat_<_Tp>::row(int y) const
 
1484
{
 
1485
    return Mat_(*this, Range(y, y+1), Range::all());
 
1486
}
 
1487
 
 
1488
template<typename _Tp> inline
 
1489
Mat_<_Tp> Mat_<_Tp>::col(int x) const
 
1490
{
 
1491
    return Mat_(*this, Range::all(), Range(x, x+1));
 
1492
}
 
1493
 
 
1494
template<typename _Tp> inline
 
1495
Mat_<_Tp> Mat_<_Tp>::diag(int d) const
 
1496
{
 
1497
    return Mat_(Mat::diag(d));
 
1498
}
 
1499
 
 
1500
template<typename _Tp> inline
 
1501
Mat_<_Tp> Mat_<_Tp>::clone() const
 
1502
{
 
1503
    return Mat_(Mat::clone());
 
1504
}
 
1505
 
 
1506
template<typename _Tp> inline
 
1507
size_t Mat_<_Tp>::elemSize() const
 
1508
{
 
1509
    CV_DbgAssert( Mat::elemSize() == sizeof(_Tp) );
 
1510
    return sizeof(_Tp);
 
1511
}
 
1512
 
 
1513
template<typename _Tp> inline
 
1514
size_t Mat_<_Tp>::elemSize1() const
 
1515
{
 
1516
    CV_DbgAssert( Mat::elemSize1() == sizeof(_Tp) / DataType<_Tp>::channels );
 
1517
    return sizeof(_Tp) / DataType<_Tp>::channels;
 
1518
}
 
1519
 
 
1520
template<typename _Tp> inline
 
1521
int Mat_<_Tp>::type() const
 
1522
{
 
1523
    CV_DbgAssert( Mat::type() == DataType<_Tp>::type );
 
1524
    return DataType<_Tp>::type;
 
1525
}
 
1526
 
 
1527
template<typename _Tp> inline
 
1528
int Mat_<_Tp>::depth() const
 
1529
{
 
1530
    CV_DbgAssert( Mat::depth() == DataType<_Tp>::depth );
 
1531
    return DataType<_Tp>::depth;
 
1532
}
 
1533
 
 
1534
template<typename _Tp> inline
 
1535
int Mat_<_Tp>::channels() const
 
1536
{
 
1537
    CV_DbgAssert( Mat::channels() == DataType<_Tp>::channels );
 
1538
    return DataType<_Tp>::channels;
 
1539
}
 
1540
 
 
1541
template<typename _Tp> inline
 
1542
size_t Mat_<_Tp>::stepT(int i) const
 
1543
{
 
1544
    return step.p[i] / elemSize();
 
1545
}
 
1546
 
 
1547
template<typename _Tp> inline
 
1548
size_t Mat_<_Tp>::step1(int i) const
 
1549
{
 
1550
    return step.p[i] / elemSize1();
 
1551
}
 
1552
 
 
1553
template<typename _Tp> inline
 
1554
Mat_<_Tp>& Mat_<_Tp>::adjustROI( int dtop, int dbottom, int dleft, int dright )
 
1555
{
 
1556
    return (Mat_<_Tp>&)(Mat::adjustROI(dtop, dbottom, dleft, dright));
 
1557
}
 
1558
 
 
1559
template<typename _Tp> inline
 
1560
Mat_<_Tp> Mat_<_Tp>::operator()( const Range& _rowRange, const Range& _colRange ) const
 
1561
{
 
1562
    return Mat_<_Tp>(*this, _rowRange, _colRange);
 
1563
}
 
1564
 
 
1565
template<typename _Tp> inline
 
1566
Mat_<_Tp> Mat_<_Tp>::operator()( const Rect& roi ) const
 
1567
{
 
1568
    return Mat_<_Tp>(*this, roi);
 
1569
}
 
1570
 
 
1571
template<typename _Tp> inline
 
1572
Mat_<_Tp> Mat_<_Tp>::operator()( const Range* ranges ) const
 
1573
{
 
1574
    return Mat_<_Tp>(*this, ranges);
 
1575
}
 
1576
 
 
1577
template<typename _Tp> inline
 
1578
_Tp* Mat_<_Tp>::operator [](int y)
 
1579
{
 
1580
    CV_DbgAssert( 0 <= y && y < rows );
 
1581
    return (_Tp*)(data + y*step.p[0]);
 
1582
}
 
1583
 
 
1584
template<typename _Tp> inline
 
1585
const _Tp* Mat_<_Tp>::operator [](int y) const
 
1586
{
 
1587
    CV_DbgAssert( 0 <= y && y < rows );
 
1588
    return (const _Tp*)(data + y*step.p[0]);
 
1589
}
 
1590
 
 
1591
template<typename _Tp> inline
 
1592
_Tp& Mat_<_Tp>::operator ()(int i0, int i1)
 
1593
{
 
1594
    CV_DbgAssert(dims <= 2);
 
1595
    CV_DbgAssert(data);
 
1596
    CV_DbgAssert((unsigned)i0 < (unsigned)size.p[0]);
 
1597
    CV_DbgAssert((unsigned)i1 < (unsigned)size.p[1]);
 
1598
    CV_DbgAssert(type() == DataType<_Tp>::type);
 
1599
    return ((_Tp*)(data + step.p[0] * i0))[i1];
 
1600
}
 
1601
 
 
1602
template<typename _Tp> inline
 
1603
const _Tp& Mat_<_Tp>::operator ()(int i0, int i1) const
 
1604
{
 
1605
    CV_DbgAssert(dims <= 2);
 
1606
    CV_DbgAssert(data);
 
1607
    CV_DbgAssert((unsigned)i0 < (unsigned)size.p[0]);
 
1608
    CV_DbgAssert((unsigned)i1 < (unsigned)size.p[1]);
 
1609
    CV_DbgAssert(type() == DataType<_Tp>::type);
 
1610
    return ((const _Tp*)(data + step.p[0] * i0))[i1];
 
1611
}
 
1612
 
 
1613
template<typename _Tp> inline
 
1614
_Tp& Mat_<_Tp>::operator ()(Point pt)
 
1615
{
 
1616
    CV_DbgAssert(dims <= 2);
 
1617
    CV_DbgAssert(data);
 
1618
    CV_DbgAssert((unsigned)pt.y < (unsigned)size.p[0]);
 
1619
    CV_DbgAssert((unsigned)pt.x < (unsigned)size.p[1]);
 
1620
    CV_DbgAssert(type() == DataType<_Tp>::type);
 
1621
    return ((_Tp*)(data + step.p[0] * pt.y))[pt.x];
 
1622
}
 
1623
 
 
1624
template<typename _Tp> inline
 
1625
const _Tp& Mat_<_Tp>::operator ()(Point pt) const
 
1626
{
 
1627
    CV_DbgAssert(dims <= 2);
 
1628
    CV_DbgAssert(data);
 
1629
    CV_DbgAssert((unsigned)pt.y < (unsigned)size.p[0]);
 
1630
    CV_DbgAssert((unsigned)pt.x < (unsigned)size.p[1]);
 
1631
    CV_DbgAssert(type() == DataType<_Tp>::type);
 
1632
    return ((const _Tp*)(data + step.p[0] * pt.y))[pt.x];
 
1633
}
 
1634
 
 
1635
template<typename _Tp> inline
 
1636
_Tp& Mat_<_Tp>::operator ()(const int* idx)
 
1637
{
 
1638
    return Mat::at<_Tp>(idx);
 
1639
}
 
1640
 
 
1641
template<typename _Tp> inline
 
1642
const _Tp& Mat_<_Tp>::operator ()(const int* idx) const
 
1643
{
 
1644
    return Mat::at<_Tp>(idx);
 
1645
}
 
1646
 
 
1647
template<typename _Tp> template<int n> inline
 
1648
_Tp& Mat_<_Tp>::operator ()(const Vec<int, n>& idx)
 
1649
{
 
1650
    return Mat::at<_Tp>(idx);
 
1651
}
 
1652
 
 
1653
template<typename _Tp> template<int n> inline
 
1654
const _Tp& Mat_<_Tp>::operator ()(const Vec<int, n>& idx) const
 
1655
{
 
1656
    return Mat::at<_Tp>(idx);
 
1657
}
 
1658
 
 
1659
template<typename _Tp> inline
 
1660
_Tp& Mat_<_Tp>::operator ()(int i0)
 
1661
{
 
1662
    return this->at<_Tp>(i0);
 
1663
}
 
1664
 
 
1665
template<typename _Tp> inline
 
1666
const _Tp& Mat_<_Tp>::operator ()(int i0) const
 
1667
{
 
1668
    return this->at<_Tp>(i0);
 
1669
}
 
1670
 
 
1671
template<typename _Tp> inline
 
1672
_Tp& Mat_<_Tp>::operator ()(int i0, int i1, int i2)
 
1673
{
 
1674
    return this->at<_Tp>(i0, i1, i2);
 
1675
}
 
1676
 
 
1677
template<typename _Tp> inline
 
1678
const _Tp& Mat_<_Tp>::operator ()(int i0, int i1, int i2) const
 
1679
{
 
1680
    return this->at<_Tp>(i0, i1, i2);
 
1681
}
 
1682
 
 
1683
template<typename _Tp> inline
 
1684
Mat_<_Tp>::operator std::vector<_Tp>() const
 
1685
{
 
1686
    std::vector<_Tp> v;
 
1687
    copyTo(v);
 
1688
    return v;
 
1689
}
 
1690
 
 
1691
template<typename _Tp> template<int n> inline
 
1692
Mat_<_Tp>::operator Vec<typename DataType<_Tp>::channel_type, n>() const
 
1693
{
 
1694
    CV_Assert(n % DataType<_Tp>::channels == 0);
 
1695
 
 
1696
#if defined _MSC_VER
 
1697
    const Mat* pMat = (const Mat*)this; // workaround for MSVS <= 2012 compiler bugs (but GCC 4.6 dislikes this workaround)
 
1698
    return pMat->operator Vec<typename DataType<_Tp>::channel_type, n>();
 
1699
#else
 
1700
    return this->Mat::operator Vec<typename DataType<_Tp>::channel_type, n>();
 
1701
#endif
 
1702
}
 
1703
 
 
1704
template<typename _Tp> template<int m, int n> inline
 
1705
Mat_<_Tp>::operator Matx<typename DataType<_Tp>::channel_type, m, n>() const
 
1706
{
 
1707
    CV_Assert(n % DataType<_Tp>::channels == 0);
 
1708
 
 
1709
#if defined _MSC_VER
 
1710
    const Mat* pMat = (const Mat*)this; // workaround for MSVS <= 2012 compiler bugs (but GCC 4.6 dislikes this workaround)
 
1711
    Matx<typename DataType<_Tp>::channel_type, m, n> res = pMat->operator Matx<typename DataType<_Tp>::channel_type, m, n>();
 
1712
    return res;
 
1713
#else
 
1714
    Matx<typename DataType<_Tp>::channel_type, m, n> res = this->Mat::operator Matx<typename DataType<_Tp>::channel_type, m, n>();
 
1715
    return res;
 
1716
#endif
 
1717
}
 
1718
 
 
1719
template<typename _Tp> inline
 
1720
MatConstIterator_<_Tp> Mat_<_Tp>::begin() const
 
1721
{
 
1722
    return Mat::begin<_Tp>();
 
1723
}
 
1724
 
 
1725
template<typename _Tp> inline
 
1726
MatConstIterator_<_Tp> Mat_<_Tp>::end() const
 
1727
{
 
1728
    return Mat::end<_Tp>();
 
1729
}
 
1730
 
 
1731
template<typename _Tp> inline
 
1732
MatIterator_<_Tp> Mat_<_Tp>::begin()
 
1733
{
 
1734
    return Mat::begin<_Tp>();
 
1735
}
 
1736
 
 
1737
template<typename _Tp> inline
 
1738
MatIterator_<_Tp> Mat_<_Tp>::end()
 
1739
{
 
1740
    return Mat::end<_Tp>();
 
1741
}
 
1742
 
 
1743
template<typename _Tp> template<typename Functor> inline
 
1744
void Mat_<_Tp>::forEach(const Functor& operation) {
 
1745
    Mat::forEach<_Tp, Functor>(operation);
 
1746
}
 
1747
 
 
1748
template<typename _Tp> template<typename Functor> inline
 
1749
void Mat_<_Tp>::forEach(const Functor& operation) const {
 
1750
    Mat::forEach<_Tp, Functor>(operation);
 
1751
}
 
1752
 
 
1753
#ifdef CV_CXX_MOVE_SEMANTICS
 
1754
 
 
1755
template<typename _Tp> inline
 
1756
Mat_<_Tp>::Mat_(Mat_&& m)
 
1757
    : Mat(m)
 
1758
{
 
1759
}
 
1760
 
 
1761
template<typename _Tp> inline
 
1762
Mat_<_Tp>& Mat_<_Tp>::operator = (Mat_&& m)
 
1763
{
 
1764
    Mat::operator = (m);
 
1765
    return *this;
 
1766
}
 
1767
 
 
1768
template<typename _Tp> inline
 
1769
Mat_<_Tp>::Mat_(Mat&& m)
 
1770
    : Mat()
 
1771
{
 
1772
    flags = (flags & ~CV_MAT_TYPE_MASK) | DataType<_Tp>::type;
 
1773
    *this = m;
 
1774
}
 
1775
 
 
1776
template<typename _Tp> inline
 
1777
Mat_<_Tp>& Mat_<_Tp>::operator = (Mat&& m)
 
1778
{
 
1779
    if( DataType<_Tp>::type == m.type() )
 
1780
    {
 
1781
        Mat::operator = ((Mat&&)m);
 
1782
        return *this;
 
1783
    }
 
1784
    if( DataType<_Tp>::depth == m.depth() )
 
1785
    {
 
1786
        Mat::operator = ((Mat&&)m.reshape(DataType<_Tp>::channels, m.dims, 0));
 
1787
        return *this;
 
1788
    }
 
1789
    CV_DbgAssert(DataType<_Tp>::channels == m.channels());
 
1790
    m.convertTo(*this, type());
 
1791
    return *this;
 
1792
}
 
1793
 
 
1794
template<typename _Tp> inline
 
1795
Mat_<_Tp>::Mat_(MatExpr&& e)
 
1796
    : Mat()
 
1797
{
 
1798
    flags = (flags & ~CV_MAT_TYPE_MASK) | DataType<_Tp>::type;
 
1799
    *this = Mat(e);
 
1800
}
 
1801
 
 
1802
#endif
 
1803
 
 
1804
///////////////////////////// SparseMat /////////////////////////////
 
1805
 
 
1806
inline
 
1807
SparseMat::SparseMat()
 
1808
    : flags(MAGIC_VAL), hdr(0)
 
1809
{}
 
1810
 
 
1811
inline
 
1812
SparseMat::SparseMat(int _dims, const int* _sizes, int _type)
 
1813
    : flags(MAGIC_VAL), hdr(0)
 
1814
{
 
1815
    create(_dims, _sizes, _type);
 
1816
}
 
1817
 
 
1818
inline
 
1819
SparseMat::SparseMat(const SparseMat& m)
 
1820
    : flags(m.flags), hdr(m.hdr)
 
1821
{
 
1822
    addref();
 
1823
}
 
1824
 
 
1825
inline
 
1826
SparseMat::~SparseMat()
 
1827
{
 
1828
    release();
 
1829
}
 
1830
 
 
1831
inline
 
1832
SparseMat& SparseMat::operator = (const SparseMat& m)
 
1833
{
 
1834
    if( this != &m )
 
1835
    {
 
1836
        if( m.hdr )
 
1837
            CV_XADD(&m.hdr->refcount, 1);
 
1838
        release();
 
1839
        flags = m.flags;
 
1840
        hdr = m.hdr;
 
1841
    }
 
1842
    return *this;
 
1843
}
 
1844
 
 
1845
inline
 
1846
SparseMat& SparseMat::operator = (const Mat& m)
 
1847
{
 
1848
    return (*this = SparseMat(m));
 
1849
}
 
1850
 
 
1851
inline
 
1852
SparseMat SparseMat::clone() const
 
1853
{
 
1854
    SparseMat temp;
 
1855
    this->copyTo(temp);
 
1856
    return temp;
 
1857
}
 
1858
 
 
1859
inline
 
1860
void SparseMat::assignTo( SparseMat& m, int _type ) const
 
1861
{
 
1862
    if( _type < 0 )
 
1863
        m = *this;
 
1864
    else
 
1865
        convertTo(m, _type);
 
1866
}
 
1867
 
 
1868
inline
 
1869
void SparseMat::addref()
 
1870
{
 
1871
    if( hdr )
 
1872
        CV_XADD(&hdr->refcount, 1);
 
1873
}
 
1874
 
 
1875
inline
 
1876
void SparseMat::release()
 
1877
{
 
1878
    if( hdr && CV_XADD(&hdr->refcount, -1) == 1 )
 
1879
        delete hdr;
 
1880
    hdr = 0;
 
1881
}
 
1882
 
 
1883
inline
 
1884
size_t SparseMat::elemSize() const
 
1885
{
 
1886
    return CV_ELEM_SIZE(flags);
 
1887
}
 
1888
 
 
1889
inline
 
1890
size_t SparseMat::elemSize1() const
 
1891
{
 
1892
    return CV_ELEM_SIZE1(flags);
 
1893
}
 
1894
 
 
1895
inline
 
1896
int SparseMat::type() const
 
1897
{
 
1898
    return CV_MAT_TYPE(flags);
 
1899
}
 
1900
 
 
1901
inline
 
1902
int SparseMat::depth() const
 
1903
{
 
1904
    return CV_MAT_DEPTH(flags);
 
1905
}
 
1906
 
 
1907
inline
 
1908
int SparseMat::channels() const
 
1909
{
 
1910
    return CV_MAT_CN(flags);
 
1911
}
 
1912
 
 
1913
inline
 
1914
const int* SparseMat::size() const
 
1915
{
 
1916
    return hdr ? hdr->size : 0;
 
1917
}
 
1918
 
 
1919
inline
 
1920
int SparseMat::size(int i) const
 
1921
{
 
1922
    if( hdr )
 
1923
    {
 
1924
        CV_DbgAssert((unsigned)i < (unsigned)hdr->dims);
 
1925
        return hdr->size[i];
 
1926
    }
 
1927
    return 0;
 
1928
}
 
1929
 
 
1930
inline
 
1931
int SparseMat::dims() const
 
1932
{
 
1933
    return hdr ? hdr->dims : 0;
 
1934
}
 
1935
 
 
1936
inline
 
1937
size_t SparseMat::nzcount() const
 
1938
{
 
1939
    return hdr ? hdr->nodeCount : 0;
 
1940
}
 
1941
 
 
1942
inline
 
1943
size_t SparseMat::hash(int i0) const
 
1944
{
 
1945
    return (size_t)i0;
 
1946
}
 
1947
 
 
1948
inline
 
1949
size_t SparseMat::hash(int i0, int i1) const
 
1950
{
 
1951
    return (size_t)(unsigned)i0 * HASH_SCALE + (unsigned)i1;
 
1952
}
 
1953
 
 
1954
inline
 
1955
size_t SparseMat::hash(int i0, int i1, int i2) const
 
1956
{
 
1957
    return ((size_t)(unsigned)i0 * HASH_SCALE + (unsigned)i1) * HASH_SCALE + (unsigned)i2;
 
1958
}
 
1959
 
 
1960
inline
 
1961
size_t SparseMat::hash(const int* idx) const
 
1962
{
 
1963
    size_t h = (unsigned)idx[0];
 
1964
    if( !hdr )
 
1965
        return 0;
 
1966
    int d = hdr->dims;
 
1967
    for(int i = 1; i < d; i++ )
 
1968
        h = h * HASH_SCALE + (unsigned)idx[i];
 
1969
    return h;
 
1970
}
 
1971
 
 
1972
template<typename _Tp> inline
 
1973
_Tp& SparseMat::ref(int i0, size_t* hashval)
 
1974
{
 
1975
    return *(_Tp*)((SparseMat*)this)->ptr(i0, true, hashval);
 
1976
}
 
1977
 
 
1978
template<typename _Tp> inline
 
1979
_Tp& SparseMat::ref(int i0, int i1, size_t* hashval)
 
1980
{
 
1981
    return *(_Tp*)((SparseMat*)this)->ptr(i0, i1, true, hashval);
 
1982
}
 
1983
 
 
1984
template<typename _Tp> inline
 
1985
_Tp& SparseMat::ref(int i0, int i1, int i2, size_t* hashval)
 
1986
{
 
1987
    return *(_Tp*)((SparseMat*)this)->ptr(i0, i1, i2, true, hashval);
 
1988
}
 
1989
 
 
1990
template<typename _Tp> inline
 
1991
_Tp& SparseMat::ref(const int* idx, size_t* hashval)
 
1992
{
 
1993
    return *(_Tp*)((SparseMat*)this)->ptr(idx, true, hashval);
 
1994
}
 
1995
 
 
1996
template<typename _Tp> inline
 
1997
_Tp SparseMat::value(int i0, size_t* hashval) const
 
1998
{
 
1999
    const _Tp* p = (const _Tp*)((SparseMat*)this)->ptr(i0, false, hashval);
 
2000
    return p ? *p : _Tp();
 
2001
}
 
2002
 
 
2003
template<typename _Tp> inline
 
2004
_Tp SparseMat::value(int i0, int i1, size_t* hashval) const
 
2005
{
 
2006
    const _Tp* p = (const _Tp*)((SparseMat*)this)->ptr(i0, i1, false, hashval);
 
2007
    return p ? *p : _Tp();
 
2008
}
 
2009
 
 
2010
template<typename _Tp> inline
 
2011
_Tp SparseMat::value(int i0, int i1, int i2, size_t* hashval) const
 
2012
{
 
2013
    const _Tp* p = (const _Tp*)((SparseMat*)this)->ptr(i0, i1, i2, false, hashval);
 
2014
    return p ? *p : _Tp();
 
2015
}
 
2016
 
 
2017
template<typename _Tp> inline
 
2018
_Tp SparseMat::value(const int* idx, size_t* hashval) const
 
2019
{
 
2020
    const _Tp* p = (const _Tp*)((SparseMat*)this)->ptr(idx, false, hashval);
 
2021
    return p ? *p : _Tp();
 
2022
}
 
2023
 
 
2024
template<typename _Tp> inline
 
2025
const _Tp* SparseMat::find(int i0, size_t* hashval) const
 
2026
{
 
2027
    return (const _Tp*)((SparseMat*)this)->ptr(i0, false, hashval);
 
2028
}
 
2029
 
 
2030
template<typename _Tp> inline
 
2031
const _Tp* SparseMat::find(int i0, int i1, size_t* hashval) const
 
2032
{
 
2033
    return (const _Tp*)((SparseMat*)this)->ptr(i0, i1, false, hashval);
 
2034
}
 
2035
 
 
2036
template<typename _Tp> inline
 
2037
const _Tp* SparseMat::find(int i0, int i1, int i2, size_t* hashval) const
 
2038
{
 
2039
    return (const _Tp*)((SparseMat*)this)->ptr(i0, i1, i2, false, hashval);
 
2040
}
 
2041
 
 
2042
template<typename _Tp> inline
 
2043
const _Tp* SparseMat::find(const int* idx, size_t* hashval) const
 
2044
{
 
2045
    return (const _Tp*)((SparseMat*)this)->ptr(idx, false, hashval);
 
2046
}
 
2047
 
 
2048
template<typename _Tp> inline
 
2049
_Tp& SparseMat::value(Node* n)
 
2050
{
 
2051
    return *(_Tp*)((uchar*)n + hdr->valueOffset);
 
2052
}
 
2053
 
 
2054
template<typename _Tp> inline
 
2055
const _Tp& SparseMat::value(const Node* n) const
 
2056
{
 
2057
    return *(const _Tp*)((const uchar*)n + hdr->valueOffset);
 
2058
}
 
2059
 
 
2060
inline
 
2061
SparseMat::Node* SparseMat::node(size_t nidx)
 
2062
{
 
2063
    return (Node*)(void*)&hdr->pool[nidx];
 
2064
}
 
2065
 
 
2066
inline
 
2067
const SparseMat::Node* SparseMat::node(size_t nidx) const
 
2068
{
 
2069
    return (const Node*)(const void*)&hdr->pool[nidx];
 
2070
}
 
2071
 
 
2072
inline
 
2073
SparseMatIterator SparseMat::begin()
 
2074
{
 
2075
    return SparseMatIterator(this);
 
2076
}
 
2077
 
 
2078
inline
 
2079
SparseMatConstIterator SparseMat::begin() const
 
2080
{
 
2081
    return SparseMatConstIterator(this);
 
2082
}
 
2083
 
 
2084
inline
 
2085
SparseMatIterator SparseMat::end()
 
2086
{
 
2087
    SparseMatIterator it(this);
 
2088
    it.seekEnd();
 
2089
    return it;
 
2090
}
 
2091
 
 
2092
inline
 
2093
SparseMatConstIterator SparseMat::end() const
 
2094
{
 
2095
    SparseMatConstIterator it(this);
 
2096
    it.seekEnd();
 
2097
    return it;
 
2098
}
 
2099
 
 
2100
template<typename _Tp> inline
 
2101
SparseMatIterator_<_Tp> SparseMat::begin()
 
2102
{
 
2103
    return SparseMatIterator_<_Tp>(this);
 
2104
}
 
2105
 
 
2106
template<typename _Tp> inline
 
2107
SparseMatConstIterator_<_Tp> SparseMat::begin() const
 
2108
{
 
2109
    return SparseMatConstIterator_<_Tp>(this);
 
2110
}
 
2111
 
 
2112
template<typename _Tp> inline
 
2113
SparseMatIterator_<_Tp> SparseMat::end()
 
2114
{
 
2115
    SparseMatIterator_<_Tp> it(this);
 
2116
    it.seekEnd();
 
2117
    return it;
 
2118
}
 
2119
 
 
2120
template<typename _Tp> inline
 
2121
SparseMatConstIterator_<_Tp> SparseMat::end() const
 
2122
{
 
2123
    SparseMatConstIterator_<_Tp> it(this);
 
2124
    it.seekEnd();
 
2125
    return it;
 
2126
}
 
2127
 
 
2128
 
 
2129
 
 
2130
///////////////////////////// SparseMat_ ////////////////////////////
 
2131
 
 
2132
template<typename _Tp> inline
 
2133
SparseMat_<_Tp>::SparseMat_()
 
2134
{
 
2135
    flags = MAGIC_VAL | DataType<_Tp>::type;
 
2136
}
 
2137
 
 
2138
template<typename _Tp> inline
 
2139
SparseMat_<_Tp>::SparseMat_(int _dims, const int* _sizes)
 
2140
    : SparseMat(_dims, _sizes, DataType<_Tp>::type)
 
2141
{}
 
2142
 
 
2143
template<typename _Tp> inline
 
2144
SparseMat_<_Tp>::SparseMat_(const SparseMat& m)
 
2145
{
 
2146
    if( m.type() == DataType<_Tp>::type )
 
2147
        *this = (const SparseMat_<_Tp>&)m;
 
2148
    else
 
2149
        m.convertTo(*this, DataType<_Tp>::type);
 
2150
}
 
2151
 
 
2152
template<typename _Tp> inline
 
2153
SparseMat_<_Tp>::SparseMat_(const SparseMat_<_Tp>& m)
 
2154
{
 
2155
    this->flags = m.flags;
 
2156
    this->hdr = m.hdr;
 
2157
    if( this->hdr )
 
2158
        CV_XADD(&this->hdr->refcount, 1);
 
2159
}
 
2160
 
 
2161
template<typename _Tp> inline
 
2162
SparseMat_<_Tp>::SparseMat_(const Mat& m)
 
2163
{
 
2164
    SparseMat sm(m);
 
2165
    *this = sm;
 
2166
}
 
2167
 
 
2168
template<typename _Tp> inline
 
2169
SparseMat_<_Tp>& SparseMat_<_Tp>::operator = (const SparseMat_<_Tp>& m)
 
2170
{
 
2171
    if( this != &m )
 
2172
    {
 
2173
        if( m.hdr ) CV_XADD(&m.hdr->refcount, 1);
 
2174
        release();
 
2175
        flags = m.flags;
 
2176
        hdr = m.hdr;
 
2177
    }
 
2178
    return *this;
 
2179
}
 
2180
 
 
2181
template<typename _Tp> inline
 
2182
SparseMat_<_Tp>& SparseMat_<_Tp>::operator = (const SparseMat& m)
 
2183
{
 
2184
    if( m.type() == DataType<_Tp>::type )
 
2185
        return (*this = (const SparseMat_<_Tp>&)m);
 
2186
    m.convertTo(*this, DataType<_Tp>::type);
 
2187
    return *this;
 
2188
}
 
2189
 
 
2190
template<typename _Tp> inline
 
2191
SparseMat_<_Tp>& SparseMat_<_Tp>::operator = (const Mat& m)
 
2192
{
 
2193
    return (*this = SparseMat(m));
 
2194
}
 
2195
 
 
2196
template<typename _Tp> inline
 
2197
SparseMat_<_Tp> SparseMat_<_Tp>::clone() const
 
2198
{
 
2199
    SparseMat_<_Tp> m;
 
2200
    this->copyTo(m);
 
2201
    return m;
 
2202
}
 
2203
 
 
2204
template<typename _Tp> inline
 
2205
void SparseMat_<_Tp>::create(int _dims, const int* _sizes)
 
2206
{
 
2207
    SparseMat::create(_dims, _sizes, DataType<_Tp>::type);
 
2208
}
 
2209
 
 
2210
template<typename _Tp> inline
 
2211
int SparseMat_<_Tp>::type() const
 
2212
{
 
2213
    return DataType<_Tp>::type;
 
2214
}
 
2215
 
 
2216
template<typename _Tp> inline
 
2217
int SparseMat_<_Tp>::depth() const
 
2218
{
 
2219
    return DataType<_Tp>::depth;
 
2220
}
 
2221
 
 
2222
template<typename _Tp> inline
 
2223
int SparseMat_<_Tp>::channels() const
 
2224
{
 
2225
    return DataType<_Tp>::channels;
 
2226
}
 
2227
 
 
2228
template<typename _Tp> inline
 
2229
_Tp& SparseMat_<_Tp>::ref(int i0, size_t* hashval)
 
2230
{
 
2231
    return SparseMat::ref<_Tp>(i0, hashval);
 
2232
}
 
2233
 
 
2234
template<typename _Tp> inline
 
2235
_Tp SparseMat_<_Tp>::operator()(int i0, size_t* hashval) const
 
2236
{
 
2237
    return SparseMat::value<_Tp>(i0, hashval);
 
2238
}
 
2239
 
 
2240
template<typename _Tp> inline
 
2241
_Tp& SparseMat_<_Tp>::ref(int i0, int i1, size_t* hashval)
 
2242
{
 
2243
    return SparseMat::ref<_Tp>(i0, i1, hashval);
 
2244
}
 
2245
 
 
2246
template<typename _Tp> inline
 
2247
_Tp SparseMat_<_Tp>::operator()(int i0, int i1, size_t* hashval) const
 
2248
{
 
2249
    return SparseMat::value<_Tp>(i0, i1, hashval);
 
2250
}
 
2251
 
 
2252
template<typename _Tp> inline
 
2253
_Tp& SparseMat_<_Tp>::ref(int i0, int i1, int i2, size_t* hashval)
 
2254
{
 
2255
    return SparseMat::ref<_Tp>(i0, i1, i2, hashval);
 
2256
}
 
2257
 
 
2258
template<typename _Tp> inline
 
2259
_Tp SparseMat_<_Tp>::operator()(int i0, int i1, int i2, size_t* hashval) const
 
2260
{
 
2261
    return SparseMat::value<_Tp>(i0, i1, i2, hashval);
 
2262
}
 
2263
 
 
2264
template<typename _Tp> inline
 
2265
_Tp& SparseMat_<_Tp>::ref(const int* idx, size_t* hashval)
 
2266
{
 
2267
    return SparseMat::ref<_Tp>(idx, hashval);
 
2268
}
 
2269
 
 
2270
template<typename _Tp> inline
 
2271
_Tp SparseMat_<_Tp>::operator()(const int* idx, size_t* hashval) const
 
2272
{
 
2273
    return SparseMat::value<_Tp>(idx, hashval);
 
2274
}
 
2275
 
 
2276
template<typename _Tp> inline
 
2277
SparseMatIterator_<_Tp> SparseMat_<_Tp>::begin()
 
2278
{
 
2279
    return SparseMatIterator_<_Tp>(this);
 
2280
}
 
2281
 
 
2282
template<typename _Tp> inline
 
2283
SparseMatConstIterator_<_Tp> SparseMat_<_Tp>::begin() const
 
2284
{
 
2285
    return SparseMatConstIterator_<_Tp>(this);
 
2286
}
 
2287
 
 
2288
template<typename _Tp> inline
 
2289
SparseMatIterator_<_Tp> SparseMat_<_Tp>::end()
 
2290
{
 
2291
    SparseMatIterator_<_Tp> it(this);
 
2292
    it.seekEnd();
 
2293
    return it;
 
2294
}
 
2295
 
 
2296
template<typename _Tp> inline
 
2297
SparseMatConstIterator_<_Tp> SparseMat_<_Tp>::end() const
 
2298
{
 
2299
    SparseMatConstIterator_<_Tp> it(this);
 
2300
    it.seekEnd();
 
2301
    return it;
 
2302
}
 
2303
 
 
2304
 
 
2305
 
 
2306
////////////////////////// MatConstIterator /////////////////////////
 
2307
 
 
2308
inline
 
2309
MatConstIterator::MatConstIterator()
 
2310
    : m(0), elemSize(0), ptr(0), sliceStart(0), sliceEnd(0)
 
2311
{}
 
2312
 
 
2313
inline
 
2314
MatConstIterator::MatConstIterator(const Mat* _m)
 
2315
    : m(_m), elemSize(_m->elemSize()), ptr(0), sliceStart(0), sliceEnd(0)
 
2316
{
 
2317
    if( m && m->isContinuous() )
 
2318
    {
 
2319
        sliceStart = m->ptr();
 
2320
        sliceEnd = sliceStart + m->total()*elemSize;
 
2321
    }
 
2322
    seek((const int*)0);
 
2323
}
 
2324
 
 
2325
inline
 
2326
MatConstIterator::MatConstIterator(const Mat* _m, int _row, int _col)
 
2327
    : m(_m), elemSize(_m->elemSize()), ptr(0), sliceStart(0), sliceEnd(0)
 
2328
{
 
2329
    CV_Assert(m && m->dims <= 2);
 
2330
    if( m->isContinuous() )
 
2331
    {
 
2332
        sliceStart = m->ptr();
 
2333
        sliceEnd = sliceStart + m->total()*elemSize;
 
2334
    }
 
2335
    int idx[] = {_row, _col};
 
2336
    seek(idx);
 
2337
}
 
2338
 
 
2339
inline
 
2340
MatConstIterator::MatConstIterator(const Mat* _m, Point _pt)
 
2341
    : m(_m), elemSize(_m->elemSize()), ptr(0), sliceStart(0), sliceEnd(0)
 
2342
{
 
2343
    CV_Assert(m && m->dims <= 2);
 
2344
    if( m->isContinuous() )
 
2345
    {
 
2346
        sliceStart = m->ptr();
 
2347
        sliceEnd = sliceStart + m->total()*elemSize;
 
2348
    }
 
2349
    int idx[] = {_pt.y, _pt.x};
 
2350
    seek(idx);
 
2351
}
 
2352
 
 
2353
inline
 
2354
MatConstIterator::MatConstIterator(const MatConstIterator& it)
 
2355
    : m(it.m), elemSize(it.elemSize), ptr(it.ptr), sliceStart(it.sliceStart), sliceEnd(it.sliceEnd)
 
2356
{}
 
2357
 
 
2358
inline
 
2359
MatConstIterator& MatConstIterator::operator = (const MatConstIterator& it )
 
2360
{
 
2361
    m = it.m; elemSize = it.elemSize; ptr = it.ptr;
 
2362
    sliceStart = it.sliceStart; sliceEnd = it.sliceEnd;
 
2363
    return *this;
 
2364
}
 
2365
 
 
2366
inline
 
2367
const uchar* MatConstIterator::operator *() const
 
2368
{
 
2369
    return ptr;
 
2370
}
 
2371
 
 
2372
inline MatConstIterator& MatConstIterator::operator += (ptrdiff_t ofs)
 
2373
{
 
2374
    if( !m || ofs == 0 )
 
2375
        return *this;
 
2376
    ptrdiff_t ofsb = ofs*elemSize;
 
2377
    ptr += ofsb;
 
2378
    if( ptr < sliceStart || sliceEnd <= ptr )
 
2379
    {
 
2380
        ptr -= ofsb;
 
2381
        seek(ofs, true);
 
2382
    }
 
2383
    return *this;
 
2384
}
 
2385
 
 
2386
inline
 
2387
MatConstIterator& MatConstIterator::operator -= (ptrdiff_t ofs)
 
2388
{
 
2389
    return (*this += -ofs);
 
2390
}
 
2391
 
 
2392
inline
 
2393
MatConstIterator& MatConstIterator::operator --()
 
2394
{
 
2395
    if( m && (ptr -= elemSize) < sliceStart )
 
2396
    {
 
2397
        ptr += elemSize;
 
2398
        seek(-1, true);
 
2399
    }
 
2400
    return *this;
 
2401
}
 
2402
 
 
2403
inline
 
2404
MatConstIterator MatConstIterator::operator --(int)
 
2405
{
 
2406
    MatConstIterator b = *this;
 
2407
    *this += -1;
 
2408
    return b;
 
2409
}
 
2410
 
 
2411
inline
 
2412
MatConstIterator& MatConstIterator::operator ++()
 
2413
{
 
2414
    if( m && (ptr += elemSize) >= sliceEnd )
 
2415
    {
 
2416
        ptr -= elemSize;
 
2417
        seek(1, true);
 
2418
    }
 
2419
    return *this;
 
2420
}
 
2421
 
 
2422
inline MatConstIterator MatConstIterator::operator ++(int)
 
2423
{
 
2424
    MatConstIterator b = *this;
 
2425
    *this += 1;
 
2426
    return b;
 
2427
}
 
2428
 
 
2429
 
 
2430
static inline
 
2431
bool operator == (const MatConstIterator& a, const MatConstIterator& b)
 
2432
{
 
2433
    return a.m == b.m && a.ptr == b.ptr;
 
2434
}
 
2435
 
 
2436
static inline
 
2437
bool operator != (const MatConstIterator& a, const MatConstIterator& b)
 
2438
{
 
2439
    return !(a == b);
 
2440
}
 
2441
 
 
2442
static inline
 
2443
bool operator < (const MatConstIterator& a, const MatConstIterator& b)
 
2444
{
 
2445
    return a.ptr < b.ptr;
 
2446
}
 
2447
 
 
2448
static inline
 
2449
bool operator > (const MatConstIterator& a, const MatConstIterator& b)
 
2450
{
 
2451
    return a.ptr > b.ptr;
 
2452
}
 
2453
 
 
2454
static inline
 
2455
bool operator <= (const MatConstIterator& a, const MatConstIterator& b)
 
2456
{
 
2457
    return a.ptr <= b.ptr;
 
2458
}
 
2459
 
 
2460
static inline
 
2461
bool operator >= (const MatConstIterator& a, const MatConstIterator& b)
 
2462
{
 
2463
    return a.ptr >= b.ptr;
 
2464
}
 
2465
 
 
2466
static inline
 
2467
ptrdiff_t operator - (const MatConstIterator& b, const MatConstIterator& a)
 
2468
{
 
2469
    if( a.m != b.m )
 
2470
        return ((size_t)(-1) >> 1);
 
2471
    if( a.sliceEnd == b.sliceEnd )
 
2472
        return (b.ptr - a.ptr)/b.elemSize;
 
2473
 
 
2474
    return b.lpos() - a.lpos();
 
2475
}
 
2476
 
 
2477
static inline
 
2478
MatConstIterator operator + (const MatConstIterator& a, ptrdiff_t ofs)
 
2479
{
 
2480
    MatConstIterator b = a;
 
2481
    return b += ofs;
 
2482
}
 
2483
 
 
2484
static inline
 
2485
MatConstIterator operator + (ptrdiff_t ofs, const MatConstIterator& a)
 
2486
{
 
2487
    MatConstIterator b = a;
 
2488
    return b += ofs;
 
2489
}
 
2490
 
 
2491
static inline
 
2492
MatConstIterator operator - (const MatConstIterator& a, ptrdiff_t ofs)
 
2493
{
 
2494
    MatConstIterator b = a;
 
2495
    return b += -ofs;
 
2496
}
 
2497
 
 
2498
 
 
2499
inline
 
2500
const uchar* MatConstIterator::operator [](ptrdiff_t i) const
 
2501
{
 
2502
    return *(*this + i);
 
2503
}
 
2504
 
 
2505
 
 
2506
 
 
2507
///////////////////////// MatConstIterator_ /////////////////////////
 
2508
 
 
2509
template<typename _Tp> inline
 
2510
MatConstIterator_<_Tp>::MatConstIterator_()
 
2511
{}
 
2512
 
 
2513
template<typename _Tp> inline
 
2514
MatConstIterator_<_Tp>::MatConstIterator_(const Mat_<_Tp>* _m)
 
2515
    : MatConstIterator(_m)
 
2516
{}
 
2517
 
 
2518
template<typename _Tp> inline
 
2519
MatConstIterator_<_Tp>::MatConstIterator_(const Mat_<_Tp>* _m, int _row, int _col)
 
2520
    : MatConstIterator(_m, _row, _col)
 
2521
{}
 
2522
 
 
2523
template<typename _Tp> inline
 
2524
MatConstIterator_<_Tp>::MatConstIterator_(const Mat_<_Tp>* _m, Point _pt)
 
2525
    : MatConstIterator(_m, _pt)
 
2526
{}
 
2527
 
 
2528
template<typename _Tp> inline
 
2529
MatConstIterator_<_Tp>::MatConstIterator_(const MatConstIterator_& it)
 
2530
    : MatConstIterator(it)
 
2531
{}
 
2532
 
 
2533
template<typename _Tp> inline
 
2534
MatConstIterator_<_Tp>& MatConstIterator_<_Tp>::operator = (const MatConstIterator_& it )
 
2535
{
 
2536
    MatConstIterator::operator = (it);
 
2537
    return *this;
 
2538
}
 
2539
 
 
2540
template<typename _Tp> inline
 
2541
_Tp MatConstIterator_<_Tp>::operator *() const
 
2542
{
 
2543
    return *(_Tp*)(this->ptr);
 
2544
}
 
2545
 
 
2546
template<typename _Tp> inline
 
2547
MatConstIterator_<_Tp>& MatConstIterator_<_Tp>::operator += (ptrdiff_t ofs)
 
2548
{
 
2549
    MatConstIterator::operator += (ofs);
 
2550
    return *this;
 
2551
}
 
2552
 
 
2553
template<typename _Tp> inline
 
2554
MatConstIterator_<_Tp>& MatConstIterator_<_Tp>::operator -= (ptrdiff_t ofs)
 
2555
{
 
2556
    return (*this += -ofs);
 
2557
}
 
2558
 
 
2559
template<typename _Tp> inline
 
2560
MatConstIterator_<_Tp>& MatConstIterator_<_Tp>::operator --()
 
2561
{
 
2562
    MatConstIterator::operator --();
 
2563
    return *this;
 
2564
}
 
2565
 
 
2566
template<typename _Tp> inline
 
2567
MatConstIterator_<_Tp> MatConstIterator_<_Tp>::operator --(int)
 
2568
{
 
2569
    MatConstIterator_ b = *this;
 
2570
    MatConstIterator::operator --();
 
2571
    return b;
 
2572
}
 
2573
 
 
2574
template<typename _Tp> inline
 
2575
MatConstIterator_<_Tp>& MatConstIterator_<_Tp>::operator ++()
 
2576
{
 
2577
    MatConstIterator::operator ++();
 
2578
    return *this;
 
2579
}
 
2580
 
 
2581
template<typename _Tp> inline
 
2582
MatConstIterator_<_Tp> MatConstIterator_<_Tp>::operator ++(int)
 
2583
{
 
2584
    MatConstIterator_ b = *this;
 
2585
    MatConstIterator::operator ++();
 
2586
    return b;
 
2587
}
 
2588
 
 
2589
 
 
2590
template<typename _Tp> inline
 
2591
Point MatConstIterator_<_Tp>::pos() const
 
2592
{
 
2593
    if( !m )
 
2594
        return Point();
 
2595
    CV_DbgAssert( m->dims <= 2 );
 
2596
    if( m->isContinuous() )
 
2597
    {
 
2598
        ptrdiff_t ofs = (const _Tp*)ptr - (const _Tp*)m->data;
 
2599
        int y = (int)(ofs / m->cols);
 
2600
        int x = (int)(ofs - (ptrdiff_t)y * m->cols);
 
2601
        return Point(x, y);
 
2602
    }
 
2603
    else
 
2604
    {
 
2605
        ptrdiff_t ofs = (uchar*)ptr - m->data;
 
2606
        int y = (int)(ofs / m->step);
 
2607
        int x = (int)((ofs - y * m->step)/sizeof(_Tp));
 
2608
        return Point(x, y);
 
2609
    }
 
2610
}
 
2611
 
 
2612
 
 
2613
template<typename _Tp> static inline
 
2614
bool operator == (const MatConstIterator_<_Tp>& a, const MatConstIterator_<_Tp>& b)
 
2615
{
 
2616
    return a.m == b.m && a.ptr == b.ptr;
 
2617
}
 
2618
 
 
2619
template<typename _Tp> static inline
 
2620
bool operator != (const MatConstIterator_<_Tp>& a, const MatConstIterator_<_Tp>& b)
 
2621
{
 
2622
    return a.m != b.m || a.ptr != b.ptr;
 
2623
}
 
2624
 
 
2625
template<typename _Tp> static inline
 
2626
MatConstIterator_<_Tp> operator + (const MatConstIterator_<_Tp>& a, ptrdiff_t ofs)
 
2627
{
 
2628
    MatConstIterator t = (const MatConstIterator&)a + ofs;
 
2629
    return (MatConstIterator_<_Tp>&)t;
 
2630
}
 
2631
 
 
2632
template<typename _Tp> static inline
 
2633
MatConstIterator_<_Tp> operator + (ptrdiff_t ofs, const MatConstIterator_<_Tp>& a)
 
2634
{
 
2635
    MatConstIterator t = (const MatConstIterator&)a + ofs;
 
2636
    return (MatConstIterator_<_Tp>&)t;
 
2637
}
 
2638
 
 
2639
template<typename _Tp> static inline
 
2640
MatConstIterator_<_Tp> operator - (const MatConstIterator_<_Tp>& a, ptrdiff_t ofs)
 
2641
{
 
2642
    MatConstIterator t = (const MatConstIterator&)a - ofs;
 
2643
    return (MatConstIterator_<_Tp>&)t;
 
2644
}
 
2645
 
 
2646
template<typename _Tp> inline
 
2647
_Tp MatConstIterator_<_Tp>::operator [](ptrdiff_t i) const
 
2648
{
 
2649
    return *(_Tp*)MatConstIterator::operator [](i);
 
2650
}
 
2651
 
 
2652
 
 
2653
 
 
2654
//////////////////////////// MatIterator_ ///////////////////////////
 
2655
 
 
2656
template<typename _Tp> inline
 
2657
MatIterator_<_Tp>::MatIterator_()
 
2658
    : MatConstIterator_<_Tp>()
 
2659
{}
 
2660
 
 
2661
template<typename _Tp> inline
 
2662
MatIterator_<_Tp>::MatIterator_(Mat_<_Tp>* _m)
 
2663
    : MatConstIterator_<_Tp>(_m)
 
2664
{}
 
2665
 
 
2666
template<typename _Tp> inline
 
2667
MatIterator_<_Tp>::MatIterator_(Mat_<_Tp>* _m, int _row, int _col)
 
2668
    : MatConstIterator_<_Tp>(_m, _row, _col)
 
2669
{}
 
2670
 
 
2671
template<typename _Tp> inline
 
2672
MatIterator_<_Tp>::MatIterator_(Mat_<_Tp>* _m, Point _pt)
 
2673
    : MatConstIterator_<_Tp>(_m, _pt)
 
2674
{}
 
2675
 
 
2676
template<typename _Tp> inline
 
2677
MatIterator_<_Tp>::MatIterator_(Mat_<_Tp>* _m, const int* _idx)
 
2678
    : MatConstIterator_<_Tp>(_m, _idx)
 
2679
{}
 
2680
 
 
2681
template<typename _Tp> inline
 
2682
MatIterator_<_Tp>::MatIterator_(const MatIterator_& it)
 
2683
    : MatConstIterator_<_Tp>(it)
 
2684
{}
 
2685
 
 
2686
template<typename _Tp> inline
 
2687
MatIterator_<_Tp>& MatIterator_<_Tp>::operator = (const MatIterator_<_Tp>& it )
 
2688
{
 
2689
    MatConstIterator::operator = (it);
 
2690
    return *this;
 
2691
}
 
2692
 
 
2693
template<typename _Tp> inline
 
2694
_Tp& MatIterator_<_Tp>::operator *() const
 
2695
{
 
2696
    return *(_Tp*)(this->ptr);
 
2697
}
 
2698
 
 
2699
template<typename _Tp> inline
 
2700
MatIterator_<_Tp>& MatIterator_<_Tp>::operator += (ptrdiff_t ofs)
 
2701
{
 
2702
    MatConstIterator::operator += (ofs);
 
2703
    return *this;
 
2704
}
 
2705
 
 
2706
template<typename _Tp> inline
 
2707
MatIterator_<_Tp>& MatIterator_<_Tp>::operator -= (ptrdiff_t ofs)
 
2708
{
 
2709
    MatConstIterator::operator += (-ofs);
 
2710
    return *this;
 
2711
}
 
2712
 
 
2713
template<typename _Tp> inline
 
2714
MatIterator_<_Tp>& MatIterator_<_Tp>::operator --()
 
2715
{
 
2716
    MatConstIterator::operator --();
 
2717
    return *this;
 
2718
}
 
2719
 
 
2720
template<typename _Tp> inline
 
2721
MatIterator_<_Tp> MatIterator_<_Tp>::operator --(int)
 
2722
{
 
2723
    MatIterator_ b = *this;
 
2724
    MatConstIterator::operator --();
 
2725
    return b;
 
2726
}
 
2727
 
 
2728
template<typename _Tp> inline
 
2729
MatIterator_<_Tp>& MatIterator_<_Tp>::operator ++()
 
2730
{
 
2731
    MatConstIterator::operator ++();
 
2732
    return *this;
 
2733
}
 
2734
 
 
2735
template<typename _Tp> inline
 
2736
MatIterator_<_Tp> MatIterator_<_Tp>::operator ++(int)
 
2737
{
 
2738
    MatIterator_ b = *this;
 
2739
    MatConstIterator::operator ++();
 
2740
    return b;
 
2741
}
 
2742
 
 
2743
template<typename _Tp> inline
 
2744
_Tp& MatIterator_<_Tp>::operator [](ptrdiff_t i) const
 
2745
{
 
2746
    return *(*this + i);
 
2747
}
 
2748
 
 
2749
 
 
2750
template<typename _Tp> static inline
 
2751
bool operator == (const MatIterator_<_Tp>& a, const MatIterator_<_Tp>& b)
 
2752
{
 
2753
    return a.m == b.m && a.ptr == b.ptr;
 
2754
}
 
2755
 
 
2756
template<typename _Tp> static inline
 
2757
bool operator != (const MatIterator_<_Tp>& a, const MatIterator_<_Tp>& b)
 
2758
{
 
2759
    return a.m != b.m || a.ptr != b.ptr;
 
2760
}
 
2761
 
 
2762
template<typename _Tp> static inline
 
2763
MatIterator_<_Tp> operator + (const MatIterator_<_Tp>& a, ptrdiff_t ofs)
 
2764
{
 
2765
    MatConstIterator t = (const MatConstIterator&)a + ofs;
 
2766
    return (MatIterator_<_Tp>&)t;
 
2767
}
 
2768
 
 
2769
template<typename _Tp> static inline
 
2770
MatIterator_<_Tp> operator + (ptrdiff_t ofs, const MatIterator_<_Tp>& a)
 
2771
{
 
2772
    MatConstIterator t = (const MatConstIterator&)a + ofs;
 
2773
    return (MatIterator_<_Tp>&)t;
 
2774
}
 
2775
 
 
2776
template<typename _Tp> static inline
 
2777
MatIterator_<_Tp> operator - (const MatIterator_<_Tp>& a, ptrdiff_t ofs)
 
2778
{
 
2779
    MatConstIterator t = (const MatConstIterator&)a - ofs;
 
2780
    return (MatIterator_<_Tp>&)t;
 
2781
}
 
2782
 
 
2783
 
 
2784
 
 
2785
/////////////////////// SparseMatConstIterator //////////////////////
 
2786
 
 
2787
inline
 
2788
SparseMatConstIterator::SparseMatConstIterator()
 
2789
    : m(0), hashidx(0), ptr(0)
 
2790
{}
 
2791
 
 
2792
inline
 
2793
SparseMatConstIterator::SparseMatConstIterator(const SparseMatConstIterator& it)
 
2794
    : m(it.m), hashidx(it.hashidx), ptr(it.ptr)
 
2795
{}
 
2796
 
 
2797
inline SparseMatConstIterator& SparseMatConstIterator::operator = (const SparseMatConstIterator& it)
 
2798
{
 
2799
    if( this != &it )
 
2800
    {
 
2801
        m = it.m;
 
2802
        hashidx = it.hashidx;
 
2803
        ptr = it.ptr;
 
2804
    }
 
2805
    return *this;
 
2806
}
 
2807
 
 
2808
template<typename _Tp> inline
 
2809
const _Tp& SparseMatConstIterator::value() const
 
2810
{
 
2811
    return *(const _Tp*)ptr;
 
2812
}
 
2813
 
 
2814
inline
 
2815
const SparseMat::Node* SparseMatConstIterator::node() const
 
2816
{
 
2817
    return (ptr && m && m->hdr) ? (const SparseMat::Node*)(const void*)(ptr - m->hdr->valueOffset) : 0;
 
2818
}
 
2819
 
 
2820
inline
 
2821
SparseMatConstIterator SparseMatConstIterator::operator ++(int)
 
2822
{
 
2823
    SparseMatConstIterator it = *this;
 
2824
    ++*this;
 
2825
    return it;
 
2826
}
 
2827
 
 
2828
inline
 
2829
void SparseMatConstIterator::seekEnd()
 
2830
{
 
2831
    if( m && m->hdr )
 
2832
    {
 
2833
        hashidx = m->hdr->hashtab.size();
 
2834
        ptr = 0;
 
2835
    }
 
2836
}
 
2837
 
 
2838
 
 
2839
static inline
 
2840
bool operator == (const SparseMatConstIterator& it1, const SparseMatConstIterator& it2)
 
2841
{
 
2842
    return it1.m == it2.m && it1.ptr == it2.ptr;
 
2843
}
 
2844
 
 
2845
static inline
 
2846
bool operator != (const SparseMatConstIterator& it1, const SparseMatConstIterator& it2)
 
2847
{
 
2848
    return !(it1 == it2);
 
2849
}
 
2850
 
 
2851
 
 
2852
 
 
2853
///////////////////////// SparseMatIterator /////////////////////////
 
2854
 
 
2855
inline
 
2856
SparseMatIterator::SparseMatIterator()
 
2857
{}
 
2858
 
 
2859
inline
 
2860
SparseMatIterator::SparseMatIterator(SparseMat* _m)
 
2861
    : SparseMatConstIterator(_m)
 
2862
{}
 
2863
 
 
2864
inline
 
2865
SparseMatIterator::SparseMatIterator(const SparseMatIterator& it)
 
2866
    : SparseMatConstIterator(it)
 
2867
{}
 
2868
 
 
2869
inline
 
2870
SparseMatIterator& SparseMatIterator::operator = (const SparseMatIterator& it)
 
2871
{
 
2872
    (SparseMatConstIterator&)*this = it;
 
2873
    return *this;
 
2874
}
 
2875
 
 
2876
template<typename _Tp> inline
 
2877
_Tp& SparseMatIterator::value() const
 
2878
{
 
2879
    return *(_Tp*)ptr;
 
2880
}
 
2881
 
 
2882
inline
 
2883
SparseMat::Node* SparseMatIterator::node() const
 
2884
{
 
2885
    return (SparseMat::Node*)SparseMatConstIterator::node();
 
2886
}
 
2887
 
 
2888
inline
 
2889
SparseMatIterator& SparseMatIterator::operator ++()
 
2890
{
 
2891
    SparseMatConstIterator::operator ++();
 
2892
    return *this;
 
2893
}
 
2894
 
 
2895
inline
 
2896
SparseMatIterator SparseMatIterator::operator ++(int)
 
2897
{
 
2898
    SparseMatIterator it = *this;
 
2899
    ++*this;
 
2900
    return it;
 
2901
}
 
2902
 
 
2903
 
 
2904
 
 
2905
////////////////////// SparseMatConstIterator_ //////////////////////
 
2906
 
 
2907
template<typename _Tp> inline
 
2908
SparseMatConstIterator_<_Tp>::SparseMatConstIterator_()
 
2909
{}
 
2910
 
 
2911
template<typename _Tp> inline
 
2912
SparseMatConstIterator_<_Tp>::SparseMatConstIterator_(const SparseMat_<_Tp>* _m)
 
2913
    : SparseMatConstIterator(_m)
 
2914
{}
 
2915
 
 
2916
template<typename _Tp> inline
 
2917
SparseMatConstIterator_<_Tp>::SparseMatConstIterator_(const SparseMat* _m)
 
2918
    : SparseMatConstIterator(_m)
 
2919
{
 
2920
    CV_Assert( _m->type() == DataType<_Tp>::type );
 
2921
}
 
2922
 
 
2923
template<typename _Tp> inline
 
2924
SparseMatConstIterator_<_Tp>::SparseMatConstIterator_(const SparseMatConstIterator_<_Tp>& it)
 
2925
    : SparseMatConstIterator(it)
 
2926
{}
 
2927
 
 
2928
template<typename _Tp> inline
 
2929
SparseMatConstIterator_<_Tp>& SparseMatConstIterator_<_Tp>::operator = (const SparseMatConstIterator_<_Tp>& it)
 
2930
{
 
2931
    return reinterpret_cast<SparseMatConstIterator_<_Tp>&>
 
2932
         (*reinterpret_cast<SparseMatConstIterator*>(this) =
 
2933
           reinterpret_cast<const SparseMatConstIterator&>(it));
 
2934
}
 
2935
 
 
2936
template<typename _Tp> inline
 
2937
const _Tp& SparseMatConstIterator_<_Tp>::operator *() const
 
2938
{
 
2939
    return *(const _Tp*)this->ptr;
 
2940
}
 
2941
 
 
2942
template<typename _Tp> inline
 
2943
SparseMatConstIterator_<_Tp>& SparseMatConstIterator_<_Tp>::operator ++()
 
2944
{
 
2945
    SparseMatConstIterator::operator ++();
 
2946
    return *this;
 
2947
}
 
2948
 
 
2949
template<typename _Tp> inline
 
2950
SparseMatConstIterator_<_Tp> SparseMatConstIterator_<_Tp>::operator ++(int)
 
2951
{
 
2952
    SparseMatConstIterator_<_Tp> it = *this;
 
2953
    SparseMatConstIterator::operator ++();
 
2954
    return it;
 
2955
}
 
2956
 
 
2957
 
 
2958
 
 
2959
///////////////////////// SparseMatIterator_ ////////////////////////
 
2960
 
 
2961
template<typename _Tp> inline
 
2962
SparseMatIterator_<_Tp>::SparseMatIterator_()
 
2963
{}
 
2964
 
 
2965
template<typename _Tp> inline
 
2966
SparseMatIterator_<_Tp>::SparseMatIterator_(SparseMat_<_Tp>* _m)
 
2967
    : SparseMatConstIterator_<_Tp>(_m)
 
2968
{}
 
2969
 
 
2970
template<typename _Tp> inline
 
2971
SparseMatIterator_<_Tp>::SparseMatIterator_(SparseMat* _m)
 
2972
    : SparseMatConstIterator_<_Tp>(_m)
 
2973
{}
 
2974
 
 
2975
template<typename _Tp> inline
 
2976
SparseMatIterator_<_Tp>::SparseMatIterator_(const SparseMatIterator_<_Tp>& it)
 
2977
    : SparseMatConstIterator_<_Tp>(it)
 
2978
{}
 
2979
 
 
2980
template<typename _Tp> inline
 
2981
SparseMatIterator_<_Tp>& SparseMatIterator_<_Tp>::operator = (const SparseMatIterator_<_Tp>& it)
 
2982
{
 
2983
    return reinterpret_cast<SparseMatIterator_<_Tp>&>
 
2984
         (*reinterpret_cast<SparseMatConstIterator*>(this) =
 
2985
           reinterpret_cast<const SparseMatConstIterator&>(it));
 
2986
}
 
2987
 
 
2988
template<typename _Tp> inline
 
2989
_Tp& SparseMatIterator_<_Tp>::operator *() const
 
2990
{
 
2991
    return *(_Tp*)this->ptr;
 
2992
}
 
2993
 
 
2994
template<typename _Tp> inline
 
2995
SparseMatIterator_<_Tp>& SparseMatIterator_<_Tp>::operator ++()
 
2996
{
 
2997
    SparseMatConstIterator::operator ++();
 
2998
    return *this;
 
2999
}
 
3000
 
 
3001
template<typename _Tp> inline
 
3002
SparseMatIterator_<_Tp> SparseMatIterator_<_Tp>::operator ++(int)
 
3003
{
 
3004
    SparseMatIterator_<_Tp> it = *this;
 
3005
    SparseMatConstIterator::operator ++();
 
3006
    return it;
 
3007
}
 
3008
 
 
3009
 
 
3010
 
 
3011
//////////////////////// MatCommaInitializer_ ///////////////////////
 
3012
 
 
3013
template<typename _Tp> inline
 
3014
MatCommaInitializer_<_Tp>::MatCommaInitializer_(Mat_<_Tp>* _m)
 
3015
    : it(_m)
 
3016
{}
 
3017
 
 
3018
template<typename _Tp> template<typename T2> inline
 
3019
MatCommaInitializer_<_Tp>& MatCommaInitializer_<_Tp>::operator , (T2 v)
 
3020
{
 
3021
    CV_DbgAssert( this->it < ((const Mat_<_Tp>*)this->it.m)->end() );
 
3022
    *this->it = _Tp(v);
 
3023
    ++this->it;
 
3024
    return *this;
 
3025
}
 
3026
 
 
3027
template<typename _Tp> inline
 
3028
MatCommaInitializer_<_Tp>::operator Mat_<_Tp>() const
 
3029
{
 
3030
    CV_DbgAssert( this->it == ((const Mat_<_Tp>*)this->it.m)->end() );
 
3031
    return Mat_<_Tp>(*this->it.m);
 
3032
}
 
3033
 
 
3034
 
 
3035
template<typename _Tp, typename T2> static inline
 
3036
MatCommaInitializer_<_Tp> operator << (const Mat_<_Tp>& m, T2 val)
 
3037
{
 
3038
    MatCommaInitializer_<_Tp> commaInitializer((Mat_<_Tp>*)&m);
 
3039
    return (commaInitializer, val);
 
3040
}
 
3041
 
 
3042
 
 
3043
 
 
3044
///////////////////////// Matrix Expressions ////////////////////////
 
3045
 
 
3046
inline
 
3047
Mat& Mat::operator = (const MatExpr& e)
 
3048
{
 
3049
    e.op->assign(e, *this);
 
3050
    return *this;
 
3051
}
 
3052
 
 
3053
template<typename _Tp> inline
 
3054
Mat_<_Tp>::Mat_(const MatExpr& e)
 
3055
{
 
3056
    e.op->assign(e, *this, DataType<_Tp>::type);
 
3057
}
 
3058
 
 
3059
template<typename _Tp> inline
 
3060
Mat_<_Tp>& Mat_<_Tp>::operator = (const MatExpr& e)
 
3061
{
 
3062
    e.op->assign(e, *this, DataType<_Tp>::type);
 
3063
    return *this;
 
3064
}
 
3065
 
 
3066
template<typename _Tp> inline
 
3067
MatExpr Mat_<_Tp>::zeros(int rows, int cols)
 
3068
{
 
3069
    return Mat::zeros(rows, cols, DataType<_Tp>::type);
 
3070
}
 
3071
 
 
3072
template<typename _Tp> inline
 
3073
MatExpr Mat_<_Tp>::zeros(Size sz)
 
3074
{
 
3075
    return Mat::zeros(sz, DataType<_Tp>::type);
 
3076
}
 
3077
 
 
3078
template<typename _Tp> inline
 
3079
MatExpr Mat_<_Tp>::ones(int rows, int cols)
 
3080
{
 
3081
    return Mat::ones(rows, cols, DataType<_Tp>::type);
 
3082
}
 
3083
 
 
3084
template<typename _Tp> inline
 
3085
MatExpr Mat_<_Tp>::ones(Size sz)
 
3086
{
 
3087
    return Mat::ones(sz, DataType<_Tp>::type);
 
3088
}
 
3089
 
 
3090
template<typename _Tp> inline
 
3091
MatExpr Mat_<_Tp>::eye(int rows, int cols)
 
3092
{
 
3093
    return Mat::eye(rows, cols, DataType<_Tp>::type);
 
3094
}
 
3095
 
 
3096
template<typename _Tp> inline
 
3097
MatExpr Mat_<_Tp>::eye(Size sz)
 
3098
{
 
3099
    return Mat::eye(sz, DataType<_Tp>::type);
 
3100
}
 
3101
 
 
3102
inline
 
3103
MatExpr::MatExpr()
 
3104
    : op(0), flags(0), a(Mat()), b(Mat()), c(Mat()), alpha(0), beta(0), s()
 
3105
{}
 
3106
 
 
3107
inline
 
3108
MatExpr::MatExpr(const MatOp* _op, int _flags, const Mat& _a, const Mat& _b,
 
3109
                 const Mat& _c, double _alpha, double _beta, const Scalar& _s)
 
3110
    : op(_op), flags(_flags), a(_a), b(_b), c(_c), alpha(_alpha), beta(_beta), s(_s)
 
3111
{}
 
3112
 
 
3113
inline
 
3114
MatExpr::operator Mat() const
 
3115
{
 
3116
    Mat m;
 
3117
    op->assign(*this, m);
 
3118
    return m;
 
3119
}
 
3120
 
 
3121
template<typename _Tp> inline
 
3122
MatExpr::operator Mat_<_Tp>() const
 
3123
{
 
3124
    Mat_<_Tp> m;
 
3125
    op->assign(*this, m, DataType<_Tp>::type);
 
3126
    return m;
 
3127
}
 
3128
 
 
3129
 
 
3130
template<typename _Tp> static inline
 
3131
MatExpr min(const Mat_<_Tp>& a, const Mat_<_Tp>& b)
 
3132
{
 
3133
    return cv::min((const Mat&)a, (const Mat&)b);
 
3134
}
 
3135
 
 
3136
template<typename _Tp> static inline
 
3137
MatExpr min(const Mat_<_Tp>& a, double s)
 
3138
{
 
3139
    return cv::min((const Mat&)a, s);
 
3140
}
 
3141
 
 
3142
template<typename _Tp> static inline
 
3143
MatExpr min(double s, const Mat_<_Tp>& a)
 
3144
{
 
3145
    return cv::min((const Mat&)a, s);
 
3146
}
 
3147
 
 
3148
template<typename _Tp> static inline
 
3149
MatExpr max(const Mat_<_Tp>& a, const Mat_<_Tp>& b)
 
3150
{
 
3151
    return cv::max((const Mat&)a, (const Mat&)b);
 
3152
}
 
3153
 
 
3154
template<typename _Tp> static inline
 
3155
MatExpr max(const Mat_<_Tp>& a, double s)
 
3156
{
 
3157
    return cv::max((const Mat&)a, s);
 
3158
}
 
3159
 
 
3160
template<typename _Tp> static inline
 
3161
MatExpr max(double s, const Mat_<_Tp>& a)
 
3162
{
 
3163
    return cv::max((const Mat&)a, s);
 
3164
}
 
3165
 
 
3166
template<typename _Tp> static inline
 
3167
MatExpr abs(const Mat_<_Tp>& m)
 
3168
{
 
3169
    return cv::abs((const Mat&)m);
 
3170
}
 
3171
 
 
3172
 
 
3173
static inline
 
3174
Mat& operator += (Mat& a, const MatExpr& b)
 
3175
{
 
3176
    b.op->augAssignAdd(b, a);
 
3177
    return a;
 
3178
}
 
3179
 
 
3180
static inline
 
3181
const Mat& operator += (const Mat& a, const MatExpr& b)
 
3182
{
 
3183
    b.op->augAssignAdd(b, (Mat&)a);
 
3184
    return a;
 
3185
}
 
3186
 
 
3187
template<typename _Tp> static inline
 
3188
Mat_<_Tp>& operator += (Mat_<_Tp>& a, const MatExpr& b)
 
3189
{
 
3190
    b.op->augAssignAdd(b, a);
 
3191
    return a;
 
3192
}
 
3193
 
 
3194
template<typename _Tp> static inline
 
3195
const Mat_<_Tp>& operator += (const Mat_<_Tp>& a, const MatExpr& b)
 
3196
{
 
3197
    b.op->augAssignAdd(b, (Mat&)a);
 
3198
    return a;
 
3199
}
 
3200
 
 
3201
static inline
 
3202
Mat& operator -= (Mat& a, const MatExpr& b)
 
3203
{
 
3204
    b.op->augAssignSubtract(b, a);
 
3205
    return a;
 
3206
}
 
3207
 
 
3208
static inline
 
3209
const Mat& operator -= (const Mat& a, const MatExpr& b)
 
3210
{
 
3211
    b.op->augAssignSubtract(b, (Mat&)a);
 
3212
    return a;
 
3213
}
 
3214
 
 
3215
template<typename _Tp> static inline
 
3216
Mat_<_Tp>& operator -= (Mat_<_Tp>& a, const MatExpr& b)
 
3217
{
 
3218
    b.op->augAssignSubtract(b, a);
 
3219
    return a;
 
3220
}
 
3221
 
 
3222
template<typename _Tp> static inline
 
3223
const Mat_<_Tp>& operator -= (const Mat_<_Tp>& a, const MatExpr& b)
 
3224
{
 
3225
    b.op->augAssignSubtract(b, (Mat&)a);
 
3226
    return a;
 
3227
}
 
3228
 
 
3229
static inline
 
3230
Mat& operator *= (Mat& a, const MatExpr& b)
 
3231
{
 
3232
    b.op->augAssignMultiply(b, a);
 
3233
    return a;
 
3234
}
 
3235
 
 
3236
static inline
 
3237
const Mat& operator *= (const Mat& a, const MatExpr& b)
 
3238
{
 
3239
    b.op->augAssignMultiply(b, (Mat&)a);
 
3240
    return a;
 
3241
}
 
3242
 
 
3243
template<typename _Tp> static inline
 
3244
Mat_<_Tp>& operator *= (Mat_<_Tp>& a, const MatExpr& b)
 
3245
{
 
3246
    b.op->augAssignMultiply(b, a);
 
3247
    return a;
 
3248
}
 
3249
 
 
3250
template<typename _Tp> static inline
 
3251
const Mat_<_Tp>& operator *= (const Mat_<_Tp>& a, const MatExpr& b)
 
3252
{
 
3253
    b.op->augAssignMultiply(b, (Mat&)a);
 
3254
    return a;
 
3255
}
 
3256
 
 
3257
static inline
 
3258
Mat& operator /= (Mat& a, const MatExpr& b)
 
3259
{
 
3260
    b.op->augAssignDivide(b, a);
 
3261
    return a;
 
3262
}
 
3263
 
 
3264
static inline
 
3265
const Mat& operator /= (const Mat& a, const MatExpr& b)
 
3266
{
 
3267
    b.op->augAssignDivide(b, (Mat&)a);
 
3268
    return a;
 
3269
}
 
3270
 
 
3271
template<typename _Tp> static inline
 
3272
Mat_<_Tp>& operator /= (Mat_<_Tp>& a, const MatExpr& b)
 
3273
{
 
3274
    b.op->augAssignDivide(b, a);
 
3275
    return a;
 
3276
}
 
3277
 
 
3278
template<typename _Tp> static inline
 
3279
const Mat_<_Tp>& operator /= (const Mat_<_Tp>& a, const MatExpr& b)
 
3280
{
 
3281
    b.op->augAssignDivide(b, (Mat&)a);
 
3282
    return a;
 
3283
}
 
3284
 
 
3285
 
 
3286
//////////////////////////////// UMat ////////////////////////////////
 
3287
 
 
3288
inline
 
3289
UMat::UMat(UMatUsageFlags _usageFlags)
 
3290
: flags(MAGIC_VAL), dims(0), rows(0), cols(0), allocator(0), usageFlags(_usageFlags), u(0), offset(0), size(&rows)
 
3291
{}
 
3292
 
 
3293
inline
 
3294
UMat::UMat(int _rows, int _cols, int _type, UMatUsageFlags _usageFlags)
 
3295
: flags(MAGIC_VAL), dims(0), rows(0), cols(0), allocator(0), usageFlags(_usageFlags), u(0), offset(0), size(&rows)
 
3296
{
 
3297
    create(_rows, _cols, _type);
 
3298
}
 
3299
 
 
3300
inline
 
3301
UMat::UMat(int _rows, int _cols, int _type, const Scalar& _s, UMatUsageFlags _usageFlags)
 
3302
: flags(MAGIC_VAL), dims(0), rows(0), cols(0), allocator(0), usageFlags(_usageFlags), u(0), offset(0), size(&rows)
 
3303
{
 
3304
    create(_rows, _cols, _type);
 
3305
    *this = _s;
 
3306
}
 
3307
 
 
3308
inline
 
3309
UMat::UMat(Size _sz, int _type, UMatUsageFlags _usageFlags)
 
3310
: flags(MAGIC_VAL), dims(0), rows(0), cols(0), allocator(0), usageFlags(_usageFlags), u(0), offset(0), size(&rows)
 
3311
{
 
3312
    create( _sz.height, _sz.width, _type );
 
3313
}
 
3314
 
 
3315
inline
 
3316
UMat::UMat(Size _sz, int _type, const Scalar& _s, UMatUsageFlags _usageFlags)
 
3317
: flags(MAGIC_VAL), dims(0), rows(0), cols(0), allocator(0), usageFlags(_usageFlags), u(0), offset(0), size(&rows)
 
3318
{
 
3319
    create(_sz.height, _sz.width, _type);
 
3320
    *this = _s;
 
3321
}
 
3322
 
 
3323
inline
 
3324
UMat::UMat(int _dims, const int* _sz, int _type, UMatUsageFlags _usageFlags)
 
3325
: flags(MAGIC_VAL), dims(0), rows(0), cols(0), allocator(0), usageFlags(_usageFlags), u(0), offset(0), size(&rows)
 
3326
{
 
3327
    create(_dims, _sz, _type);
 
3328
}
 
3329
 
 
3330
inline
 
3331
UMat::UMat(int _dims, const int* _sz, int _type, const Scalar& _s, UMatUsageFlags _usageFlags)
 
3332
: flags(MAGIC_VAL), dims(0), rows(0), cols(0), allocator(0), usageFlags(_usageFlags), u(0), offset(0), size(&rows)
 
3333
{
 
3334
    create(_dims, _sz, _type);
 
3335
    *this = _s;
 
3336
}
 
3337
 
 
3338
inline
 
3339
UMat::UMat(const UMat& m)
 
3340
: flags(m.flags), dims(m.dims), rows(m.rows), cols(m.cols), allocator(m.allocator),
 
3341
  usageFlags(m.usageFlags), u(m.u), offset(m.offset), size(&rows)
 
3342
{
 
3343
    addref();
 
3344
    if( m.dims <= 2 )
 
3345
    {
 
3346
        step[0] = m.step[0]; step[1] = m.step[1];
 
3347
    }
 
3348
    else
 
3349
    {
 
3350
        dims = 0;
 
3351
        copySize(m);
 
3352
    }
 
3353
}
 
3354
 
 
3355
 
 
3356
template<typename _Tp> inline
 
3357
UMat::UMat(const std::vector<_Tp>& vec, bool copyData)
 
3358
: flags(MAGIC_VAL | DataType<_Tp>::type | CV_MAT_CONT_FLAG), dims(2), rows((int)vec.size()),
 
3359
cols(1), allocator(0), usageFlags(USAGE_DEFAULT), u(0), offset(0), size(&rows)
 
3360
{
 
3361
    if(vec.empty())
 
3362
        return;
 
3363
    if( !copyData )
 
3364
    {
 
3365
        // !!!TODO!!!
 
3366
        CV_Error(Error::StsNotImplemented, "");
 
3367
    }
 
3368
    else
 
3369
        Mat((int)vec.size(), 1, DataType<_Tp>::type, (uchar*)&vec[0]).copyTo(*this);
 
3370
}
 
3371
 
 
3372
 
 
3373
inline
 
3374
UMat& UMat::operator = (const UMat& m)
 
3375
{
 
3376
    if( this != &m )
 
3377
    {
 
3378
        const_cast<UMat&>(m).addref();
 
3379
        release();
 
3380
        flags = m.flags;
 
3381
        if( dims <= 2 && m.dims <= 2 )
 
3382
        {
 
3383
            dims = m.dims;
 
3384
            rows = m.rows;
 
3385
            cols = m.cols;
 
3386
            step[0] = m.step[0];
 
3387
            step[1] = m.step[1];
 
3388
        }
 
3389
        else
 
3390
            copySize(m);
 
3391
        allocator = m.allocator;
 
3392
        if (usageFlags == USAGE_DEFAULT)
 
3393
            usageFlags = m.usageFlags;
 
3394
        u = m.u;
 
3395
        offset = m.offset;
 
3396
    }
 
3397
    return *this;
 
3398
}
 
3399
 
 
3400
inline
 
3401
UMat UMat::row(int y) const
 
3402
{
 
3403
    return UMat(*this, Range(y, y + 1), Range::all());
 
3404
}
 
3405
 
 
3406
inline
 
3407
UMat UMat::col(int x) const
 
3408
{
 
3409
    return UMat(*this, Range::all(), Range(x, x + 1));
 
3410
}
 
3411
 
 
3412
inline
 
3413
UMat UMat::rowRange(int startrow, int endrow) const
 
3414
{
 
3415
    return UMat(*this, Range(startrow, endrow), Range::all());
 
3416
}
 
3417
 
 
3418
inline
 
3419
UMat UMat::rowRange(const Range& r) const
 
3420
{
 
3421
    return UMat(*this, r, Range::all());
 
3422
}
 
3423
 
 
3424
inline
 
3425
UMat UMat::colRange(int startcol, int endcol) const
 
3426
{
 
3427
    return UMat(*this, Range::all(), Range(startcol, endcol));
 
3428
}
 
3429
 
 
3430
inline
 
3431
UMat UMat::colRange(const Range& r) const
 
3432
{
 
3433
    return UMat(*this, Range::all(), r);
 
3434
}
 
3435
 
 
3436
inline
 
3437
UMat UMat::clone() const
 
3438
{
 
3439
    UMat m;
 
3440
    copyTo(m);
 
3441
    return m;
 
3442
}
 
3443
 
 
3444
inline
 
3445
void UMat::assignTo( UMat& m, int _type ) const
 
3446
{
 
3447
    if( _type < 0 )
 
3448
        m = *this;
 
3449
    else
 
3450
        convertTo(m, _type);
 
3451
}
 
3452
 
 
3453
inline
 
3454
void UMat::create(int _rows, int _cols, int _type, UMatUsageFlags _usageFlags)
 
3455
{
 
3456
    _type &= TYPE_MASK;
 
3457
    if( dims <= 2 && rows == _rows && cols == _cols && type() == _type && u )
 
3458
        return;
 
3459
    int sz[] = {_rows, _cols};
 
3460
    create(2, sz, _type, _usageFlags);
 
3461
}
 
3462
 
 
3463
inline
 
3464
void UMat::create(Size _sz, int _type, UMatUsageFlags _usageFlags)
 
3465
{
 
3466
    create(_sz.height, _sz.width, _type, _usageFlags);
 
3467
}
 
3468
 
 
3469
inline
 
3470
void UMat::addref()
 
3471
{
 
3472
    if( u )
 
3473
        CV_XADD(&(u->urefcount), 1);
 
3474
}
 
3475
 
 
3476
inline void UMat::release()
 
3477
{
 
3478
    if( u && CV_XADD(&(u->urefcount), -1) == 1 )
 
3479
        deallocate();
 
3480
    for(int i = 0; i < dims; i++)
 
3481
        size.p[i] = 0;
 
3482
    u = 0;
 
3483
}
 
3484
 
 
3485
inline
 
3486
UMat UMat::operator()( Range _rowRange, Range _colRange ) const
 
3487
{
 
3488
    return UMat(*this, _rowRange, _colRange);
 
3489
}
 
3490
 
 
3491
inline
 
3492
UMat UMat::operator()( const Rect& roi ) const
 
3493
{
 
3494
    return UMat(*this, roi);
 
3495
}
 
3496
 
 
3497
inline
 
3498
UMat UMat::operator()(const Range* ranges) const
 
3499
{
 
3500
    return UMat(*this, ranges);
 
3501
}
 
3502
 
 
3503
inline
 
3504
bool UMat::isContinuous() const
 
3505
{
 
3506
    return (flags & CONTINUOUS_FLAG) != 0;
 
3507
}
 
3508
 
 
3509
inline
 
3510
bool UMat::isSubmatrix() const
 
3511
{
 
3512
    return (flags & SUBMATRIX_FLAG) != 0;
 
3513
}
 
3514
 
 
3515
inline
 
3516
size_t UMat::elemSize() const
 
3517
{
 
3518
    return dims > 0 ? step.p[dims - 1] : 0;
 
3519
}
 
3520
 
 
3521
inline
 
3522
size_t UMat::elemSize1() const
 
3523
{
 
3524
    return CV_ELEM_SIZE1(flags);
 
3525
}
 
3526
 
 
3527
inline
 
3528
int UMat::type() const
 
3529
{
 
3530
    return CV_MAT_TYPE(flags);
 
3531
}
 
3532
 
 
3533
inline
 
3534
int UMat::depth() const
 
3535
{
 
3536
    return CV_MAT_DEPTH(flags);
 
3537
}
 
3538
 
 
3539
inline
 
3540
int UMat::channels() const
 
3541
{
 
3542
    return CV_MAT_CN(flags);
 
3543
}
 
3544
 
 
3545
inline
 
3546
size_t UMat::step1(int i) const
 
3547
{
 
3548
    return step.p[i] / elemSize1();
 
3549
}
 
3550
 
 
3551
inline
 
3552
bool UMat::empty() const
 
3553
{
 
3554
    return u == 0 || total() == 0;
 
3555
}
 
3556
 
 
3557
inline
 
3558
size_t UMat::total() const
 
3559
{
 
3560
    if( dims <= 2 )
 
3561
        return (size_t)rows * cols;
 
3562
    size_t p = 1;
 
3563
    for( int i = 0; i < dims; i++ )
 
3564
        p *= size[i];
 
3565
    return p;
 
3566
}
 
3567
 
 
3568
#ifdef CV_CXX_MOVE_SEMANTICS
 
3569
 
 
3570
inline
 
3571
UMat::UMat(UMat&& m)
 
3572
: flags(m.flags), dims(m.dims), rows(m.rows), cols(m.cols), allocator(m.allocator),
 
3573
  usageFlags(m.usageFlags), u(m.u), offset(m.offset), size(&rows)
 
3574
{
 
3575
    if (m.dims <= 2)  // move new step/size info
 
3576
    {
 
3577
        step[0] = m.step[0];
 
3578
        step[1] = m.step[1];
 
3579
    }
 
3580
    else
 
3581
    {
 
3582
        CV_DbgAssert(m.step.p != m.step.buf);
 
3583
        step.p = m.step.p;
 
3584
        size.p = m.size.p;
 
3585
        m.step.p = m.step.buf;
 
3586
        m.size.p = &m.rows;
 
3587
    }
 
3588
    m.flags = MAGIC_VAL; m.dims = m.rows = m.cols = 0;
 
3589
    m.allocator = NULL;
 
3590
    m.u = NULL;
 
3591
    m.offset = 0;
 
3592
}
 
3593
 
 
3594
inline
 
3595
UMat& UMat::operator = (UMat&& m)
 
3596
{
 
3597
    release();
 
3598
    flags = m.flags; dims = m.dims; rows = m.rows; cols = m.cols;
 
3599
    allocator = m.allocator; usageFlags = m.usageFlags;
 
3600
    u = m.u;
 
3601
    offset = m.offset;
 
3602
    if (step.p != step.buf) // release self step/size
 
3603
    {
 
3604
        fastFree(step.p);
 
3605
        step.p = step.buf;
 
3606
        size.p = &rows;
 
3607
    }
 
3608
    if (m.dims <= 2) // move new step/size info
 
3609
    {
 
3610
        step[0] = m.step[0];
 
3611
        step[1] = m.step[1];
 
3612
    }
 
3613
    else
 
3614
    {
 
3615
        CV_DbgAssert(m.step.p != m.step.buf);
 
3616
        step.p = m.step.p;
 
3617
        size.p = m.size.p;
 
3618
        m.step.p = m.step.buf;
 
3619
        m.size.p = &m.rows;
 
3620
    }
 
3621
    m.flags = MAGIC_VAL; m.dims = m.rows = m.cols = 0;
 
3622
    m.allocator = NULL;
 
3623
    m.u = NULL;
 
3624
    m.offset = 0;
 
3625
    return *this;
 
3626
}
 
3627
 
 
3628
#endif
 
3629
 
 
3630
 
 
3631
inline bool UMatData::hostCopyObsolete() const { return (flags & HOST_COPY_OBSOLETE) != 0; }
 
3632
inline bool UMatData::deviceCopyObsolete() const { return (flags & DEVICE_COPY_OBSOLETE) != 0; }
 
3633
inline bool UMatData::deviceMemMapped() const { return (flags & DEVICE_MEM_MAPPED) != 0; }
 
3634
inline bool UMatData::copyOnMap() const { return (flags & COPY_ON_MAP) != 0; }
 
3635
inline bool UMatData::tempUMat() const { return (flags & TEMP_UMAT) != 0; }
 
3636
inline bool UMatData::tempCopiedUMat() const { return (flags & TEMP_COPIED_UMAT) == TEMP_COPIED_UMAT; }
 
3637
 
 
3638
inline void UMatData::markDeviceMemMapped(bool flag)
 
3639
{
 
3640
  if(flag)
 
3641
    flags |= DEVICE_MEM_MAPPED;
 
3642
  else
 
3643
    flags &= ~DEVICE_MEM_MAPPED;
 
3644
}
 
3645
 
 
3646
inline void UMatData::markHostCopyObsolete(bool flag)
 
3647
{
 
3648
    if(flag)
 
3649
        flags |= HOST_COPY_OBSOLETE;
 
3650
    else
 
3651
        flags &= ~HOST_COPY_OBSOLETE;
 
3652
}
 
3653
inline void UMatData::markDeviceCopyObsolete(bool flag)
 
3654
{
 
3655
    if(flag)
 
3656
        flags |= DEVICE_COPY_OBSOLETE;
 
3657
    else
 
3658
        flags &= ~DEVICE_COPY_OBSOLETE;
 
3659
}
 
3660
 
 
3661
inline UMatDataAutoLock::UMatDataAutoLock(UMatData* _u) : u(_u) { u->lock(); }
 
3662
inline UMatDataAutoLock::~UMatDataAutoLock() { u->unlock(); }
 
3663
 
 
3664
//! @endcond
 
3665
 
 
3666
} //cv
 
3667
 
 
3668
#endif