Skip to content

Commit 05a2a51

Browse files
committed
fix: add vignette and other filter logic to helpers.c
1 parent 895d856 commit 05a2a51

File tree

1 file changed

+32
-4
lines changed

1 file changed

+32
-4
lines changed

helpers.c

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,21 @@ int max(int a,int b){
99
return b;
1010
}
1111
void grayscale(int height, int width, RGBTRIPLE image[height][width]){
12-
return;
13-
14-
// Convert image to grayscale
15-
12+
13+
for(int i = 0; i < height; i++){
14+
for(int j = 0; j < width; j++){
15+
int red = image[i][j].rgbtRed;
16+
int green = image[i][j].rgbtGreen;
17+
int blue = image[i][j].rgbtBlue;
18+
int avg = (red + green + blue) / 3;
19+
image[i][j].rgbtRed = avg;
20+
image[i][j].rgbtGreen = avg;
21+
image[i][j].rgbtBlue = avg;
22+
}
23+
}
1624
}
1725

26+
1827
void invert(int height, int width, RGBTRIPLE image[height][width]){
1928
for(int i = 0; i<height; i++){
2029
for(int j = 0; j<width; j++){
@@ -66,4 +75,23 @@ void blur(int height, int width, RGBTRIPLE image[height][width]){
6675

6776
// Blur image
6877

78+
}
79+
void vignette(int height, int width, RGBTRIPLE image[height][width]){
80+
float cx = width / 2.0; // center of the image
81+
float cy= height / 2.0;
82+
float max_dis= sqrt(cx * cx + cy * cy);
83+
for(int i = 0; i < height; i++){
84+
for(int j = 0; j < width; j++){
85+
float disx = j - cx;
86+
float disy = i - cy;
87+
float dist= sqrt(disx * disx + disy * disy);
88+
// (0.0 = dark, 1.0 = og)
89+
float vig = 1.0 - (dist/ max_dis);
90+
if(vig< 0.0) vig = 0.0;
91+
if(vig > 1.0) vig = 1.0;
92+
image[i][j].rgbtRed = (int)(image[i][j].rgbtRed * vig);
93+
image[i][j].rgbtGreen = (int)(image[i][j].rgbtGreen * vig);
94+
image[i][j].rgbtBlue = (int)(image[i][j].rgbtBlue * vig);
95+
}
96+
}
6997
}

0 commit comments

Comments
 (0)