Skip to content

Commit 2c8d257

Browse files
committed
feat: fix brightness filter implementation for proper GCC compatibility and argument parsing
1 parent 7ae89a1 commit 2c8d257

File tree

4 files changed

+17
-50
lines changed

4 files changed

+17
-50
lines changed

README.md

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,6 @@ 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.) Threshold Filter (Black & White)
117-
- **Flag:** `-t`
118-
- **Description:** Converts each pixel in the image to pure black or white based on its intensity. If the average of red, green, and blue is greater than or equal to 128, the pixel becomes white (255,255,255); otherwise, it becomes black (0,0,0).
119-
- **Usage example:**
120-
```sh
121-
./filter -t input.bmp output.bmp
122-
```
123-
124116
### 6.) Brightness Adjustment Filter
125117
- **Flag:** `-B <value>`
126118
- **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.

filter.c

Lines changed: 13 additions & 21 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 = "bgrsitB";
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
@@ -126,27 +126,19 @@ int main(int argc, char *argv[])
126126
invert(height,width,image);
127127
break;
128128

129-
// Threshold (black & white)
130-
case 't':
131-
threshold(height, width, image);
129+
// Vignette
130+
case 'v':
131+
vignette(height, width, image);
132132
break;
133133

134134
// Brightness Adjust
135135
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-
}
136+
int brightness_value = atoi(optarg);
145137
brightness(height, width, image, brightness_value);
146138
break;
147139
}
148140
default:
149-
printf("%c", &filterArr[i]);
141+
printf("Unknown filter: %c\n", filterArr[i]);
150142
break;
151143

152144
}

helpers.c

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -102,23 +102,6 @@ void blur(int height, int width, RGBTRIPLE image[height][width]) {
102102
free(temp);
103103
}
104104

105-
void threshold(int height, int width, RGBTRIPLE image[height][width]) {
106-
for (int i = 0; i < height; i++) {
107-
for (int j = 0; j < width; j++) {
108-
int avg = (image[i][j].rgbtRed + image[i][j].rgbtGreen + image[i][j].rgbtBlue) / 3;
109-
if (avg >= 128) {
110-
image[i][j].rgbtRed = 255;
111-
image[i][j].rgbtGreen = 255;
112-
image[i][j].rgbtBlue = 255;
113-
} else {
114-
image[i][j].rgbtRed = 0;
115-
image[i][j].rgbtGreen = 0;
116-
image[i][j].rgbtBlue = 0;
117-
}
118-
}
119-
}
120-
}
121-
122105
void brightness(int height, int width, RGBTRIPLE image[height][width], int value) {
123106
for (int i = 0; i < height; i++) {
124107
for (int j = 0; j < width; j++) {

helpers.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ void reflect(int height, int width, RGBTRIPLE image[height][width]);
1515
// Blur image
1616
void blur(int height, int width, RGBTRIPLE image[height][width]);
1717

18-
// Threshold filter (black and white)
19-
void threshold(int height, int width, RGBTRIPLE image[height][width]);
20-
2118
// Brightness adjustment filter
22-
void brightness(int height, int width, RGBTRIPLE image[height][width], int value);
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)