-
-
Save kevinkell-y/bb5e7129f5c2154689ecd1feea14f88f to your computer and use it in GitHub Desktop.
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 <math.h> | |
// Convert image to grayscale | |
void grayscale(int height, int width, RGBTRIPLE image[height][width]) | |
{ | |
//loop through the image one row at a time | |
for (int i = 0; i < height - 1; i++) | |
{ | |
for (int j = 0; j < width; j++) | |
{ | |
// find the average RGB for each pixel in a row, round it | |
float avg = ( image[i][j].rgbtRed + image[i][j].rgbtGreen + image[i][j].rgbtBlue) / 3; | |
image[i][j].rgbtRed = (int) round(avg); | |
image[i][j].rgbtBlue = (int) round(avg); | |
image[i][j].rgbtGreen = (int) round(avg); | |
} | |
} | |
return; | |
} | |
// Convert image to sepia | |
void sepia(int height, int width, RGBTRIPLE image[height][width]) | |
{ | |
float sepiaRed; | |
float sepiaGreen; | |
float sepiaBlue; | |
//loop through the image one row at a time | |
for (int i = 0; i < height - 1; i++) | |
{ | |
// go through each pixel | |
for (int j = 0; j < width; j++) | |
{ | |
// apply sepia formula | |
sepiaRed = .393 * image[i][j].rgbtRed + .769 * image[i][j].rgbtGreen + .189 * image[i][j].rgbtBlue; | |
sepiaGreen = .349 * image[i][j].rgbtRed + .686 * image[i][j].rgbtGreen + .168 * image[i][j].rgbtBlue; | |
sepiaBlue = .272 * image[i][j].rgbtRed + .534 * image[i][j].rgbtGreen + .131 * image[i][j].rgbtBlue; | |
// round and 'cast' to an int | |
sepiaRed = (int) round(sepiaRed); | |
sepiaBlue = (int) round(sepiaBlue); | |
sepiaGreen = (int) round(sepiaGreen); | |
// max out at 255 | |
if (sepiaRed > 255) | |
{ | |
sepiaRed = 255; | |
} | |
if (sepiaBlue > 255) | |
{ | |
sepiaBlue = 255; | |
} | |
if (sepiaGreen > 255) | |
{ | |
sepiaGreen = 255; | |
} | |
// Assign the sepia | |
image[i][j].rgbtRed = sepiaRed; | |
image[i][j].rgbtGreen = sepiaGreen; | |
image[i][j].rgbtBlue = sepiaBlue; | |
} // For-Loop ROWS | |
} // For-Loop HEIGHT | |
return; | |
} | |
// Reflect image horizontally | |
void reflect(int height, int width, RGBTRIPLE image[height][width]) | |
{ | |
BYTE tmpRed; | |
BYTE tmpGreen; | |
BYTE tmpBlue; | |
// Rows | |
for (int i = 0; i < height; i++) | |
{ | |
// Width of rows | |
for ( int j = 0; j < (width/2) - 1; j++) | |
{ | |
tmpRed = image[i][j].rgbtRed; | |
tmpGreen = image[i][j].rgbtGreen; | |
tmpBlue = image[i][j].rgbtBlue; | |
image[i][j].rgbtBlue = image[i][width - 1 - j].rgbtBlue; | |
image[i][j].rgbtGreen = image[i][width - 1 - j].rgbtGreen; | |
image[i][j].rgbtRed = image[i][width - 1 - j].rgbtRed; | |
image[i][width - 1 - j].rgbtBlue = tmpBlue; | |
image[i][width - 1 - j].rgbtGreen = tmpGreen; | |
image[i][width - 1 - j].rgbtRed = tmpRed; | |
} | |
} | |
return; | |
} | |
// Blur image | |
void blur(int height, int width, RGBTRIPLE image[height][width]) | |
{ | |
float reds = 0; | |
float greens = 0; | |
float blues = 0; | |
RGBTRIPLE tmp[height][width]; | |
for (int i = 0; i < height; i++) | |
{ | |
for (int k = 0; k < width; k++) | |
{ // make a copy | |
tmp[i][k] = image[i][k]; | |
} | |
} | |
for (int i = 0; i < height; i++) | |
{ | |
for (int j = 0; j < width; j++) | |
{ | |
// Pixel in question | |
reds = tmp[i][j].rgbtRed; | |
greens = tmp[i][j].rgbtGreen; | |
blues = tmp[i][j].rgbtBlue; | |
int counter = 1; | |
// Anything to the left | |
if (j - 1 > -1) | |
{ | |
reds += tmp[i][j-1].rgbtRed; | |
greens += tmp[i][j-1].rgbtGreen; | |
blues += tmp[i][j-1].rgbtBlue; | |
counter++; | |
} | |
// Anything above | |
if (i - 1 > -1) | |
{ | |
reds += tmp[i-1][j].rgbtRed; | |
greens += tmp[i-1][j].rgbtGreen; | |
blues += tmp[i-1][j].rgbtBlue; | |
counter++; | |
} | |
// Anything to the right | |
if (j + 1 < width) | |
{ | |
reds += tmp[i][j+1].rgbtRed; | |
greens += tmp[i][j+1].rgbtGreen; | |
blues += tmp[i][j+1].rgbtBlue; | |
counter++; | |
} | |
// Anything below | |
if (i + 1 < height) | |
{ | |
reds += tmp[i+1][j].rgbtRed; | |
greens += tmp[i+1][j].rgbtGreen; | |
blues += tmp[i+1][j].rgbtBlue; | |
counter++; | |
} | |
// Below Left | |
if (j - 1 > -1 && i + 1 < height) | |
{ | |
reds += tmp[i+1][j-1].rgbtRed; | |
greens += tmp[i+1][j-1].rgbtGreen; | |
blues += tmp[i+1][j-1].rgbtBlue; | |
counter++; | |
} | |
// Below Right | |
if (j + 1 < width && i + 1 < height) | |
{ | |
reds += tmp[i+1][j+1].rgbtRed; | |
greens += tmp[i+1][j+1].rgbtGreen; | |
blues += tmp[i+1][j+1].rgbtBlue; | |
counter++; | |
} | |
// Above Right | |
if (j + 1 < width && i - 1 > -1) | |
{ | |
reds += tmp[i-1][j+1].rgbtRed; | |
greens += tmp[i-1][j+1].rgbtGreen; | |
blues += tmp[i-1][j+1].rgbtBlue; | |
counter++; | |
} | |
// Above Left | |
if (j - 1 > -1 && i - 1 > -1) | |
{ | |
reds += tmp[i-1][j-1].rgbtRed; | |
greens += tmp[i-1][j-1].rgbtGreen; | |
blues += tmp[i-1][j-1].rgbtBlue; | |
counter++; | |
} | |
reds = reds/counter; | |
greens = greens/counter; | |
blues = blues/counter; | |
image[i][j].rgbtRed = round(reds); | |
image[i][j].rgbtGreen = round(greens); | |
image[i][j].rgbtBlue = round(blues); | |
} | |
} | |
return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment