-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathjs_matx.hpp
More file actions
87 lines (64 loc) · 2.16 KB
/
js_matx.hpp
File metadata and controls
87 lines (64 loc) · 2.16 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
#ifndef JS_MATX_HPP
#define JS_MATX_HPP
#include "include/jsbindings.hpp"
#include "util.hpp"
#include "js_alloc.hpp"
#include <quickjs.h>
#include <opencv2/core/types.hpp>
template<class T, size_t rows, size_t cols> using JSMatxData = cv::Matx<T, rows, cols>;
extern "C" int js_matx_init(JSContext*, JSModuleDef*);
extern "C" {
extern thread_local JSValue matx_class, matx_proto;
extern thread_local JSClassID js_matx_class_id;
int js_matx_init(JSContext*, JSModuleDef*);
}
/*template<class T, size_t rows, size_t cols>
static inline JSValue
js_matx_new(JSContext* ctx, const JSMatxData<T, rows, cols>& matx) {
JSValue ret = JS_NewArray(ctx);
for(size_t y = 0; y < rows; ++y) {
JSValue row = JS_NewArray(ctx);
JS_SetPropertyUint32(ctx, ret, y, row);
for(size_t x = 0; x < cols; ++x) {
JS_SetPropertyUint32(ctx, row, x, js_value_from<T>(ctx, matx(y, x)));
}
JS_FreeValue(ctx, row);
}
return ret;
}*/
template<class T, size_t rows, size_t cols>
static inline JSValue
js_matx_new(JSContext* ctx, const JSMatxData<T, rows, cols>& matx) {
JSValue ret = JS_NewObjectProtoClass(ctx, matx_proto, js_matx_class_id);
auto* ptr = js_allocate<JSInputOutputArray>(ctx);
*ptr = matx;
JS_SetOpaque(ret, ptr);
return ret;
}
template<class T, size_t rows, size_t cols>
static inline int
js_matx_read(JSContext* ctx, JSValueConst matx, JSMatxData<T, rows, cols>* out) {
for(size_t y = 0; y < rows; ++y) {
JSValue row = JS_GetPropertyUint32(ctx, matx, y);
for(size_t x = 0; x < cols; ++x) {
JSValue col = JS_GetPropertyUint32(ctx, row, x);
if(js_value_to(ctx, col, out->operator()(y, x)))
return 1;
JS_FreeValue(ctx, col);
}
JS_FreeValue(ctx, row);
}
return 0;
}
template<class T, size_t rows, size_t cols>
static inline int
js_value_to(JSContext* ctx, JSValueConst value, JSMatxData<T, rows, cols>& matx) {
return js_matx_read(ctx, value, &matx);
}
template<class T, size_t rows, size_t cols>
static inline JSValue
js_value_from(JSContext* ctx, const JSMatxData<T, rows, cols>& matx) {
return js_matx_new(ctx, matx);
}
extern "C" int js_matx_init(JSContext*, JSModuleDef*);
#endif /* defined(JS_MATX_HPP) */