From 0119323ae1850f95e8f04cd178ed31bc3f633016 Mon Sep 17 00:00:00 2001 From: saumya1317 Date: Wed, 22 Oct 2025 13:35:19 +0530 Subject: [PATCH 1/4] Add threshold filter (-t flag), update documentation and filter logic --- README.md | 10 ++++++++++ filter.c | 7 ++++++- helpers.c | 17 +++++++++++++++++ helpers.h | 3 +++ 4 files changed, 36 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 3c92eea..0f54b7d 100644 --- a/README.md +++ b/README.md @@ -113,6 +113,16 @@ For a pixel along the edge or corner, like pixel 15, we would still look for all 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. +### 5.) Threshold Filter (Black & White) + +- **Flag:** `-t` +- **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). +- **Usage example:** + +```sh +./filter -t input.bmp output.bmp +``` + You should not modify any of the function signatures, nor should you modify any other files other than helpers.c. Consider the following grid of pixels, where we’ve numbered each pixel. diff --git a/filter.c b/filter.c index f6f8115..8cacdf1 100644 --- a/filter.c +++ b/filter.c @@ -7,7 +7,7 @@ int main(int argc, char *argv[]) { // Define allowable filters - char *filters = "bgrsi"; + char *filters = "bgrsit"; char filterArr[argc-3]; @@ -125,6 +125,11 @@ int main(int argc, char *argv[]) case 'i': invert(height,width,image); break; + + // Threshold (black & white) + case 't': + threshold(height, width, image); + break; default: printf("%c", &filterArr[i]); break; diff --git a/helpers.c b/helpers.c index 8275ef6..dfb43a9 100644 --- a/helpers.c +++ b/helpers.c @@ -66,4 +66,21 @@ void blur(int height, int width, RGBTRIPLE image[height][width]){ // Blur image +} + +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 = (image[i][j].rgbtRed + image[i][j].rgbtGreen + image[i][j].rgbtBlue) / 3; + 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; + } + } + } } \ No newline at end of file diff --git a/helpers.h b/helpers.h index 89790e5..2103d5e 100644 --- a/helpers.h +++ b/helpers.h @@ -14,3 +14,6 @@ void reflect(int height, int width, RGBTRIPLE image[height][width]); // Blur image void blur(int height, int width, RGBTRIPLE image[height][width]); + +// Threshold filter (black and white) +void threshold(int height, int width, RGBTRIPLE image[height][width]); \ No newline at end of file From 63b0705bcf27322a8b4298bf8409ba0a47a0525c Mon Sep 17 00:00:00 2001 From: saumya1317 Date: Wed, 22 Oct 2025 13:45:36 +0530 Subject: [PATCH 2/4] Add brightness adjust filter (-B flag), update documentation and filter logic --- README.md | 11 +++++++++++ filter.c | 17 ++++++++++++++++- helpers.c | 19 +++++++++++++++++++ helpers.h | 5 ++++- 4 files changed, 50 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 0f54b7d..114433d 100644 --- a/README.md +++ b/README.md @@ -123,6 +123,17 @@ If you apply the above algorithm to each pixel in the image, the result should l ./filter -t input.bmp output.bmp ``` +### 6.) Brightness Adjustment Filter + +- **Flag:** `-B ` +- **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. +- **Usage example:** + +```sh +./filter -B 40 input.bmp output.bmp # Increase brightness by 40 +./filter -B -30 input.bmp output.bmp # Decrease brightness by 30 +``` + You should not modify any of the function signatures, nor should you modify any other files other than helpers.c. Consider the following grid of pixels, where we’ve numbered each pixel. diff --git a/filter.c b/filter.c index 8cacdf1..aaad81c 100644 --- a/filter.c +++ b/filter.c @@ -7,7 +7,7 @@ int main(int argc, char *argv[]) { // Define allowable filters - char *filters = "bgrsit"; + char *filters = "bgrsitB"; char filterArr[argc-3]; @@ -130,6 +130,21 @@ int main(int argc, char *argv[]) case 't': threshold(height, width, image); break; + + // Brightness Adjust + case 'B': { + int brightness_value = 0; + // Get brightness value from argv, after -B flag (assume it appears as -B val) + if (optind < argc) { + brightness_value = atoi(argv[optind]); + optind++; + } else { + printf("Missing value for -B (brightness) flag.\n"); + return 8; + } + brightness(height, width, image, brightness_value); + break; + } default: printf("%c", &filterArr[i]); break; diff --git a/helpers.c b/helpers.c index dfb43a9..e929efc 100644 --- a/helpers.c +++ b/helpers.c @@ -83,4 +83,23 @@ void threshold(int height, int width, RGBTRIPLE image[height][width]){ } } } +} + +void brightness(int height, int width, RGBTRIPLE image[height][width], int value) { + for (int i = 0; i < height; i++) { + for (int j = 0; j < width; j++) { + int r = image[i][j].rgbtRed + value; + int g = image[i][j].rgbtGreen + value; + int b = image[i][j].rgbtBlue + value; + if (r > 255) r = 255; + if (g > 255) g = 255; + if (b > 255) b = 255; + if (r < 0) r = 0; + if (g < 0) g = 0; + if (b < 0) b = 0; + image[i][j].rgbtRed = r; + image[i][j].rgbtGreen = g; + image[i][j].rgbtBlue = b; + } + } } \ No newline at end of file diff --git a/helpers.h b/helpers.h index 2103d5e..317efb3 100644 --- a/helpers.h +++ b/helpers.h @@ -16,4 +16,7 @@ void reflect(int height, int width, RGBTRIPLE image[height][width]); void blur(int height, int width, RGBTRIPLE image[height][width]); // Threshold filter (black and white) -void threshold(int height, int width, RGBTRIPLE image[height][width]); \ No newline at end of file +void threshold(int height, int width, RGBTRIPLE image[height][width]); + +// Brightness adjustment filter +void brightness(int height, int width, RGBTRIPLE image[height][width], int value); \ No newline at end of file From 7ae89a107473dfc49a4557ab92799c7a0f44dc06 Mon Sep 17 00:00:00 2001 From: saumya1317 Date: Wed, 22 Oct 2025 13:57:47 +0530 Subject: [PATCH 3/4] Resolve merge conflicts, retain all filter functions --- README.md | 14 +++--- helpers.c | 131 ++++++++++++++++++++++++++++++++++++++---------------- 2 files changed, 101 insertions(+), 44 deletions(-) diff --git a/README.md b/README.md index 114433d..be21640 100644 --- a/README.md +++ b/README.md @@ -114,26 +114,30 @@ For a pixel along the edge or corner, like pixel 15, we would still look for all 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. ### 5.) Threshold Filter (Black & White) - - **Flag:** `-t` - **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). - **Usage example:** - ```sh ./filter -t input.bmp output.bmp ``` ### 6.) Brightness Adjustment Filter - - **Flag:** `-B ` - **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. -- **Usage example:** - +- **Usage examples:** ```sh ./filter -B 40 input.bmp output.bmp # Increase brightness by 40 ./filter -B -30 input.bmp output.bmp # Decrease brightness by 30 ``` +### 7.) Vignette Filter +- **Flag:** `-v` +- **Description:** Applies a vignette effect that darkens the corners of the image to focus attention towards the center. +- **Usage example:** +```sh +./filter -v input.bmp output.bmp +``` + You should not modify any of the function signatures, nor should you modify any other files other than helpers.c. Consider the following grid of pixels, where we’ve numbered each pixel. diff --git a/helpers.c b/helpers.c index e929efc..a4affbb 100644 --- a/helpers.c +++ b/helpers.c @@ -1,76 +1,110 @@ #include "helpers.h" +#include +#include +#include +#include "bmp.h" -int min(int a,int b){ - if(ab) return a; - return b; +int max(int a, int b) { + return (a > b) ? a : b; } -void grayscale(int height, int width, RGBTRIPLE image[height][width]){ - return; - -// Convert image to grayscale +void grayscale(int height, int width, RGBTRIPLE image[height][width]) { + for (int i = 0; i < height; i++) { + for (int j = 0; j < width; j++) { + int red = image[i][j].rgbtRed; + int green = image[i][j].rgbtGreen; + int blue = image[i][j].rgbtBlue; + int avg = (red + green + blue) / 3; + image[i][j].rgbtRed = avg; + image[i][j].rgbtGreen = avg; + image[i][j].rgbtBlue = avg; + } + } } -void invert(int height, int width, RGBTRIPLE image[height][width]){ - for(int i = 0; i= 0 && ni < height && nj >= 0 && nj < width) { + sumRed += image[ni][nj].rgbtRed; + sumGreen += image[ni][nj].rgbtGreen; + sumBlue += image[ni][nj].rgbtBlue; + count++; + } + } + } + temp[i][j].rgbtRed = (uint8_t)(sumRed / count); + temp[i][j].rgbtGreen = (uint8_t)(sumGreen / count); + temp[i][j].rgbtBlue = (uint8_t)(sumBlue / count); + } + } + for (int i = 0; i < height; i++) + for (int j = 0; j < width; j++) + image[i][j] = temp[i][j]; + } + for (int i = 0; i < height; i++) + free(temp[i]); + free(temp); } -void threshold(int height, int width, RGBTRIPLE image[height][width]){ - for(int i = 0; i < height; i++){ - for(int j = 0; j < width; j++){ +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 = (image[i][j].rgbtRed + image[i][j].rgbtGreen + image[i][j].rgbtBlue) / 3; if (avg >= 128) { image[i][j].rgbtRed = 255; @@ -102,4 +136,23 @@ void brightness(int height, int width, RGBTRIPLE image[height][width], int value image[i][j].rgbtBlue = b; } } +} + +void vignette(int height, int width, RGBTRIPLE image[height][width]) { + float cx = width / 2.0; + float cy = height / 2.0; + float max_dis = sqrt(cx * cx + cy * cy); + for (int i = 0; i < height; i++) { + for (int j = 0; j < width; j++) { + float disx = j - cx; + float disy = i - cy; + float dist = sqrt(disx * disx + disy * disy); + float vig = 1.0 - (dist / max_dis); + if (vig < 0.0) vig = 0.0; + if (vig > 1.0) vig = 1.0; + image[i][j].rgbtRed = (int)(image[i][j].rgbtRed * vig); + image[i][j].rgbtGreen = (int)(image[i][j].rgbtGreen * vig); + image[i][j].rgbtBlue = (int)(image[i][j].rgbtBlue * vig); + } + } } \ No newline at end of file From 7e99a6a4cae767c42ff728a63fe677ae4975c364 Mon Sep 17 00:00:00 2001 From: saumya1317 Date: Wed, 22 Oct 2025 15:17:02 +0530 Subject: [PATCH 4/4] Merge all features, update documentation and resolve any prior conflicts --- README.md | 8 -------- filter.c | 5 ----- helpers.c | 17 ----------------- helpers.h | 3 --- 4 files changed, 33 deletions(-) diff --git a/README.md b/README.md index be21640..971857d 100644 --- a/README.md +++ b/README.md @@ -113,14 +113,6 @@ For a pixel along the edge or corner, like pixel 15, we would still look for all 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. -### 5.) Threshold Filter (Black & White) -- **Flag:** `-t` -- **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). -- **Usage example:** -```sh -./filter -t input.bmp output.bmp -``` - ### 6.) Brightness Adjustment Filter - **Flag:** `-B ` - **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. diff --git a/filter.c b/filter.c index aaad81c..f3d92b5 100644 --- a/filter.c +++ b/filter.c @@ -126,11 +126,6 @@ int main(int argc, char *argv[]) invert(height,width,image); break; - // Threshold (black & white) - case 't': - threshold(height, width, image); - break; - // Brightness Adjust case 'B': { int brightness_value = 0; diff --git a/helpers.c b/helpers.c index a4affbb..e9ac3de 100644 --- a/helpers.c +++ b/helpers.c @@ -102,23 +102,6 @@ void blur(int height, int width, RGBTRIPLE image[height][width]) { free(temp); } -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 = (image[i][j].rgbtRed + image[i][j].rgbtGreen + image[i][j].rgbtBlue) / 3; - 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; - } - } - } -} - void brightness(int height, int width, RGBTRIPLE image[height][width], int value) { for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { diff --git a/helpers.h b/helpers.h index 317efb3..eee14e3 100644 --- a/helpers.h +++ b/helpers.h @@ -15,8 +15,5 @@ void reflect(int height, int width, RGBTRIPLE image[height][width]); // Blur image void blur(int height, int width, RGBTRIPLE image[height][width]); -// Threshold filter (black and white) -void threshold(int height, int width, RGBTRIPLE image[height][width]); - // Brightness adjustment filter void brightness(int height, int width, RGBTRIPLE image[height][width], int value); \ No newline at end of file