-
Notifications
You must be signed in to change notification settings - Fork 1
/
gaussian_blur.c
executable file
·152 lines (113 loc) · 4.11 KB
/
gaussian_blur.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bitmap.h"
/*
* The Gaussian Blur filter
*/
void gaussian_blur_filter(Bitmap *bmp) {
int width = bmp->width;
int height = bmp->height;
int n = 0;
Pixel *before = malloc(sizeof(Pixel)*width);
Pixel *current = malloc(sizeof(Pixel)*width);
// read the first row
for(int i = 0; i < width; i++) {
Pixel *pixel = malloc(sizeof(Pixel));
n = fread(pixel, 1, sizeof(Pixel), stdin);
if (n == -1) {
perror("read");
exit(1);
} else if(n == 0) {
fprintf(stderr, "Error: could not read from stdin");
exit(1);
}
before[i] = *pixel;
free(pixel);
}
// read the second row
for(int j = 0; j < width; j++) {
Pixel *pixel = malloc(sizeof(Pixel));
n = fread(pixel, 1, sizeof(Pixel), stdin);
if (n == -1) {
perror("read");
exit(1);
} else if(n == 0) {
fprintf(stderr, "Error: could not read from stdin");
exit(1);
}
current[j] = *pixel;
free(pixel);
}
// we already read row0 and row1, now read the rows from 2 - 199
//these are all the rows that will be stored in after
for(int i = 2; i < height; i++) {
// we want to store 3 pixel rows at a time
Pixel *after = malloc(sizeof(Pixel)*width);
//read all the pixels in the ith row and store them in after
for(int j = 0; j < width; j++) {
Pixel *pixel = malloc(sizeof(Pixel));
n = fread(pixel, 1, sizeof(Pixel), stdin);
if (n == -1) {
perror("read");
exit(1);
} else if(n == 0) {
fprintf(stderr, "Error: could not read from stdin");
exit(1);
}
after[j] = *pixel;
free(pixel);
}
Pixel *blurred_row = malloc(sizeof(Pixel)*width);
//read out the first 3 pixels of before, current and after to get the 3x3 matrix
for(int k = 0; k <= width - 3; k++) {
Pixel *first = malloc(sizeof(Pixel) * 3);
Pixel *middle = malloc(sizeof(Pixel) * 3);
Pixel *last = malloc(sizeof(Pixel) * 3);
//read the first 3 pixels from before, current and after
for(int a = 0; a < 3; a++) {
first[a] = before[k + a];
middle[a] = current[k + a];
last[a] = after[k + a];
}
//apply the gaussian filter and get result
Pixel result = apply_gaussian_kernel(first, middle, last);
//get result and change that in the middle row
memcpy(&blurred_row[k+1], &result, sizeof(Pixel));
if(k == 0) {
memcpy(&blurred_row[k], &result, sizeof(Pixel));
}
else if(k == width - 3) {
memcpy(&blurred_row[k+2], &result, sizeof(Pixel));
}
// free everything
free(first);
free(middle);
free(last);
}
// now 3 rows stored have been modified, write out the first row (that is, before)
if(i == 2 || i == height - 1) {
for(int r = 0; r < 2; r++) {
for(int y = 0; y < width; y++) {
fwrite(&(blurred_row[y]), 1, sizeof(Pixel), stdout);
}
}
}
else {
for(int y = 0; y < width; y++) {
fwrite(&(blurred_row[y]), 1, sizeof(Pixel), stdout);
}
}
free(blurred_row);
//update the next before and current using memcpy
memcpy(before, current, sizeof(Pixel)*width);
memcpy(current, after, sizeof(Pixel)*width);
free(after);
}
free(before);
free(current);
}
int main() {
run_filter(gaussian_blur_filter, 1);
return 0;
}