0

As title says, I am not passing check50 for blur. I am aware that my method is probably extremely tedious and not the best method, but I came up with it myself and would like to find out why it isn't working as opposed to copying a solution online.

The following is the output from check50. I seem to be off by one or two numbers everytime.

:( blur correctly filters middle pixel
    expected "127 140 149\n", not "126 140 149\n"
:) blur correctly filters pixel on edge
:) blur correctly filters pixel in corner
:( blur correctly filters 3x3 image
    expected "70 85 95\n80 9...", not "70 85 95\n80 9..."
:( blur correctly filters 4x4 image
    expected "70 85 95\n80 9...", not "70 85 95\n80 9..."

Here is my code:

// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
    RGBTRIPLE buffer[height][width];
    for (int i = 0; i < height; i++)
    {

        for (int j = 0; j < width; j++)
        {
            if (i == 0)
            {
                if (j == 0)
                {
                    buffer[i][j].rgbtRed = (image[i][j].rgbtRed + image[i][j + 1].rgbtRed + image[i + 1][j].rgbtRed + image[i + 1][j + 1].rgbtRed) / 4;
                    buffer[i][j].rgbtGreen = (image[i][j].rgbtGreen + image[i][j + 1].rgbtGreen + image[i + 1][j].rgbtGreen + image[i + 1][j + 1].rgbtGreen) / 4;
                    buffer[i][j].rgbtBlue = (image[i][j].rgbtBlue + image[i][j + 1].rgbtBlue + image[i + 1][j].rgbtBlue + image[i + 1][j + 1].rgbtBlue) / 4;

                }
                else if (j == width - 1)
                {
                    buffer[i][j].rgbtRed = (image[i][j].rgbtRed + image[i][j - 1].rgbtRed + image[i + 1][j].rgbtRed + image[i + 1][j - 1].rgbtRed) / 4;
                    buffer[i][j].rgbtGreen = (image[i][j].rgbtGreen + image[i][j - 1].rgbtGreen + image[i + 1][j].rgbtGreen + image[i + 1][j - 1].rgbtGreen) / 4;
                    buffer[i][j].rgbtBlue = (image[i][j].rgbtBlue + image[i][j - 1].rgbtBlue + image[i + 1][j].rgbtBlue + image[i + 1][j - 1].rgbtBlue) / 4;
                }
                else
                {
                    buffer[i][j].rgbtRed = (image[i][j].rgbtRed + image[i][j - 1].rgbtRed + image[i][j + 1].rgbtRed + image[i + 1][j].rgbtRed + image[i + 1][j - 1].rgbtRed + image[i + 1][j + 1].rgbtRed) / 6;
                    buffer[i][j].rgbtGreen = (image[i][j].rgbtGreen + image[i][j - 1].rgbtGreen + image[i][j + 1].rgbtGreen + image[i + 1][j].rgbtGreen + image[i + 1][j - 1].rgbtGreen + image[i + 1][j + 1].rgbtGreen) / 6;
                    buffer[i][j].rgbtBlue = (image[i][j].rgbtBlue + image[i][j - 1].rgbtBlue + image[i][j + 1].rgbtBlue + image[i + 1][j].rgbtBlue + image[i + 1][j - 1].rgbtBlue + image[i + 1][j + 1].rgbtBlue) / 6;
                }

            }
            else if (i == height - 1)
            {
                if (j == 0)
                {
                    buffer[i][j].rgbtRed = (image[i][j].rgbtRed + image[i][j + 1].rgbtRed + image[i - 1][j].rgbtRed + image[i - 1][j + 1].rgbtRed) / 4.00;
                    buffer[i][j].rgbtGreen = (image[i][j].rgbtGreen + image[i][j + 1].rgbtGreen + image[i - 1][j].rgbtGreen + image[i - 1][j + 1].rgbtGreen) / 4.00;
                    buffer[i][j].rgbtBlue = (image[i][j].rgbtBlue + image[i][j + 1].rgbtBlue + image[i - 1][j].rgbtBlue + image[i - 1][j + 1].rgbtBlue) / 4.00;

                }
                else if (j == width - 1)
                {
                    buffer[i][j].rgbtRed = (image[i][j].rgbtRed + image[i][j - 1].rgbtRed + image[i - 1][j].rgbtRed + image[i - 1][j - 1].rgbtRed) / 4.00;
                    buffer[i][j].rgbtGreen = (image[i][j].rgbtGreen + image[i][j - 1].rgbtGreen + image[i - 1][j].rgbtGreen + image[i - 1][j - 1].rgbtGreen) / 4.00;
                    buffer[i][j].rgbtBlue = (image[i][j].rgbtBlue + image[i][j - 1].rgbtBlue + image[i + 1][j].rgbtBlue + image[i - 1][j - 1].rgbtBlue) / 4.00;
                }
                else
                {
                    buffer[i][j].rgbtRed = (image[i][j].rgbtRed + image[i][j - 1].rgbtRed + image[i][j + 1].rgbtRed + image[i - 1][j].rgbtRed + image[i - 1][j - 1].rgbtRed + image[i - 1][j + 1].rgbtRed) / 6.00;
                    buffer[i][j].rgbtGreen = (image[i][j].rgbtGreen + image[i][j - 1].rgbtGreen + image[i][j + 1].rgbtGreen + image[i - 1][j].rgbtGreen + image[i - 1][j - 1].rgbtGreen + image[i - 1][j + 1].rgbtGreen) / 6.00;
                    buffer[i][j].rgbtBlue = (image[i][j].rgbtBlue + image[i][j - 1].rgbtBlue + image[i][j + 1].rgbtBlue + image[i - 1][j].rgbtBlue + image[i - 1][j - 1].rgbtBlue + image[i - 1][j + 1].rgbtBlue) / 6.00;
                }
            }
            else if (j == 0)
            {
                buffer[i][j].rgbtRed = (image[i][j].rgbtRed + image[i + 1][j].rgbtRed + image[i - 1][j].rgbtRed + image[i - 1][j + 1].rgbtRed + image[i + 1][j + 1].rgbtRed + image[i][j + 1].rgbtRed) / 6.00;
                buffer[i][j].rgbtGreen = (image[i][j].rgbtGreen + image[i + 1][j].rgbtGreen + image[i - 1][j].rgbtGreen + image[i - 1][j + 1].rgbtGreen + image[i + 1][j + 1].rgbtGreen + image[i][j + 1].rgbtGreen) / 6.00;
                buffer[i][j].rgbtBlue = (image[i][j].rgbtBlue + image[i + 1][j].rgbtBlue + image[i - 1][j].rgbtBlue + image[i - 1][j + 1].rgbtBlue + image[i + 1][j + 1].rgbtBlue + image[i][j + 1].rgbtBlue) / 6.00;
            }
            else if (j == height - 1)
            {

            }
            else
            {
                 buffer[i][j].rgbtRed = (image[i][j].rgbtRed + image[i][j - 1].rgbtRed + image[i][j + 1].rgbtRed + image[i - 1][j].rgbtRed + image[i - 1][j + 1].rgbtRed + image[i - 1][j - 1].rgbtRed + image[i + 1][j - 1].rgbtRed + image[i + 1][j].rgbtRed + image[i + 1][j + 1].rgbtRed) / 9.00;
                 buffer[i][j].rgbtGreen = (image[i][j].rgbtGreen + image[i][j - 1].rgbtGreen + image[i][j + 1].rgbtGreen + image[i - 1][j].rgbtGreen + image[i - 1][j + 1].rgbtGreen + image[i - 1][j - 1].rgbtGreen + image[i + 1][j - 1].rgbtGreen + image[i + 1][j].rgbtGreen + image[i + 1][j + 1].rgbtGreen) / 9.00;
                 buffer[i][j].rgbtBlue = (image[i][j].rgbtBlue + image[i][j - 1].rgbtBlue + image[i][j + 1].rgbtBlue + image[i - 1][j].rgbtBlue + image[i - 1][j + 1].rgbtBlue + image[i - 1][j - 1].rgbtBlue + image[i + 1][j - 1].rgbtBlue + image[i + 1][j].rgbtBlue + image[i + 1][j + 1].rgbtBlue) / 9.00;
            }


        }
    }
    for (int x = 0; x < width; x++)
        {
            for (int y = 0; y < width; y++)
            {
                image[x][y].rgbtRed = round(buffer[x][y].rgbtRed);
                image[x][y].rgbtGreen = round(buffer[x][y].rgbtGreen);
                image[x][y].rgbtBlue = round(buffer[x][y].rgbtBlue);
            }
        }
    return;
}

0

Dividing an integer by an integer does not always give the expected answer. To a computer 24/5 is 4. Consider this hint: "The values of a pixel’s rgbtRed, rgbtGreen, and rgbtBlue components are all integers, so be sure to round any floating-point numbers to the nearest integer when assigning them to a pixel value!"

2
  • I already used the round function right at the end. Is this enough, or should I be using round everytime.
    – 10forever
    Apr 25 at 19:48
  • You should use round when you change a float to an int. You should cast the result of division to a float. Since the buffer is an RGBTRIPLE, which is made of ints, these 2 constructs (rounding and casting) should happen on the lines with division.
    – juls
    Apr 25 at 20:45
0
for (int x = 0; x < width; x++) // <- width ??
    {
        for (int y = 0; y < width; y++)
        {
            image[x][y].rgbtRed = round(buffer[x][y].rgbtRed);
            image[x][y].rgbtGreen = round(buffer[x][y].rgbtGreen);
            image[x][y].rgbtBlue = round(buffer[x][y].rgbtBlue);
        }
    }

Surely you meant to write height at the top there.

The last else branch in your blur loop is out of bounds, and I think you're reading past the edges elsewhere as well. It's hard to say. You made this kinda complicated.

I would suggest combining the various conditions into one. But many roads lead to Rome you know.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .