Created
August 26, 2022 08:33
-
-
Save MagedMohamedTurk/6f594a32bdf2081cb4ce63f5d8f10d34 to your computer and use it in GitHub Desktop.
CS50 filter-less Solution
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "helpers.h" | |
#include <math.h> | |
#include <stdio.h> | |
typedef struct | |
{ | |
/* Constants Statement for Sepia Filter */ | |
float RED; | |
float GREEN; | |
float BLUE; | |
} | |
SEPIACONSTANT; | |
// 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++) | |
{ | |
BYTE averageColor = round((image[i][j].rgbtBlue + image[i][j].rgbtRed + image[i][j].rgbtGreen) / 3.0); | |
image[i][j].rgbtBlue = image[i][j].rgbtGreen = image[i][j].rgbtRed = averageColor; | |
} | |
} | |
} | |
// Convert image to sepia | |
void sepia(int height, int width, RGBTRIPLE image[height][width]) | |
{ | |
/* | |
tr = 0.393R + 0.769G + 0.189B | |
tg = 0.349R + 0.686G + 0.168B | |
tb = 0.272R + 0.534G + 0.131B | |
*/ | |
SEPIACONSTANT sepiaRedConst; | |
sepiaRedConst.RED = 0.393; | |
sepiaRedConst.BLUE = 0.189; | |
sepiaRedConst.GREEN = 0.769; | |
SEPIACONSTANT sepiaBlueConst; | |
sepiaBlueConst.RED = 0.272; | |
sepiaBlueConst.BLUE = 0.131; | |
sepiaBlueConst.GREEN = 0.534; | |
SEPIACONSTANT sepiaGreenConst; | |
sepiaGreenConst.RED = 0.349; | |
sepiaGreenConst.BLUE = 0.168; | |
sepiaGreenConst.GREEN = 0.686; | |
for (int i = 0; i < height; i++) | |
{ | |
for (int j = 0; j < width; j++) | |
{ | |
RGBTRIPLE pixel = image[i][j]; | |
float filterRed = sepiaRedConst.RED * pixel.rgbtRed + | |
sepiaRedConst.GREEN * pixel.rgbtGreen + | |
sepiaRedConst.BLUE * pixel.rgbtBlue; | |
float filterBlue = sepiaBlueConst.RED * pixel.rgbtRed + | |
sepiaBlueConst.GREEN * pixel.rgbtGreen + | |
sepiaBlueConst.BLUE * pixel.rgbtBlue; | |
float filterGreen = sepiaGreenConst.RED * pixel.rgbtRed + | |
sepiaGreenConst.GREEN * pixel.rgbtGreen + | |
sepiaGreenConst.BLUE * pixel.rgbtBlue; | |
filterRed = round(filterRed); | |
filterGreen = round(filterGreen); | |
filterBlue = round(filterBlue); | |
if (filterRed > 255) pixel.rgbtRed = 255; else pixel.rgbtRed = filterRed; | |
if (filterGreen > 255) pixel.rgbtGreen = 255; else pixel.rgbtGreen = filterGreen; | |
if (filterBlue > 255) pixel.rgbtBlue = 255; else pixel.rgbtBlue = filterBlue; | |
image[i][j] = pixel; | |
} | |
} | |
} | |
// Reflect image horizontally | |
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++) | |
{ | |
RGBTRIPLE temp = image[i][j]; | |
temp.rgbtRed = image[i][width - 1 - j].rgbtRed; | |
image[i][width - 1 - j].rgbtRed = image[i][j].rgbtRed; | |
image[i][j].rgbtRed = temp.rgbtRed; | |
temp.rgbtBlue = image[i][width - 1 - j].rgbtBlue; | |
image[i][width - 1 - j].rgbtBlue = image[i][j].rgbtBlue; | |
image[i][j].rgbtBlue = temp.rgbtBlue; | |
temp.rgbtGreen = image[i][width - 1 - j].rgbtGreen; | |
image[i][width - 1 - j].rgbtGreen = image[i][j].rgbtGreen; | |
image[i][j].rgbtGreen = temp.rgbtGreen; | |
} | |
} | |
} | |
// Blur image | |
void blur(int height, int width, RGBTRIPLE image[height][width]) | |
{ | |
RGBTRIPLE tempImage[height][width]; | |
for (int i = 0; i < height; i++) | |
{ | |
for (int j = 0; j < width; j++) | |
{ | |
tempImage[i][j] = image[i][j]; | |
} | |
} | |
for (int i = 0; i < height; i++) | |
{ | |
for (int j = 0; j < width; j++) | |
{ | |
float pixelCount = 0; | |
float pixelRedSum = 0; | |
float pixelBlueSum = 0; | |
float pixelGreenSum = 0; | |
for (int m = i-1; m < i+2; m++) | |
{ | |
if (m >= 0 && m < height) | |
{ | |
for (int n = j-1; n < j+2; n++) | |
{ | |
if (n >= 0 && n < width) | |
{ | |
pixelCount++; | |
pixelRedSum += image[m][n].rgbtRed; | |
pixelBlueSum += image[m][n].rgbtBlue; | |
pixelGreenSum += image[m][n].rgbtGreen; | |
} | |
} | |
} | |
} | |
tempImage[i][j].rgbtRed = round(pixelRedSum / pixelCount); | |
tempImage[i][j].rgbtBlue = round(pixelBlueSum / pixelCount); | |
tempImage[i][j].rgbtGreen = round(pixelGreenSum / pixelCount); | |
} | |
} | |
for (int i = 0; i < height; i++) | |
{ | |
for (int j = 0; j < width; j++) | |
{ | |
image[i][j] = tempImage[i][j]; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment