From 5e4a52593ecb529cc1358503a44ffdefdaabfb1a Mon Sep 17 00:00:00 2001 From: Harsh Sankhla Date: Wed, 22 Oct 2025 16:18:39 +0530 Subject: [PATCH] Revert "feat(brightness): add brightness adjust filter (-B flag)" --- README.md | 16 ------ filter.c | 19 ++----- helpers.c | 146 +++++++++++++++++++++++++++++++++--------------------- helpers.h | 5 +- 4 files changed, 95 insertions(+), 91 deletions(-) diff --git a/README.md b/README.md index f7cf5ae..dc5f032 100644 --- a/README.md +++ b/README.md @@ -113,22 +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. -### 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 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 -``` ### 5.) The Vignette Algorithm The vignette filter gives an image a cinematic look by gradually darkening its corners. diff --git a/filter.c b/filter.c index e01e3ab..59b1037 100644 --- a/filter.c +++ b/filter.c @@ -7,7 +7,7 @@ int main(int argc, char *argv[]) { // Define allowable filters - char *filters = "bgrsitB"; + char *filters = "bgrsiv"; char filterArr[argc-3]; @@ -124,21 +124,10 @@ int main(int argc, char *argv[]) case 'i': invert(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); + // Vignette + case 'v': + vignette(height, width, image); break; - } default: printf("%c", &filterArr[i]); break; diff --git a/helpers.c b/helpers.c index a72c4bb..4925808 100644 --- a/helpers.c +++ b/helpers.c @@ -1,19 +1,19 @@ #include "helpers.h" #include #include -#include #include "bmp.h" - -int min(int a, int b) { - return (a < b) ? a : b; +int min(int a,int b){ + if(a b) ? a : b; +int max(int a,int b){ + if(a>b) return a; + return b; } - -void grayscale(int height, int width, RGBTRIPLE image[height][width]) { - for (int i = 0; i < height; i++) { - for (int j = 0; j < width; j++) { +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; @@ -25,62 +25,108 @@ void grayscale(int height, int width, RGBTRIPLE image[height][width]) { } } -void invert(int height, int width, RGBTRIPLE image[height][width]) { - for (int i = 0; i < height; i++) { - for (int j = 0; j < width; j++) { + +void invert(int height, int width, RGBTRIPLE image[height][width]){ + for(int i = 0; i 255) + sepiaRed = 255; + if (sepiaGreen > 255) + sepiaGreen = 255; + if (sepiaBlue > 255) + sepiaBlue = 255; + + image[i][j].rgbtRed = sepiaRed; + image[i][j].rgbtGreen = sepiaGreen; + image[i][j].rgbtBlue = sepiaBlue; + } + } -void sepia(int height, int width, RGBTRIPLE image[height][width]) { - // TODO: Implement sepia filter (currently empty) } -void reflect(int height, int width, RGBTRIPLE image[height][width]) { - for (int i = 0; i < height; i++) { - for (int j = 0; j < width / 2; j++) { + +void reflect(int height, int width, RGBTRIPLE image[height][width]){ + + // Loop over each row + for (int i = 0; i < height; i++) + { + // Swap pixels horizontally (mirror) + for (int j = 0; j < width / 2; j++) + { + // Swap left pixel with right pixel RGBTRIPLE temp = image[i][j]; image[i][j] = image[i][width - 1 - j]; image[i][width - 1 - j] = temp; } } + } -void blur(int height, int width, RGBTRIPLE image[height][width]) { + +void blur(int height, int width, RGBTRIPLE image[height][width]){ + // Allocate temporary array on heap RGBTRIPLE **temp = malloc(height * sizeof(RGBTRIPLE *)); for (int i = 0; i < height; i++) temp[i] = malloc(width * sizeof(RGBTRIPLE)); - int kernelSize = 21; + int kernelSize = 21; // large kernel for heavy blur int offset = kernelSize / 2; - for (int repeat = 0; repeat < 3; repeat++) { - for (int i = 0; i < height; i++) { - for (int j = 0; j < width; j++) { - int sumRed = 0, sumGreen = 0, sumBlue = 0, count = 0; - for (int ki = -offset; ki <= offset; ki++) { - for (int kj = -offset; kj <= offset; kj++) { + // Repeating blur 2-3 times for ultra blur effect + //because in single time effect not much visible + for(int repeat = 0; repeat < 3; repeat++){ + for(int i = 0; i < height; i++){ + for (int j = 0; j < width; j++){ + int sumRed = 0, sumGreen = 0, sumBlue = 0; + int count = 0; + for (int ki = -offset; ki <= offset; ki++){ + for(int kj = -offset; kj <= offset; kj++){ int ni = i + ki; int nj = j + kj; - if (ni >= 0 && ni < height && nj >= 0 && nj < width) { + if(ni >= 0 && ni < height && nj >= 0 && nj < width){ sumRed += image[ni][nj].rgbtRed; sumGreen += image[ni][nj].rgbtGreen; sumBlue += image[ni][nj].rgbtBlue; @@ -93,6 +139,7 @@ void blur(int height, int width, RGBTRIPLE image[height][width]) { temp[i][j].rgbtBlue = (uint8_t)(sumBlue / count); } } + // Copy blurred array back to orig for (int i = 0; i < height; i++) for (int j = 0; j < width; j++) image[i][j] = temp[i][j]; @@ -102,37 +149,22 @@ void blur(int height, int width, RGBTRIPLE image[height][width]) { free(temp); } -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; - } - } -} +// Blur image + -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++) { +void vignette(int height, int width, RGBTRIPLE image[height][width]){ + float cx = width / 2.0; // center of the image + 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; + float dist= sqrt(disx * disx + disy * disy); + // (0.0 = dark, 1.0 = og) + 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); diff --git a/helpers.h b/helpers.h index 025f6ef..c7447c2 100644 --- a/helpers.h +++ b/helpers.h @@ -14,6 +14,5 @@ void reflect(int height, int width, RGBTRIPLE image[height][width]); // Blur image void blur(int height, int width, RGBTRIPLE image[height][width]); - -// Brightness adjustment filter -void brightness(int height, int width, RGBTRIPLE image[height][width], int value); +// Vignette image +void vignette(int height, int width, RGBTRIPLE image[height][width]); \ No newline at end of file