0

I am SO close on reflect but am still getting a few final errors. My code for pixels on the edge and in the corners seems to work fine. However, there is an issue with my code on the center pixels and I can't figure out what I'm doing wrong. With Check50 I am getting the following errors:

:( blur correctly filters middle pixel expected "127 140 149\n", not "126 140 149\n"
:( 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..."

Any ideas?

void blur(int height, int width, RGBTRIPLE image[height][width])
{
    RGBTRIPLE temp[height][width]; // Declare temporary array

    for (int i = 0; i < height; i++)
    {
        for (int k = 0; k < width; k++) // Copy values from original bmp into temporary array
        {
            temp[i][k].rgbtRed = image[i][k].rgbtRed;
            temp[i][k].rgbtBlue = image[i][k].rgbtBlue;
            temp[i][k].rgbtGreen = image[i][k].rgbtGreen;
        }
    }

    for (int x = 0; x < height; x++)
    {
        for (int y = 0; y < width; y++)
        {
            if (x == 0 && y == 0) // Blur top left corner pixel
            {
                image[x][y].rgbtRed = roundf((temp[x][y].rgbtRed + temp[x + 1][y].rgbtRed + temp[x][y + 1].rgbtRed + temp[x + 1][y + 1].rgbtRed) / 4);
                image[x][y].rgbtGreen = roundf((temp[x][y].rgbtGreen + temp[x + 1][y].rgbtGreen + temp[x][y + 1].rgbtGreen + temp[x + 1][y + 1].rgbtGreen) / 4);
                image[x][y].rgbtBlue = roundf((temp[x][y].rgbtBlue + temp[x + 1][y].rgbtBlue + temp[x][y + 1].rgbtBlue + temp[x + 1][y + 1].rgbtBlue) / 4);
            }
            if (x == 0 && y == height) // Blur bottom left corner pixel
            {
                image[x][y].rgbtRed = roundf((temp[x][y].rgbtRed + temp[x][y - 1].rgbtRed + temp[x + 1][y].rgbtRed + temp[x + 1][y - 1].rgbtRed) / 4);
                image[x][y].rgbtGreen = roundf((temp[x][y].rgbtGreen + temp[x][y - 1].rgbtGreen + temp[x + 1][y].rgbtGreen + temp[x + 1][y - 1].rgbtGreen) / 4);
                image[x][y].rgbtBlue = roundf((temp[x][y].rgbtBlue + temp[x][y - 1].rgbtBlue + temp[x + 1][y].rgbtBlue + temp[x + 1][y - 1].rgbtBlue) / 4);
            }
            if (x == width && y == height) // Blur bottom right corner pixel
            {
                image[x][y].rgbtRed = roundf((temp[x][y].rgbtRed + temp[x - 1][y - 1].rgbtRed + temp[x][y - 1].rgbtRed + temp[x - 1][y - 1].rgbtRed) / 4);
                image[x][y].rgbtGreen = roundf((temp[x][y].rgbtGreen + temp[x - 1][y - 1].rgbtGreen + temp[x][y - 1].rgbtGreen + temp[x - 1][y - 1].rgbtGreen) / 4);
                image[x][y].rgbtBlue = roundf((temp[x][y].rgbtBlue + temp[x - 1][y - 1].rgbtBlue + temp[x][y - 1].rgbtBlue + temp[x - 1][y - 1].rgbtBlue) / 4);
            }
            if (x == width && y == 0) // Blur top right corner pixel
            {
                image[x][y].rgbtRed = roundf((temp[x][y].rgbtRed + temp[x - 1][y + 1].rgbtRed + temp[x + 1][y].rgbtRed + temp[x - 1][y].rgbtRed) / 4);
                image[x][y].rgbtGreen = roundf((temp[x][y].rgbtGreen + temp[x - 1][y + 1].rgbtGreen + temp[x + 1][y].rgbtGreen + temp[x - 1][y].rgbtGreen) / 4);
                image[x][y].rgbtBlue = roundf((temp[x][y].rgbtBlue + temp[x - 1][y + 1].rgbtBlue + temp[x + 1][y].rgbtBlue + temp[x - 1][y].rgbtBlue) / 4);
            }
            if (x == 0 && y != 0 && y != height) // Blur left edge
            {
                image[x][y].rgbtRed = roundf((temp[x][y - 1].rgbtRed + temp[x + 1][y - 1].rgbtRed + temp[x][y].rgbtRed + temp[x][y + 1].rgbtRed + temp[x + 1][y].rgbtRed + temp[x + 1][y + 1].rgbtRed) / 6);
                image[x][y].rgbtGreen = roundf((temp[x][y - 1].rgbtGreen + temp[x + 1][y - 1].rgbtGreen + temp[x][y].rgbtGreen + temp[x][y + 1].rgbtGreen + temp[x + 1][y].rgbtGreen + temp[x + 1][y + 1].rgbtGreen) / 6);
                image[x][y].rgbtBlue = roundf((temp[x][y - 1].rgbtBlue + temp[x + 1][y - 1].rgbtBlue + temp[x][y].rgbtBlue + temp[x][y + 1].rgbtBlue + temp[x + 1][y].rgbtBlue + temp[x + 1][y + 1].rgbtBlue) / 6);
            }
            if (x == width && y != 0 && y != height) // Blur right edge
            {
                image[x][y].rgbtRed = roundf((temp[x][y - 1].rgbtRed + temp[x][y + 1].rgbtRed + temp[x][y].rgbtRed + temp[x - 1][y].rgbtRed + temp[x - 1][y + 1].rgbtRed + temp[x - 1][y - 1].rgbtRed) / 6);
                image[x][y].rgbtGreen = roundf((temp[x][y - 1].rgbtGreen + temp[x][y + 1].rgbtGreen + temp[x][y].rgbtGreen + temp[x - 1][y].rgbtGreen + temp[x -  1][y + 1].rgbtGreen + temp[x - 1][y - 1].rgbtGreen) / 6);
                image[x][y].rgbtBlue = roundf((temp[x][y - 1].rgbtBlue + temp[x][y + 1].rgbtBlue + temp[x][y].rgbtBlue + temp[x - 1][y].rgbtBlue + temp[x - 1][y + 1].rgbtBlue + temp[x + 1][y - 1].rgbtBlue) / 6);
            }
            if (y == 0 && x != 0 && x != width) // Blur top edge
            {
                image[x][y].rgbtRed = roundf((temp[x][y + 1].rgbtRed + temp[x + 1][y + 1].rgbtRed + temp[x][y].rgbtRed + temp[x - 1][y].rgbtRed + temp[x + 1][y].rgbtRed + temp[x][y + 1].rgbtRed) / 6);
                image[x][y].rgbtGreen = roundf((temp[x][y +  1].rgbtGreen + temp[x + 1][y + 1].rgbtGreen + temp[x][y].rgbtGreen + temp[x - 1][y].rgbtGreen + temp[x + 1][y].rgbtGreen + temp[x][y + 1].rgbtGreen) / 6);
                image[x][y].rgbtBlue = roundf((temp[x][y + 1].rgbtBlue + temp[x + 1][y + 1].rgbtBlue + temp[x][y].rgbtBlue + temp[x - 1][y].rgbtBlue + temp[x + 1][y].rgbtBlue + temp[x][y - 1].rgbtBlue) / 6);
            }
            if (y == height && x != 0 && x != width) // Blur bottom edge
            {
                image[x][y].rgbtRed = roundf((temp[x][y - 1].rgbtRed + temp[x + 1][y].rgbtRed + temp[x][y].rgbtRed + temp[x - 1][y].rgbtRed + temp[x + 1][y].rgbtRed + temp[x - 1][y - 1].rgbtRed) / 6);
                image[x][y].rgbtGreen = roundf((temp[x][y - 1].rgbtGreen + temp[x + 1][y].rgbtGreen + temp[x][y].rgbtGreen + temp[x - 1][y].rgbtGreen + temp[x + 1][y].rgbtGreen + temp[x - 1][y - 1].rgbtGreen) / 6);
                image[x][y].rgbtBlue = roundf((temp[x][y - 1].rgbtBlue + temp[x + 1][y].rgbtBlue + temp[x][y].rgbtBlue + temp[x - 1][y].rgbtBlue + temp[x + 1][y].rgbtBlue + temp[x + 1][y - 1].rgbtBlue) / 6);
            }
            if (y != height && y != 0 && x != 0 && x != width) // Blur pixels in center
            {
                image[x][y].rgbtRed = roundf((temp[x - 1][y - 1].rgbtRed + temp[x - 1][y].rgbtRed + temp[x - 1][y + 1].rgbtRed + temp[x][y - 1].rgbtRed + temp[x][y].rgbtRed + temp[x][y + 1].rgbtRed + temp[x + 1][y - 1].rgbtRed + temp[x + 1][y].rgbtRed + temp[x + 1][y + 1].rgbtRed) / 9);
                image[x][y].rgbtGreen = roundf((temp[x - 1][y - 1].rgbtGreen + temp[x - 1][y].rgbtGreen + temp[x - 1][y + 1].rgbtGreen + temp[x][y - 1].rgbtGreen + temp[x][y].rgbtGreen + temp[x][y + 1].rgbtGreen + temp[x + 1][y - 1].rgbtGreen + temp[x + 1][y].rgbtGreen + temp[x + 1][y + 1].rgbtGreen) / 9);
                image[x][y].rgbtBlue = roundf((temp[x - 1][y - 1].rgbtBlue + temp[x - 1][y].rgbtBlue + temp[x - 1][y + 1].rgbtBlue + temp[x][y - 1].rgbtBlue+ temp[x][y].rgbtBlue + temp[x][y + 1].rgbtBlue + temp[x + 1][y - 1].rgbtBlue + temp[x + 1][y].rgbtBlue + temp[x + 1][y + 1].rgbtBlue) / 9);
            }

        }
    }
    return;
}

I also tried solving it in the following two ways but am still getting it wrong. What am I missing here?

   if (y < height && y > 0 && x > 0 && x < width) // Blur pixels in center
    {
        int d = 0;
        int c = 0;
        int w = 0;
        
        for (int t = x - 1; t <= x + 1; t++)
        {
            for (int s = y - 1; s <= y + 1; s++)
            {
                d = d + temp[t][s].rgbtRed;
                c = c + temp[t][s].rgbtBlue;
                w = w + temp[t][s].rgbtGreen;
            }
        }
        
        image[x][y].rgbtRed = roundf(d / 9.0);
        image[x][y].rgbtBlue = roundf(c / 9.0);
        image[x][y].rgbtGreen = roundf(w / 9.0);
    }
}

You must log in to answer this question.

Browse other questions tagged .