Skip to content

Commit 80ab27b

Browse files
Merge pull request #27 from Satvik-Singh192/threshold-filter
feat: add threshold (black & white) filter with -t flag
2 parents e0dbcf9 + 293a28d commit 80ab27b

File tree

4 files changed

+51
-2
lines changed

4 files changed

+51
-2
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,22 @@ The vignette filter gives an image a cinematic look by gradually darkening its c
128128

129129
If you apply this algorithm, your resulting image will have gently darkened corners and an undisturbed/sunnier center.
130130

131+
### 6.) The Threshold Algorithm
132+
133+
The “threshold” filter converts a colorful image into a pure black-and-white one based on pixel intensity.
134+
135+
For each pixel, the intensity is calculated as the average of its red, green, and blue values:
136+
137+
\[
138+
\text{intensity} = \frac{(R + G + B)}{3}
139+
\]
140+
141+
If the intensity is **greater than or equal to 128**, the pixel is set to **white** (`R = G = B = 255`).
142+
Otherwise, it is set to **black** (`R = G = B = 0`).
143+
144+
This results in a high-contrast, two-tone image where all intermediate shades are eliminated — essentially a hard binary “black-and-white” conversion.
145+
146+
131147
---
132148

133149
### Usage

filter.c

Lines changed: 4 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 = "bgrsiv";
10+
char *filters = "bgrsivt";
1111

1212

1313
char filterArr[argc-3];
@@ -128,6 +128,9 @@ int main(int argc, char *argv[])
128128
case 'v':
129129
vignette(height, width, image);
130130
break;
131+
case 't':
132+
threshold(height, width, image);
133+
break;
131134
default:
132135
printf("%c", &filterArr[i]);
133136
break;

helpers.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,30 @@ void vignette(int height, int width, RGBTRIPLE image[height][width]){
171171
}
172172
}
173173
}
174+
175+
//Threshold Filter(Black & White)
176+
void threshold(int height, int width, RGBTRIPLE image[height][width])
177+
{
178+
for (int i = 0; i < height; i++)
179+
{
180+
for (int j = 0; j < width; j++)
181+
{
182+
int avg = round((image[i][j].rgbtRed +
183+
image[i][j].rgbtGreen +
184+
image[i][j].rgbtBlue) / 3.0);
185+
186+
if (avg >= 128)
187+
{
188+
image[i][j].rgbtRed = 255;
189+
image[i][j].rgbtGreen = 255;
190+
image[i][j].rgbtBlue = 255;
191+
}
192+
else
193+
{
194+
image[i][j].rgbtRed = 0;
195+
image[i][j].rgbtGreen = 0;
196+
image[i][j].rgbtBlue = 0;
197+
}
198+
}
199+
}
200+
}

helpers.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,7 @@ 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
// Vignette image
18-
void vignette(int height, int width, RGBTRIPLE image[height][width]);
18+
void vignette(int height, int width, RGBTRIPLE image[height][width]);
19+
20+
//Threshold Filter(Black & White)
21+
void threshold(int height, int width, RGBTRIPLE image[height][width]);

0 commit comments

Comments
 (0)