Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Code for CS50 Stack Exchange
#include "helpers.h"
#include <math.h>
#include <stdio.h>
#include <cs50.h>
// Convert image to grayscale
void grayscale(int height, int width, RGBTRIPLE image[height][width])
{
// each row
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
// average num
float preAverage = (image[y][x].rgbtBlue + image[y][x].rgbtGreen + image[y][x].rgbtRed) / 3;
int average = round(preAverage);
// apply
image[y][x].rgbtBlue = average;
image[y][x].rgbtGreen = average;
image[y][x].rgbtRed = average;
}
}
}
// Convert image to sepia
void sepia(int height, int width, RGBTRIPLE image[height][width])
{
// temporary values - no conflict
int tempBlue, tempRed, tempGreen;
for (int y = 0; y < height; y++) // row
{
for (int x = 0; x < width; x++) // each column in row
{
tempRed = round(.393 * image[y][x].rgbtRed + .769 * image[y][x].rgbtGreen + .189 * image[y][x].rgbtBlue);
tempGreen = round(.349 * image[y][x].rgbtRed + .686 * image[y][x].rgbtGreen + .168 * image[y][x].rgbtBlue);
tempBlue = round(.272 * image[y][x].rgbtRed + .534 * image[y][x].rgbtGreen + .131 * image[y][x].rgbtBlue);
image[y][x].rgbtBlue = tempBlue;
image[y][x].rgbtRed = tempRed;
image[y][x].rgbtGreen = tempGreen;
}
}
}
// Reflect image horizontally
void reflect(int height, int width, RGBTRIPLE image[height][width])
{
// temp for no conflict
RGBTRIPLE image2[height][width];
// each row
for (int y = 0; y < height; y++)
{
// each column in the row
for (int x = 0; x < width; x++)
{
image2[y][x].rgbtBlue = image[y][width - x - 1].rgbtBlue;
image2[y][x].rgbtRed = image[y][width - x - 1].rgbtRed;
image2[y][x].rgbtGreen = image[y][width - x - 1].rgbtGreen;
}
}
// each row
for (int y = 0; y < height; y++)
{
// each column in the row
for (int x = 0; x < width; x++)
{
// matchup
image[y][x].rgbtBlue = image2[y][x].rgbtBlue;
image[y][x].rgbtRed = image2[y][x].rgbtRed;
image[y][x].rgbtGreen = image2[y][x].rgbtGreen;
}
}
}
// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
RGBTRIPLE image2[height][width];
for (int y = 0; y < height; y++) // each row
{
// each column in the row
for (int x = 0; x < width; x++)
{
int spots[3][3][3] = {{{0, 0, 0}, {0, image[y][x].rgbtRed, 0}, {0, 0, 0}}, {{0, 0, 0}, {0, image[y][x].rgbtGreen, 0}, {0, 0, 0}}, {{0, 0, 0}, {0, image[y][x].rgbtBlue, 0}, {0, 0, 0}}};
int counter[3] = {0, 0, 0};
int counter2[3] = {0, 0, 0};
bool left = true, right = true, up = true, down = true;
if (x == 0)
{
left = false;
}
else if (x == width - 1)
{
right = false;
}
if (y == 0)
{
up = false;
}
else if (y == height - 1)
{
down = false;
}
if (left)
{
spots[0][1][0] = image[y][x - 1].rgbtRed;
spots[1][1][0] = image[y][x - 1].rgbtGreen;
spots[2][1][0] = image[y][x - 1].rgbtBlue;
}
if (right)
{
spots[0][1][2] = image[y][x + 1].rgbtRed;
spots[1][1][2] = image[y][x + 1].rgbtGreen;
spots[2][1][2] = image[y][x + 1].rgbtBlue;
}
if (up)
{
spots[0][0][1] = image[y - 1][x].rgbtRed;
spots[1][0][1] = image[y - 1][x].rgbtGreen;
spots[2][0][1] = image[y - 1][x].rgbtBlue;
if (left)
{
spots[0][0][0] = image[y - 1][x - 1].rgbtRed;
spots[1][0][0] = image[y - 1][x - 1].rgbtGreen;
spots[2][0][0] = image[y - 1][x - 1].rgbtBlue;
}
if (right)
{
spots[0][0][2] = image2[y - 1][x + 1].rgbtRed;
spots[1][0][2] = image[y - 1][x + 1].rgbtGreen;
spots[2][0][2] = image[y - 1][x + 1].rgbtBlue;
}
}
if (down)
{
spots[0][2][1] = image[y + 1][x].rgbtRed;
spots[1][2][1] = image[y + 1][x].rgbtGreen;
spots[2][2][1] = image[y + 1][x].rgbtBlue;
if (left)
{
spots[0][2][0] = image[y + 1][x - 1].rgbtRed;
spots[1][2][0] = image[y + 1][x - 1].rgbtGreen;
spots[2][2][0] = image[y + 1][x - 1].rgbtBlue;
}
if (right)
{
spots[0][2][2] = image[y + 1][x + 1].rgbtRed;
spots[1][2][2] = image[y + 1][x + 1].rgbtGreen;
spots[2][2][2] = image[y + 1][x + 1].rgbtBlue;
}
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
for (int k = 0; k < 3; k++)
{
if (spots[i][j][k] != 0)
{
counter2[i] += spots[i][j][k];
counter[i]++;
}
}
}
}
if (counter[0] != 0 && counter[1] != 0 && counter[2] != 0)
{
image2[y][x].rgbtRed = counter2[0] / counter[0];
image2[y][x].rgbtGreen = counter2[1] / counter[1];
image2[y][x].rgbtBlue = counter2[2] / counter[2];
}
}
}
// each row
for (int y = 0; y < height; y++)
{
// each column in the row
for (int x = 0; x < width; x++)
{
// matchup
image[y][x].rgbtBlue = image2[y][x].rgbtBlue;
image[y][x].rgbtRed = image2[y][x].rgbtRed;
image[y][x].rgbtGreen = image2[y][x].rgbtGreen;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment