# pset4 Blur calculation does not work properly

for some reason I can't get my calculation for the blur value running. The pictures are really odd and contain crazy colors. Any ideas to why this could be the case? I compared it already to working solutions and can't find what I did wrong here..

``````void blur(int height, int width, RGBTRIPLE image[height][width])
``````

{

``````double red[height][width];
double green[height][width];
double blue[height][width];

for (int i = 0; i < height; i ++)
{
for (int j = 0; j < width; j ++)
{
red[i][j] = image[i][j].rgbtRed;
green[i][j] = image[i][j].rgbtGreen;
blue[i][j] = image[i][j].rgbtBlue;
}
}

for (int i = 0; i < height; i ++)
{
if (i == 0)
{
for (int j = 0; j < width; j ++)
{

if (j == 0)
{
// Calculating RGB values of pixels nearby
red[i][j] = round((double)(image[i][j].rgbtRed + image[i][j + 1].rgbtRed) + (double)(image[i + 1][j].rgbtRed + image[i + 1][j + 1].rgbtRed) / (double)4);

green[i][j] = round((double)(image[i][j].rgbtGreen + image[i][j + 1].rgbtGreen) + (double)(image[i + 1][j].rgbtGreen + image[i + 1][j + 1].rgbtGreen) / (double)4);

blue[i][j] = round((double)(image[i][j].rgbtBlue + image[i][j + 1].rgbtBlue) + (double)(image[i + 1][j].rgbtBlue + image[i + 1][j + 1].rgbtBlue) / (double)4);

}

if (j != 0)
{
red[i][j] = round((double)(image[i][j - 1].rgbtRed + image[i][j].rgbtRed + image[i][j + 1].rgbtRed) + (double)(image[i + 1][j - 1].rgbtRed + image[i + 1][j].rgbtRed + image[i + 1][j + 1].rgbtRed) / (double)6);

green[i][j] = round((double)(image[i][j - 1].rgbtGreen + image[i][j].rgbtGreen + image[i][j + 1].rgbtGreen) + (double)(image[i + 1][j - 1].rgbtGreen + image[i + 1][j].rgbtGreen + image[i + 1][j + 1].rgbtGreen) / (double)6);

blue[i][j] = round((double)(image[i][j - 1].rgbtBlue + image[i][j].rgbtBlue + image[i][j + 1].rgbtBlue) + (double)(image[i + 1][j - 1].rgbtBlue + image[i + 1][j].rgbtBlue + image[i + 1][j + 1].rgbtBlue) / (double)6);

}
}
}

if (i != 0)
{

for (int j = 0; j < width; j ++)
{

if (j == 0)
{
// Calculating RGB values of pixels nearby
red[i][j] = round((double)(image[i][j].rgbtRed + image[i][j + 1].rgbtRed) + (double)(image[i + 1][j].rgbtRed + image[i + 1][j + 1].rgbtRed) + (double)(image[i - 1][j].rgbtRed + image[i - 1][j + 1].rgbtRed) / (double)6);

green[i][j] = round((double)(image[i][j].rgbtGreen + image[i][j + 1].rgbtGreen) + (double)(image[i + 1][j].rgbtGreen + image[i + 1][j + 1].rgbtGreen) + (double)(image[i - 1][j].rgbtGreen + image[i - 1][j + 1].rgbtGreen) / (double)6);

blue[i][j] = round((double)(image[i][j].rgbtBlue + image[i][j + 1].rgbtBlue) + (double)(image[i + 1][j].rgbtBlue + image[i + 1][j + 1].rgbtBlue) + (double)(image[i - 1][j].rgbtBlue + image[i - 1][j + 1].rgbtBlue) / (double)6);

}

if (j != 0)
{

red[i][j] = round((double)(image[i][j].rgbtRed + image[i][j + 1].rgbtRed + image[i][j - 1].rgbtRed) + (double)(image[i + 1][j].rgbtRed + image[i + 1][j + 1].rgbtRed + image[i + 1][j - 1].rgbtRed) + (double)(image[i - 1][j].rgbtRed + image[i - 1][j + 1].rgbtRed + image[i - 1][j - 1].rgbtRed) / (double)9);

green[i][j] = round((double)(image[i][j].rgbtGreen + image[i][j + 1].rgbtGreen  + image[i][j - 1].rgbtGreen) + (double)(image[i + 1][j].rgbtGreen + image[i + 1][j + 1].rgbtGreen  + image[i + 1][j - 1].rgbtGreen) + (double)(image[i - 1][j].rgbtGreen + image[i - 1][j + 1].rgbtGreen + image[i - 1][j - 1].rgbtGreen) / (double)9);

blue[i][j] = round((double)(image[i][j].rgbtBlue + image[i][j + 1].rgbtBlue + image[i][j - 1].rgbtBlue) + (double)(image[i + 1][j].rgbtBlue + image[i + 1][j + 1].rgbtBlue + image[i + 1][j - 1].rgbtBlue) + (double)(image[i - 1][j].rgbtBlue + image[i - 1][j + 1].rgbtBlue + image[i - 1][j - 1].rgbtBlue) / (double)9);

}
}
}

}

for (int i = 0; i < height; i ++)
{
for (int j = 0; j < width; j ++)
{
image[i][j].rgbtRed = red[i][j];
image[i][j].rgbtGreen = green[i][j];
image[i][j].rgbtBlue = blue[i][j];
}
}

return;
``````

}

I see a couple of issues. First, I see you dividing an `int` by a `double`. You need to cast the the whole thing to a double to get decimal values (`round((double)(numerator/denominator))`). Second, I do not know why your equation has a backslash `\` character in it--this is not a C operator that I am familiar with. I would remove the `\` and fix the casting and it should work as I do not see any other issues jumping out to me from my cursory read of your provided code.
• Thanks, I have never seen that! All of the code I have worked with just moves the code down and indents it twice (two tabs/3 or 4 spaces, depending on the code base's convention) after one of the mathematical operators. When you moved the `(double)` to before the numerator, did you put both the numerator and denominator into quotes like I did? Jan 18 '21 at 19:54
• Oh, I found the problem. You are assigning the new calculated values to `image` after every pass of your nested loops. This means that you lose your original values during the first pass and are never able to calculate the correct value again. You want to not assign values to `image` until you are done with your calculations, this is why you made your buffer arrays. Jan 18 '21 at 20:35