Good day,
I have written the code for CS50 blur filter and I understand there's a better way to code it (with less if statements), but before rewriting I would like to understand why it doesn't work at the moment. It produces a very dark image, it seems the RGB values are too low. I am stuck and would gladly welcome some help. Thank you.
void blur(int height, int width, RGBTRIPLE image[height][width])
{
RGBTRIPLE temp[height][width];
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
int counter = 1;
temp[i][j].rgbtRed = image[i][j].rgbtRed;
temp[i][j].rgbtGreen = image[i][j].rgbtGreen;
temp[i][j].rgbtBlue = image[i][j].rgbtBlue;
if (i != height - 1)
{
temp[i][j].rgbtRed += image[i+1][j].rgbtRed;
temp[i][j].rgbtGreen += image[i+1][j].rgbtGreen;
temp[i][j].rgbtBlue += image[i+1][j].rgbtBlue;
counter++;
}
if (i != 0)
{
temp[i][j].rgbtRed += image[i-1][j].rgbtRed;
temp[i][j].rgbtGreen += image[i-1][j].rgbtGreen;
temp[i][j].rgbtBlue += image[i-1][j].rgbtBlue;
counter++;
}
if (j != width - 1)
{
temp[i][j].rgbtRed += image[i][j+1].rgbtRed;
temp[i][j].rgbtGreen += image[i][j+1].rgbtGreen;
temp[i][j].rgbtBlue += image[i][j+1].rgbtBlue;
counter++;
}
if (j != 0)
{
temp[i][j].rgbtRed += image[i][j-1].rgbtRed;
temp[i][j].rgbtGreen += image[i][j-1].rgbtGreen;
temp[i][j].rgbtBlue += image[i][j-1].rgbtBlue;
counter++;
}
if (i != 0 && j != 0)
{
temp[i][j].rgbtRed += image[i-1][j-1].rgbtRed;
temp[i][j].rgbtGreen += image[i-1][j-1].rgbtGreen;
temp[i][j].rgbtBlue += image[i-1][j-1].rgbtBlue;
counter++;
}
if (i != height - 1 && j != width - 1)
{
temp[i][j].rgbtRed += image[i+1][j+1].rgbtRed;
temp[i][j].rgbtGreen += image[i+1][j+1].rgbtGreen;
temp[i][j].rgbtBlue += image[i+1][j+1].rgbtBlue;
counter++;
}
if (i != height - 1 && j != 0)
{
temp[i][j].rgbtRed += image[i+1][j-1].rgbtRed;
temp[i][j].rgbtGreen += image[i+1][j-1].rgbtGreen;
temp[i][j].rgbtBlue += image[i+1][j-1].rgbtBlue;
counter++;
}
if (i != 0 && j != width - 1)
{
temp[i][j].rgbtRed += image[i-1][j+1].rgbtRed;
temp[i][j].rgbtGreen += image[i-1][j+1].rgbtGreen;
temp[i][j].rgbtBlue += image[i-1][j+1].rgbtBlue;
counter++;
}
image[i][j].rgbtRed = round(temp[i][j].rgbtRed / (counter * 1.0));
image[i][j].rgbtGreen = round(temp[i][j].rgbtGreen / (counter * 1.0));
image[i][j].rgbtBlue = round(temp[i][j].rgbtBlue / (counter * 1.0));
}
}
return;
}