-
Notifications
You must be signed in to change notification settings - Fork 4
/
util.hpp
512 lines (441 loc) · 11.8 KB
/
util.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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
#ifndef UTIL_HPP
#define UTIL_HPP
#include <opencv2/core/hal/interface.h>
#include <opencv2/core/mat.hpp>
#include <opencv2/core/mat.inl.hpp>
#include <opencv2/core/matx.hpp>
#include <opencv2/core/types.hpp>
#include <quickjs.h>
#include <stddef.h>
#include <cstdint>
#include <sys/stat.h>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <numeric>
#include <string>
#include <vector>
#if defined(_WIN32) || defined(__MINGW32__)
#define VISIBLE __declspec(dllexport)
#define HIDDEN
#else
#define VISIBLE __attribute__((visibility("default")))
#define HIDDEN __attribute__((visibility("hidden")))
#endif
#ifndef thread_local
#ifdef _Thread_local
#define thread_local _Thread_local
#elif defined(__GNUC__) || defined(__INTEL_COMPILER) || defined(__SUNPRO_CC) || defined(__IBMCPP__)
#define thread_local __thread
#elif defined(_WIN32)
#define thread_local __declspec(thread)
#else
#error No TLS implementation found.
#endif
#endif
#define COLOR_BLACK "\x1b[30m"
#define COLOR_RED "\x1b[31m"
#define COLOR_GREEN "\x1b[32m"
#define COLOR_YELLOW "\x1b[33m"
#define COLOR_BLUE "\x1b[34m"
#define COLOR_MAGENTA "\x1b[35m"
#define COLOR_CYAN "\x1b[36m"
#define COLOR_WHITE "\x1b[37m"
#define COLOR_GRAY "\x1b[1;30m"
#define COLOR_LIGHTRED "\x1b[1;31m"
#define COLOR_LIGHTGREEN "\x1b[1;32m"
#define COLOR_LIGHTYELLOW "\x1b[1;33m"
#define COLOR_LIGHTBLUE "\x1b[1;34m"
#define COLOR_LIGHTMAGENTA "\x1b[1;35m"
#define COLOR_LIGHTCYAN "\x1b[1;36m"
#define COLOR_LIGHTWHITE "\x1b[1;37m"
#define COLOR_NONE "\x1b[m"
#define ALIGN_CENTER 0
#define ALIGN_LEFT 1
#define ALIGN_RIGHT 2
#define ALIGN_HORIZONTAL (ALIGN_LEFT | ALIGN_RIGHT)
#define ALIGN_MIDDLE 0
#define ALIGN_TOP 4
#define ALIGN_BOTTOM 8
#define ALIGN_VERTICAL (ALIGN_TOP | ALIGN_BOTTOM)
bool str_end(const char* str, const char* suffix);
bool str_end(const std::string& str, const std::string& suffix);
std::string to_string(const cv::Scalar& scalar);
std::string make_filename(const std::string& name, int count, const std::string& ext, const std::string& dir = "tmp");
inline int32_t
get_mtime(const char* filename) {
#if __STDC_VERSION__ >= 201710L
return std::filesystem::last_write_time(filename);
#else
struct stat st;
if(stat(filename, &st) != -1) {
uint32_t ret = st.st_mtime;
return ret;
}
#endif
return -1;
}
template<class Char, class Value>
inline std::ostream&
operator<<(std::ostream& os, const std::vector<Value>& c) {
typedef typename std::vector<Value>::const_iterator iterator_type;
iterator_type end = c.end();
for(iterator_type it = c.begin(); it != end; ++it) {
os << ' ';
os << to_string(*it);
}
return os;
}
inline std::string
to_string(const cv::Scalar& scalar) {
const int pad = 3;
std::ostringstream oss;
oss << '[' << std::setfill(' ') << std::setw(pad) << scalar[0] << ',' << std::setfill(' ') << std::setw(pad) << scalar[1] << ',' << std::setfill(' ')
<< std::setw(pad) << scalar[2] << ',' << std::setfill(' ') << std::setw(pad) << scalar[3] << ']';
return oss.str();
}
template<class Iterator>
static inline std::string
join(const Iterator& start, const Iterator& end, const std::string& delim) {
return std::accumulate(start, end, std::string(), [&delim](const std::string& a, const std::string& b) -> std::string {
return a + (a.length() > 0 ? delim : "") + b;
});
}
extern "C" void* get_heap_base();
typedef struct JSMatDimensions {
uint32_t rows, cols;
operator cv::Size() const { return cv::Size(cols, rows); }
} JSMatDimensions;
static inline int
mattype_depth(int type) {
return type & 0x7;
}
static inline int
mattype_channels(int type) {
return (type >> 3) + 1;
}
static inline bool
mattype_floating(int type) {
switch(mattype_depth(type)) {
case CV_32F:
case CV_64F: return true;
default: return false;
}
}
static inline bool
mattype_signed(int type) {
switch(mattype_depth(type)) {
case CV_8S:
case CV_16S:
case CV_32S: return true;
default: return false;
}
}
JSMatDimensions mat_dimensions(const cv::Mat& mat);
JSMatDimensions mat_dimensions(const cv::UMat& mat);
static inline uint8_t*
mat_ptr(cv::Mat& mat) {
return reinterpret_cast<uint8_t*>(mat.ptr());
}
static inline uint8_t*
mat_ptr(cv::UMat& mat) {
cv::UMatData* u;
if((u = mat.u))
return reinterpret_cast<uint8_t*>(u->data);
return nullptr;
}
template<class T, int rows, int cols>
static inline T*
mat_ptr(cv::Matx<T, rows, cols>& mat) {
return &static_cast<T&>(mat(0, 0));
}
static inline size_t
mat_offset(const cv::Mat& mat, uint32_t row, uint32_t col) {
const uchar *base, *ptr;
base = mat.ptr<uchar>();
ptr = mat.ptr<uchar>(row, col);
return ptr - base;
}
static inline size_t
mat_offset(const cv::UMat& mat, uint32_t row, uint32_t col) {
return (size_t(mat.cols) * row + col) * mat.elemSize();
}
template<class T>
static inline T&
mat_at(const cv::Mat& mat, uint32_t row, uint32_t col) {
return *const_cast<cv::Mat*>(&mat)->ptr<T>(row, col);
}
template<class T>
static inline T&
mat_at(cv::UMat& mat, uint32_t row, uint32_t col) {
size_t offs = mat_offset(mat, row, col);
return *reinterpret_cast<T*>(mat_ptr(mat) + offs);
}
template<class T, int rows, int cols>
static inline T&
mat_at(const cv::Matx<T, rows, cols>& mat, uint32_t row, uint32_t col) {
return mat(row, col);
}
static inline cv::Size
mat_size(const cv::Mat& mat) {
return cv::Size(mat.cols, mat.rows);
}
template<class T, int rows, int cols>
static inline cv::Size
mat_size(const cv::Matx<T, rows, cols>& mat) {
return cv::Size(cols, rows);
}
template<class T, int rows, int cols>
static inline std::array<T, rows * cols>&
mat_array(cv::Matx<T, rows, cols>& mat) {
return *reinterpret_cast<std::array<T, rows * cols>*>(mat_ptr(mat));
}
/*
template<class T, int rows, int cols>
static inline std::array<T, rows * cols> const&
mat_array(cv::Matx<T, rows, cols> const& mat) {
return *reinterpret_cast<std::array<T, rows * cols> const*>(mat_ptr(mat));
}
*/
static inline size_t
mat_bytesize(const cv::Mat& mat) {
return mat.ptr<uchar>(mat.rows - 1, mat.cols - 1) - mat.ptr<uchar>() + mat.elemSize();
// return mat.elemSize() * mat.total();
}
int mat_depth(const cv::Mat& mat);
int mat_channels(const cv::Mat& mat);
bool mat_signed(const cv::Mat& mat);
bool mat_floating(const cv::Mat& mat);
int mat_depth(const cv::UMat& mat);
int mat_channels(const cv::UMat& mat);
bool mat_signed(const cv::UMat& mat);
bool mat_floating(const cv::UMat& mat);
template<typename T = int>
static inline T
mat_col(const cv::Mat& mat, const T col) {
T x = col;
if(x < 0 && -x >= T(mat.cols))
x %= T(mat.cols);
if(x < 0)
x += mat.cols;
return x;
}
template<typename T = int>
static inline T
mat_row(const cv::Mat& mat, const T row) {
T y = row;
if(y < 0 && -y >= T(mat.rows))
y %= T(mat.rows);
if(y < 0)
y += mat.rows;
return y;
}
template<class T, int N>
static inline T*
begin(cv::Vec<T, N>& v) {
return &v[0];
}
template<class T, int N>
static inline T*
end(cv::Vec<T, N>& v) {
return &v[N];
}
template<class T, int N>
static inline T const*
begin(cv::Vec<T, N> const& v) {
return &v[0];
}
template<class T, int N>
static inline T const*
end(cv::Vec<T, N> const& v) {
return &v[N];
}
/*
template<class T>
static inline typename T::value_type const*
begin(const T& obj) {
return obj.begin();
}
template<class T>
static inline typename T::value_type*
begin(T& obj) {
return obj.begin();
}
template<class T>
static inline typename T::value_type const*
end(const T& obj) {
return obj.end();
}
template<class T>
static inline typename T::value_type*
end(T& obj) {
return obj.end();
}
*/
template<class T>
static inline T*
begin(std::vector<T>& v) {
return &v[0];
}
template<class T>
static inline T*
end(std::vector<T>& v) {
return &v[v.size()];
}
template<class T>
static inline T const*
begin(std::vector<T> const& v) {
return &v[0];
}
template<class T>
static inline T const*
end(std::vector<T> const& v) {
return &v[v.size()];
}
static inline uint8_t*
begin(cv::Mat& mat) {
return mat.ptr<uint8_t>();
}
static inline uint8_t*
end(cv::Mat& mat) {
return mat.ptr<uint8_t>() + (mat.total() * mat.elemSize());
}
static inline uint8_t const*
begin(cv::Mat const& mat) {
return mat.ptr<uint8_t const>();
}
static inline uint8_t const*
end(cv::Mat const& mat) {
return mat.ptr<uint8_t const>() + (mat.total() * mat.elemSize());
}
template<class T> class range_view {
public:
// range_view(range_view<T> const& range) : p(range.begin()), n(range.size()) {}
range_view(T* const base, size_t size) : p(base), n(size) {}
range_view<T>&
operator=(range_view<T> const& range) {
p = range.begin();
n = range.size();
return *this;
}
// clang-format off
T* const begin() const {return p; }
T* const end() const {return p + n; }
size_t size() const {return n; }
// clang-format on
private:
T* p;
size_t n;
};
template<class T>
static inline range_view<T>
argument_range(int argc, T argv[]) {
return range_view<T>(argv, argc);
}
template<class T>
static inline range_view<T>
sized_range(T ptr, size_t len) {
return range_view<T>(ptr, ptr + len);
}
template<class T>
static inline range_view<T>
range(T* begin, T* end) {
return range_view<T>(begin, end - begin);
}
template<class Container>
static inline range_view<typename Container::value_type>
range(Container& c) {
return range_view<typename Container::value_type>(begin(c), end(c) - begin(c));
}
template<class T, class Container>
static inline range_view<T>
range(Container& c) {
return range_view<T>(reinterpret_cast<T>(begin(c)), reinterpret_cast<T>(end(c)));
}
std::string js_prop_flags(int flags);
std::ostream& operator<<(std::ostream& s, const JSCFunctionListEntry& entry);
template<class Stream, class Item>
Stream&
operator<<(Stream& s, const std::vector<Item>& vector) {
size_t i = 0;
for(auto entry : vector) {
s << "#" << i << " ";
s << entry;
i++;
}
return s;
}
template<class T>
static inline cv::Point_<T>
add(const cv::Point_<T>& a, const cv::Point_<T>& b) {
return cv::Point_<T>(a.x + b.x, a.y + b.y);
}
template<class T, class U>
static inline cv::Point_<double>
add(const cv::Point_<T>& a, const cv::Point_<U>& b) {
return cv::Point_<double>(a.x + b.x, a.y + b.y);
}
template<class T>
static inline cv::Point_<T>
sub(const cv::Point_<T>& a, const cv::Point_<T>& b) {
return cv::Point_<T>(a.x - b.x, a.y - b.y);
}
template<class T, class U>
static inline cv::Point_<double>
sub(const cv::Point_<T>& a, const cv::Point_<U>& b) {
return cv::Point_<double>(a.x - b.x, a.y - b.y);
}
template<class T>
static inline cv::Point_<T>
div(const cv::Point_<T>& p, T d) {
return cv::Point_<T>(p.x / d, p.y / d);
}
template<class T>
static inline cv::Point_<T>
div(const cv::Point_<T>& p, const cv::Size_<T>& s) {
return cv::Point_<T>(p.x / s.width, p.y / s.height);
}
template<class T>
static inline cv::Point_<T>
mul(const cv::Point_<T>& p, T f) {
return cv::Point_<T>(p.x * f, p.y * f);
}
template<class T>
static inline cv::Point_<T>
mul(const cv::Point_<T>& p, const cv::Size_<T>& s) {
return cv::Point_<T>(p.x * s.width, p.y * s.height);
}
static inline std::string
dump(const cv::_InputArray& arr) {
std::ostringstream os;
os << "{ ";
os << "type: " << arr.type();
os << ", depth: " << arr.depth();
os << ", channels: " << arr.channels();
os << ", total: " << arr.total();
os << " }";
return os.str();
}
static inline std::string
dump(const cv::_InputOutputArray& arr) {
std::ostringstream os;
os << "{ ";
os << "type: " << arr.type();
os << ", depth: " << arr.depth();
os << ", channels: " << arr.channels();
os << ", total: " << arr.total();
os << " }";
return os.str();
}
static inline std::string
dump(const cv::_OutputArray& arr) {
std::ostringstream os;
os << "{ ";
os << "type: " << arr.type();
os << ", depth: " << arr.depth();
os << ", channels: " << arr.channels();
os << ", total: " << arr.total();
os << " }";
return os.str();
}
#endif // defined(UTIL_HPP)