// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
RGBTRIPLE tmp_image[height][width];
for (int y = 0; y < height; y++)
for (int x = 0; x < width; x++)
{
// averages
int av_red = 0;
int av_green = 0;
int av_blue = 0;
int dividor = 0;
for (int _y = -1; _y < 1; _y++)
for (int _x = -1; _x < 1; _x++)
{
// if inside images bounds
if ((y + _y) < height && (y + _y) >= 0 && (x + _x) < width && (x + _x) >= 0)
{
av_red += image[y + _y][x + _x].rgbtRed;
av_green += image[y + _y][x + _x].rgbtGreen;
av_blue += image[y + _y][x + _x].rgbtBlue;
dividor++;
}
}
if (dividor > 0)
{
av_red = (int) round((double)av_red / (double)dividor);
if (av_red > 255)
av_red = 255;
av_green = (int) round((double)av_green / (double)dividor);
if (av_green > 255)
av_green = 255;
av_blue = (int) round((double)av_blue / (double)dividor);
if (av_blue > 255)
av_blue = 255;
// set tmp_images colors
tmp_image[y][x].rgbtRed = av_red;
tmp_image[y][x].rgbtGreen = av_green;
tmp_image[y][x].rgbtBlue = av_blue;
}
}
// set current image to tmp_image
for (int y = 0; y < height; y++)
for (int x = 0; x < width; x++)
{
image[y][x] = tmp_image[y][x];
}
}
The image blurs correctly but the numbers are slightly off.
the errors
:( blur correctly filters middle pixel
expected "127 140 149\n", not "70 85 95\n"
:( blur correctly filters pixel on edge
expected "80 95 105\n", not "25 35 45\n"
:( blur correctly filters pixel in corner
expected "70 85 95\n", not "10 20 30\n"
:( blur correctly filters 3x3 image
expected "70 85 95\n80 9...", not "10 20 30\n25 3..."
:( blur correctly filters 4x4 image
expected "70 85 95\n80 9...", not "10 20 30\n25 3..."