Skip to content

Commit 78f6724

Browse files
Merge pull request #19 from saumya1317/feature/vignette-filter
Add Cinematic Vignette Filter and CLI Tag to Image Filtering Suite
2 parents f8ec7f6 + 9778d3d commit 78f6724

File tree

4 files changed

+62
-2
lines changed

4 files changed

+62
-2
lines changed

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,39 @@ For a pixel along the edge or corner, like pixel 15, we would still look for all
113113

114114
If you apply the above algorithm to each pixel in the image, the result should look like a blurry, out-of-focus version of the original.
115115

116+
### 5.) The Vignette Algorithm
117+
118+
The vignette filter gives an image a cinematic look by gradually darkening its corners.
119+
120+
**Algorithm:**
121+
1. Compute the center of the image (cx, cy).
122+
2. Calculate the maximum distance from the center to any corner (max_dis).
123+
3. For each pixel at (i, j):
124+
- Calculate the Euclidean distance from this pixel to the center: dist = sqrt((j - cx)^2 + (i - cy)^2)
125+
- Compute a vignette factor: vig = 1.0 - (dist / max_dis)
126+
- Clamp vig between 0.0 (fully dark) and 1.0 (original color).
127+
- Multiply the pixel's R, G, B values by vig to darken farther-away pixels more thoroughly.
128+
129+
If you apply this algorithm, your resulting image will have gently darkened corners and an undisturbed/sunnier center.
130+
131+
---
132+
133+
### Usage
134+
135+
To apply a filter via command-line:
136+
- `g`: grayscale
137+
- `s`: sepia
138+
- `r`: reflect
139+
- `b`: blur
140+
- `i`: invert
141+
- `v`: vignette
142+
143+
For vignette:
144+
```
145+
./filter v input.bmp output.bmp
146+
```
147+
You can also chain multiple filters by supplying multiple tags (e.g., `./filter vg input.bmp output.bmp` for vignette then grayscale).
148+
116149
You should not modify any of the function signatures, nor should you modify any other files other than helpers.c.
117150

118151
Consider the following grid of pixels, where we’ve numbered each pixel.

filter.c

Lines changed: 5 additions & 2 deletions
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 = "bgrsi";
10+
char *filters = "bgrsiv";
1111

1212

1313
char filterArr[argc-3];
@@ -120,11 +120,14 @@ int main(int argc, char *argv[])
120120
case 's':
121121
sepia(height, width, image);
122122
break;
123-
124123
//invert
125124
case 'i':
126125
invert(height,width,image);
127126
break;
127+
// Vignette
128+
case 'v':
129+
vignette(height, width, image);
130+
break;
128131
default:
129132
printf("%c", &filterArr[i]);
130133
break;

helpers.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,25 @@ void blur(int height, int width, RGBTRIPLE image[height][width]){
126126
free(temp);
127127
}
128128

129+
// Blur image
130+
131+
}
132+
void vignette(int height, int width, RGBTRIPLE image[height][width]){
133+
float cx = width / 2.0; // center of the image
134+
float cy= height / 2.0;
135+
float max_dis= sqrt(cx * cx + cy * cy);
136+
for(int i = 0; i < height; i++){
137+
for(int j = 0; j < width; j++){
138+
float disx = j - cx;
139+
float disy = i - cy;
140+
float dist= sqrt(disx * disx + disy * disy);
141+
// (0.0 = dark, 1.0 = og)
142+
float vig = 1.0 - (dist/ max_dis);
143+
if(vig< 0.0) vig = 0.0;
144+
if(vig > 1.0) vig = 1.0;
145+
image[i][j].rgbtRed = (int)(image[i][j].rgbtRed * vig);
146+
image[i][j].rgbtGreen = (int)(image[i][j].rgbtGreen * vig);
147+
image[i][j].rgbtBlue = (int)(image[i][j].rgbtBlue * vig);
148+
}
149+
}
150+
}

helpers.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,5 @@ void reflect(int height, int width, RGBTRIPLE image[height][width]);
1414

1515
// Blur image
1616
void blur(int height, int width, RGBTRIPLE image[height][width]);
17+
// Vignette image
18+
void vignette(int height, int width, RGBTRIPLE image[height][width]);

0 commit comments

Comments
 (0)