diff --git a/README.md b/README.md index 3c92eea..dc5f032 100644 --- a/README.md +++ b/README.md @@ -113,6 +113,39 @@ 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.) The Vignette Algorithm + +The vignette filter gives an image a cinematic look by gradually darkening its corners. + +**Algorithm:** +1. Compute the center of the image (cx, cy). +2. Calculate the maximum distance from the center to any corner (max_dis). +3. For each pixel at (i, j): + - Calculate the Euclidean distance from this pixel to the center: dist = sqrt((j - cx)^2 + (i - cy)^2) + - Compute a vignette factor: vig = 1.0 - (dist / max_dis) + - Clamp vig between 0.0 (fully dark) and 1.0 (original color). + - Multiply the pixel's R, G, B values by vig to darken farther-away pixels more thoroughly. + +If you apply this algorithm, your resulting image will have gently darkened corners and an undisturbed/sunnier center. + +--- + +### Usage + +To apply a filter via command-line: +- `g`: grayscale +- `s`: sepia +- `r`: reflect +- `b`: blur +- `i`: invert +- `v`: vignette + +For vignette: +``` +./filter v input.bmp output.bmp +``` +You can also chain multiple filters by supplying multiple tags (e.g., `./filter vg input.bmp output.bmp` for vignette then grayscale). + 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..59b1037 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 = "bgrsiv"; char filterArr[argc-3]; @@ -120,11 +120,14 @@ int main(int argc, char *argv[]) case 's': sepia(height, width, image); break; - //invert case 'i': invert(height,width,image); break; + // Vignette + case 'v': + vignette(height, width, image); + break; default: printf("%c", &filterArr[i]); break; diff --git a/helpers.c b/helpers.c index c585f8a..2e06ebd 100644 --- a/helpers.c +++ b/helpers.c @@ -126,3 +126,25 @@ void blur(int height, int width, RGBTRIPLE image[height][width]){ free(temp); } +// Blur image + +} +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); + // (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 89790e5..c7447c2 100644 --- a/helpers.h +++ b/helpers.h @@ -14,3 +14,5 @@ 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