0

I am stumped on this problem, more specifically, on the blur filter. I can't see anything wrong with the program, but it still errors out on check50. It seems that the middle function is correct, but the edges and corners are getting frowny faces. Anyone know what's wrong? Here's the code:

void blur(int height, int width, RGBTRIPLE image[height][width])
{
    BYTE br = 0;
    BYTE bg = 0;
    BYTE bb = 0;
    RGBTRIPLE (*imagetmp)[width] = calloc(height, width * (sizeof(RGBTRIPLE)));
    memcpy(imagetmp, image, height * width * sizeof(RGBTRIPLE));
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            //Middle
            if ((0 < i && i < (height - 1)) && (0 < j && j < (width - 1)))
            {
                br = ((image[i+1][j+1].rgbtRed + image[i+1][j].rgbtRed + image[i+1][j-1].rgbtRed + image[i][j+1].rgbtRed + image[i][j-1].rgbtRed + image[i-1][j+1].rgbtRed + image[i-1][j].rgbtRed + image[i-1][j-1].rgbtRed)/8);
                bg = ((image[i+1][j+1].rgbtGreen + image[i+1][j].rgbtGreen + image[i+1][j-1].rgbtGreen + image[i][j+1].rgbtGreen + image[i][j-1].rgbtGreen + image[i-1][j+1].rgbtGreen + image[i-1][j].rgbtGreen + image[i-1][j-1].rgbtGreen)/8);
                bb = ((image[i+1][j+1].rgbtBlue + image[i+1][j].rgbtBlue + image[i+1][j-1].rgbtBlue + image[i][j+1].rgbtBlue + image[i][j-1].rgbtBlue + image[i-1][j+1].rgbtBlue + image[i-1][j].rgbtBlue + image[i-1][j-1].rgbtBlue)/8);
                imagetmp[i][j].rgbtRed = br;
                imagetmp[i][j].rgbtGreen = bg;
                imagetmp[i][j].rgbtBlue = bb;
                continue;
            }
            //Corners
            else if (i == 0 && j == 0)
            {
                br = ((image[i+1][j+1].rgbtRed+image[i][j+1].rgbtRed+image[i+1][j].rgbtRed)/3);
                bg = ((image[i+1][j+1].rgbtGreen+image[i][j+1].rgbtGreen+image[i+1][j].rgbtGreen)/3);
                bb = ((image[i+1][j+1].rgbtBlue+image[i][j+1].rgbtBlue+image[i+1][j].rgbtBlue)/3);
                imagetmp[i][j].rgbtRed = br;
                imagetmp[i][j].rgbtGreen = bg;
                imagetmp[i][j].rgbtBlue = bb;
                continue;
            }
            else if (i == (height - 1) && j == (width - 1))
            {
                br = ((image[i][j-1].rgbtRed+image[i-1][j-1].rgbtRed+image[i-1][j].rgbtRed)/3);
                bg = ((image[i][j-1].rgbtGreen+image[i-1][j-1].rgbtGreen+image[i-1][j].rgbtGreen)/3);
                bb = ((image[i][j-1].rgbtBlue+image[i-1][j-1].rgbtBlue+image[i-1][j].rgbtBlue)/3);
                imagetmp[i][j].rgbtRed = br;
                imagetmp[i][j].rgbtGreen = bg;
                imagetmp[i][j].rgbtBlue = bb;
                continue;

            }
            else if (i == 0 && j == (width - 1))
            {
                br = ((image[i][j-1].rgbtRed+image[i+1][j-1].rgbtRed+image[i+1][j].rgbtRed)/3);
                bg = ((image[i][j-1].rgbtGreen+image[i+1][j-1].rgbtGreen+image[i+1][j].rgbtGreen)/3);
                bb = ((image[i][j-1].rgbtBlue+image[i+1][j-1].rgbtBlue+image[i+1][j].rgbtBlue)/3);
                imagetmp[i][j].rgbtRed = br;
                imagetmp[i][j].rgbtGreen = bg;
                imagetmp[i][j].rgbtBlue = bb;
                continue;
            }
            else if (i == (height - 1) && j == 0)
            {
                br = ((image[i][j+1].rgbtRed+image[i-1][j+1].rgbtRed+image[i-1][j].rgbtRed)/3);
                bg = ((image[i][j+1].rgbtGreen+image[i-1][j+1].rgbtGreen+image[i-1][j].rgbtGreen)/3);
                bb = ((image[i][j+1].rgbtBlue+image[i-1][j+1].rgbtBlue+image[i-1][j].rgbtBlue)/3);
                imagetmp[i][j].rgbtRed = br;
                imagetmp[i][j].rgbtGreen = bg;
                imagetmp[i][j].rgbtBlue = bb;
                continue;
            }
            // Edges
            //done
            else if (0 <= i && i < (height - 2) && j == 1)
            {
                br = ((image[i][j+1].rgbtRed+image[i-1][j+1].rgbtRed+image[i-1][j].rgbtRed+image[i+1][j+1].rgbtRed+image[i+1][j].rgbtRed)/5);
                bg = ((image[i][j+1].rgbtGreen+image[i-1][j+1].rgbtGreen+image[i-1][j].rgbtGreen+image[i+1][j+1].rgbtGreen+image[i+1][j].rgbtGreen)/5);
                bb = ((image[i][j+1].rgbtBlue+image[i-1][j+1].rgbtBlue+image[i-1][j].rgbtBlue+image[i+1][j+1].rgbtBlue+image[i+1][j].rgbtBlue)/5);
                imagetmp[i][j].rgbtRed = br;
                imagetmp[i][j].rgbtGreen = bg;
                imagetmp[i][j].rgbtBlue = bb;
                continue;
            }
            //done
            else if (i == 0 && 0 < j && j < (width - 2))
            {
                br = ((image[i][j+1].rgbtRed+image[i][j-1].rgbtRed+image[i+1][j+1].rgbtRed+image[i+1][j-1].rgbtRed+image[i+1][j].rgbtRed)/5);
                bg = ((image[i][j+1].rgbtGreen+image[i][j-1].rgbtGreen+image[i+1][j+1].rgbtGreen+image[i+1][j-1].rgbtGreen+image[i+1][j].rgbtGreen)/5);
                bb = ((image[i][j+1].rgbtBlue+image[i][j-1].rgbtBlue+image[i+1][j+1].rgbtBlue+image[i+1][j-1].rgbtBlue+image[i+1][j].rgbtBlue)/5);
                imagetmp[i][j].rgbtRed = br;
                imagetmp[i][j].rgbtGreen = bg;
                imagetmp[i][j].rgbtBlue = bb;
                continue;
            }
            //done
            else if (0 <= i && i < (height - 2) && j == (width - 2))
            {
                br = ((image[i+1][j-1].rgbtRed+image[i+1][j].rgbtRed+image[i-1][j-1].rgbtRed+image[i-1][j].rgbtRed+image[i][j-1].rgbtRed)/5);
                bg = ((image[i+1][j-1].rgbtGreen+image[i+1][j].rgbtGreen+image[i-1][j-1].rgbtGreen+image[i-1][j].rgbtGreen+image[i][j-1].rgbtGreen)/5);
                bb = ((image[i+1][j-1].rgbtBlue+image[i+1][j].rgbtBlue+image[i-1][j-1].rgbtBlue+image[i-1][j].rgbtBlue+image[i][j-1].rgbtBlue)/5);
                imagetmp[i][j].rgbtRed = br;
                imagetmp[i][j].rgbtGreen = bg;
                imagetmp[i][j].rgbtBlue = bb;
                continue;
            }
            //in progress
            else if (0 <= i && i < (height - 2) && j == (width - 2))
            {
                br = ((image[i][j+1].rgbtRed+image[i][j-1].rgbtRed+image[i-1][j+1].rgbtRed+image[i-1][j].rgbtRed+image[i-1][j-1].rgbtRed)/5);
                bg = ((image[i][j+1].rgbtGreen+image[i][j-1].rgbtGreen+image[i-1][j+1].rgbtGreen+image[i-1][j].rgbtGreen+image[i-1][j-1].rgbtGreen)/5);
                bb = ((image[i][j+1].rgbtBlue+image[i][j-1].rgbtBlue+image[i-1][j+1].rgbtBlue+image[i-1][j].rgbtBlue+image[i-1][j-1].rgbtBlue)/5);
                imagetmp[i][j].rgbtRed = br;
                imagetmp[i][j].rgbtGreen = bg;
                imagetmp[i][j].rgbtBlue = bb;
                continue;
            }

        }
    }
    memcpy(image, imagetmp, height * width * sizeof(RGBTRIPLE));
    return;
}
0

All the problems come down to this. In calculating the blurred values, the code needs to include the values in the target cell itself, not just the cells around it.

I'm actually surprised it passed the test for middle pixels. Looks like a unique case where your code gives a false pass in a test. I'll have to bring this up with staff. Maybe change the test data being used. This is a good demo of why it's important to create quality test data and to do extensive testing of code. ;-)

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

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