Skip to content

Commit 129f220

Browse files
feat: Add glow filter
Enhances bright regions to produce a soft glow effect. Closes #28
1 parent 80ab27b commit 129f220

File tree

3 files changed

+79
-1
lines changed

3 files changed

+79
-1
lines changed

filter.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
int main(int argc, char *argv[])
88
{
99
// Define allowable filters
10-
char *filters = "bgrsivt";
10+
char *filters = "bgrsivtw";
1111

1212

1313
char filterArr[argc-3];
@@ -131,6 +131,9 @@ int main(int argc, char *argv[])
131131
case 't':
132132
threshold(height, width, image);
133133
break;
134+
case 'w':
135+
glow(height, width, image);
136+
break;
134137
default:
135138
printf("%c", &filterArr[i]);
136139
break;

helpers.c

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,3 +198,75 @@ void threshold(int height, int width, RGBTRIPLE image[height][width])
198198
}
199199
}
200200
}
201+
202+
void glow(int height, int width, RGBTRIPLE image[height][width])
203+
{
204+
// Step 1: make a copy of the original
205+
RGBTRIPLE **original = malloc(height * sizeof(RGBTRIPLE *));
206+
RGBTRIPLE **blurred = malloc(height * sizeof(RGBTRIPLE *));
207+
for (int i = 0; i < height; i++)
208+
{
209+
original[i] = malloc(width * sizeof(RGBTRIPLE));
210+
blurred[i] = malloc(width * sizeof(RGBTRIPLE));
211+
for (int j = 0; j < width; j++)
212+
{
213+
original[i][j] = image[i][j];
214+
}
215+
}
216+
217+
// Step 2: apply a *mild* blur to copy (smaller kernel)
218+
int kernelSize = 11;
219+
int offset = kernelSize / 2;
220+
221+
for (int i = 0; i < height; i++)
222+
{
223+
for (int j = 0; j < width; j++)
224+
{
225+
int sumRed = 0, sumGreen = 0, sumBlue = 0, count = 0;
226+
227+
for (int ki = -offset; ki <= offset; ki++)
228+
{
229+
for (int kj = -offset; kj <= offset; kj++)
230+
{
231+
int ni = i + ki, nj = j + kj;
232+
if (ni >= 0 && ni < height && nj >= 0 && nj < width)
233+
{
234+
sumRed += original[ni][nj].rgbtRed;
235+
sumGreen += original[ni][nj].rgbtGreen;
236+
sumBlue += original[ni][nj].rgbtBlue;
237+
count++;
238+
}
239+
}
240+
}
241+
242+
blurred[i][j].rgbtRed = sumRed / count;
243+
blurred[i][j].rgbtGreen = sumGreen / count;
244+
blurred[i][j].rgbtBlue = sumBlue / count;
245+
}
246+
}
247+
248+
// Step 3: blend original + blurred to produce glow
249+
for (int i = 0; i < height; i++)
250+
{
251+
for (int j = 0; j < width; j++)
252+
{
253+
float blend = 0.4; // glow intensity
254+
int newRed = (1 - blend) * original[i][j].rgbtRed + blend * blurred[i][j].rgbtRed;
255+
int newGreen = (1 - blend) * original[i][j].rgbtGreen + blend * blurred[i][j].rgbtGreen;
256+
int newBlue = (1 - blend) * original[i][j].rgbtBlue + blend * blurred[i][j].rgbtBlue;
257+
258+
image[i][j].rgbtRed = min(255, newRed);
259+
image[i][j].rgbtGreen = min(255, newGreen);
260+
image[i][j].rgbtBlue = min(255, newBlue);
261+
}
262+
}
263+
264+
// Step 4: cleanup
265+
for (int i = 0; i < height; i++)
266+
{
267+
free(original[i]);
268+
free(blurred[i]);
269+
}
270+
free(original);
271+
free(blurred);
272+
}

helpers.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,6 @@ void vignette(int height, int width, RGBTRIPLE image[height][width]);
1919

2020
//Threshold Filter(Black & White)
2121
void threshold(int height, int width, RGBTRIPLE image[height][width]);
22+
23+
// Glow filter
24+
void glow(int height, int width, RGBTRIPLE image[height][width]);

0 commit comments

Comments
 (0)