-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
365 lines (327 loc) · 11.5 KB
/
main.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#include <iostream>
#include <stdlib.h>
#include "BitmapRawConverter.h"
#include "main.h"
#include <chrono>
#include <tbb/task_group.h>
#define __ARG_NUM__ 6
#define FILTER_SIZE 5
#define THRESHOLD 128
#define GAP_SIZE (FILTER_SIZE - 1) / 2
#define EDGE_DISTANCE 2
using namespace std;
using namespace chrono;
using namespace tbb;
// Prewitt operators
//int filterHor[FILTER_SIZE * FILTER_SIZE] = { -1, 0, 1, -1, 0, 1, -1, 0, 1 };
//int filterVer[FILTER_SIZE * FILTER_SIZE] = { -1, -1, -1, 0, 0, 0, 1, 1, 1 };
int filterHor[FILTER_SIZE * FILTER_SIZE] = {
9, 9, 9, 9, 9,
9, 5, 5, 5, 9,
-7, -3, 0, -3, -7,
-7, -3, -3, -3, -7
-7, -7, -7, -7, -7
};
int filterVer[FILTER_SIZE * FILTER_SIZE] = {
9, 9, -7, -7, -7,
9, 5, -3, -3, -7,
9, 5, 0, -3, -7,
9, 5, -3, -3, -7,
9, 9, -7, -7, -7
};
/**
* @brief Prewitt filter algorithm implemented on a single inBuffer field
*
* @param inBuffer buffer of input image
* @param row the row of the inBuffer field that will be filtered
* @param column the column of the inBuffer field that will be filtered
* @param width image width
*/
int apply_prewitt_filter(int* inBuffer, int row, int column, int width)
{
int horizontalSum = 0;
int verticalSum = 0;
for (int i = row - GAP_SIZE; i <= row + GAP_SIZE; i++)
{
for (int j = column - GAP_SIZE; j <= column + GAP_SIZE; j++)
{
horizontalSum += inBuffer[i * width + j] * filterHor[(i - row + GAP_SIZE) * FILTER_SIZE + j - column + GAP_SIZE];
verticalSum += inBuffer[i * width + j] * filterVer[(i - row + GAP_SIZE) * FILTER_SIZE + j - column + GAP_SIZE];
}
}
int totalSum = abs(horizontalSum) + abs(verticalSum);
if (totalSum < THRESHOLD)
totalSum = 0;
else
totalSum = 255;
return totalSum;
}
/**
* @brief Serial version of edge detection algorithm implementation using Prewitt operator
*
* @param inBuffer buffer of input image
* @param outBuffer buffer of output image
* @param width image width
* @param height image height
*/
void filter_serial_prewitt(int* inBuffer, int* outBuffer, int width, int height)
{
for (int i = GAP_SIZE; i < height - GAP_SIZE; i++)
{
for (int j = GAP_SIZE; j < width - GAP_SIZE; j++)
{
outBuffer[i * width + j] = apply_prewitt_filter(inBuffer, i, j, width);
}
}
}
/**
* @brief One task of the parallel version of edge detection algorithm implementation using Prewitt operator
*
* @param inBuffer buffer of input image
* @param outBuffer buffer of output image
* @param width width of the part of inBuffer matrix that this task is responsible for
* @param height height of the part of inBuffer matrix that this task is responsible for
* @param row the starting row of the inBuffer matrix this task is responsible for
* @param column the starting column of the inBuffer matrix this task is responsible for
* @param fullWidth image width
*/
void perform_parallel_prewitt(int* inBuffer, int* outBuffer, int width, int height, int row, int column, int fullWidth)
{
if (width <= fullWidth / 16)
{
for (int i = row; i < row + height; i++)
{
for (int j = column; j < column + width; j++)
{
outBuffer[i * fullWidth + j] = apply_prewitt_filter(inBuffer, i, j, fullWidth);
}
}
}
else
{
task_group group;
group.run([&] {perform_parallel_prewitt(inBuffer, outBuffer, width / 2, height / 2, row, column, fullWidth);});
group.run([&] {perform_parallel_prewitt(inBuffer, outBuffer, width / 2, (height + 1) / 2, row + height / 2, column, fullWidth);});
group.run([&] {perform_parallel_prewitt(inBuffer, outBuffer, (width + 1) / 2, height / 2, row, column + width / 2, fullWidth);});
group.run([&] {perform_parallel_prewitt(inBuffer, outBuffer, (width + 1) / 2, (height + 1) / 2, row + height / 2, column + width / 2, fullWidth);});
group.wait();
}
}
/**
* @brief Parallel version of edge detection algorithm implementation using Prewitt operator
*
* @param inBuffer buffer of input image
* @param outBuffer buffer of output image
* @param width image width
* @param height image height
*/
void filter_parallel_prewitt(int* inBuffer, int* outBuffer, int width, int height)
{
perform_parallel_prewitt(inBuffer, outBuffer, width - GAP_SIZE * 2, height - GAP_SIZE * 2, GAP_SIZE, GAP_SIZE, width);
}
/**
* @brief Applies the edge detection filter algorithm to one matrix field
*
* @param inBuffer buffer of input image
* @param row the row of the inBuffer matrix field the filter will be applied to
* @param column the column of the inBuffer matrix field the filter will be applied to
* @param width image width
*/
int apply_edge_filter(int* inBuffer, int row, int column, int width)
{
int P = 0;
int O = 1;
for (int i = row - EDGE_DISTANCE; i <= row + EDGE_DISTANCE; i++)
{
for (int j = column - EDGE_DISTANCE; j <= column + EDGE_DISTANCE; j++)
{
if (i == row && j == column)
continue;
if (inBuffer[i * width + j] > THRESHOLD)
{
P = 1;
}
else
{
O = 0;
}
}
}
int result = abs(P) - abs(O);
return result * 255;
}
/**
* @brief Serial version of edge detection algorithm
*
* @param inBuffer buffer of input image
* @param outBuffer buffer of output image
* @param width image width
* @param height image height
*/
void filter_serial_edge_detection(int* inBuffer, int* outBuffer, int width, int height)
{
for (int i = EDGE_DISTANCE; i < height - EDGE_DISTANCE; i++)
{
for (int j = EDGE_DISTANCE; j < width - EDGE_DISTANCE; j++)
{
outBuffer[i * width + j] = apply_edge_filter(inBuffer, i, j, width);
}
}
}
/**
* @brief One task of the parallel version of edge detection algorithm
*
* @param inBuffer buffer of input image
* @param outBuffer buffer of output image
* @param width width of the part of inBuffer matrix that this task is responsible for
* @param height height of the part of inBuffer matrix that this task is responsible for
* @param row the starting row of the inBuffer matrix this task is responsible for
* @param column the starting column of the inBuffer matrix this task is responsible for
* @param fullWidth image width
*/
void perform_parallel_edge_detection(int* inBuffer, int* outBuffer, int width, int height, int row, int column, int fullWidth)
{
if (width <= fullWidth / 16)
{
for (int i = row; i < row + height; i++)
{
for (int j = column; j < column + width; j++)
{
outBuffer[i * fullWidth + j] = apply_edge_filter(inBuffer, i, j, fullWidth);
}
}
}
else
{
task_group group;
group.run([&] {perform_parallel_edge_detection(inBuffer, outBuffer, width / 2, height / 2, row, column, fullWidth);});
group.run([&] {perform_parallel_edge_detection(inBuffer, outBuffer, width / 2, (height + 1) / 2, row + height / 2, column, fullWidth);});
group.run([&] {perform_parallel_edge_detection(inBuffer, outBuffer, (width + 1) / 2, height / 2, row, column + width / 2, fullWidth);});
group.run([&] {perform_parallel_edge_detection(inBuffer, outBuffer, (width + 1) / 2, (height + 1) / 2, row + height / 2, column + width / 2, fullWidth);});
group.wait();
}
}
/**
* @brief Parallel version of edge detection algorithm
*
* @param inBuffer buffer of input image
* @param outBuffer buffer of output image
* @param width image width
* @param height image height
*/
void filter_parallel_edge_detection(int* inBuffer, int* outBuffer, int width, int height)
{
perform_parallel_edge_detection(inBuffer, outBuffer, width - 2 * EDGE_DISTANCE, height - 2 * EDGE_DISTANCE, EDGE_DISTANCE, EDGE_DISTANCE, width);
}
/**
* @brief Function for running test.
*
* @param testNr test identification, 1: for serial version, 2: for parallel version
* @param ioFile input/output file, firstly it's holding buffer from input image and than to hold filtered data
* @param outFileName output file name
* @param outBuffer buffer of output image
* @param width image width
* @param height image height
*/
void run_test_nr(int testNr, BitmapRawConverter* ioFile, char* outFileName, int* outBuffer, unsigned int width, unsigned int height)
{
steady_clock::time_point start = steady_clock::now();
switch (testNr)
{
case 1:
cout << "Running serial version of edge detection using Prewitt operator" << endl;
filter_serial_prewitt(ioFile->getBuffer(), outBuffer, width, height);
break;
case 2:
cout << "Running parallel version of edge detection using Prewitt operator" << endl;
filter_parallel_prewitt(ioFile->getBuffer(), outBuffer, width, height);
break;
case 3:
cout << "Running serial version of edge detection" << endl;
filter_serial_edge_detection(ioFile->getBuffer(), outBuffer, width, height);
break;
case 4:
cout << "Running parallel version of edge detection" << endl;
filter_parallel_edge_detection(ioFile->getBuffer(), outBuffer, width, height);
break;
default:
cout << "ERROR: invalid test case, must be 1, 2, 3 or 4!";
break;
}
steady_clock::time_point end = steady_clock::now();
cout << "Time to execute = " << duration_cast<milliseconds>(end - start).count() << "ms" << endl;
ioFile->setBuffer(outBuffer);
ioFile->pixelsToBitmap(outFileName);
}
/**
* @brief Print program usage.
*/
void usage()
{
cout << "\nERROR: call program like: " << endl << endl;
cout << "ProjekatPP.exe";
cout << " input.bmp";
cout << " outputSerialPrewitt.bmp";
cout << " outputParallelPrewitt.bmp";
cout << " outputSerialEdge.bmp";
cout << " outputParallelEdge.bmp" << endl << endl;
}
int main(int argc, char* argv[])
{
if (argc != __ARG_NUM__)
{
usage();
return 0;
}
BitmapRawConverter inputFile(argv[1]);
BitmapRawConverter outputFileSerialPrewitt(argv[1]);
BitmapRawConverter outputFileParallelPrewitt(argv[1]);
BitmapRawConverter outputFileSerialEdge(argv[1]);
BitmapRawConverter outputFileParallelEdge(argv[1]);
unsigned int width, height;
int test;
width = inputFile.getWidth();
height = inputFile.getHeight();
int* outBufferSerialPrewitt = new int[width * height];
int* outBufferParallelPrewitt = new int[width * height];
memset(outBufferSerialPrewitt, 0x0, width * height * sizeof(int));
memset(outBufferParallelPrewitt, 0x0, width * height * sizeof(int));
int* outBufferSerialEdge = new int[width * height];
int* outBufferParallelEdge = new int[width * height];
memset(outBufferSerialEdge, 0x0, width * height * sizeof(int));
memset(outBufferParallelEdge, 0x0, width * height * sizeof(int));
// serial version Prewitt
run_test_nr(1, &outputFileSerialPrewitt, argv[2], outBufferSerialPrewitt, width, height);
// parallel version Prewitt
run_test_nr(2, &outputFileParallelPrewitt, argv[3], outBufferParallelPrewitt, width, height);
// serial version special
run_test_nr(3, &outputFileSerialEdge, argv[4], outBufferSerialEdge, width, height);
// parallel version special
run_test_nr(4, &outputFileParallelEdge, argv[5], outBufferParallelEdge, width, height);
// verification
cout << "Verification: ";
test = memcmp(outBufferSerialPrewitt, outBufferParallelPrewitt, width * height * sizeof(int));
if (test != 0)
{
cout << "Prewitt FAIL!" << endl;
}
else
{
cout << "Prewitt PASS." << endl;
}
test = memcmp(outBufferSerialEdge, outBufferParallelEdge, width * height * sizeof(int));
if (test != 0)
{
cout << "Edge detection FAIL!" << endl;
}
else
{
cout << "Edge detection PASS." << endl;
}
// clean up
delete[] outBufferSerialPrewitt;
delete[] outBufferParallelPrewitt;
delete[] outBufferSerialEdge;
delete[] outBufferParallelEdge;
return 0;
}