-
Notifications
You must be signed in to change notification settings - Fork 4
/
image.cpp
325 lines (266 loc) · 8.32 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
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
#include "image.hpp"
#include <cstdio>
#include <iostream>
#include <fstream>
#include <FreeImage.h>
#include <omp.h>
#define SCALE_FACTOR 256
using namespace std;
// class Image
Image::Image(const int width, const int height, const float *data) {
if (width < 0)
cerr << "Image(int,int,float*): Argument width = "<< width <<"!" << endl;
if (height < 0)
cerr << "Image(int,int,float*): Argument height = "<< height <<"!" << endl;
this->width = width;
this->height = height;
size = width * height;
this->data = new float[size];
for (int i = 0; i < size; i++) {
this->data[i] = data[i];
}
offsetL = 0; offsetR = 0; offsetT = 0;
}
Image::Image(const int width, const int height) {
if (width < 0)
cerr << "Image(int,int): Argument width = "<< width <<"!" << endl;
if (height < 0)
cerr << "Image(int,int): Argument height = "<< height <<"!" << endl;
this->width = width;
this->height = height;
size = width * height;
this->data = new float[size];
offsetL = 0; offsetR = 0; offsetT = 0;
}
Image::Image(const Image *copy) {
width = copy->getWidth();
height = copy->getHeight();
size = copy->getSize();
data = new float[size];
float *copyData = copy->getData();
for (int i = 0; i < size; i++) {
this->data[i] = copyData[i];
}
offsetL = copy->getOffsetL();
offsetR = copy->getOffsetR();
offsetT = copy->getOffsetT();
}
Image::Image(Image const ©) {
width = copy.getWidth();
height = copy.getHeight();
size = copy.getSize();
float *copyData = copy.data;
for (int i = 0; i < size; i++)
data[i] = copyData[i];
offsetL = copy.offsetL;
offsetR = copy.offsetR;
offsetT = copy.offsetT;
}
Image::~Image() {
delete [] data;
}
void Image::swap(Image &s) {
int temp = width; width = s.width; s.width = temp;
temp = height; height = s.height; s.height = temp;
temp = size; size = s.size; s.size = temp;
temp = offsetL; offsetL = s.offsetL; s.offsetL = temp;
temp = offsetR; offsetR = s.offsetR; s.offsetR = temp;
temp = offsetT; offsetT = s.offsetT; s.offsetT = temp;
float *temp2 = data; data = s.data; s.data = temp2;
}
Image *Image::getScaledImage(const int width, const int height, const float *data) {
int size = width * height;
float *newData = new float[size];
for (int i = 0; i < size; i++) newData[i] = data[i] / SCALE_FACTOR;
Image *ret = new Image(width, height, newData);
delete [] newData;
return ret;
}
int Image::getWidth() const {
return width;
}
int Image::getHeight() const {
return height;
}
float *Image::getData() const {
return data;
}
int Image::getSize() const {
return size;
}
float Image::getPixel(int y, int x) const {
y += offsetT; x += offsetL;
// Adress pixels outside of image (border replication)
if (y < offsetT) y = offsetT;
else if (y >= height) y = height-1+offsetT;
if (x < offsetL) x = offsetL;
else if (x >= width) x = width-1+offsetL;
return data[y*(width+offsetL+offsetR) + x];
}
void Image::setPixel(int y, int x, float val) {
if (y < 0 || y >= (height) || y < 0 || x >= width)
cerr << "Image::setPixel("<< y <<","<< x <<") out of bounds "<< height <<","<< width << endl;
y += offsetT; x += offsetL;
data[y*(width+offsetL+offsetR) + x] = val;
}
Image *Image::getSubRegion(struct SubRegion *R0) {
int x = R0->x, y = R0->y, r = R0->r;
// border locations in this image
int xR = min(x+r, width-1), yB = min(y+r, height-1);
int xL = x-r;
if (xL < 0) xL = 0; else R0->x = r;
int yT = y-r;
if (yT < 0) yT = 0; else R0->y = r;
// new dimensions
const int newWidth = xR+1 - xL;
const int newHeight = yB+1 - yT;
if (newWidth<0 || newHeight<0 || xL<0 || xR<0 || yT<0 || yB<0) {
cerr << "Failure in Image::getSubRegion:" << endl;
cerr << "Parameters: r = "<< r <<", x = "<< x <<", y = "<< y << endl;
cerr << "Size of Image: "<< width <<" x "<< height << endl;
cerr << "(xL, xR) = ("<< xL <<", "<< xR <<")" << endl;
cerr << "(yT, yB) = ("<< yT <<", "<< yB <<")" << endl;
cerr << "(newWidth, newHeight) = ("<< newWidth <<", "<< newHeight <<")" << endl;
}
// create sub-image
Image *ret = new Image(newWidth, newHeight);
#ifdef _PARALLEL_
#pragma omp parallel for
#endif
for (int i = 0; i < newHeight; i++)
for (int j = 0; j < newWidth; j++)
ret->setPixel(i,j, getPixel(i+yT, j+xL));
return ret;
}
void Image::cutBorders(bool left, bool right, bool top, bool bottom) {
if (left) {
offsetL++;
width--;
}
if (right) {
offsetR++;
width--;
}
if (top) {
offsetT++;
height--;
}
if (bottom)
height--;
}
Image *Image::copy() const {
return new Image(width, height, data);
}
void Image::print() const {
for (int r = 0; r < height; r++) {
for (int c = 0; c < width; c++) {
cout << getPixel(r, c) << " ";
}
cout << endl;
}
}
void Image::saveImage(const char *fileName) const {
/* unsigned char *dataRGB = new unsigned char[size * 3];
for (int i = 0; i < size; i++) {
dataRGB[i*3] = (unsigned char) (data[i] * SCALE_FACTOR);
dataRGB[i*3 + 1] = (unsigned char) (data[i] * SCALE_FACTOR);
dataRGB[i*3 + 2] = (unsigned char) (data[i] * SCALE_FACTOR);
}
FILE *f = fopen(fileName, "wb+");
fwrite(ImageRGB::getInfoHeader(), sizeof(unsigned char), 54, f);
fwrite(dataRGB, sizeof(unsigned char), size*3, f);
fclose(f);*/
// FIBITMAP *bitmap = FreeImage_Allocate(width, heigth, 24);
unsigned char *dataGrey = new unsigned char[size];
for (int i = 0; i < size; i++)
dataGrey[i] = (BYTE) (data[i] * SCALE_FACTOR);
FIBITMAP *bitmap = FreeImage_ConvertFromRawBits(dataGrey, width, height, width, 8, 0,0,0, false);
FreeImage_Save(FIF_PNG, bitmap, fileName);
FreeImage_Unload(bitmap);
delete [] dataGrey;
}
/*Image &Image::operator=(const Image &a) {
width = a.getWidth();
height = a.getHeight();
size = a.getSize();
float *oldData = a.getData();
for (int i = 0; i < size; i++)
data[i] = oldData[i];
return *this;
}
*/
// class ImageRGB
unsigned char ImageRGB::info[54];
ImageRGB::ImageRGB(const char *filePath) {
FILE *f = fopen(filePath, "rb");
fread(info, sizeof(unsigned char), 54, f); // read the 54-byte header
// extract image height and width from header
width = *(int*)&info[18];
height = *(int*)&info[22];
size = 3 * width * height;
dataRGB = new unsigned char[size]; // allocate 3 bytes per pixel
dataYUV = new float[size];
fread(dataRGB, sizeof(unsigned char), size, f); // read the rest of the data at once
fclose(f);
for (int i = 0; i < size; i += 3) {
unsigned char r = dataRGB[i];
unsigned char g = dataRGB[i+1];
unsigned char b = dataRGB[i+2];
// convert to YUV color space
float y = 0.299 * (float) r + 0.587 * (float) g + 0.144 * (float) b;
float u = 0.493 * ((float) b - y);
float v = 0.877 * ((float) r - y);
dataYUV[i] = y;
dataYUV[i+1] = u;
dataYUV[i+2] = v;
}
}
ImageRGB::~ImageRGB() {
delete [] dataRGB;
delete [] dataYUV;
}
int ImageRGB::getWidth() const {
return width;
}
int ImageRGB::getHeight() const {
return height;
}
int ImageRGB::getSize() const {
return size;
}
Image *ImageRGB::getYChannel() const {
float *dataY = new float[size/3];
for (int i = 0; i < size/3; i++)
dataY[i] = dataYUV[i*3];
Image *ret = Image::getScaledImage(width, height, dataY);
delete [] dataY;
return ret;
}
void ImageRGB::setYChannel(Image *Y) {
// cout << "ImageRGB::setYChannel()" <<endl;
float *dataY = Y->getData();
// cout << " - Y->getData()" <<endl;
for (int i = 0; i < size; i += 3)
dataYUV[i] = dataY[i/3] * SCALE_FACTOR;
// cout << " - Data copied into ImageRGB object" <<endl;
for (int i = 0; i < size; i += 3) {
float y = dataYUV[i];
float u = dataYUV[i+1];
float v = dataYUV[i+2];
unsigned char r = (unsigned char) (y + v/0.877);
unsigned char g = (unsigned char) (y - 0.39393 * u - 0.58081 * v);
unsigned char b = (unsigned char) (y + u/0.493);
dataRGB[i] = r;
dataRGB[i+1] = g;
dataRGB[i+2] = b;
}
}
void ImageRGB::saveImage(const char *fileName) const {
FILE *f = fopen(fileName, "wb+");
fwrite(info, sizeof(unsigned char), 54, f);
fwrite(dataRGB, sizeof(unsigned char), size, f);
fclose(f);
}
unsigned char *ImageRGB::getInfoHeader() {
return info;
}