I've hit a bit of a brick wall writing a blur function for filter (more). This is my second go at refactoring the code, and although it produces a blurred image without any issues (no weird edges, missing areas etc), it fails check50 (details below code)Deleted code as it was very similar to the solution. Any ideas, as I'm pretty stuck?
// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
// try again, last time it got far too complicated! h is height and w is width now. try auto itterate all possible combos
RGBTRIPLE(*blurImage)[width] = calloc(height, width * sizeof(RGBTRIPLE)); // alloc memory for a temp array
for (int i = 0; i < height; i ++)
{
for (int j = 0; j < width; j ++)
{
double count = 0; // initialise counters and reset to zero each loop
int totalBlue = 0;
int totalGreen = 0;
int totalRed = 0;
for (int h = -1; h < 2; h ++)
{
for (int w = -1; w < 2; w ++)
{
// image[y][x]
int y = (i + h-1); int x = (j + w-1);
//int y = (i + h); int x = (j + w);
// only deal with valid pixels (i.e not those off screen)
//if (y >=0 & y != (height -1) & x >= 0 & x != (width -1))
if (y >=0 & y != height & x >= 0 & x != width)
{
totalBlue += image[y][x].rgbtBlue;
totalGreen += image[y][x].rgbtGreen;
totalRed += image[y][x].rgbtRed;
//printf("Loop 1 y:%i x:%i\n", y, x);
count ++;
}
}
}
//printf("-------\n");
// stuff to be done for the current block of pixels
double avgBlue = totalBlue / count;
double avgGreen = totalGreen / count;
double avgRed = totalRed / count;
// Run the same loop again to get the same position from earlier.
for (int h = -1; h < 2; h ++)
{
for (int w = -1; w < 2; w ++)
{
int y = (i + h-1); int x = (j + w-1);
//int y = (i + h); int x = (j + w);
// only deal with valid pixels (i.e not those off screen)
//if (y >=0 & y != (height -1) & x >= 0 & x != (width -1))
if (y >=0 & y != height & x >= 0 & x != width)
{
//printf("double%f\n", round(avgBlue));
// write to the new pixels
blurImage[y][x].rgbtBlue = round(avgBlue);
blurImage[y][x].rgbtGreen = round(avgGreen);
blurImage[y][x].rgbtRed = round(avgRed);
//printf("Loop 2 y:%i x:%i\n", y, x);
}
}
} //printf("------\n");
}
}
// copy from the temp to the image array
for (int i = 0; i < height; i ++)
{
for (int j = 0; j < width; j ++)
{
image[i][j].rgbtBlue = blurImage[i][j].rgbtBlue;
image[i][j].rgbtGreen = blurImage[i][j].rgbtGreen;
image[i][j].rgbtRed = blurImage[i][j].rgbtRed;
}
}
free(blurImage);
return;
}
The only test passed is 'blur correctly filters middle pixel'.
The full output is:
:) blur correctly filters middle pixel
:( blur correctly filters pixel on edge
expected "80 95 105\n", not "127 140 149\n"
:( blur correctly filters pixel in corner
expected "70 85 95\n", not "127 140 149\n"
:( blur correctly filters 3x3 image
expected "70 85 95\n80 9...", not "127 140 149\n1..."
:( blur correctly filters 4x4 image
expected "70 85 95\n80 9...", not "123 136 145\n1..."
:( edges correctly filters middle pixel
expected "210 150 60\n", not "30 40 100\n"
:( edges correctly filters pixel on edge
expected "213 228 255\n", not "0 10 30\n"
:( edges correctly filters pixel in corner
expected "76 117 255\n", not "0 10 25\n"
:( edges correctly filters 3x3 image
expected "76 117 255\n21...", not "0 10 25\n0 10 ..."
:( edges correctly filters 4x4 image
expected "76 117 255\n21...", not "0 10 25\n0 10 ..."