Skip to content

Commit 63b0705

Browse files
committed
Add brightness adjust filter (-B flag), update documentation and filter logic
1 parent 0119323 commit 63b0705

File tree

4 files changed

+50
-2
lines changed

4 files changed

+50
-2
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,17 @@ If you apply the above algorithm to each pixel in the image, the result should l
123123
./filter -t input.bmp output.bmp
124124
```
125125

126+
### 6.) Brightness Adjustment Filter
127+
128+
- **Flag:** `-B <value>`
129+
- **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.
130+
- **Usage example:**
131+
132+
```sh
133+
./filter -B 40 input.bmp output.bmp # Increase brightness by 40
134+
./filter -B -30 input.bmp output.bmp # Decrease brightness by 30
135+
```
136+
126137
You should not modify any of the function signatures, nor should you modify any other files other than helpers.c.
127138

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

filter.c

Lines changed: 16 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 = "bgrsit";
10+
char *filters = "bgrsitB";
1111

1212

1313
char filterArr[argc-3];
@@ -130,6 +130,21 @@ int main(int argc, char *argv[])
130130
case 't':
131131
threshold(height, width, image);
132132
break;
133+
134+
// Brightness Adjust
135+
case 'B': {
136+
int brightness_value = 0;
137+
// Get brightness value from argv, after -B flag (assume it appears as -B val)
138+
if (optind < argc) {
139+
brightness_value = atoi(argv[optind]);
140+
optind++;
141+
} else {
142+
printf("Missing value for -B (brightness) flag.\n");
143+
return 8;
144+
}
145+
brightness(height, width, image, brightness_value);
146+
break;
147+
}
133148
default:
134149
printf("%c", &filterArr[i]);
135150
break;

helpers.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,23 @@ void threshold(int height, int width, RGBTRIPLE image[height][width]){
8383
}
8484
}
8585
}
86+
}
87+
88+
void brightness(int height, int width, RGBTRIPLE image[height][width], int value) {
89+
for (int i = 0; i < height; i++) {
90+
for (int j = 0; j < width; j++) {
91+
int r = image[i][j].rgbtRed + value;
92+
int g = image[i][j].rgbtGreen + value;
93+
int b = image[i][j].rgbtBlue + value;
94+
if (r > 255) r = 255;
95+
if (g > 255) g = 255;
96+
if (b > 255) b = 255;
97+
if (r < 0) r = 0;
98+
if (g < 0) g = 0;
99+
if (b < 0) b = 0;
100+
image[i][j].rgbtRed = r;
101+
image[i][j].rgbtGreen = g;
102+
image[i][j].rgbtBlue = b;
103+
}
104+
}
86105
}

helpers.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,7 @@ void reflect(int height, int width, RGBTRIPLE image[height][width]);
1616
void blur(int height, int width, RGBTRIPLE image[height][width]);
1717

1818
// Threshold filter (black and white)
19-
void threshold(int height, int width, RGBTRIPLE image[height][width]);
19+
void threshold(int height, int width, RGBTRIPLE image[height][width]);
20+
21+
// Brightness adjustment filter
22+
void brightness(int height, int width, RGBTRIPLE image[height][width], int value);

0 commit comments

Comments
 (0)