This repository has been archived by the owner on Jan 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 115
/
mz.cpp
116 lines (98 loc) · 3.77 KB
/
mz.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
// See the file "LICENSE.md" for the full license governing this code.
#include "compile.h"
#if COMPILE_DMZ
#include "stdint.h"
#include "mz.h"
#if CYTHON_DMZ
// this is where we'll pre-allocate and setup OpenGL textures, compile our program,
// and maintain a reference to all this. Return a reference to some allocated object or struct
// that tracks this information, for use by later dmz functions.
void *mz_create(void) {
return NULL;
}
// free up any persistent references to OpenGL textures, programs, shaders, etc, as well
// as any wrapping objects.
void mz_destroy(void *mz) {
}
// Perform any necessary operations prior to app backgrounding (e.g., calling glFinish() on any OpenGL contexts)
void mz_prepare_for_backgrounding(void *mz) {
}
IplImage *py_mz_create_from_cv_image_data(char *image_data, int image_size,
int width, int height,
int64_t depth, int n_channels,
int roi_x_offset, int roi_y_offset, int roi_width, int roi_height) {
int bits_per_pixel = 8;
switch(depth) {
case IPL_DEPTH_1U:
bits_per_pixel = 1;
break;
case IPL_DEPTH_8S:
case IPL_DEPTH_8U:
bits_per_pixel = 8;
break;
case IPL_DEPTH_16S:
case IPL_DEPTH_16U:
bits_per_pixel = 16;
break;
case IPL_DEPTH_32S:
case IPL_DEPTH_32F:
bits_per_pixel = 32;
break;
case IPL_DEPTH_64F:
bits_per_pixel = 64;
break;
default:
bits_per_pixel = 8;
break;
}
int step = (bits_per_pixel * n_channels * width) / 8; // bits_per_pixel is in bits, step is in bytes
// step += (4 - (step % 4)) % 4; // pad to multiple of 4 bytes
//
// Some people claim that IplImage row-padding can be to either 4 or 8 bytes. But according to
// http://opencv.willowgarage.com/documentation/c/core_basic_structures.html#CvMat it's always 4 bytes.
//
// For the Python version of IplImage, http://opencv.willowgarage.com/documentation/python/core_basic_structures.html#CvMat
// does not say anything at all about padding.
//
// For now, let's simply assert if there appears to be any padding:
if (image_size > 0 && step * height != image_size) {
fprintf(stderr, "\nstep: %d, height: %d, image_size: %d => padding: %d\n", step, height, image_size, image_size/height % step);
assert(FALSE);
}
IplImage *image = cvCreateImageHeader(cvSize(width, height), depth, n_channels);
cvSetData(image, image_data, step);
cvSetImageROI(image, cvRect(roi_x_offset, roi_y_offset, roi_width, roi_height));
return image;
}
void py_mz_release_ipl_image(IplImage *image) {
cvReleaseImageHeader(&image);
}
void py_mz_get_cv_image_data(IplImage *source,
char **image_data, int *image_size,
int *width, int *height,
int64_t *depth, int *n_channels,
int *roi_x_offset, int *roi_y_offset, int *roi_width, int *roi_height
) {
*image_data = source->imageData;
*image_size = source->imageSize;
*width = source->width;
*height = source->height;
*depth = source->depth;
*n_channels = source->nChannels;
CvRect roi = cvGetImageROI(source);
*roi_x_offset = roi.x;
*roi_y_offset = roi.y;
*roi_width = roi.width;
*roi_height = roi.height;
}
void py_mz_cvSetImageROI(IplImage* image, int left, int top, int width, int height) {
cvSetImageROI(image, cvRect(left, top, width, height));
}
void py_mz_cvResetImageROI(IplImage* image) {
cvResetImageROI(image);
}
//void py_mz_expiry_fill_borders(IplImage *character_image_float, int char_top, int char_left) {
// expiry_fill_borders(character_image_float, char_top, char_left);
//}
#endif
#endif // COMPILE_DMZ