0

I was working on my code(sorry for it being so long), then I kept getting the same error and I don't understand why

:) blur correctly filters middle pixel
:) 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..."

This is my code: (I've checked it multiple times but I couldn't find an error...)

// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
    //Make a copy of the image and work on that. Then paste
    RGBTRIPLE copy[height][width];

    //Edges and corners
    //Corners
    copy[0][0].rgbtRed = (int)((float)(image[0][0].rgbtRed + image[0][1].rgbtRed + image[1][0].rgbtRed + image[1][1].rgbtRed) / 4 +
                               0.5);
    copy[0][0].rgbtGreen = (int)((float)(image[0][0].rgbtGreen + image[0][1].rgbtGreen + image[1][0].rgbtGreen + 
                                         image[1][1].rgbtGreen) / 4 + 0.5);
    copy[0][0].rgbtBlue = (int)((float)(image[0][0].rgbtBlue + image[0][1].rgbtBlue + image[1][0].rgbtBlue + image[1][1].rgbtBlue) / 4 +
                                0.5);

    copy[0][width - 1].rgbtRed = (int)((float)(image[0][width - 1].rgbtRed + image[0][width - 1].rgbtRed + image[0][width - 1].rgbtRed +
                                       image[0][width - 1].rgbtRed) / 4 + 0.5);
    copy[0][width - 1].rgbtGreen = (int)((float)(image[0][width - 1].rgbtGreen + image[0][width - 1].rgbtGreen + image[0][width -
                                         1].rgbtGreen + image[0][width - 1].rgbtGreen) / 4 + 0.5);
    copy[0][width - 1].rgbtBlue = (int)((float)(image[0][width - 1].rgbtBlue + image[0][width - 1].rgbtBlue + image[0][width -
                                        1].rgbtBlue + image[0][width - 1].rgbtBlue) / 4 + 0.5);

    copy[height - 1][0].rgbtRed = (int)((float)(image[height - 1][0].rgbtRed + image[height - 1][0].rgbtRed + image[height -
                                        1][0].rgbtRed + image[height - 1][0].rgbtRed) / 4 + 0.5);
    copy[height - 1][0].rgbtGreen = (int)((float)(image[height - 1][0].rgbtGreen + image[height - 1][0].rgbtGreen + image[height -
                                          1][0].rgbtGreen + image[height - 1][0].rgbtGreen) / 4 + 0.5);
    copy[height - 1][0].rgbtBlue = (int)((float)(image[height - 1][0].rgbtBlue + image[height - 1][0].rgbtBlue + image[height -
                                         1][0].rgbtBlue + image[height - 1][0].rgbtBlue) / 4 + 0.5);

    copy[height - 1][width - 1].rgbtRed = (int)((float)(image[height - 1][width - 1].rgbtRed + image[height - 1][width - 1].rgbtRed +
                                          image[height - 1][width - 1].rgbtRed + image[height - 1][width - 1].rgbtRed) / 4 + 0.5);
    copy[height - 1][width - 1].rgbtGreen = (int)((float)(image[height - 1][width - 1].rgbtGreen + image[height - 1][width -
                                            1].rgbtGreen + image[height - 1][width - 1].rgbtGreen + image[height - 1][width - 1].rgbtGreen) / 4 + 0.5);
    copy[height - 1][width - 1].rgbtBlue = (int)((float)(image[height - 1][width - 1].rgbtBlue + image[height - 1][width - 1].rgbtBlue +
                                           image[height - 1][width - 1].rgbtBlue + image[height - 1][width - 1].rgbtBlue) / 4 + 0.5);

    //Top
    for (int i = 1; i < width - 1; i++)
    {
        copy[0][i].rgbtRed = (int)((float)(image[0][i - 1].rgbtRed + image[0][i].rgbtRed + image[0][i + 1].rgbtRed + image[1][i - 1].rgbtRed
                                           + image[1][i].rgbtRed + image[1][i + 1].rgbtRed) / 6 + 0.5);
        copy[0][i].rgbtGreen = (int)((float)(image[0][i - 1].rgbtGreen + image[0][i].rgbtGreen + image[0][i + 1].rgbtGreen + image[1][i -
                                             1].rgbtGreen + image[1][i].rgbtGreen + image[1][i + 1].rgbtGreen) / 6 + 0.5);
        copy[0][i].rgbtBlue = (int)((float)(image[0][i - 1].rgbtBlue + image[0][i].rgbtBlue + image[0][i + 1].rgbtBlue + image[1][i -
                                            1].rgbtBlue + image[1][i].rgbtBlue + image[1][i + 1].rgbtBlue) / 6 + 0.5);
    }

    //Bottom
    for (int i = 1; i < width - 1; i++)
    {
        copy[height - 1][i].rgbtRed = (int)((float)(image[height - 1][i - 1].rgbtRed + image[height - 1][i].rgbtRed + image[height - 1][i +
                                            1].rgbtRed + image[height - 2][i - 1].rgbtRed + image[height - 2][i].rgbtRed + image[height - 2][i + 1].rgbtRed) / 6 + 0.5);
        copy[height - 1][i].rgbtGreen = (int)((float)(image[height - 1][i - 1].rgbtGreen + image[height - 1][i].rgbtGreen + image[height -
                                              1][i + 1].rgbtGreen + image[height - 2][i - 1].rgbtGreen + image[height - 2][i].rgbtGreen + image[height - 2][i + 1].rgbtGreen) / 6
                                              + 0.5);
        copy[height - 1][i].rgbtBlue = (int)((float)(image[height - 1][i - 1].rgbtBlue + image[height - 1][i].rgbtBlue + image[height - 1][i
                                             + 1].rgbtBlue + image[height - 2][i - 1].rgbtBlue + image[height - 2][i].rgbtBlue + image[height - 2][i + 1].rgbtBlue) / 6 + 0.5);
    }

    //Left
    for (int i = 1; i < height - 1; i++)
    {
        copy[i][0].rgbtRed = (int)((float)(image[i - 1][0].rgbtRed + image[i][0].rgbtRed + image[i + 1][0].rgbtRed + image[i - 1][1].rgbtRed
                                           + image[i][1].rgbtRed + image[i + 1][1].rgbtRed) / 6 + 0.5);
        copy[i][0].rgbtGreen = (int)((float)(image[i - 1][0].rgbtGreen + image[i][0].rgbtGreen + image[i + 1][0].rgbtGreen + image[i -
                                             1][1].rgbtGreen + image[i][1].rgbtGreen + image[i + 1][1].rgbtGreen) / 6 + 0.5);
        copy[i][0].rgbtBlue = (int)((float)(image[i - 1][0].rgbtBlue + image[i][0].rgbtBlue + image[i + 1][0].rgbtBlue + image[i -
                                            1][1].rgbtBlue + image[i][1].rgbtBlue + image[i + 1][1].rgbtBlue) / 6 + 0.5);
    }

    //Right
    for (int i = 1; i < height - 1; i++)
    {
        copy[i][width - 1].rgbtRed = (int)((float)(image[i - 1][width - 1].rgbtRed + image[i][width - 1].rgbtRed + image[i + 1][width -
                                           1].rgbtRed + image[i - 1][width - 2].rgbtRed + image[i][width - 2].rgbtRed + image[i + 1][width - 2].rgbtRed) / 6 + 0.5);
        copy[i][width - 1].rgbtGreen = (int)((float)(image[i - 1][width - 1].rgbtGreen + image[i][width - 1].rgbtGreen + image[i + 1][width
                                             - 1].rgbtGreen + image[i - 1][width - 2].rgbtGreen + image[i][width - 2].rgbtGreen + image[i + 1][width - 2].rgbtGreen) / 6 + 0.5);
        copy[i][width - 1].rgbtBlue = (int)((float)(image[i - 1][width - 1].rgbtBlue + image[i][width - 1].rgbtBlue + image[i + 1][width -
                                            1].rgbtBlue + image[i - 1][width - 2].rgbtBlue + image[i][width - 2].rgbtBlue + image[i + 1][width - 2].rgbtBlue) / 6 + 0.5);
    }

    //Normal pixels
    for (int i = 1; i < height - 1; i++)
    {
        for (int j = 1; j < width - 1; j++)
        {
            copy[i][j].rgbtRed = (int)((float)(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].rgbtRed + image[i][j + 1].rgbtRed + image[i + 1][j - 1].rgbtRed + image[i + 1][j].rgbtRed + image[i +
                                                       1][j + 1].rgbtRed) / 9 + 0.5);
            copy[i][j].rgbtGreen = (int)((float)(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].rgbtGreen + image[i][j + 1].rgbtGreen + image[i + 1][j - 1].rgbtGreen + image[i +
                                                         1][j].rgbtGreen + image[i + 1][j + 1].rgbtGreen) / 9 + 0.5);
            copy[i][j].rgbtBlue = (int)((float)(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].rgbtBlue + image[i][j + 1].rgbtBlue + image[i + 1][j - 1].rgbtBlue + image[i + 1][j].rgbtBlue
                                                + image[i + 1][j + 1].rgbtBlue) / 9 + 0.5);
        }
    }
    
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            image[i][j] = copy[i][j];
        }
    }
    return;
}

The code works as follows: Make a copy of the image of same size, put the 4 corners first(in the copy), then the left, right, top, bottom edges in, then the pixels that are not part of the above("normal") inside, then, paste all the contents of the copy into the original image.

You must log in to answer this question.

Browse other questions tagged .