Skip to content

Commit fb2260f

Browse files
Merge pull request #26 from saumya1317/feature/brightness-adjust
feat: fix brightness filter implementation for proper GCC compatibility
2 parents 80ab27b + 9d009a5 commit fb2260f

File tree

4 files changed

+47
-31
lines changed

4 files changed

+47
-31
lines changed

README.md

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -113,20 +113,14 @@ 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.
116+
### 6.) Brightness Adjustment Filter
117+
- **Flag:** `-B <value>`
118+
- **Description:** Increases or decreases the brightness of the image by adding a fixed value to each pixel's R, G, and B channels. The value should be an integer—positive to increase, negative to decrease.
119+
- **Usage examples:**
120+
```sh
121+
./filter -B 40 input.bmp output.bmp # Increase brightness by 40
122+
./filter -B -30 input.bmp output.bmp # Decrease brightness by 30
123+
```
130124

131125
### 6.) The Threshold Algorithm
132126

filter.c

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

1212

1313
char filterArr[argc-3];
14+
int filterCount = 0;
1415

1516
// gets all filter flags and checks validity
16-
for(int i=0; i<argc; i++){
17-
char temp = getopt(argc,argv,filters);
18-
if(temp == -1) break;
19-
filterArr[i]= temp;
20-
if(filterArr[i] == '?') {
21-
printf("Invalid filter option");
17+
int opt;
18+
while ((opt = getopt(argc, argv, filters)) != -1) {
19+
if (opt == '?') {
20+
printf("Invalid filter option\n");
2221
return 1;
2322
}
23+
filterArr[filterCount++] = opt;
2424
}
2525

2626

@@ -98,7 +98,7 @@ int main(int argc, char *argv[])
9898
}
9999

100100
// Filter image
101-
for(int i=0; i<argc-3; i++){
101+
for(int i=0; i<filterCount; i++){
102102
switch (filterArr[i])
103103
{
104104
// Blur
@@ -124,15 +124,20 @@ int main(int argc, char *argv[])
124124
case 'i':
125125
invert(height,width,image);
126126
break;
127+
127128
// Vignette
128129
case 'v':
129130
vignette(height, width, image);
130131
break;
131-
case 't':
132-
threshold(height, width, image);
132+
133+
// Brightness Adjust
134+
case 'B': {
135+
int brightness_value = atoi(optarg);
136+
brightness(height, width, image, brightness_value);
133137
break;
138+
}
134139
default:
135-
printf("%c", &filterArr[i]);
140+
printf("Unknown filter: %c\n", filterArr[i]);
136141
break;
137142

138143
}

helpers.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,24 @@ void blur(int height, int width, RGBTRIPLE image[height][width]){
149149
free(temp);
150150
}
151151

152-
// Blur image
153-
152+
void brightness(int height, int width, RGBTRIPLE image[height][width], int value) {
153+
for (int i = 0; i < height; i++) {
154+
for (int j = 0; j < width; j++) {
155+
int r = image[i][j].rgbtRed + value;
156+
int g = image[i][j].rgbtGreen + value;
157+
int b = image[i][j].rgbtBlue + value;
158+
if (r > 255) r = 255;
159+
if (g > 255) g = 255;
160+
if (b > 255) b = 255;
161+
if (r < 0) r = 0;
162+
if (g < 0) g = 0;
163+
if (b < 0) b = 0;
164+
image[i][j].rgbtRed = r;
165+
image[i][j].rgbtGreen = g;
166+
image[i][j].rgbtBlue = b;
167+
}
168+
}
169+
}
154170

155171
void vignette(int height, int width, RGBTRIPLE image[height][width]){
156172
float cx = width / 2.0; // center of the image

helpers.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ 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]);
1917

20-
//Threshold Filter(Black & White)
21-
void threshold(int height, int width, RGBTRIPLE image[height][width]);
18+
// Brightness adjustment filter
19+
void brightness(int height, int width, RGBTRIPLE image[height][width], int value);
20+
21+
// Vignette filter
22+
void vignette(int height, int width, RGBTRIPLE image[height][width]);

0 commit comments

Comments
 (0)