1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html
#include "opencv2/core/mat.hpp"
#include "precomp.hpp"
namespace cv {
NAryMatIterator::NAryMatIterator()
: arrays(0), planes(0), ptrs(0), narrays(0), nplanes(0), size(0), iterdepth(0), idx(0)
{
}
NAryMatIterator::NAryMatIterator(const Mat** _arrays, Mat* _planes, int _narrays)
: arrays(0), planes(0), ptrs(0), narrays(0), nplanes(0), size(0), iterdepth(0), idx(0)
{
init(_arrays, _planes, 0, _narrays);
}
NAryMatIterator::NAryMatIterator(const Mat** _arrays, uchar** _ptrs, int _narrays)
: arrays(0), planes(0), ptrs(0), narrays(0), nplanes(0), size(0), iterdepth(0), idx(0)
{
init(_arrays, 0, _ptrs, _narrays);
}
void NAryMatIterator::init(const Mat** _arrays, Mat* _planes, uchar** _ptrs, int _narrays)
{
CV_Assert( _arrays && (_ptrs || _planes) );
int i, j, d1=0, i0 = -1, d = -1;
arrays = _arrays;
ptrs = _ptrs;
planes = _planes;
narrays = _narrays;
nplanes = 0;
size = 0;
if( narrays < 0 )
{
for( i = 0; _arrays[i] != 0; i++ )
;
narrays = i;
CV_Assert(narrays <= 1000);
}
iterdepth = 0;
for( i = 0; i < narrays; i++ )
{
CV_Assert(arrays[i] != 0);
const Mat& A = *arrays[i];
if( ptrs )
ptrs[i] = A.data;
if( !A.data )
continue;
if( i0 < 0 )
{
i0 = i;
d = A.dims;
// find the first dimensionality which is different from 1;
// in any of the arrays the first "d1" step do not affect the continuity
for( d1 = 0; d1 < d; d1++ )
if( A.size[d1] > 1 )
break;
}
else
CV_Assert( A.size == arrays[i0]->size );
if( !A.isContinuous() )
{
CV_Assert( A.step[d-1] == A.elemSize() );
for( j = d-1; j > d1; j-- )
if( A.step[j]*A.size[j] < A.step[j-1] )
break;
iterdepth = std::max(iterdepth, j);
}
}
if( i0 >= 0 )
{
size = arrays[i0]->size[d-1];
for( j = d-1; j > iterdepth; j-- )
{
int64 total1 = (int64)size*arrays[i0]->size[j-1];
if( total1 != (int)total1 )
break;
size = (int)total1;
}
iterdepth = j;
if( iterdepth == d1 )
iterdepth = 0;
nplanes = 1;
for( j = iterdepth-1; j >= 0; j-- )
nplanes *= arrays[i0]->size[j];
}
else
iterdepth = 0;
idx = 0;
if( !planes )
return;
for( i = 0; i < narrays; i++ )
{
CV_Assert(arrays[i] != 0);
const Mat& A = *arrays[i];
if( !A.data )
{
planes[i] = Mat();
continue;
}
planes[i] = Mat(1, (int)size, A.type(), A.data);
}
}
NAryMatIterator& NAryMatIterator::operator ++()
{
if( idx >= nplanes-1 )
return *this;
++idx;
if( iterdepth == 1 )
{
if( ptrs )
{
for( int i = 0; i < narrays; i++ )
{
if( !ptrs[i] )
continue;
ptrs[i] = arrays[i]->data + arrays[i]->step[0]*idx;
}
}
if( planes )
{
for( int i = 0; i < narrays; i++ )
{
if( !planes[i].data )
continue;
planes[i].data = arrays[i]->data + arrays[i]->step[0]*idx;
}
}
}
else
{
for( int i = 0; i < narrays; i++ )
{
const Mat& A = *arrays[i];
if( !A.data )
continue;
int _idx = (int)idx;
uchar* data = A.data;
for( int j = iterdepth-1; j >= 0 && _idx > 0; j-- )
{
int szi = A.size[j], t = _idx/szi;
data += (_idx - t * szi)*A.step[j];
_idx = t;
}
if( ptrs )
ptrs[i] = data;
if( planes )
planes[i].data = data;
}
}
return *this;
}
NAryMatIterator NAryMatIterator::operator ++(int)
{
NAryMatIterator it = *this;
++*this;
return it;
}
//==================================================================================================
Point MatConstIterator::pos() const
{
if( !m )
return Point();
CV_DbgAssert(m->dims <= 2);
ptrdiff_t ofs = ptr - m->ptr();
int y = (int)(ofs/m->step[0]);
return Point((int)((ofs - y*m->step[0])/elemSize), y);
}
void MatConstIterator::pos(int* _idx) const
{
CV_Assert(m != 0 && _idx);
ptrdiff_t ofs = ptr - m->ptr();
for( int i = 0; i < m->dims; i++ )
{
size_t s = m->step[i], v = ofs/s;
ofs -= v*s;
_idx[i] = (int)v;
}
}
ptrdiff_t MatConstIterator::lpos() const
{
if(!m)
return 0;
if( m->isContinuous() )
return (ptr - sliceStart)/elemSize;
ptrdiff_t ofs = ptr - m->ptr();
int i, d = m->dims;
if( d == 2 )
{
ptrdiff_t y = ofs/m->step[0];
return y*m->cols + (ofs - y*m->step[0])/elemSize;
}
ptrdiff_t result = 0;
for( i = 0; i < d; i++ )
{
size_t s = m->step[i], v = ofs/s;
ofs -= v*s;
result = result*m->size[i] + v;
}
return result;
}
void MatConstIterator::seek(ptrdiff_t ofs, bool relative)
{
if( m->isContinuous() )
{
ptr = (relative ? ptr : sliceStart) + ofs*elemSize;
if( ptr < sliceStart )
ptr = sliceStart;
else if( ptr > sliceEnd )
ptr = sliceEnd;
return;
}
int d = m->dims;
if( d == 2 )
{
ptrdiff_t ofs0, y;
if( relative )
{
ofs0 = ptr - m->ptr();
y = ofs0/m->step[0];
ofs += y*m->cols + (ofs0 - y*m->step[0])/elemSize;
}
y = ofs/m->cols;
int y1 = std::min(std::max((int)y, 0), m->rows-1);
sliceStart = m->ptr(y1);
sliceEnd = sliceStart + m->cols*elemSize;
ptr = y < 0 ? sliceStart : y >= m->rows ? sliceEnd :
sliceStart + (ofs - y*m->cols)*elemSize;
return;
}
if( relative )
ofs += lpos();
if( ofs < 0 )
ofs = 0;
int szi = m->size[d-1];
ptrdiff_t t = ofs/szi;
int v = (int)(ofs - t*szi);
ofs = t;
ptr = m->ptr() + v*elemSize;
sliceStart = m->ptr();
for( int i = d-2; i >= 0; i-- )
{
szi = m->size[i];
t = ofs/szi;
v = (int)(ofs - t*szi);
ofs = t;
sliceStart += v*m->step[i];
}
sliceEnd = sliceStart + m->size[d-1]*elemSize;
if( ofs > 0 )
ptr = sliceEnd;
else
ptr = sliceStart + (ptr - m->ptr());
}
void MatConstIterator::seek(const int* _idx, bool relative)
{
int d = m->dims;
ptrdiff_t ofs = 0;
if( !_idx )
;
else if( d == 2 )
ofs = _idx[0]*m->size[1] + _idx[1];
else
{
for( int i = 0; i < d; i++ )
ofs = ofs*m->size[i] + _idx[i];
}
seek(ofs, relative);
}
//==================================================================================================
SparseMatConstIterator::SparseMatConstIterator(const SparseMat* _m) : m((SparseMat*)_m), hashidx(0), ptr(0)
{
if(!_m || !_m->hdr)
return;
SparseMat::Hdr& hdr = *m->hdr;
const std::vector<size_t>& htab = hdr.hashtab;
size_t i, hsize = htab.size();
for( i = 0; i < hsize; i++ )
{
size_t nidx = htab[i];
if( nidx )
{
hashidx = i;
ptr = &hdr.pool[nidx] + hdr.valueOffset;
return;
}
}
}
SparseMatConstIterator& SparseMatConstIterator::operator ++()
{
if( !ptr || !m || !m->hdr )
return *this;
SparseMat::Hdr& hdr = *m->hdr;
size_t next = ((const SparseMat::Node*)(ptr - hdr.valueOffset))->next;
if( next )
{
ptr = &hdr.pool[next] + hdr.valueOffset;
return *this;
}
size_t i = hashidx + 1, sz = hdr.hashtab.size();
for( ; i < sz; i++ )
{
size_t nidx = hdr.hashtab[i];
if( nidx )
{
hashidx = i;
ptr = &hdr.pool[nidx] + hdr.valueOffset;
return *this;
}
}
hashidx = sz;
ptr = 0;
return *this;
}
} // cv::