Debian Codegraph @

src/arrays.hpp

raw symbols SCIP
1 /* -*- coding: utf-8;mode:c++;c-file-style:"stroustrup" -*- */
2 /*
3 Licensed under the terms of the BSD 3-Clause
4 (see plotpy/__init__.py for details)
5 */
6 #ifndef __ARRAYS_HPP__
7 #define __ARRAYS_HPP__
8
9 #include "debug.hpp"
10
11 template <class Image>
12 class PixelIterator
13 {
14 public:
15 typedef typename Image::value_type value_type;
16
17 PixelIterator(Image &_img) : img(_img), cur(_img.base)
18 {
19 }
20
21 value_type &operator()()
22 {
23 check_img_ptr("pixeliter:", cur, out, img);
24 return *cur;
25 }
26 value_type &operator()(int dx, int dy)
27 {
28 return *(cur + dy * img.si + dx * img.sj);
29 }
30 void move(int dx, int dy)
31 {
32 cur += dy * img.si + dx * img.sj;
33 }
34 void moveto(int x, int y)
35 {
36 cur = img.base + y * img.si + x * img.sj;
37 }
38
39 protected:
40 Image &img;
41 value_type *cur;
42 value_type out;
43 };
44
45 template <class T>
46 class Array1D
47 {
48 public:
49 typedef T value_type; // The type of pixel data from the image
50 class iterator : public std::iterator<std::random_access_iterator_tag, T>
51 {
52 public:
53 iterator() : pos(0L), stride(0) {}
54 iterator(const Array1D &arr) : pos(arr.base), stride(arr.si) {}
55 iterator(const iterator &it, int n = 0) : pos(it.pos), stride(it.stride) { *this += n; }
56 T &operator*() { return *pos; }
57 const T &operator*() const { return *pos; }
58 T &operator[](int n) { return *(pos + n * stride); }
59 const T &operator[](int n) const { return *(pos + n * stride); }
60 iterator &operator+=(int n)
61 {
62 pos += stride * n;
63 return *this;
64 }
65 int operator-(const iterator &it) { return (pos - it.pos) / stride; }
66 iterator operator+(int n) { return iterator(*this, n); }
67 iterator operator-(int n) { return iterator(*this, -n); }
68 iterator &operator=(const iterator &it)
69 {
70 pos = it.pos;
71 stride = it.stride;
72 return *this;
73 }
74 iterator &operator++()
75 {
76 pos += stride;
77 return *this;
78 }
79 iterator &operator--()
80 {
81 pos += stride;
82 return *this;
83 }
84 iterator operator++(int)
85 {
86 iterator it(*this);
87 pos += stride;
88 return it;
89 }
90 iterator operator--(int)
91 {
92 iterator it(*this);
93 pos += stride;
94 return it;
95 }
96 bool operator<(const iterator &it) { return pos < it.pos; }
97 bool operator==(const iterator &it) { return pos == it.pos; }
98 bool operator!=(const iterator &it) { return pos != it.pos; }
99
100 protected:
101 T *pos;
102 int stride;
103 };
104 Array1D() {}
105 Array1D(PyArrayObject *arr)
106 {
107 base = (value_type *)PyArray_DATA(arr);
108 ni = PyArray_DIM(arr, 0);
109 si = PyArray_STRIDE(arr, 0) / sizeof(value_type);
110 }
111
112 Array1D(value_type *_base, int _ni, int _si) : base(_base), ni(_ni),
113 si(_si / sizeof(value_type))
114 {
115 }
116 iterator begin() { return iterator(*this); }
117 iterator end()
118 {
119 iterator it(*this);
120 it += ni;
121 return it;
122 }
123 void init(value_type *_base, int _ni, int _si)
124 {
125 base = _base;
126 ni = _ni;
127 si = _si;
128 }
129
130 // Pixel accessors
131 value_type &value(int x)
132 {
133 check("array1d:", x, ni, outside);
134 return *(base + x * si);
135 }
136 const value_type &value(int x) const
137 {
138 check("array1d:", x, ni, outside);
139 return *(base + x * si);
140 }
141
142 public:
143 value_type outside;
144 value_type *base;
145 int ni; // dimensions
146 int si; // strides in sizeof(value_type)
147 };
148
149 template <class T>
150 class Array2D
151 {
152 public:
153 typedef T value_type; // The type of pixel data from the image
154
155 Array2D() {}
156 Array2D(PyArrayObject *arr)
157 {
158 base = (value_type *)PyArray_DATA(arr);
159 ni = PyArray_DIM(arr, 0);
160 nj = PyArray_DIM(arr, 1);
161 si = PyArray_STRIDE(arr, 0) / sizeof(value_type);
162 sj = PyArray_STRIDE(arr, 1) / sizeof(value_type);
163 }
164
165 Array2D(value_type *_base, int _ni, int _nj, int _si, int _sj) : base(_base), ni(_ni), nj(_nj),
166 si(_si / sizeof(value_type)), sj(_sj / sizeof(value_type))
167 {
168 }
169 void init(value_type *_base, int _ni, int _nj, int _si, int _sj)
170 {
171 base = _base;
172 ni = _ni;
173 nj = _nj;
174 si = _si;
175 sj = _sj;
176 }
177
178 // Pixel accessors
179 value_type &value(int x, int y)
180 {
181 check("array2d x:", x, nj, outside);
182 check("array2d y:", y, ni, outside);
183 return *(base + x * sj + y * si);
184 }
185 const value_type &value(int x, int y) const
186 {
187 check("array2d x:", x, nj, outside);
188 check("array2d y:", y, ni, outside);
189 return *(base + x * sj + y * si);
190 }
191
192 public:
193 value_type outside;
194 value_type *base;
195 int ni, nj; // dimensions
196 int si, sj; // strides in sizeof(value_type)
197 };
198
199 template <class Image>
200 void set_array(Image &img, PyArrayObject *arr)
201 {
202 img.init((typename Image::value_type *)PyArray_DATA(arr),
203 PyArray_DIM(arr, 0), PyArray_DIM(arr, 1),
204 PyArray_STRIDE(arr, 0) / sizeof(typename Image::value_type),
205 PyArray_STRIDE(arr, 1) / sizeof(typename Image::value_type));
206 }
207
208 #endif