I'm having difficulty with pset4 Blur. I cannot seem to pass the 3x3 and 4x4 test in check50. The actual output values always seem to diverge from the expected output values after a few pixels have been looped through.
This is my 3rd attempt at the blur function, i.e. I've already completely rewritten the code twice. First time my approach was to loop clockwise through each pixel if it existed, then second attempt I tried to target corners, edges and middle pixels separately but the code got too complicated. It now makes sense to me to loop through each row of the grid which 'could' surround the current pixel in the loop.
If anyone could give me some advice I'd be much appreciated.
void blur(int height, int width, RGBTRIPLE image[height][width])
{
int row, col;
int a, b;
RGBTRIPLE copy[height][width];
for (row = 0; row < height; row++)
{
for (col = 0; col < width; col++)
{
int sumRed = 0;
int sumGreen = 0;
int sumBlue = 0;
float totalPixels = 0.00;
// pixel grid layout
// image[row - 1][col - 1] image[row - 1][col] image[row - 1][col + 1]
// image[row][col - 1] image[row][col] image[row][col + 1];
// image[row + 1][col - 1] image[row + 1][col] image[row + 1][col + 1]
// prevent out of bounds
// top row
for (int top = col -1; top <= col +1; top++)
{
// if the pixels in the top row are in the bounds of the array
if (row -1 >= 0 && (top >= 0 && top < width))
{
sumRed += image[row - 1][top].rgbtRed;
sumGreen += image[row - 1][top].rgbtGreen;
sumBlue += image[row - 1][top].rgbtBlue;
totalPixels++;
}
}
// middle row
for (int mid = col -1; mid <= col +1; mid++)
{
if (row >= 0 && (mid >= 0 && mid < width))
{
sumRed += image[row][mid].rgbtRed;
sumGreen += image[row][mid].rgbtGreen;
sumBlue += image[row][mid].rgbtBlue;
totalPixels++;
}
}
// bottom row
for (int low = col -1; low <= col +1; low++)
{
if (row +1 >= 0 && (low >= 0 && low < width))
{
sumRed += image[row +1][low].rgbtRed;
sumGreen += image[row +1][low].rgbtGreen;
sumBlue += image[row +1][low].rgbtBlue;
totalPixels++;
}
}
copy[row][col].rgbtRed = round((sumRed / totalPixels));
copy[row][col].rgbtGreen = round((sumGreen / totalPixels));
copy[row][col].rgbtBlue = round((sumBlue / totalPixels));
}
}
for (a = 0; a < height; a++)
{
for (b = 0; b < width; b++)
{
image[a][b].rgbtRed = copy[a][b].rgbtRed;
image[a][b].rgbtGreen = copy[a][b].rgbtGreen;
image[a][b].rgbtBlue = copy[a][b].rgbtBlue;
}
}
return;
}```