-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_processing_c.c
333 lines (276 loc) · 11.2 KB
/
image_processing_c.c
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
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <omp.h>
#include "ppm.h"
typedef struct {
float red,green,blue;
} AccuratePixel;
typedef struct {
int x, y;
AccuratePixel *data;
} AccurateImage;
// Convert ppm to high precision format.
AccurateImage *convertToAccurateImage(PPMImage *image) {
// Make a copy
AccurateImage *imageAccurate;
imageAccurate = (AccurateImage *)malloc(sizeof(AccurateImage));
int dim = image->x * image->y;
imageAccurate->data = (AccuratePixel*)malloc(dim * sizeof(AccuratePixel));
#pragma omp parallel
{
int thread_id = omp_get_thread_num();
int n_threads = omp_get_num_threads();
int tilePart = (dim)/n_threads;
int tileStart = tilePart*thread_id;
int top = ((tileStart+tilePart)<dim)?((tileStart+tilePart)):dim;
for(int i = tileStart; i < top; i++) {
imageAccurate->data[i].red = (float) image->data[i].red;
imageAccurate->data[i].green = (float) image->data[i].green;
imageAccurate->data[i].blue = (float) image->data[i].blue;
}
}
imageAccurate->x = image->x;
imageAccurate->y = image->y;
return imageAccurate;
}
AccurateImage *convertToBlankAccurateImage(PPMImage *image) {
// Make a copy
AccurateImage *imageAccurate;
imageAccurate = (AccurateImage *)malloc(sizeof(AccurateImage));
int dim = image->x * image->y;
imageAccurate->data = (AccuratePixel*)malloc(dim * sizeof(AccuratePixel));
imageAccurate->x = image->x;
imageAccurate->y = image->y;
return imageAccurate;
}
PPMImage * convertToPPPMImage(AccurateImage *imageIn) {
PPMImage *imageOut;
imageOut = (PPMImage *)malloc(sizeof(PPMImage));
int dim = imageIn->x * imageIn->y;
imageOut->data = (PPMPixel*)malloc(dim * sizeof(PPMPixel));
imageOut->x = imageIn->x;
imageOut->y = imageIn->y;
#pragma omp parallel
{
int thread_id = omp_get_thread_num();
int n_threads = omp_get_num_threads();
int tilePart = (dim)/n_threads;
int tileStart = tilePart*thread_id;
int top = ((tileStart+tilePart)<dim)?((tileStart+tilePart)):dim;
for(int i = tileStart; i < top; i++) {
imageOut->data[i].red = imageIn->data[i].red;
imageOut->data[i].green = imageIn->data[i].green;
imageOut->data[i].blue = imageIn->data[i].blue;
}
}
return imageOut;
}
// blur one color channel
void blurCornersIteration(AccurateImage *imageOut, AccurateImage *imageIn, int size) {
unsigned short corners[2][4] = {
{
0, size,
},
{
imageIn->x-size, imageIn->x,
}
};
for(unsigned short i = 0; i < 2; i++) {
#pragma omp parallel
{
unsigned short thread_id = omp_get_thread_num();
unsigned short n_threads = omp_get_num_threads();
short sX = corners[i][0], eX = corners[i][1];
unsigned short tilePart = (imageIn->y)/n_threads;
unsigned short tileStart = tilePart*thread_id;
unsigned short topY = ((tileStart+tilePart)< imageIn->y)?((tileStart+tilePart)):(imageIn->y);
for(short senterY = tileStart; senterY < topY; senterY++) {
unsigned short endY = (senterY+size < imageIn->y)? senterY+size+1:imageIn->y;
unsigned short startY = ((senterY-size)>0)? senterY-size:0;
for(short senterX = sX; senterX < eX; senterX++) {
// For each pixel we compute the magic number
float sumR = 0, sumG = 0, sumB = 0;
unsigned short endX = (senterX+size < imageIn->x)? senterX+size+1:imageIn->x;
unsigned short startX = ((senterX-size)>0)? senterX-size:0;
float countIncluded = (endX-startX)*(endY-startY);
for(unsigned short x = startX; x < endX; x++) {
for(unsigned short y = startY; y < endY; y++) {
// Now we can begin
int offsetOfThePixel = (imageIn->x * y + x);
sumR += imageIn->data[offsetOfThePixel].red;
sumG += imageIn->data[offsetOfThePixel].green;
sumB += imageIn->data[offsetOfThePixel].blue;
// Keep track of how many values we have included
}
}
int offsetOfThePixel = (imageOut->x * senterY + senterX);
imageOut->data[offsetOfThePixel].red = sumR/countIncluded;
imageOut->data[offsetOfThePixel].green = sumG/countIncluded;
imageOut->data[offsetOfThePixel].blue = sumB/countIncluded;
}
}
}
}
}
void iterate_sub_col(AccuratePixel* cols, int* index, int width, float red, float green, float blue) {
*index+=1;
if (*index >= width) *index = 0;
cols[*index].red = red;
cols[*index].green = green;
cols[*index].blue = blue;
}
AccuratePixel* take_col(AccuratePixel* cols, int* index, int width) {
*index+=1;
if (*index >= width) *index = 0;
return &cols[*index];
}
void blurIteration(AccurateImage *imageOut, AccurateImage *imageIn, int size) {
// Iterate over each pixel
blurCornersIteration(imageOut, imageIn, size);
#pragma omp parallel
{
unsigned short thread_id = omp_get_thread_num();
unsigned short n_threads = omp_get_num_threads();
unsigned short tilePart = imageIn->y/n_threads;
unsigned short tileStart = tilePart*thread_id;
unsigned short topY = ((tileStart+tilePart)< imageIn->y)?((tileStart+tilePart)):(imageIn->y);
unsigned short width = size+size+1;
AccuratePixel* prev_col_val = malloc(width*sizeof(AccuratePixel));
int* rep_col_pointer = 0;
int* col_pointer = 0;
for(unsigned short senterY = tileStart; senterY < topY; senterY++) {
unsigned short topY = (senterY+size < imageIn->y)? senterY+size:imageIn->y-1;
unsigned short bottomY = ((senterY-size)>0)? senterY-size:0;
float numElements = (size+size+1)*(topY-bottomY+1);
numElements = pow(numElements,-1);
int offsetOfThePixel = (imageIn->x * senterY + size);
float sumR = 0, sumG = 0, sumB = 0;
for(unsigned short x = 0; x <= (size+size); x++) {
float csumR = 0, csumG = 0, csumB = 0;
for(unsigned short y = bottomY; y <= topY; y++) {
int offsetOfThePixel = (imageIn->x * y + x);
csumR += imageIn->data[offsetOfThePixel].red;
csumG += imageIn->data[offsetOfThePixel].green;
csumB += imageIn->data[offsetOfThePixel].blue;
}
iterate_sub_col(prev_col_val,&rep_col_pointer, width, csumR, csumG, csumB);
sumR+=csumR;
sumG+=csumG;
sumB+=csumB;
}
imageOut->data[offsetOfThePixel].red = sumR*numElements;
imageOut->data[offsetOfThePixel].green = sumG*numElements;
imageOut->data[offsetOfThePixel].blue = sumB*numElements;
int yRow = imageIn->x * bottomY;
for(unsigned short senterX = size+1; senterX < imageIn->x-size; senterX++) {
// For each pixel we compute the magic number
offsetOfThePixel = (imageIn->x * senterY + senterX);
unsigned short leftX = senterX-size-1;
unsigned short rightX = senterX+size;
int rightOffset = (yRow + rightX);
float csumR = 0, csumG = 0, csumB = 0;
for(int y = bottomY; y <= topY; y++) {
csumR += imageIn->data[rightOffset].red;
csumG+= imageIn->data[rightOffset].green;
csumB+= imageIn->data[rightOffset].blue;
rightOffset+=imageIn->x;
}
AccuratePixel* r_col = take_col(prev_col_val, &col_pointer, width);
sumR= sumR+csumR-r_col->red;
sumG= sumG+csumG-r_col->green;
sumB= sumB+csumB-r_col->blue;
iterate_sub_col(prev_col_val,&rep_col_pointer, width, csumR, csumG, csumB);
imageOut->data[offsetOfThePixel].red = sumR*numElements;
imageOut->data[offsetOfThePixel].green = sumG*numElements;
imageOut->data[offsetOfThePixel].blue = sumB*numElements;
}
}
}
}
// Perform the final step, and return it as ppm.
PPMImage * imageDifference(AccurateImage *imageInSmall, AccurateImage *imageInLarge) {
PPMImage *imageOut;
imageOut = (PPMImage *)malloc(sizeof(PPMImage));
int dim = imageInSmall->x * imageInSmall->y;
imageOut->data = (PPMPixel*)malloc(dim * sizeof(PPMPixel));
imageOut->x = imageInSmall->x;
imageOut->y = imageInSmall->y;
#pragma omp parallel
{
short thread_id = omp_get_thread_num();
short n_threads = omp_get_num_threads();
int tilePart = (dim)/n_threads;
int tileStart = tilePart*thread_id;
int top = ((tileStart+tilePart)<dim)?((tileStart+tilePart)):dim;
for(int i = tileStart; i < top; i++) {
imageOut->data[i].red =((short)(imageInLarge->data[i].red - imageInSmall->data[i].red))%255;
imageOut->data[i].green = ((short)(imageInLarge->data[i].green - imageInSmall->data[i].green))%255;
imageOut->data[i].blue = ((short)(imageInLarge->data[i].blue - imageInSmall->data[i].blue))%255;
}
}
return imageOut;
}
int main(int argc, char** argv) {
// read image
PPMImage *image;
// select where to read the image from
if(argc > 1) {
// from file for debugging (with argument)
image = readPPM("flower.ppm");
} else {
// from stdin for cmb
image = readStreamPPM(stdin);
}
AccurateImage *imageAccurate = convertToAccurateImage(image);
AccurateImage *imageAccurate1_tiny = convertToBlankAccurateImage(image);
AccurateImage *imageAccurate2_tiny = convertToBlankAccurateImage(image);
// Process the tiny case:
int size = 2;
blurIteration(imageAccurate2_tiny, imageAccurate, size);
blurIteration(imageAccurate1_tiny, imageAccurate2_tiny, size);
blurIteration(imageAccurate2_tiny, imageAccurate1_tiny, size);
blurIteration(imageAccurate1_tiny, imageAccurate2_tiny, size);
blurIteration(imageAccurate2_tiny, imageAccurate1_tiny, size);
AccurateImage *imageAccurate1_small = convertToBlankAccurateImage(image);
AccurateImage *imageAccurate2_small = convertToBlankAccurateImage(image);
// Process the small case:
size = 3;
blurIteration(imageAccurate2_small, imageAccurate, size);
blurIteration(imageAccurate1_small, imageAccurate2_small, size);
blurIteration(imageAccurate2_small, imageAccurate1_small, size);
blurIteration(imageAccurate1_small, imageAccurate2_small, size);
blurIteration(imageAccurate2_small, imageAccurate1_small, size);
AccurateImage *imageAccurate1_medium = convertToBlankAccurateImage(image);
AccurateImage *imageAccurate2_medium = convertToBlankAccurateImage(image);
// Process the medium case:
size = 5;
blurIteration(imageAccurate2_medium, imageAccurate, size);
blurIteration(imageAccurate1_medium, imageAccurate2_medium, size);
blurIteration(imageAccurate2_medium, imageAccurate1_medium, size);
blurIteration(imageAccurate1_medium, imageAccurate2_medium, size);
blurIteration(imageAccurate2_medium, imageAccurate1_medium, size);
AccurateImage *imageAccurate1_large = convertToBlankAccurateImage(image);
AccurateImage *imageAccurate2_large = convertToBlankAccurateImage(image);
// Do each color channel
size = 8;
blurIteration(imageAccurate2_large, imageAccurate, size);
blurIteration(imageAccurate1_large, imageAccurate2_large, size);
blurIteration(imageAccurate2_large, imageAccurate1_large, size);
blurIteration(imageAccurate1_large, imageAccurate2_large, size);
blurIteration(imageAccurate2_large, imageAccurate1_large, size);
// calculate difference
PPMImage *final_tiny = imageDifference(imageAccurate2_tiny, imageAccurate2_small);
PPMImage *final_small = imageDifference(imageAccurate2_small, imageAccurate2_medium);
PPMImage *final_medium = imageDifference(imageAccurate2_medium, imageAccurate2_large);
// Save the images.
if(argc > 1) {
writePPM("flower_tiny.ppm", final_tiny);
writePPM("flower_small.ppm", final_small);
writePPM("flower_medium.ppm", final_medium);
} else {
writeStreamPPM(stdout, final_tiny);
writeStreamPPM(stdout, final_small);
writeStreamPPM(stdout, final_medium);
}
}