From 293a28dbcb025a420f40ebadc10779d653e9ff49 Mon Sep 17 00:00:00 2001 From: Satvik-Singh192 Date: Wed, 22 Oct 2025 19:15:40 +0530 Subject: [PATCH] feat: add threshold (black & white) filter with -t flag --- README.md | 16 ++++++++++++++++ filter.c | 5 ++++- helpers.c | 27 +++++++++++++++++++++++++++ helpers.h | 5 ++++- 4 files changed, 51 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index dc5f032..1b9bf53 100644 --- a/README.md +++ b/README.md @@ -128,6 +128,22 @@ The vignette filter gives an image a cinematic look by gradually darkening its c If you apply this algorithm, your resulting image will have gently darkened corners and an undisturbed/sunnier center. +### 6.) The Threshold Algorithm + +The “threshold” filter converts a colorful image into a pure black-and-white one based on pixel intensity. + +For each pixel, the intensity is calculated as the average of its red, green, and blue values: + +\[ +\text{intensity} = \frac{(R + G + B)}{3} +\] + +If the intensity is **greater than or equal to 128**, the pixel is set to **white** (`R = G = B = 255`). +Otherwise, it is set to **black** (`R = G = B = 0`). + +This results in a high-contrast, two-tone image where all intermediate shades are eliminated — essentially a hard binary “black-and-white” conversion. + + --- ### Usage diff --git a/filter.c b/filter.c index 59b1037..ac19958 100644 --- a/filter.c +++ b/filter.c @@ -7,7 +7,7 @@ int main(int argc, char *argv[]) { // Define allowable filters - char *filters = "bgrsiv"; + char *filters = "bgrsivt"; char filterArr[argc-3]; @@ -128,6 +128,9 @@ int main(int argc, char *argv[]) case 'v': vignette(height, width, image); break; + case 't': + threshold(height, width, image); + break; default: printf("%c", &filterArr[i]); break; diff --git a/helpers.c b/helpers.c index 4925808..34323ce 100644 --- a/helpers.c +++ b/helpers.c @@ -171,3 +171,30 @@ void vignette(int height, int width, RGBTRIPLE image[height][width]){ } } } + +//Threshold Filter(Black & White) +void threshold(int height, int width, RGBTRIPLE image[height][width]) +{ + for (int i = 0; i < height; i++) + { + for (int j = 0; j < width; j++) + { + int avg = round((image[i][j].rgbtRed + + image[i][j].rgbtGreen + + image[i][j].rgbtBlue) / 3.0); + + if (avg >= 128) + { + image[i][j].rgbtRed = 255; + image[i][j].rgbtGreen = 255; + image[i][j].rgbtBlue = 255; + } + else + { + image[i][j].rgbtRed = 0; + image[i][j].rgbtGreen = 0; + image[i][j].rgbtBlue = 0; + } + } + } +} diff --git a/helpers.h b/helpers.h index c7447c2..ade29b6 100644 --- a/helpers.h +++ b/helpers.h @@ -15,4 +15,7 @@ void reflect(int height, int width, RGBTRIPLE image[height][width]); // Blur image void blur(int height, int width, RGBTRIPLE image[height][width]); // Vignette image -void vignette(int height, int width, RGBTRIPLE image[height][width]); \ No newline at end of file +void vignette(int height, int width, RGBTRIPLE image[height][width]); + +//Threshold Filter(Black & White) +void threshold(int height, int width, RGBTRIPLE image[height][width]);