#include "helpers.h"
#include <math.h>
#include <stdio.h>
// function prototype
void swap(RGBTRIPLE *lp, RGBTRIPLE *rp);
// Convert image to grayscale
void grayscale(int height, int width, RGBTRIPLE image[height][width])
{
int R, G, B, Gr;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
// calculate average of R G B values. for each pixel.
R = image[i][j].rgbtRed;
G = image[i][j].rgbtGreen;
B = image[i][j].rgbtBlue;
Gr = round((R + B + G) / (double) 3);
// put new values to original array
image[i][j].rgbtRed = Gr;
image[i][j].rgbtGreen = Gr;
image[i][j].rgbtBlue = Gr;
}
}
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++)
{
// swap the pixels
swap(&image[i][j], &image[i][width - j - 1]);
}
}
return;
}
// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
RGBTRIPLE imagetemp[height][width];
int Rblur, Gblur, Bblur;
double neighborcount;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
// calculate red green blue amount of pixel at row i, column j
Rblur = 0, Gblur = 0, Bblur = 0, neighborcount = 0;
for (int row = -1; row < 2; row++)
{
if (i + row > -1 && i + row < height)
{
for (int column = -1; column < 2; column++)
{
if (j + column > -1 && j + column < width)
{
Rblur += image[i + row][j + column].rgbtRed;
Gblur += image[i + row][j + column].rgbtGreen;
Bblur += image[i + row][j + column].rgbtBlue;
neighborcount++;
}
}
}
}
// put new values to temporary array
imagetemp[i][j].rgbtRed = round(Rblur / neighborcount);
imagetemp[i][j].rgbtGreen = round(Gblur / neighborcount);
imagetemp[i][j].rgbtBlue = round(Bblur / neighborcount);
}
}
// put new values in temporary array into the original array
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
image[i][j].rgbtRed = imagetemp[i][j].rgbtRed;
image[i][j].rgbtGreen = imagetemp[i][j].rgbtGreen;
image[i][j].rgbtBlue = imagetemp[i][j].rgbtBlue;
}
}
return;
}
// Detect edges
void edges(int height, int width, RGBTRIPLE image[height][width])
{
RGBTRIPLE imagetemp[height][width];
float RtempGx, GtempGx, BtempGx, RtempGy, GtempGy, BtempGy;
int R, G, B;
// defining & initializing arrays according to Gx and Gy kernels
int Gx[3][3] = {{-1, 0, 1}, {-2, 0, 2}, {-1, 0, 1}};
int Gy[3][3] = {{-1, -2, -1}, {0, 0, 0}, {-1, -2, -1}};
// iterate through every pixel in image
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
// zeroing buffer arrays and temp variables for each iteration
RtempGx = 0, GtempGx = 0, BtempGx = 0, RtempGy = 0, GtempGy = 0, BtempGy = 0;
for (int row = -1; row < 2; row++)
{
int rowplus = row + 1;
if (i + row > -1 && i + row < height)
{
for (int column = -1; column < 2; column++)
{
int columnplus = column + 1;
if (j + column > -1 && j + column < width)
{
RtempGx += Gx[rowplus][columnplus] * image[i + row][j + column].rgbtRed;
GtempGx += Gx[rowplus][columnplus] * image[i + row][j + column].rgbtGreen;
BtempGx += Gx[rowplus][columnplus] * image[i + row][j + column].rgbtBlue;
// printf("%f\n", Xbufferbox[rowplus][columnplus][2]);
RtempGy += Gy[rowplus][columnplus] * image[i + row][j + column].rgbtRed;
GtempGy += Gy[rowplus][columnplus] * image[i + row][j + column].rgbtGreen;
BtempGy += Gy[rowplus][columnplus] * image[i + row][j + column].rgbtBlue;
}
}
}
}
R
= round
(sqrt((RtempGx
* RtempGx
) + (RtempGy
* RtempGy
)));
G
= round
(sqrt((GtempGx
* GtempGx
) + (GtempGy
* GtempGy
)));
B
= round
(sqrt((BtempGx
* BtempGx
) + (BtempGy
* BtempGy
)));
if (R > 255)
{
R = 255;
}
if (G > 255)
{
G = 255;
}
if (B > 255)
{
B = 255;
}
// put new values into temporary array
imagetemp[i][j].rgbtRed = R;
imagetemp[i][j].rgbtGreen = G;
imagetemp[i][j].rgbtBlue = B;
}
}
// put new values in temporary array into the original array
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
image[i][j].rgbtRed = imagetemp[i][j].rgbtRed;
image[i][j].rgbtGreen = imagetemp[i][j].rgbtGreen;
image[i][j].rgbtBlue = imagetemp[i][j].rgbtBlue;
}
}
return;
}
// swap arrays using pointers
void swap(RGBTRIPLE *lp, RGBTRIPLE *rp)
{
RGBTRIPLE temp = *lp;
*lp = *rp;
*rp = temp;
}