forked from discord/lilliput
-
Notifications
You must be signed in to change notification settings - Fork 0
/
opencv.cpp
209 lines (181 loc) · 5.3 KB
/
opencv.cpp
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
#include "opencv.hpp"
#include <stdbool.h>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
opencv_mat opencv_mat_create(int width, int height, int type)
{
return new cv::Mat(height, width, type);
}
opencv_mat opencv_mat_create_from_data(int width, int height, int type, void* data, size_t data_len)
{
size_t total_size = width * height * CV_ELEM_SIZE(type);
if (total_size > data_len) {
return NULL;
}
auto mat = new cv::Mat(height, width, type, data);
mat->datalimit = (uint8_t*)data + data_len;
return mat;
}
opencv_mat opencv_mat_create_empty_from_data(int length, void* data)
{
// this is slightly sketchy - what we're going to do is build a 1x0 matrix
// and then set its data* properties to reflect the capacity (given by length arg here)
// this tells opencv internally that the Mat can store more but has nothing in it
// this is directly analogous to Go's len and cap
auto mat = new cv::Mat(0, 1, CV_8U, data);
mat->datalimit = mat->data + length;
return mat;
}
bool opencv_mat_set_row_stride(opencv_mat mat, size_t stride)
{
auto m = static_cast<cv::Mat*>(mat);
if (m->step == stride) {
return true;
}
size_t width = m->cols;
size_t height = m->rows;
auto type = m->type();
auto width_stride = width * CV_ELEM_SIZE(type);
if (stride < width_stride) {
return false;
}
if (m->step != width_stride) {
// refuse to set the stride if it's already set
// the math for that is confusing and probably unnecessary to figure out
return false;
}
size_t total_size = stride * height;
if ((m->datastart + total_size) > m->datalimit) {
// don't exceed end of data array
return false;
}
m->step = stride;
return true;
}
void opencv_mat_release(opencv_mat mat)
{
auto m = static_cast<cv::Mat*>(mat);
delete m;
}
int opencv_type_depth(int type)
{
return CV_ELEM_SIZE1(type) * 8;
}
int opencv_type_channels(int type)
{
return CV_MAT_CN(type);
}
int opencv_type_convert_depth(int t, int depth)
{
return CV_MAKETYPE(depth, CV_MAT_CN(t));
}
opencv_decoder opencv_decoder_create(const opencv_mat buf)
{
auto mat = static_cast<const cv::Mat*>(buf);
cv::ImageDecoder* d = new cv::ImageDecoder(*mat);
if (d->empty()) {
delete d;
d = NULL;
}
return d;
}
const char* opencv_decoder_get_description(const opencv_decoder d)
{
auto d_ptr = static_cast<cv::ImageDecoder*>(d);
return d_ptr->getDescription().c_str();
}
void opencv_decoder_release(opencv_decoder d)
{
auto d_ptr = static_cast<cv::ImageDecoder*>(d);
delete d_ptr;
}
bool opencv_decoder_read_header(opencv_decoder d)
{
auto d_ptr = static_cast<cv::ImageDecoder*>(d);
return d_ptr->readHeader();
}
int opencv_decoder_get_width(const opencv_decoder d)
{
auto d_ptr = static_cast<cv::ImageDecoder*>(d);
return d_ptr->width();
}
int opencv_decoder_get_height(const opencv_decoder d)
{
auto d_ptr = static_cast<cv::ImageDecoder*>(d);
return d_ptr->height();
}
int opencv_decoder_get_pixel_type(const opencv_decoder d)
{
auto d_ptr = static_cast<cv::ImageDecoder*>(d);
return d_ptr->type();
}
int opencv_decoder_get_orientation(const opencv_decoder d)
{
auto d_ptr = static_cast<cv::ImageDecoder*>(d);
return d_ptr->orientation();
}
bool opencv_decoder_read_data(opencv_decoder d, opencv_mat dst)
{
auto d_ptr = static_cast<cv::ImageDecoder*>(d);
auto* mat = static_cast<cv::Mat*>(dst);
return d_ptr->readData(*mat);
}
opencv_encoder opencv_encoder_create(const char* ext, opencv_mat dst)
{
auto* mat = static_cast<cv::Mat*>(dst);
return new cv::ImageEncoder(ext, *mat);
}
void opencv_encoder_release(opencv_encoder e)
{
auto e_ptr = static_cast<cv::ImageEncoder*>(e);
delete e_ptr;
}
bool opencv_encoder_write(opencv_encoder e, const opencv_mat src, const int* opt, size_t opt_len)
{
auto e_ptr = static_cast<cv::ImageEncoder*>(e);
auto mat = static_cast<const cv::Mat*>(src);
std::vector<int> params;
for (size_t i = 0; i < opt_len; i++) {
params.push_back(opt[i]);
}
return e_ptr->write(*mat, params);
};
void opencv_mat_resize(const opencv_mat src,
opencv_mat dst,
int width,
int height,
int interpolation)
{
cv::resize(*static_cast<const cv::Mat*>(src),
*static_cast<cv::Mat*>(dst),
cv::Size(width, height),
0,
0,
interpolation);
}
opencv_mat opencv_mat_crop(const opencv_mat src, int x, int y, int width, int height)
{
auto ret = new cv::Mat;
*ret = (*static_cast<const cv::Mat*>(src))(cv::Rect(x, y, width, height));
return ret;
}
void opencv_mat_orientation_transform(CVImageOrientation orientation, opencv_mat mat)
{
auto cvMat = static_cast<cv::Mat*>(mat);
cv::OrientationTransform(int(orientation), *cvMat);
}
int opencv_mat_get_width(const opencv_mat mat)
{
auto cvMat = static_cast<const cv::Mat*>(mat);
return cvMat->cols;
}
int opencv_mat_get_height(const opencv_mat mat)
{
auto cvMat = static_cast<const cv::Mat*>(mat);
return cvMat->rows;
}
void* opencv_mat_get_data(const opencv_mat mat)
{
auto cvMat = static_cast<const cv::Mat*>(mat);
return cvMat->data;
}