From 2471f842af1c855c7d4a304f10291a73cecb6f5f Mon Sep 17 00:00:00 2001 From: bot50 Date: Fri, 8 Mar 2024 11:15:08 +0000 Subject: [PATCH] kukemuna-cs50/problems/2024/x/filter/less@20240308T111509.001640609Z --- helpers.c | 118 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 helpers.c diff --git a/helpers.c b/helpers.c new file mode 100644 index 0000000..c7db2d4 --- /dev/null +++ b/helpers.c @@ -0,0 +1,118 @@ +#include +#include "helpers.h" + +// 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 avg = round((image[i][j].rgbtBlue + image[i][j].rgbtGreen + image[i][j].rgbtRed) / 3); + image[i][j].rgbtBlue = image[i][j].rgbtGreen = image[i][j].rgbtRed = avg; + } + } + return; +} + +// Convert image to sepia +void sepia(int height, int width, RGBTRIPLE image[height][width]) +{ + for (int i = 0; i < height; i++) + { + for (int j = 0; j < width; j++) + { + int sepiaRed = round(.393 * image[i][j].rgbtRed + .769 * image[i][j].rgbtGreen + .189 * image[i][j].rgbtBlue); + int sepiaGreen = round(.349 * image[i][j].rgbtRed + .686 * image[i][j].rgbtGreen + .168 * image[i][j].rgbtBlue); + int sepiaBlue = round(.272 * image[i][j].rgbtRed + .534 * image[i][j].rgbtGreen + .131 * image[i][j].rgbtBlue); + + if (sepiaRed > 255) + { + sepiaRed = 255; + } + if (sepiaGreen > 255) + { + sepiaGreen = 255; + } + if (sepiaBlue > 255) + { + sepiaBlue = 255; + } + + image[i][j].rgbtBlue = sepiaBlue; + image[i][j].rgbtGreen = sepiaGreen; + image[i][j].rgbtRed = sepiaRed; + } + } + + return; +} + +// Reflect image horizontally +void reflect(int height, int width, RGBTRIPLE image[height][width]) +{ + RGBTRIPLE tmp[height][width]; + + for (int i = 0; i < height; i++) + { + for (int j = 0; j < width; j++) + { + tmp[i][j] = image[i][width - j - 1]; + } + } + + for (int i = 0; i < height; i++) + { + for (int j = 0; j < width; j++) + { + image[i][j] = tmp[i][j]; + } + } + return; +} + +// Blur image +void blur(int height, int width, RGBTRIPLE image[height][width]) +{ + RGBTRIPLE copy[height][width]; + + for (int i = 0; i < height; i++) + { + for (int j = 0; j < width; j++) + { + copy[i][j] = image[i][j]; + } + } + + for (int i = 0; i < height; i++) + { + for (int j = 0; j < width; j++) + { + float sumRed = 0, sumGreen = 0, sumBlue = 0; + int count = 0; + + for (int k = -1; k < 2; k++) + { + for (int l = -1; l < 2; l++) + { + if (i + k <= 0 || i + k >= height) + { + continue; + } + if (j + l <= 0 || j + l >= width) + { + continue; + } + sumRed += copy[i + k][j + l].rgbtRed; + sumGreen += copy[i + k][j + l].rgbtGreen; + sumBlue += copy[i + k][j + l].rgbtBlue; + count++; + } + } + image[i][j].rgbtRed = round(sumRed / count); + image[i][j].rgbtGreen = round(sumGreen / count); + image[i][j].rgbtBlue = round(sumBlue / count); + } + } + return; +}