# CS50 Pset4 Blur

Greatings fellow Humans

So I am about at my wit's end with this one. I have set it all out and have the maths (I beleive) correct on the averages. BUT... check 50 says that some of the averages are coming out incorrect. I have been looking at the code for hours now, and cannot see the fault.

Help me ObiWan, you are my only hope.

// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
// | /  |J-1 | J  |J+1 |
// |_/__|____|____|____|
// |I-1 |I-1 |I-1 |I-1 |
// |____|J-1_|J___|J+1_|
// | I  |I   | IJ |I   |
// |____|J-1_|____|J+1_|
// |I+1 |I+1 |I+1 |I+1 |
// |____|J-1_|J___|J+1_|
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
// Local variables to calculate average colour levels
float pix_count = 0.0;
float tot_red = 0.0;
float tot_green = 0.0;
float tot_blue = 0.0;

// These used for ease of syntax in qualifying statemtns
int h = height - 1;
int w = width - 1;

// Top left pixel
// Checking edge cases
// Correct
if (i - 1 >= 0 && j - 1 >= 0)
{
tot_red = tot_red + image[i - 1][j - 1].rgbtRed;
tot_green = tot_green + image[i - 1][j - 1].rgbtGreen;
tot_blue = tot_blue + image[i - 1][j - 1].rgbtBlue;
pix_count = pix_count + 1;
}

// Top middle pixel
// Correct
if (i - 1 >= 0)
{
tot_red = tot_red + image[i - 1][j].rgbtRed;
tot_green = tot_green + image[i - 1][j].rgbtGreen;
tot_blue = tot_blue + image[i - 1][j].rgbtBlue;
pix_count = pix_count + 1;
}

// Top right pixel
// Correct
if (i - 1 >= 0 && j + 1 <= w)
{
tot_red = tot_red + image[i - 1][j + 1].rgbtRed;
tot_green = tot_green + image[i - 1][j + 1].rgbtGreen;
tot_blue = tot_blue + image[i - 1][j + 1].rgbtBlue;
pix_count = pix_count + 1;
}

// Middle left pixel
// Correct
if (j - 1 >= 0)
{
tot_red = tot_red + image[i][j - 1].rgbtRed;
tot_green = tot_green + image[i][j - 1].rgbtGreen;
tot_blue = tot_blue + image[i][j - 1].rgbtBlue;
pix_count = pix_count + 1;
}

// Middle pixel
// Correct
tot_red = tot_red + image[i][j].rgbtRed;
tot_green = tot_green + image[i][j].rgbtGreen;
tot_blue = tot_blue + image[i][j].rgbtBlue;
pix_count = pix_count + 1;

// Middle right pixel
// Correct
if (j + 1 <= w)
{
tot_red = tot_red + image[i][j + 1].rgbtRed;
tot_green = tot_green + image[i][j + 1].rgbtGreen;
tot_blue = tot_blue + image[i][j + 1].rgbtBlue;
pix_count = pix_count + 1;
}

// Bottom right pixel
// Correct
if (i + 1 <= h && j - 1 >= 0)
{
tot_red = tot_red + image[i + 1][j - 1].rgbtRed;
tot_green = tot_green + image[i + 1][j - 1].rgbtGreen;
tot_blue = tot_blue + image[i + 1][j - 1].rgbtBlue;
pix_count = pix_count + 1;
}

// Bottom middle pixel
// Correct
if (i + 1 <= h)
{
tot_red = tot_red + image[i + 1][j].rgbtRed;
tot_green = tot_green + image[i + 1][j].rgbtGreen;
tot_blue = tot_blue + image[i + 1][j].rgbtBlue;
pix_count = pix_count + 1;
}

// Bottom right pixel
//
if (i + 1 <= h && j + 1 <= w)
{
tot_red = tot_red + image[i + 1][j + 1].rgbtRed;
tot_green = tot_green + image[i + 1][j + 1].rgbtGreen;
tot_blue = tot_blue + image[i + 1][j + 1].rgbtBlue;
pix_count = pix_count + 1;
}
// Averaging the colour levels
float av_red = round(tot_red / pix_count);
float av_green = round(tot_green / pix_count);
float av_blue = round(tot_blue / pix_count);

// Assigning back to central pixel
image[i][j].rgbtRed = av_red;
image[i][j].rgbtGreen = av_green;
image[i][j].rgbtBlue = av_blue;
}
}

return;
}

Can someone please point out to a blind old fart where he went wrong?

Ed