-
Notifications
You must be signed in to change notification settings - Fork 124
/
opencv.cpp
345 lines (293 loc) · 9.79 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
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
#include "opencv.hpp"
#include <stdbool.h>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <jpeglib.h>
#include <png.h>
#include <setjmp.h>
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, const bool preserve_alpha_channel)
{
auto e_ptr = static_cast<cv::ImageEncoder*>(e);
auto mat = static_cast<const cv::Mat*>(src);
cv::Mat output;
if (mat->channels() < 4 || preserve_alpha_channel) {
// No alpha channel or we're encoding to a format that supports alpha channels, use the original mat
output = *mat;
} else {
// Handle alpha channel by blending it with a white background
std::vector<cv::Mat> channels(4);
cv::split(*mat, channels);
cv::Mat bgrChannels[] = {channels[0], channels[1], channels[2]};
cv::Mat alphaChannel = channels[3];
// Create a white background image
cv::Mat whiteBackground(mat->rows, mat->cols, CV_8UC3, cv::Scalar(255, 255, 255));
// Blend the BGR channels of the source image onto the white background using the alpha channel as a mask
cv::Mat bgr;
cv::merge(bgrChannels, 3, bgr);
bgr.copyTo(whiteBackground, alphaChannel);
// Set the output to the blended image
output = whiteBackground;
// Convert back to 8-bit if necessary
if (output.type() != CV_8UC3) {
output.convertTo(output, CV_8UC3);
}
}
std::vector<int> params;
for (size_t i = 0; i < opt_len; i++) {
params.push_back(opt[i]);
}
return e_ptr->write(output, 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;
}
struct opencv_jpeg_error_mgr {
struct jpeg_error_mgr pub;
jmp_buf setjmp_buffer;
};
void opencv_jpeg_error_exit(j_common_ptr cinfo) {
opencv_jpeg_error_mgr* myerr = (opencv_jpeg_error_mgr*) cinfo->err;
(*cinfo->err->output_message)(cinfo);
longjmp(myerr->setjmp_buffer, 1);
}
int opencv_decoder_get_jpeg_icc(void* src, size_t src_len, void* dest, size_t dest_len) {
struct jpeg_decompress_struct cinfo;
struct opencv_jpeg_error_mgr jerr;
cinfo.err = jpeg_std_error(&jerr.pub);
jerr.pub.error_exit = opencv_jpeg_error_exit;
if (setjmp(jerr.setjmp_buffer)) {
// JPEG processing error
jpeg_destroy_decompress(&cinfo);
return 0;
}
jpeg_create_decompress(&cinfo);
jpeg_mem_src(&cinfo, static_cast<unsigned char*>(src), src_len);
// Ask libjpeg to save markers that might be ICC profiles
jpeg_save_markers(&cinfo, JPEG_APP0 + 2, 0xFFFF);
// Read JPEG header
if (jpeg_read_header(&cinfo, TRUE) != JPEG_HEADER_OK) {
jpeg_destroy_decompress(&cinfo);
return 0;
}
// Check if ICC profile is available
JOCTET *icc_profile = nullptr;
unsigned int icc_length = 0;
if (jpeg_read_icc_profile(&cinfo, &icc_profile, &icc_length)) {
if (icc_length > 0 && icc_length <= dest_len) {
memcpy(dest, icc_profile, icc_length);
free(icc_profile);
jpeg_destroy_decompress(&cinfo);
return icc_length;
}
}
if (icc_profile) {
// Free the ICC profile if it was allocated but not copied
free(icc_profile);
}
jpeg_destroy_decompress(&cinfo);
return 0;
}
void opencv_decoder_png_read(png_structp png_ptr, png_bytep data, png_size_t length) {
auto buffer_info = reinterpret_cast<std::pair<const char**, size_t*>*>(png_get_io_ptr(png_ptr));
const char* &buffer = *buffer_info->first;
size_t &buffer_size = *buffer_info->second;
if (buffer_size < length) {
png_error(png_ptr, "Read error: attempting to read beyond buffer size");
return;
}
memcpy(data, buffer, length);
buffer += length;
buffer_size -= length;
}
int opencv_decoder_get_png_icc(void* src, size_t src_len, void* dest, size_t dest_len) {
// Set up libpng to read from memory
const char* buffer = reinterpret_cast<const char*>(src);
size_t buffer_size = src_len;
std::pair<const char**, size_t*> buffer_info(&buffer, &buffer_size);
png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
png_infop info_ptr = png_create_info_struct(png_ptr);
if (setjmp(png_jmpbuf(png_ptr))) {
png_destroy_read_struct(&png_ptr, &info_ptr, nullptr);
return 0;
}
png_set_read_fn(png_ptr, &buffer_info, opencv_decoder_png_read);
png_read_info(png_ptr, info_ptr);
// Check for ICC profile
png_charp icc_name;
int compression_type;
png_bytep icc_profile;
png_uint_32 icc_length;
if (png_get_iCCP(png_ptr, info_ptr, &icc_name, &compression_type, &icc_profile, &icc_length)) {
if (icc_length > 0 && icc_length <= dest_len) {
memcpy(dest, icc_profile, icc_length);
png_destroy_read_struct(&png_ptr, &info_ptr, nullptr); // handles freeing icc_profile
return icc_length;
}
}
png_destroy_read_struct(&png_ptr, &info_ptr, nullptr);
return 0;
}