I'm working on a program that will slightly blur an image in c.
Knowing that I need the average and rgb values of the 8 surrounding pixels and the selected pixel to change the color of that pixel, I've added them together and taken the average.
I know that the way I'm implementing this is not the most efficient way this can be done, so if there are any suggestions on how to simplify this, please let me know.
I plan on copy tempimage back to image in a third for-loop at the end.
The struct RGBTRIPLE contain the rgb values for a pixel.
BYTE rgbtBlue;
BYTE rgbtGreen;
BYTE rgbtRed;
My issue now is dealing with special cases, such as the edges of the image or the pixels on the side.
How can I get the values of the surrounding pixels when the selected pixel is not surrounded by 9 pixels?
Here is my code so far:
void blur(int height, int width, RGBTRIPLE image[height][width])
{
// copy all values to temporary image
RGBTRIPLE tempimage[height][width];
int avgRed = 0;
int avgGreen = 0;
int avgBlue = 0;
//copy pixels to temp image
for ( int x = 0; x < height; x++)
{
for (int y = 0; y < width; y++)
{
tempimage[x][y] = image[x][y];
}
}
//get average of surrounding pixels
for ( int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
//TODO: edge check
//surrounding pixels
avgRed = round(((float)(image[i][j].rgbtRed
+ image[i][j - 1].rgbtRed + image[i][j + 1].rgbtRed
+ image[i - 1][j].rgbtRed + image[i - 1][j - 1].rgbtRed
+ image[i - 1][j + 1].rgbtRed + image[i + 1][j].rgbtRed
+ image[i + 1][j - 1].rgbtRed + image[i + 1][j + 1].rgbtRed)) / 9);
avgGreen = round(((float)(image[i][j].rgbtGreen
+ image[i][j - 1].rgbtGreen + image[i][j + 1].rgbtGreen
+ image[i - 1][j].rgbtGreen + image[i - 1][j - 1].rgbtGreen
+ image[i - 1][j + 1].rgbtGreen + image[i + 1][j].rgbtGreen
+ image[i + 1][j - 1].rgbtGreen + image[i + 1][j + 1].rgbtGreen)) / 9);
avgBlue = round(((float)(image[i][j].rgbtBlue
+ image[i][j - 1].rgbtBlue + image[i][j + 1].rgbtBlue
+ image[i - 1][j].rgbtBlue + image[i - 1][j - 1].rgbtBlue
+ image[i - 1][j + 1].rgbtBlue + image[i + 1][j].rgbtBlue
+ image[i + 1][j - 1].rgbtBlue + image[i + 1][j + 1].rgbtBlue)) / 9);
tempimage[i][j].rgbtRed = avgRed;
tempimage[i][j].rgbtGreen = avgGreen;
tempimage[i][j].rgbtBlue = avgBlue;
}
}
//TODO: for loop to copy tempimage back to image here
return;
}