forked from abduld/libwb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwbImage.cpp
118 lines (94 loc) · 3.13 KB
/
wbImage.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
#include <wb.h>
static inline float _min(float x, float y) { return x < y ? x : y; }
static inline float _max(float x, float y) { return x > y ? x : y; }
static inline float _clamp(float x, float start, float end) {
return _min(_max(x, start), end);
}
wbImage_t wbImage_new(int width, int height, int channels) {
float *data;
wbImage_t img;
img = wbNew(struct st_wbImage_t);
wbImage_setWidth(img, width);
wbImage_setHeight(img, height);
wbImage_setChannels(img, channels);
wbImage_setPitch(img, width * channels);
data = wbNewArray(float, width * height * channels);
wbImage_setData(img, data);
return img;
}
wbImage_t wbImage_new(int width, int height) {
return wbImage_new(width, height, wbImage_channels);
}
void wbImage_delete(wbImage_t img) {
if (img != NULL) {
if (wbImage_getData(img) != NULL) {
wbDelete(wbImage_getData(img));
}
wbDelete(img);
}
}
static inline void wbImage_setPixel(wbImage_t img, int x, int y, int c,
float val) {
float *data = wbImage_getData(img);
int channels = wbImage_getChannels(img);
int pitch = wbImage_getPitch(img);
data[y * pitch + x * channels + c] = val;
return;
}
static inline float wbImage_getPixel(wbImage_t img, int x, int y, int c) {
float *data = wbImage_getData(img);
int channels = wbImage_getChannels(img);
int pitch = wbImage_getPitch(img);
return data[y * pitch + x * channels + c];
}
wbBool
wbImage_sameQ(wbImage_t a, wbImage_t b, wbImage_onSameFunction_t onUnSame) {
if (a == NULL || b == NULL) {
wbLog(ERROR, "Comparing null images.");
return wbFalse;
} else if (a == b) {
return wbTrue;
} else if (wbImage_getWidth(a) != wbImage_getWidth(b)) {
wbLog(ERROR, "Image widths do not match.");
return wbFalse;
} else if (wbImage_getHeight(a) != wbImage_getHeight(b)) {
wbLog(ERROR, "Image heights do not match.");
return wbFalse;
} else if (wbImage_getChannels(a) != wbImage_getChannels(b)) {
wbLog(ERROR, "Image channels do not match.");
return wbFalse;
} else {
float *aData, *bData;
int width, height, channels;
int ii, jj, kk;
aData = wbImage_getData(a);
bData = wbImage_getData(b);
wbAssert(aData != NULL);
wbAssert(bData != NULL);
width = wbImage_getWidth(a);
height = wbImage_getHeight(a);
channels = wbImage_getChannels(a);
for (ii = 0; ii < height; ii++) {
for (jj = 0; jj < width; jj++) {
for (kk = 0; kk < channels; kk++) {
float x = _clamp(*aData++, 0, 1);
float y = _clamp(*bData++, 0, 1);
if (wbUnequalQ(x, y)) {
if (onUnSame != NULL) {
string str = wbString("Image pixels do not match at position (",
wbString(ii, ", ", jj, ", ", kk, "). [ "),
wbString(x, ", ", y, "]"));
onUnSame(str);
}
return wbFalse;
}
}
}
}
return wbTrue;
}
}
static void wbImage_onUnsameFunction(string str) { wbLog(ERROR, str); }
wbBool wbImage_sameQ(wbImage_t a, wbImage_t b) {
return wbImage_sameQ(a, b, wbImage_onUnsameFunction);
}