I am trying to solve the final part of problem set 4 doing CS50, which is to blur a picture using the neighbouring pixels. I don't understand why my code is not working.
I am supposed to use the neighbouring pixels, and take into account that the pixels on the edges shouldn't go outside the boundaries.
So I have tried to make different loops for the top left, top right, bottom left, and bottom right pixels.
I have also made separate loops for the top row, bottom row, left column, and right column.
Then I made a loop for all the pixels in between.
void blur(int height, int width, RGBTRIPLE image[height][width])
{
RGBTRIPLE Bluenew;
RGBTRIPLE Rednew;
RGBTRIPLE Greennew;
for (int row = 0; row <= height -1; row ++)
{
for (int column = 0; column <= width - 1; width ++)
{
if (row == 0 && column == 0)
{
image[row][column].Bluenew = (1/4) * (image[row][column].rgbtBlue + image[row][column+1].rgbtBlue + image[row+1][column].rgbtBlue + image[row+1][column+1].rgbtBlue);
image[row][column].Rednew = (1/4) * (image[row][column].rgbtRed + image[row][column+1].rgbtRed + image[row+1][column].rgbtRed + image[row+1][column+1].rgbtRed);
image[row][column].Greennew = (1/4) * (image[row][column].rgbtGreen + image[row][column+1].rgbtGreen + image[row+1][column].rgbtGreen + image[row+1][column+1].rgbtGreen);
}
else if (row == 0 && column == width - 1)
{
image[row][column].Bluenew = (1/4) * (image[row][column-1].rgbtBlue + image[row][column].rgbtBlue + image[row+1][column-1].rgbtBlue + image[row+1][column].rgbtBlue);
image[row][column].Rednew = (1/4) * (image[row][column-1].rgbtRed + image[row][column].rgbtRed + image[row+1][column-1].rgbtRed + image[row+1][column].rgbtRed);
image[row][column].Greennew = (1/4) * (image[row][column-1].rgbtGreen + image[row][column].rgbtGreen + image[row+1][column-1].rgbtGreen + image[row+1][column].rgbtGreen);
}
else if (row == height - 1 && column == 0)
{
image[row][column].Bluenew = (1/4) * (image[row-1][column].rgbtBlue + image[row-1][column+1].rgbtBlue + image[row][column].rgbtBlue + image[row][column+1].rgbtBlue);
image[row][column].Rednew = (1/4) * (image[row-1][column].rgbtRed + image[row-1][column+1].rgbtRed + image[row][column].rgbtRed + image[row][column+1].rgbtRed);
image[row][column].Greennew = (1/4) * (image[row-1][column].rgbtGreen + image[row-1][column+1].rgbtGreen + image[row][column].rgbtGreen + image[row][column+1].rgbtGreen);
}
else if (row == height - 1 && column == width - 1)
{
image[row][column].Bluenew = (1/4) * (image[row-1][column-1].rgbtBlue + image[row-1][column].rgbtBlue + image[row][column-1].rgbtBlue + image[row][column].rgbtBlue);
image[row][column].Rednew = (1/4) * (image[row-1][column-1].rgbtRed + image[row-1][column].rgbtRed + image[row][column-1].rgbtRed + image[row][column].rgbtRed);
image[row][column].Greennew = (1/4) * (image[row-1][column-1].rgbtGreen + image[row-1][column].rgbtGreen + image[row][column-1].rgbtGreen + image[row][column].rgbtGreen);
}
}
}
for (int row = 1; row <= height -2; row ++)
{
for (int column = 1; column <= width - 2; width ++)
{
image[row][column].Bluenew = (1/9) * (image[row-1][column-1].rgbtBlue + image[row-1][column].rgbtBlue + image[row-1][column+1].rgbtBlue +
image[row][column-1].rgbtBlue + image[row][column].rgbtBlue + image[row][column+1].rgbtBlue +
image[row+1][column-1].rgbtBlue + image[row+1][column].rgbtBlue + image[row+1][column+1].rgbtBlue);
image[row][column].Rednew = (1/9) * (image[row-1][column-1].rgbtRed + image[row-1][column].rgbtRed + image[row-1][column+1].rgbtRed +
image[row][column-1].rgbtRed + image[row][column].rgbtRed + image[row][column+1].rgbtRed +
image[row+1][column-1].rgbtRed + image[row+1][column].rgbtRed + image[row+1][column+1].rgbtRed);
image[row][column].Greennew = (1/9) * (image[row-1][column-1].rgbtGreen + image[row-1][column].rgbtGreen + image[row-1][column+1].rgbtGreen +
image[row][column-1].rgbtGreen + image[row][column].rgbtGreen + image[row][column+1].rgbtGreen +
image[row+1][column-1].rgbtGreen + image[row+1][column].rgbtGreen + image[row+1][column+1].rgbtGreen);
}
}
for (int row = 1; row <= height -2; row ++)
{
image[row][0].Bluenew = (1/6) * (image[row-1][0].rgbtBlue + image[row-1][1].rgbtBlue + image[row][0].rgbtBlue +
image[row+1][0].rgbtBlue + image[row][1].rgbtBlue + image[row+1][1].rgbtBlue);
image[row][0].Rednew = (1/6) * (image[row-1][0].rgbtRed + image[row-1][1].rgbtRed + image[row][0].rgbtRed +
image[row+1][0].rgbtRed + image[row][1].rgbtRed + image[row+1][1].rgbtRed);
image[row][0].Greennew = (1/6) * (image[row-1][0].rgbtGreen + image[row-1][1].rgbtGreen + image[row][0].rgbtGreen +
image[row+1][0].rgbtGreen + image[row][1].rgbtGreen + image[row+1][1].rgbtGreen);
}
for (int row = 1; row <= height -2; row ++)
{
image[row][width-1].Bluenew = (1/6) * (image[row-1][width-2].rgbtBlue + image[row-1][width-1].rgbtBlue + image[row][width-2].rgbtBlue +
image[row][width-1].rgbtBlue + image[row+1][width-2].rgbtBlue + image[row+1][width-1].rgbtBlue);
image[row][width-1].Rednew = (1/6) * (image[row-1][width-2].rgbtRed + image[row-1][width-1].rgbtRed + image[row][width-2].rgbtRed +
image[row][width-1].rgbtRed + image[row+1][width-2].rgbtRed + image[row+1][width-1].rgbtRed);
image[row][width-1].Greennew = (1/6) * (image[row-1][width-2].rgbtGreen + image[row-1][width-1].rgbtGreen + image[row][width-2].rgbtGreen +
image[row][width-1].rgbtGreen + image[row+1][width-2].rgbtGreen + image[row+1][width-1].rgbtGreen);
}
for (int column = 1; column <= width -2; column ++)
{
image[0][column].Bluenew= (1/6) * (image[0][column-1].rgbtBlue + image[0][column].rgbtBlue + image[0][column+1].rgbtBlue +
image[1][column-1].rgbtBlue + image[1][column].rgbtBlue + image[1][column+1].rgbtBlue);
image[0][column].Rednew = (1/6) * (image[0][column-1].rgbtRed + image[0][column].rgbtRed + image[0][column+1].rgbtRed +
image[1][column-1].rgbtRed + image[1][column].rgbtRed + image[1][column+1].rgbtRed);
image[0][column].Greennew = (1/6) * (image[0][column-1].rgbtGreen + image[0][column].rgbtGreen + image[0][column+1].rgbtGreen +
image[1][column-1].rgbtGreen + image[1][column].rgbtGreen + image[1][column+1].rgbtGreen);
}
for (int column = 1; column <= width -2; column ++)
{
image[height-1][column].Bluenew = (1/6) * (image[height-2][column-1].rgbtBlue + image[height-2][column].rgbtBlue + image[height-2][column+1].rgbtBlue +
image[height-1][column-1].rgbtBlue + image[height-1][column].rgbtBlue + image[height-1][column+1].rgbtBlue);
image[height-1][column].Rednew = (1/6) * (image[height-2][column-1].rgbtRed + image[height-2][column].rgbtRed + image[height-2][column+1].rgbtRed +
image[height-1][column-1].rgbtRed + image[height-1][column].rgbtRed + image[height-1][column+1].rgbtRed);
image[height-1][column].Greennew = (1/6) * (image[height-2][column-1].rgbtGreen + image[height-2][column].rgbtGreen + image[height-2][column+1].rgbtGreen +
image[height-1][column-1].rgbtGreen + image[height-1][column].rgbtGreen + image[height-1][column+1].rgbtGreen);
}
return;
}
// RGBTRIPLE is defined like this:
typedef struct
{
BYTE rgbtBlue;
BYTE rgbtGreen;
BYTE rgbtRed;
BYTE Bluenew;
BYTE Greennew;
BYTE Rednew;
} __attribute__((__packed__))
RGBTRIPLE;