-
Notifications
You must be signed in to change notification settings - Fork 0
/
Image.cpp
112 lines (94 loc) · 2.69 KB
/
Image.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
#include "Image.h"
#include "Utils.h"
#include <iostream>
#include <cassert>
#include <stdlib.h>
Image_t* Image_new(int width, int height, int channels, float *data) {
Image_t* img;
img = (Image_t*) malloc(sizeof(Image_t));
Image_setWidth(img, width);
Image_setHeight(img, height);
Image_setChannels(img, channels);
Image_setPitch(img, width * channels);
Image_setData(img, data);
return img;
}
Image_t* Image_new(int width, int height, int channels) {
float *data = (float*) malloc(sizeof(float) * width * height * channels);
return Image_new(width, height, channels, data);
}
Image_t* Image_new(int width, int height) {
return Image_new(width, height, Image_channels);
}
void Image_delete(Image_t* img) {
if (img != NULL) {
if (Image_getData(img) != NULL) {
free(Image_getData(img));
}
free(img);
}
}
void Image_setPixel(Image_t* img, int x, int y, int c, float val) {
float *data = Image_getData(img);
int channels = Image_getChannels(img);
int pitch = Image_getPitch(img);
data[y * pitch + x * channels + c] = val;
return;
}
float Image_getPixel(Image_t* img, int x, int y, int c) {
float *data = Image_getData(img);
int channels = Image_getChannels(img);
int pitch = Image_getPitch(img);
return data[y * pitch + x * channels + c];
}
bool Image_is_same(Image_t* a, Image_t* b) {
if (a == NULL || b == NULL) {
std::cerr << "Comparing null images." << std::endl;
return false;
} else if (a == b) {
return true;
} else if (Image_getWidth(a) != Image_getWidth(b)) {
std::cerr << "Image widths do not match." << std::endl;
return false;
} else if (Image_getHeight(a) != Image_getHeight(b)) {
std::cerr << "Image heights do not match." << std::endl;
return false;
} else if (Image_getChannels(a) != Image_getChannels(b)) {
std::cerr << "Image channels do not match." << std::endl;
return false;
} else {
float *aData, *bData;
int width, height, channels;
int ii, jj, kk;
aData = Image_getData(a);
bData = Image_getData(b);
assert(aData != NULL);
assert(bData != NULL);
width = Image_getWidth(a);
height = Image_getHeight(a);
channels = Image_getChannels(a);
for (ii = 0; ii < height; ii++) {
for (jj = 0; jj < width; jj++) {
for (kk = 0; kk < channels; kk++) {
float x, y;
if (channels <= 3) {
x = clamp(*aData++, 0, 1);
y = clamp(*bData++, 0, 1);
} else {
x = *aData++;
y = *bData++;
}
if (almostUnequalFloat(x, y)) {
std::cerr
<< "Image pixels do not match at position ( row = "
<< ii << ", col = " << jj << ", channel = "
<< kk << ") expecting a value of " << y
<< " but got a value of " << x << std::endl;
return false;
}
}
}
}
return true;
}
}