-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_expr.hpp
445 lines (388 loc) · 11.4 KB
/
image_expr.hpp
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
#pragma once
#include <functional>
#include <type_traits>
template <typename T>
class img_t;
class dummy_img_expr_t {
};
template <typename T>
class img_expr_t : public dummy_img_expr_t {
public:
typedef T value_type;
//int w, h, d;
// not defined here
// T operator[](int i) const;
};
template <typename T>
class scalar_img_expr_t : public img_expr_t<T> {
public:
T val;
int size = 0;
int w = 0;
int h = 0;
int d = 0;
scalar_img_expr_t(T val) : val(val) {
}
T operator[](int i) const {
return val;
}
template <typename E>
bool similar(const E& o) const {
return true;
}
};
template <typename T>
class raw_img_expr_t : public img_expr_t<T> {
public:
const img_t<T>* img;
int size, w, h, d;
raw_img_expr_t(const img_t<T>* img) : img(img), size(img->size),
w(img->w), h(img->h), d(img->d) {
}
T operator[](int i) const {
return (*img)[i];
}
template <typename E>
bool similar(const E& o) const {
return o.similar(*img);
}
};
template <typename E>
typename std::enable_if<std::is_base_of<dummy_img_expr_t, E>::value, E>::type
to_expr(const E& e) {
return e;
}
// important to avoid unecessary image copy
// use code should use to_expr when returning a raw image
template <typename T>
raw_img_expr_t<T> to_expr(const img_t<T>& i) {
return raw_img_expr_t<T>(&i);
}
template <typename T>
typename std::enable_if<!std::is_base_of<dummy_img_expr_t, T>::value, scalar_img_expr_t<T>>::type
to_expr(T t) {
return scalar_img_expr_t<T>(t);
}
template <typename T>
class func0_img_expr_t : public img_expr_t<T> {
public:
std::function<T(int)> f;
int size = 0;
int w = 0;
int h = 0;
int d = 0;
func0_img_expr_t(std::function<T(int i)> f) : f(f) {
}
T operator[](int i) const {
return f(i);
}
template <typename T2>
bool similar(const img_t<T2>& o) const {
return true;
}
};
template <typename T, typename E>
class func1_img_expr_t : public img_expr_t<T> {
public:
std::function<T(typename E::value_type)> f;
E e;
int size, w, h, d;
func1_img_expr_t(std::function<T(typename E::value_type)> f, const E& e)
: f(f), e(e), size(e.size), w(e.w), h(e.h), d(e.d) {
}
T operator[](int i) const {
return f(e[i]);
}
template <typename E2>
bool similar(const E2& o) const {
return e.similar(o);
}
};
template <typename T, typename E1, typename E2>
class func2_img_expr_t : public img_expr_t<T> {
public:
std::function<T(typename E1::value_type, typename E2::value_type)> f;
E1 e1;
E2 e2;
int size, w, h, d;
func2_img_expr_t(std::function<T(typename E1::value_type, typename E2::value_type)> f,
const E1& e1, const E2& e2) : f(f), e1(e1), e2(e2), size(e1.size | e2.size),
w(e1.w | e2.w), h(e1.h | e2.h), d(e1.d | e2.d) {
}
T operator[](int i) const {
return f(e1[i], e2[i]);
}
template <typename E3>
bool similar(const E3& o) const {
return e1.similar(o) && e2.similar(o);
}
};
#define DEFINE_EXPR_1(name, operand) \
template <typename E> \
class name : \
public img_expr_t<decltype(operand std::declval<typename E::value_type>())> { \
typedef decltype(operand std::declval<typename E::value_type>()) res_type; \
public: \
E e; \
int size, w, h, d; \
\
name(const E& e) : e(e), size(e.size), w(e.w), h(e.h), d(e.d) { \
} \
\
res_type operator[](int i) const { \
return operand e[i]; \
} \
\
template <typename E3> \
bool similar(const E3& o) const { \
return e.similar(o); \
} \
}; \
\
template <typename E> \
name<decltype(to_expr(std::declval<E>()))> operator operand(const E& e) { \
return name<decltype(to_expr(e))>(to_expr(e)); \
}; \
#define DEFINE_EXPR_2(name, operand) \
template <typename E1, typename E2> \
class name : \
public img_expr_t<decltype(std::declval<typename E1::value_type>() operand std::declval<typename E2::value_type>())> { \
typedef decltype(std::declval<typename E1::value_type>() operand std::declval<typename E2::value_type>()) res_type;\
public: \
E1 e1; \
E2 e2; \
int size, w, h, d; \
\
name(const E1& e1, const E2& e2) : e1(e1), e2(e2), size(e1.size | e2.size), \
w(e1.w | e2.w), h(e1.h | e2.h), d(e1.d | e2.d) { \
} \
\
res_type operator[](int i) const { \
return e1[i] operand e2[i]; \
} \
\
template <typename E3> \
bool similar(const E3& o) const { \
return e1.similar(o) && e2.similar(o); \
} \
}; \
\
template <typename E1, typename E2> \
name<decltype(to_expr(std::declval<E1>())), decltype(to_expr(std::declval<E2>()))> operator operand(const E1& e1, const E2& e2) { \
return name<decltype(to_expr(e1)), decltype(to_expr(e2))>(to_expr(e1), to_expr(e2)); \
}; \
DEFINE_EXPR_1(minus_img_expr_t, -)
DEFINE_EXPR_2(add_img_expr_t, +)
DEFINE_EXPR_2(sub_img_expr_t, -)
DEFINE_EXPR_2(div_img_expr_t, /)
DEFINE_EXPR_2(mul_img_expr_t, *)
#define DEFINE_FUNC_1(name, call) \
template <typename E> \
auto name(const E& e) { \
return func1_img_expr_t<decltype(call(std::declval<typename decltype(to_expr(e))::value_type>())), \
decltype(to_expr(e))>( \
[](auto e) { return call(e); }, to_expr(e)); \
} \
#define DEFINE_FUNC_2(name, call) \
template <typename E1, typename E2> \
auto name(const E1& e1, const E2& e2) { \
return func2_img_expr_t<decltype(call(std::declval<typename decltype(to_expr(e1))::value_type>(), \
std::declval<typename decltype(to_expr(e2))::value_type>())), \
decltype(to_expr(e1)), decltype(to_expr(e2))> \
([](auto e1, auto e2) { return call(e1, e2); }, to_expr(e1), to_expr(e2)); \
} \
namespace std {
DEFINE_FUNC_1(conj, std::conj)
DEFINE_FUNC_1(real, std::real)
DEFINE_FUNC_1(hypot, std::hypot)
DEFINE_FUNC_1(log, std::log)
DEFINE_FUNC_1(abs, std::abs)
DEFINE_FUNC_1(arg, std::arg)
DEFINE_FUNC_1(exp, std::exp)
DEFINE_FUNC_1(floor, std::floor)
DEFINE_FUNC_1(round, std::round)
DEFINE_FUNC_2(hypot, std::hypot)
DEFINE_FUNC_2(pow, std::pow)
}
template <typename T, typename E>
img_t<T> to_img(const E& e)
{
img_t<T> img(e.w, e.h, e.d);
img.map(e);
return img;
}
template <typename T>
const img_t<T>& to_img(const img_t<T>& img)
{
return img;
}
template <typename T, typename T2>
const img_t<T2>& to_img(const img_t<T>& img)
{
img_t<T2> img2; img2.resize(img); img2.map(img);
return img2;
}
template <typename E>
img_t<typename E::value_type> to_img(const E& e)
{
return to_img<typename E::value_type>(e);
}
static struct slice_t {
int s, e;
slice_t operator()(int s) const {
return {.s=s, .e=s};
}
slice_t operator()(int s, int e) const {
return {.s=s, .e=e};
}
} _ = {.s=0, .e=-1};
template <typename E>
class slice_img_expr_t : public img_expr_t<typename E::value_type> {
using T = typename E::value_type;
public:
E e;
slice_t _w, _h, _d;
int size, w, h, d;
slice_img_expr_t(const E& e, slice_t _w, slice_t _h, slice_t _d)
: e(e), _w(_w), _h(_h), _d(_d) {
this->_w.s = (_w.s + e.w) % e.w;
this->_h.s = (_h.s + e.h) % e.h;
this->_d.s = (_d.s + e.d) % e.d;
this->_w.e = (_w.e + e.w) % e.w;
this->_h.e = (_h.e + e.h) % e.h;
this->_d.e = (_d.e + e.d) % e.d;
w = this->_w.e - this->_w.s + 1;
h = this->_h.e - this->_h.s + 1;
d = this->_d.e - this->_d.s + 1;
size = w * h * d;
//assert(similar(e));
// check other size compatibility with e
}
T operator[](int i) const {
int dd = i % d;
int xy = i / d;
int x = xy % w;
int y = xy / w;
x += _w.s;
y += _h.s;
dd += _d.s;
int j = dd + e.d * (x + e.w * y);
return e[j];
}
template <typename E2>
bool similar(const E2& o) const {
return o.similar(*this);
}
};
template <typename T>
class mappable_slice_img_expr_t : public img_expr_t<T> {
public:
img_t<T>* img;
slice_t _w, _h, _d;
int size, w, h, d;
mappable_slice_img_expr_t(img_t<T>* img, slice_t _w, slice_t _h, slice_t _d)
: img(img), _w(_w), _h(_h), _d(_d) {
this->_w.s = (_w.s + img->w) % img->w;
this->_h.s = (_h.s + img->h) % img->h;
this->_d.s = (_d.s + img->d) % img->d;
this->_w.e = (_w.e + img->w) % img->w;
this->_h.e = (_h.e + img->h) % img->h;
this->_d.e = (_d.e + img->d) % img->d;
w = this->_w.e - this->_w.s + 1;
h = this->_h.e - this->_h.s + 1;
d = this->_d.e - this->_d.s + 1;
size = w * h * d;
// check other size compatibility with img
}
T operator[](int i) const {
int dd = i % d;
int xy = i / d;
int x = xy % w;
int y = xy / w;
x += _w.s;
y += _h.s;
dd += _d.s;
int j = dd + img->d * (x + img->w * y);
return (*img)[j];
}
T& operator[](int i) {
int dd = i % d;
int xy = i / d;
int x = xy % w;
int y = xy / w;
x += _w.s;
y += _h.s;
dd += _d.s;
int j = dd + img->d * (x + img->w * y);
return (*img)[j];
}
template <typename E>
void map(const E& o) {
auto e = to_expr(o);
assert(e.similar(*this));
for (int i = 0; i < size; i++)
(*this)[i] = e[i];
}
template <typename E>
bool similar(const E& o) const {
bool similar = w == o.w && h == o.h && d == o.d;
if (!similar)
fprintf(stderr, "%dx%dx%d (type %s) != %dx%dx%d (type %s)\n",
w, h, d, typeid(*this).name(), o.w, o.h, o.d, typeid(o).name());
return similar;
}
};
template <typename E>
auto slice(const E& e, slice_t w, slice_t h, slice_t d=_)
{
return slice_img_expr_t<decltype(to_expr(e))>(to_expr(e), w, h, d);
}
template <typename T>
auto slice(img_t<T>& i, slice_t w, slice_t h, slice_t d=_)
{
return mappable_slice_img_expr_t<T>(&i, w, h, d);
}
enum {
AXIS_D=1,
AXIS_X=2,
AXIS_Y=4,
} axis_e;
#define REDUCE_HEAD(name) \
template <typename E> \
class name : public img_expr_t<typename E::value_type> { \
using T = typename E::value_type; \
public: \
E e; \
std::function<typename E::value_type(typename E::value_type, typename E::value_type)> reductor; \
int size, w, h, d; \
\
reduce1_img_expr_t(const E& e, std::function<typename E::value_type(typename E::value_type, typename E::value_type)> reductor) \
: e(e), reductor(reductor) { \
#define REDUCE_BODY \
size = w * h * d; \
} \
T operator[](int i) const { \
#define REDUCE_TAIL \
return val; \
} \
\
template <typename E2> \
bool similar(const E2& o) const { \
return o.similar(*this); \
} \
}; \
REDUCE_HEAD(reduce1_img_expr_t)
w = e.w;
h = e.h;
d = 1;
REDUCE_BODY
T val = e[i];
for (int d = 1; d < e.d; d++) {
val = reductor(val, e[i*e.d + d]);
}
REDUCE_TAIL
template <typename E>
auto reduce_d(const E& e, std::function<typename E::value_type(typename E::value_type, typename E::value_type)> reductor)
{
return reduce1_img_expr_t<decltype(to_expr(e))>(to_expr(e), reductor);
}