-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.c
237 lines (191 loc) · 6.75 KB
/
helpers.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
#include "helpers.h"
#include <math.h>
// Convert image to grayscale
void grayscale(int height, int width, RGBTRIPLE image[height][width])
{
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
int rgbt = round((image[i][j].rgbtBlue + image[i][j].rgbtGreen + image[i][j].rgbtRed) / 3.0);
image[i][j].rgbtBlue = image[i][j].rgbtGreen = image[i][j].rgbtRed = rgbt;
}
}
return;
}
// Reflect image horizontally
void reflect(int height, int width, RGBTRIPLE image[height][width])
{
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width / 2; j++)
{
RGBTRIPLE temp = image[i][j]; // Swap pixels one at a time
image[i][j] = image[i][width - j - 1];
image[i][width - j - 1] = temp;
}
}
return;
}
// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
// Allocate memory dynamically for temp
RGBTRIPLE (*temp)[width] = malloc(height * sizeof(RGBTRIPLE[width]));
if (temp == NULL)
{
printf("Memory allocation failed.\n");
return;
}
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
float sumBlue = 0, sumGreen = 0, sumRed = 0;
float counter = 0;
// Sum surrounding pixels
for (int r = -1; r <= 1; r++)
{
for (int c = -1; c <= 1; c++)
{
int newRow = i + r;
int newCol = j + c;
// Ensure we stay within image bounds
if (newRow >= 0 && newRow < height && newCol >= 0 && newCol < width)
{
sumBlue += image[newRow][newCol].rgbtBlue;
sumGreen += image[newRow][newCol].rgbtGreen;
sumRed += image[newRow][newCol].rgbtRed;
counter++;
}
}
}
// Compute the average and assign to temp
temp[i][j].rgbtBlue = round(sumBlue / counter);
temp[i][j].rgbtGreen = round(sumGreen / counter);
temp[i][j].rgbtRed = round(sumRed / counter);
}
}
// Copy the blurred values back to the original image
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
image[i][j] = temp[i][j];
}
}
// Free the dynamically allocated memory
free(temp);
return;
}
// Detect edges
void edges(int height, int width, RGBTRIPLE image[height][width])
{
// Allocate memory dynamically for temp
RGBTRIPLE (*temp)[width] = malloc(height * sizeof(RGBTRIPLE[width]));
if (temp == NULL)
{
printf("Memory allocation failed.\n");
return;
}
int gx[3][3] = {{-1, 0, 1}, {-2, 0, 2}, {-1, 0, 1}};
int gy[3][3] = {{-1, -2, -1}, {0, 0, 0}, {1, 2, 1}};
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
int gxBlue = 0, gyBlue = 0;
int gxGreen = 0, gyGreen = 0;
int gxRed = 0, gyRed = 0;
// Loop through the kernel
for (int r = -1; r <= 1; r++)
{
for (int c = -1; c <= 1; c++)
{
int newRow = i + r;
int newCol = j + c;
// Ensure we stay within image bounds
if (newRow >= 0 && newRow < height && newCol >= 0 && newCol < width)
{
gxBlue += image[newRow][newCol].rgbtBlue * gx[r + 1][c + 1];
gyBlue += image[newRow][newCol].rgbtBlue * gy[r + 1][c + 1];
gxGreen += image[newRow][newCol].rgbtGreen * gx[r + 1][c + 1];
gyGreen += image[newRow][newCol].rgbtGreen * gy[r + 1][c + 1];
gxRed += image[newRow][newCol].rgbtRed * gx[r + 1][c + 1];
gyRed += image[newRow][newCol].rgbtRed * gy[r + 1][c + 1];
}
}
}
// Compute the gradient magnitude and clamp to 255
int blue = round(sqrt(gxBlue * gxBlue + gyBlue * gyBlue));
int green = round(sqrt(gxGreen * gxGreen + gyGreen * gyGreen));
int red = round(sqrt(gxRed * gxRed + gyRed * gyRed));
temp[i][j].rgbtBlue = (blue > 255) ? 255 : blue;
temp[i][j].rgbtGreen = (green > 255) ? 255 : green;
temp[i][j].rgbtRed = (red > 255) ? 255 : red;
}
}
// Copy the edge-detected values back to the original image
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
image[i][j] = temp[i][j];
}
}
// Free the dynamically allocated memory
free(temp);
return;
}
// Emboss filter function
void emboss(int height, int width, RGBTRIPLE image[height][width])
{
// Allocate memory dynamically for temp
RGBTRIPLE (*temp)[width] = malloc(height * sizeof(RGBTRIPLE[width]));
if (temp == NULL)
{
printf("Memory allocation failed.\n");
return;
}
int emboss[3][3] = {{-2, -1, 0},
{-1, 1, 1},
{ 0, 1, 2}};
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
int red = 0, green = 0, blue = 0;
// Loop through the 3x3 convolution kernel
for (int r = -1; r <= 1; r++)
{
for (int c = -1; c <= 1; c++)
{
int neighbor_row = i + r;
int neighbor_col = j + c;
// Ensure we stay within image bounds
if (neighbor_row >= 0 && neighbor_row < height && neighbor_col >= 0 && neighbor_col < width)
{
red += image[neighbor_row][neighbor_col].rgbtRed * emboss[r + 1][c + 1];
green += image[neighbor_row][neighbor_col].rgbtGreen * emboss[r + 1][c + 1];
blue += image[neighbor_row][neighbor_col].rgbtBlue * emboss[r + 1][c + 1];
}
}
}
// Clamp the values between 0 and 255
temp[i][j].rgbtRed = (red > 255) ? 255 : (red < 0) ? 0 : red;
temp[i][j].rgbtGreen = (green > 255) ? 255 : (green < 0) ? 0 : green;
temp[i][j].rgbtBlue = (blue > 255) ? 255 : (blue < 0) ? 0 : blue;
}
}
// Copy the embossed values back to the original image
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
image[i][j] = temp[i][j];
}
}
// Free the dynamically allocated memory
free(temp);
return;
}