-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy patharrays.hpp
More file actions
208 lines (191 loc) · 5.75 KB
/
arrays.hpp
File metadata and controls
208 lines (191 loc) · 5.75 KB
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
/* -*- coding: utf-8;mode:c++;c-file-style:"stroustrup" -*- */
/*
Licensed under the terms of the BSD 3-Clause
(see plotpy/__init__.py for details)
*/
#ifndef __ARRAYS_HPP__
#define __ARRAYS_HPP__
#include "debug.hpp"
template <class Image>
class PixelIterator
{
public:
typedef typename Image::value_type value_type;
PixelIterator(Image &_img) : img(_img), cur(_img.base)
{
}
value_type &operator()()
{
check_img_ptr("pixeliter:", cur, out, img);
return *cur;
}
value_type &operator()(npy_intp dx, npy_intp dy)
{
return *(cur + dy * img.si + dx * img.sj);
}
void move(npy_intp dx, npy_intp dy)
{
cur += dy * img.si + dx * img.sj;
}
void moveto(npy_intp x, npy_intp y)
{
cur = img.base + y * img.si + x * img.sj;
}
protected:
Image &img;
value_type *cur;
value_type out;
};
template <class T>
class Array1D
{
public:
typedef T value_type; // The type of pixel data from the image
class iterator : public std::iterator<std::random_access_iterator_tag, T>
{
public:
iterator() : pos(0L), stride(0) {}
iterator(const Array1D &arr) : pos(arr.base), stride(arr.si) {}
iterator(const iterator &it, npy_intp n = 0) : pos(it.pos), stride(it.stride) { *this += n; }
T &operator*() { return *pos; }
const T &operator*() const { return *pos; }
T &operator[](npy_intp n) { return *(pos + n * stride); }
const T &operator[](npy_intp n) const { return *(pos + n * stride); }
iterator &operator+=(npy_intp n)
{
pos += stride * n;
return *this;
}
npy_intp operator-(const iterator &it) { return (pos - it.pos) / stride; }
iterator operator+(npy_intp n) { return iterator(*this, n); }
iterator operator-(npy_intp n) { return iterator(*this, -n); }
iterator &operator=(const iterator &it)
{
pos = it.pos;
stride = it.stride;
return *this;
}
iterator &operator++()
{
pos += stride;
return *this;
}
iterator &operator--()
{
pos += stride;
return *this;
}
iterator operator++(int)
{
iterator it(*this);
pos += stride;
return it;
}
iterator operator--(int)
{
iterator it(*this);
pos += stride;
return it;
}
bool operator<(const iterator &it) { return pos < it.pos; }
bool operator==(const iterator &it) { return pos == it.pos; }
bool operator!=(const iterator &it) { return pos != it.pos; }
protected:
T *pos;
npy_intp stride;
};
Array1D() {}
Array1D(PyArrayObject *arr)
{
base = (value_type *)PyArray_DATA(arr);
ni = PyArray_DIM(arr, 0);
si = PyArray_STRIDE(arr, 0) / sizeof(value_type);
}
Array1D(value_type *_base, npy_intp _ni, npy_intp _si) : base(_base), ni(_ni),
si(_si / sizeof(value_type))
{
}
iterator begin() { return iterator(*this); }
iterator end()
{
iterator it(*this);
it += ni;
return it;
}
void init(value_type *_base, npy_intp _ni, npy_intp _si)
{
base = _base;
ni = _ni;
si = _si;
}
// Pixel accessors
value_type &value(npy_intp x)
{
check("array1d:", x, ni, outside);
return *(base + x * si);
}
const value_type &value(npy_intp x) const
{
check("array1d:", x, ni, outside);
return *(base + x * si);
}
public:
value_type outside;
value_type *base;
npy_intp ni; // dimensions
npy_intp si; // strides in sizeof(value_type)
};
template <class T>
class Array2D
{
public:
typedef T value_type; // The type of pixel data from the image
Array2D() {}
Array2D(PyArrayObject *arr)
{
base = (value_type *)PyArray_DATA(arr);
ni = PyArray_DIM(arr, 0);
nj = PyArray_DIM(arr, 1);
si = PyArray_STRIDE(arr, 0) / sizeof(value_type);
sj = PyArray_STRIDE(arr, 1) / sizeof(value_type);
}
Array2D(value_type *_base, npy_intp _ni, npy_intp _nj, npy_intp _si, npy_intp _sj) : base(_base), ni(_ni), nj(_nj),
si(_si / sizeof(value_type)), sj(_sj / sizeof(value_type))
{
}
void init(value_type *_base, npy_intp _ni, npy_intp _nj, npy_intp _si, npy_intp _sj)
{
base = _base;
ni = _ni;
nj = _nj;
si = _si;
sj = _sj;
}
// Pixel accessors
value_type &value(npy_intp x, npy_intp y)
{
check("array2d x:", x, nj, outside);
check("array2d y:", y, ni, outside);
return *(base + x * sj + y * si);
}
const value_type &value(npy_intp x, npy_intp y) const
{
check("array2d x:", x, nj, outside);
check("array2d y:", y, ni, outside);
return *(base + x * sj + y * si);
}
public:
value_type outside;
value_type *base;
npy_intp ni, nj; // dimensions
npy_intp si, sj; // strides in sizeof(value_type)
};
template <class Image>
void set_array(Image &img, PyArrayObject *arr)
{
img.init((typename Image::value_type *)PyArray_DATA(arr),
PyArray_DIM(arr, 0), PyArray_DIM(arr, 1),
PyArray_STRIDE(arr, 0) / sizeof(typename Image::value_type),
PyArray_STRIDE(arr, 1) / sizeof(typename Image::value_type));
}
#endif