Created
June 10, 2020 21:04
-
-
Save kinoute/4625089759e92eee3b496adefc7de3f1 to your computer and use it in GitHub Desktop.
Harvard CS50 filters
This file contains 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 <stdio.h> | |
#include <math.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 = (image[i][j].rgbtRed + image[i][j].rgbtBlue + image[i][j].rgbtGreen) / 3; | |
image[i][j].rgbtRed = avg; | |
image[i][j].rgbtBlue = avg; | |
image[i][j].rgbtGreen = 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++) | |
{ | |
float sepiaRed = .393 * image[i][j].rgbtRed + .769 * image[i][j].rgbtGreen + .189 * image[i][j].rgbtBlue; | |
float sepiaGreen = .349 * image[i][j].rgbtRed + .686 * image[i][j].rgbtGreen + .168 * image[i][j].rgbtBlue; | |
float sepiaBlue = .272 * image[i][j].rgbtRed + .534 * image[i][j].rgbtGreen + .131 * image[i][j].rgbtBlue; | |
image[i][j].rgbtRed = (int) (sepiaRed <= 255) ? sepiaRed : 255; | |
image[i][j].rgbtBlue = (int) (sepiaBlue <= 255) ? sepiaBlue : 255; | |
image[i][j].rgbtGreen = (int) (sepiaGreen <= 255) ? sepiaGreen : 255; | |
} | |
} | |
return; | |
} | |
// 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++) { | |
int tempRed = image[i][j].rgbtRed; | |
int tempBlue = image[i][j].rgbtBlue; | |
int tempGreen = image[i][j].rgbtGreen; | |
image[i][j].rgbtRed = image[i][width - 1 - j].rgbtRed; | |
image[i][width - 1 - j].rgbtRed = tempRed; | |
image[i][j].rgbtGreen = image[i][width - 1 - j].rgbtGreen; | |
image[i][width - 1 - j].rgbtGreen = tempGreen; | |
image[i][j].rgbtBlue = image[i][width - 1 - j].rgbtBlue; | |
image[i][width - 1 - j].rgbtBlue = tempBlue; | |
} | |
} | |
return; | |
} | |
// Blur image | |
void blur(int height, int width, RGBTRIPLE image[height][width]) | |
{ | |
RGBTRIPLE ogImage[height][width]; | |
for (int i = 0; i < height; i++) | |
{ | |
for (int j = 0; j < width; j++) | |
{ | |
ogImage[i][j] = image[i][j]; | |
} | |
} | |
for (int i = 0, red, green, blue, counter; i < height; i++) | |
{ | |
for (int j = 0; j < width; j++) | |
{ | |
red = green = blue = counter = 0; | |
if (i >= 0 && j >= 0) | |
{ | |
red += ogImage[i][j].rgbtRed; | |
green += ogImage[i][j].rgbtGreen; | |
blue += ogImage[i][j].rgbtBlue; | |
counter++; | |
} | |
if (i >= 0 && j - 1 >= 0) | |
{ | |
red += ogImage[i][j-1].rgbtRed; | |
green += ogImage[i][j-1].rgbtGreen; | |
blue += ogImage[i][j-1].rgbtBlue; | |
counter++; | |
} | |
if ((i >= 0 && j + 1 >= 0) && (i >= 0 && j + 1 < width)) | |
{ | |
red += ogImage[i][j+1].rgbtRed; | |
green += ogImage[i][j+1].rgbtGreen; | |
blue += ogImage[i][j+1].rgbtBlue; | |
counter++; | |
} | |
if (i - 1 >= 0 && j >= 0) | |
{ | |
red += ogImage[i-1][j].rgbtRed; | |
green += ogImage[i-1][j].rgbtGreen; | |
blue += ogImage[i-1][j].rgbtBlue; | |
counter++; | |
} | |
if (i - 1 >= 0 && j - 1 >= 0) | |
{ | |
red += ogImage[i-1][j-1].rgbtRed; | |
green += ogImage[i-1][j-1].rgbtGreen; | |
blue += ogImage[i-1][j-1].rgbtBlue; | |
counter++; | |
} | |
if ((i - 1 >= 0 && j + 1 >= 0) && (i - 1 >= 0 && j + 1 < width)) | |
{ | |
red += ogImage[i-1][j+1].rgbtRed; | |
green += ogImage[i-1][j+1].rgbtGreen; | |
blue += ogImage[i-1][j+1].rgbtBlue; | |
counter++; | |
} | |
if ((i + 1 >= 0 && j >= 0) && (i + 1 < height && j >= 0)) | |
{ | |
red += ogImage[i+1][j].rgbtRed; | |
green += ogImage[i+1][j].rgbtGreen; | |
blue += ogImage[i+1][j].rgbtBlue; | |
counter++; | |
} | |
if ((i + 1 >= 0 && j - 1 >= 0) && (i + 1 < height && j - 1 >= 0)) | |
{ | |
red += ogImage[i+1][j-1].rgbtRed; | |
green += ogImage[i+1][j-1].rgbtGreen; | |
blue += ogImage[i+1][j-1].rgbtBlue; | |
counter++; | |
} | |
if ((i + 1 >= 0 && j + 1 >= 0) && (i + 1 < height && j + 1 < width)) | |
{ | |
red += ogImage[i+1][j+1].rgbtRed; | |
green += ogImage[i+1][j+1].rgbtGreen; | |
blue += ogImage[i+1][j+1].rgbtBlue; | |
counter++; | |
} | |
image[i][j].rgbtRed = round(red / (counter * 1.0)); | |
image[i][j].rgbtGreen = round(green / (counter * 1.0)); | |
image[i][j].rgbtBlue = round(blue / (counter * 1.0)); | |
} | |
} | |
return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
can u please tell me what is this this for filter less or more??