# PSET4 FILTER(LESS) BLUR HELP!

I'm struggling to figure out exactly what's wrong with the code since I think it mathematically checks out and covers every possibility of an image. Please do take a look at my code and maybe you'll see something obvious that I don't:

``````// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
int avgRed, avgBlue, avgGreen;
float favgRed, favgBlue, favgGreen;
RGBTRIPLE tempimage[height][width];

//Loop through each pixel
for(int i = 0; i < height; i++)
{
for(int j = 0; j < width; j++)
{
//Special case: First row
if(i == 0)
{
//Special case: First row first column
if(j == 0)
{
favgRed = (image[i][j].rgbtRed + image[i][j+1].rgbtRed + image[i+1][j].rgbtRed + image[i+1][j+1].rgbtRed)/4.0;
favgGreen = (image[i][j].rgbtGreen + image[i][j+1].rgbtGreen + image[i+1][j].rgbtGreen + image[i+1][j+1].rgbtGreen)/4.0;
favgBlue = (image[i][j].rgbtBlue + image[i][j+1].rgbtBlue + image[i+1][j].rgbtBlue + image[i+1][j+1].rgbtBlue)/4.0;
}
//Special case: First row last column
else if(j == width - 1)
{
favgRed = (image[i][j].rgbtRed + image[i][j-1].rgbtRed + image[i+1][j].rgbtRed + image[i+1][j-1].rgbtRed)/4.0;
favgGreen = (image[i][j].rgbtGreen + image[i][j-1].rgbtGreen + image[i+1][j].rgbtGreen + image[i+1][j-1].rgbtGreen)/4.0;
favgBlue = (image[i][j].rgbtBlue + image[i][j-1].rgbtBlue + image[i+1][j].rgbtBlue + image[i+1][j-1].rgbtBlue)/4.0;
}
//Special case: First row middle
else
{
favgRed = (image[i][j].rgbtRed + image[i][j+1].rgbtRed + image[i+1][j].rgbtRed + image[i+1][j+1].rgbtRed
+ image[i][j-1].rgbtRed + image[i-1][j-1].rgbtRed)/6.0;
favgGreen = (image[i][j].rgbtGreen + image[i][j+1].rgbtGreen + image[i+1][j].rgbtGreen + image[i+1][j+1].rgbtGreen
+ image[i][j-1].rgbtGreen + image[i-1][j-1].rgbtGreen)/6.0;
favgBlue = (image[i][j].rgbtBlue + image[i][j+1].rgbtBlue + image[i+1][j].rgbtBlue + image[i+1][j+1].rgbtBlue
+ image[i][j-1].rgbtBlue + image[i-1][j-1].rgbtBlue)/6.0;
}
}
//Special case: Last row
else if(i == height - 1)
{
//Special case: Last row first column
if(j == 0)
{
favgRed = (image[i][j].rgbtRed + image[i][j+1].rgbtRed + image[i-1][j].rgbtRed + image[i-1][j+1].rgbtRed)/4.0;
favgGreen = (image[i][j].rgbtGreen + image[i][j+1].rgbtGreen + image[i-1][j].rgbtGreen + image[i-1][j+1].rgbtGreen)/4.0;
favgBlue = (image[i][j].rgbtBlue + image[i][j+1].rgbtBlue + image[i-1][j].rgbtBlue + image[i-1][j+1].rgbtBlue)/4.0;
}
//Special case: Last row last column
else if(j == width - 1)
{
favgRed = (image[i][j].rgbtRed + image[i][j-1].rgbtRed + image[i-1][j].rgbtRed + image[i-1][j-1].rgbtRed)/4.0;
favgGreen = (image[i][j].rgbtGreen + image[i][j-1].rgbtGreen + image[i-1][j].rgbtGreen + image[i-1][j-1].rgbtGreen)/4.0;
favgBlue = (image[i][j].rgbtBlue + image[i][j-1].rgbtBlue + image[i-1][j].rgbtBlue + image[i-1][j-1].rgbtBlue)/4.0;
}
//Special case: Last row middle
else
{
favgRed = (image[i][j].rgbtRed + image[i][j+1].rgbtRed + image[i-1][j].rgbtRed + image[i-1][j+1].rgbtRed
+ image[i][j-1].rgbtRed + image[i-1][j-1].rgbtRed)/6.0;
favgGreen = (image[i][j].rgbtGreen + image[i][j+1].rgbtGreen + image[i-1][j].rgbtGreen + image[i-1][j+1].rgbtGreen
+ image[i][j-1].rgbtGreen + image[i-1][j-1].rgbtGreen)/6.0;
favgBlue = (image[i][j].rgbtBlue + image[i][j+1].rgbtBlue + image[i-1][j].rgbtBlue + image[i-1][j+1].rgbtBlue
+ image[i][j-1].rgbtBlue + image[i-1][j-1].rgbtBlue)/6.0;
}
}
//If not first or last row
else
{
//Special case: First column middle
if(j == 0)
{
favgRed = (image[i][j].rgbtRed + image[i][j + 1].rgbtRed + image[i + 1][j].rgbtRed + image[i - 1][j + 1].rgbtRed
+ image[i - 1][j].rgbtRed + image[i + 1][j + 1].rgbtRed)/6.0;
favgGreen = (image[i][j].rgbtGreen + image[i][j + 1].rgbtGreen + image[i + 1][j].rgbtGreen + image[i - 1][j + 1].rgbtGreen
+ image[i - 1][j].rgbtGreen + image[i + 1][j + 1].rgbtGreen)/6.0;
favgBlue = (image[i][j].rgbtBlue + image[i][j + 1].rgbtBlue + image[i + 1][j].rgbtBlue + image[i - 1][j + 1].rgbtBlue
+ image[i - 1][j].rgbtBlue + image[i + 1][j + 1].rgbtBlue)/6.0;
}
//Special case: Last column middle
else if(j == width - 1)
{
favgRed = (image[i][j].rgbtRed + image[i][j - 1].rgbtRed + image[i + 1][j].rgbtRed + image[i - 1][j - 1].rgbtRed
+ image[i - 1][j].rgbtRed + image[i + 1][j - 1].rgbtRed)/6.0;
favgGreen = (image[i][j].rgbtGreen + image[i][j - 1].rgbtGreen + image[i + 1][j].rgbtGreen + image[i - 1][j - 1].rgbtGreen
+ image[i - 1][j].rgbtGreen + image[i + 1][j - 1].rgbtGreen)/6.0;
favgBlue = (image[i][j].rgbtBlue + image[i][j - 1].rgbtBlue + image[i + 1][j].rgbtBlue + image[i - 1][j - 1].rgbtBlue
+ image[i - 1][j].rgbtBlue + image[i + 1][j - 1].rgbtBlue)/6.0;
}
//Any other pixel
else
{
favgRed = (image[i][j].rgbtRed + image[i][j - 1].rgbtRed + image[i + 1][j].rgbtRed + image[i - 1][j - 1].rgbtRed
+ image[i - 1][j].rgbtRed + image[i + 1][j - 1].rgbtRed + image[i][j + 1].rgbtRed + image[i - 1][j + 1].rgbtRed
+ image[i + 1][j + 1].rgbtRed)/9.0;
favgGreen = (image[i][j].rgbtGreen + image[i][j - 1].rgbtGreen + image[i + 1][j].rgbtGreen + image[i - 1][j - 1].rgbtGreen
+ image[i - 1][j].rgbtGreen + image[i + 1][j - 1].rgbtGreen + image[i][j + 1].rgbtGreen + image[i - 1][j + 1].rgbtGreen
+ image[i + 1][j + 1].rgbtGreen)/9.0;
favgBlue = (image[i][j].rgbtBlue + image[i][j - 1].rgbtBlue + image[i + 1][j].rgbtBlue + image[i - 1][j - 1].rgbtBlue
+ image[i - 1][j].rgbtBlue + image[i + 1][j - 1].rgbtBlue + image[i][j + 1].rgbtBlue + image[i - 1][j + 1].rgbtBlue
+ image[i + 1][j + 1].rgbtBlue)/9.0;
}
}

avgRed = round(favgRed);
avgGreen = round(favgGreen);
avgBlue = round(favgBlue);

tempimage[i][j].rgbtRed = avgRed;
tempimage[i][j].rgbtGreen = avgGreen;
tempimage[i][j].rgbtBlue = avgBlue;
}
}

for(int i = 0; i < height; i++)
{
for(int j = 0; j < width; j++)
{
image[i][j].rgbtRed = tempimage[i][j].rgbtRed;
image[i][j].rgbtGreen = tempimage[i][j].rgbtGreen;
image[i][j].rgbtBlue = tempimage[i][j].rgbtBlue;
}
}
return;
}
``````

Here are the errors on the command-line after running check50:

``````:) blur correctly filters middle pixel
:( blur correctly filters pixel on edge
expected "80 95 105\n", not "65 78 82\n"
:) blur correctly filters pixel in corner
:( blur correctly filters 3x3 image
expected "70 85 95\n80 9...", not "70 85 95\n65 7..."
:( blur correctly filters 4x4 image
expected "70 85 95\n80 9...", not "70 85 95\n63 7..."
``````

Any help is much appreciated. Thank you!

• Consider using loops, you're repeating a lot of code. – Meisam Mulla May 30 at 2:58

When I was about to give up, I got lucky and find your misspell: In the code following this line: `//Special case: First row middle`, something with your indexes is not right. can you see it?