Skip to content

Commit 9d009a5

Browse files
authored
Merge branch 'main' into feature/brightness-adjust
2 parents 8b507b5 + 80ab27b commit 9d009a5

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,22 @@ If you apply the above algorithm to each pixel in the image, the result should l
122122
./filter -B -30 input.bmp output.bmp # Decrease brightness by 30
123123
```
124124

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

127143
### Usage

helpers.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,3 +187,30 @@ void vignette(int height, int width, RGBTRIPLE image[height][width]){
187187
}
188188
}
189189
}
190+
191+
//Threshold Filter(Black & White)
192+
void threshold(int height, int width, RGBTRIPLE image[height][width])
193+
{
194+
for (int i = 0; i < height; i++)
195+
{
196+
for (int j = 0; j < width; j++)
197+
{
198+
int avg = round((image[i][j].rgbtRed +
199+
image[i][j].rgbtGreen +
200+
image[i][j].rgbtBlue) / 3.0);
201+
202+
if (avg >= 128)
203+
{
204+
image[i][j].rgbtRed = 255;
205+
image[i][j].rgbtGreen = 255;
206+
image[i][j].rgbtBlue = 255;
207+
}
208+
else
209+
{
210+
image[i][j].rgbtRed = 0;
211+
image[i][j].rgbtGreen = 0;
212+
image[i][j].rgbtBlue = 0;
213+
}
214+
}
215+
}
216+
}

helpers.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ void blur(int height, int width, RGBTRIPLE image[height][width]);
1919
void brightness(int height, int width, RGBTRIPLE image[height][width], int value);
2020

2121
// Vignette filter
22-
void vignette(int height, int width, RGBTRIPLE image[height][width]);
22+
void vignette(int height, int width, RGBTRIPLE image[height][width]);

0 commit comments

Comments
 (0)