Skip to content

Commit 0119323

Browse files
committed
Add threshold filter (-t flag), update documentation and filter logic
1 parent 18b2111 commit 0119323

File tree

4 files changed

+36
-1
lines changed

4 files changed

+36
-1
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,16 @@ 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+
118+
- **Flag:** `-t`
119+
- **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).
120+
- **Usage example:**
121+
122+
```sh
123+
./filter -t input.bmp output.bmp
124+
```
125+
116126
You should not modify any of the function signatures, nor should you modify any other files other than helpers.c.
117127

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

filter.c

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

1212

1313
char filterArr[argc-3];
@@ -125,6 +125,11 @@ int main(int argc, char *argv[])
125125
case 'i':
126126
invert(height,width,image);
127127
break;
128+
129+
// Threshold (black & white)
130+
case 't':
131+
threshold(height, width, image);
132+
break;
128133
default:
129134
printf("%c", &filterArr[i]);
130135
break;

helpers.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,21 @@ void blur(int height, int width, RGBTRIPLE image[height][width]){
6666

6767
// Blur image
6868

69+
}
70+
71+
void threshold(int height, int width, RGBTRIPLE image[height][width]){
72+
for(int i = 0; i < height; i++){
73+
for(int j = 0; j < width; j++){
74+
int avg = (image[i][j].rgbtRed + image[i][j].rgbtGreen + image[i][j].rgbtBlue) / 3;
75+
if (avg >= 128) {
76+
image[i][j].rgbtRed = 255;
77+
image[i][j].rgbtGreen = 255;
78+
image[i][j].rgbtBlue = 255;
79+
} else {
80+
image[i][j].rgbtRed = 0;
81+
image[i][j].rgbtGreen = 0;
82+
image[i][j].rgbtBlue = 0;
83+
}
84+
}
85+
}
6986
}

helpers.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,6 @@ 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+
18+
// Threshold filter (black and white)
19+
void threshold(int height, int width, RGBTRIPLE image[height][width]);

0 commit comments

Comments
 (0)