-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgaussianfilter.c
More file actions
270 lines (233 loc) · 7.07 KB
/
gaussianfilter.c
File metadata and controls
270 lines (233 loc) · 7.07 KB
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
#include "helpers.h"
#include <stdio.h>
#include <pthread.h>
#include <string.h>
typedef struct coords
{
size_t x;
size_t y;
} coords;
coords posToCoord(size_t width, size_t pos)
{
coords coords = {pos % width, pos / width};
return coords;
}
size_t CoordToPos(size_t width, coords coords)
{
return coords.y * width + coords.x;
}
Pixel compute_blur(coords start, coords end, const Pixel_array * data)
{
Pixel new_value = {0,0,0};
for (int x = start.x; x <= end.x; ++x)
{
for (int y = start.y; y <= end.y; ++y)
{
coords c = {x, y};
new_value.r += data->array[CoordToPos(data->width, c)].r;
new_value.g += data->array[CoordToPos(data->width, c)].g;
new_value.b += data->array[CoordToPos(data->width, c)].b;
}
}
new_value.r /= 9;
new_value.g /= 9;
new_value.b /= 9;
return new_value;
}
Pixel mid_blur(const Pixel_array *data, size_t pos)
{
coords curr = posToCoord(data->width, pos), strt = {curr.x - 1, curr.y - 1}, end = {curr.x + 1, curr.y + 1};
return compute_blur(strt, end, data);
}
Pixel top_blur(const Pixel_array *data, size_t pos)
{
coords curr = posToCoord(data->width, pos), strt = {curr.x, curr.y - 1}, end = {curr.x + 2, curr.y + 1};
return compute_blur(strt, end, data);
}
Pixel bottom_blur(const Pixel_array *data, size_t pos)
{
coords curr = posToCoord(data->width, pos), strt = {curr.x - 2, curr.y - 1}, end = {curr.x, curr.y + 1};
return compute_blur(strt, end, data);
}
Pixel left_blur(const Pixel_array *data, size_t pos)
{
coords curr = posToCoord(data->width, pos), strt = {curr.x - 1, curr.y}, end = {curr.x + 1, curr.y + 2};
return compute_blur(strt, end, data);
}
Pixel right_blur(const Pixel_array *data, size_t pos)
{
coords curr = posToCoord(data->width, pos), strt = {curr.x - 1, curr.y - 2}, end = {curr.x + 1, curr.y};
return compute_blur(strt, end, data);
}
Pixel top_right_blur(const Pixel_array *data, size_t pos)
{
coords curr = posToCoord(data->width, pos), strt = {curr.x, curr.y - 2}, end = {curr.x + 2, curr.y};
return compute_blur(strt, end, data);
}
Pixel top_left_blur(const Pixel_array *data, size_t pos)
{
coords curr = posToCoord(data->width, pos), strt = {curr.x, curr.y}, end = {curr.x + 2, curr.y + 2};
return compute_blur(strt, end, data);
}
Pixel bottom_left_blur(const Pixel_array *data, size_t pos)
{
coords curr = posToCoord(data->width, pos), strt = {curr.x - 2, curr.y}, end = {curr.x, curr.y + 2};
return compute_blur(strt, end, data);
}
Pixel bottom_right_blur(const Pixel_array *data, size_t pos)
{
coords curr = posToCoord(data->width, pos), strt = {curr.x, curr.y - 1}, end = {curr.x + 2, curr.y + 1};
return compute_blur(strt, end, data);
}
/*
void blur(Pixel_array *data)
{
size_t width = data->width;
Pixel_P new_data = (Pixel_P) malloc(data->sz*sizeof(Pixel));
for (int i = 0; i < data->sz; ++i)
{
printf("%d of %lu pixels done.\n", i + 1, data->sz);
if (i % width == 0)
{
if (i / width == 0)
{
new_data[i] = top_left_blur(data, i);
continue;
}
else if (i / width == width - 1)
{
new_data[i] = bottom_left_blur(data, i);
continue;
}
else
{
new_data[i] = left_blur(data, i);
continue;
}
}
else if (i % width == width - 1)
{
if (i / width == 0)
{
new_data[i] = top_right_blur(data, i);
continue;
}
else if (i / width == width - 1)
{
new_data[i] = bottom_right_blur(data, i);
continue;
}
else
{
new_data[i] = right_blur(data, i);
continue;
}
}
else if (i / width == 0)
{
new_data[i] = top_blur(data, i);
continue;
}
else if (i / width == width - 1)
{
new_data[i] = bottom_blur(data, i);
continue;
}
}
//free(data->array);
data->array = new_data;
} */
Pixel do_blur(const Pixel_array * data, size_t pos)
{
size_t width = data->width;
if (pos % width == 0)
{
if (pos / width == 0)
return top_left_blur(data, pos);
else if ( pos / width == width - 1)
return bottom_left_blur(data, pos);
else
return left_blur(data, pos);
}
else if (pos % width == width - 1)
{
if (pos / width == 0)
return top_right_blur(data, pos);
else if (pos / width == width - 1)
return bottom_right_blur(data,pos);
else
return right_blur(data, pos);
}
else if (pos / width == 0)
return top_blur(data, pos);
else if (pos / width == width - 1)
return bottom_blur(data, pos);
else
return mid_blur(data, pos);
}
typedef struct thread_data
{
const Pixel_array * data;
Pixel_P *part;
size_t start;
size_t end;
} thread_data;
void * blur_par_sup(void * arg)
{
static int gen_id = 1;
int thread_id = gen_id++;
const Pixel_array * data = ((thread_data *) arg)->data;
Pixel_P *part = ((thread_data *) arg)->part;
size_t strt = ((thread_data *) arg)->start, end = ((thread_data *) arg)->end;
*part = (Pixel_P) malloc((end - strt) * sizeof(Pixel));
for (int i = strt; i < end; ++i)
{
(*part)[i - strt] = do_blur(data, i);
}
printf("\nthread no. %d is done\n", thread_id);
return NULL ;
}
void blur_parallel(Pixel_array *data)
{
// divide data into n segments
unsigned noSegments = 8;
size_t sz_segment = data->sz / noSegments;
Pixel_P * parts = (Pixel_P * ) malloc(noSegments * sizeof(Pixel_P));
// create a thread for each segment which blurs it individually
pthread_t * threads = (pthread_t *) malloc(noSegments * sizeof(pthread_t));
thread_data **t_data = (thread_data **) malloc(noSegments * sizeof(thread_data *));
for (int i = 0; i < noSegments; ++i)
{
t_data[i] = (thread_data *) malloc(sizeof(thread_data));
t_data[i]->data = data;
t_data[i]->part = &parts[i];
t_data[i]->start = i * sz_segment;
t_data[i]->end = (i + 1) * sz_segment;
if ( i == noSegments - 1)
{
t_data[i]->end = data->sz;
pthread_create(threads + i, NULL, blur_par_sup,(void *) &t_data[i]);
}
else
pthread_create(threads + i, NULL, blur_par_sup,(void *) &t_data[i]);
}
for (int i = 0; i < noSegments; ++i)
{
pthread_join(threads[i], NULL);
}
// combine all the data the end
// replace the data
for (int i = 0; i < data->sz; ++i)
{
data->array[i] = parts[i / sz_segment][i % sz_segment];
}
/* // clean
for (int i = 0; i < noSegments; ++i)
{
free(t_data[i]);
free(parts[i]);
}
free(parts);
free(threads);
free(t_data); */
}