0

This code won't win any style contests, however comma all Check50 test pass except the 3x3 and 4x4. I made a copy of the image, blurred original values, and copied new values to the original image at the end and still can't pass all Check50 tests. I verified my [i][j] calculations and if statements. Check50 results here: https://submit.cs50.io/check50/dce713d3767d5a5df99c3674e58bc300918fa4b4

Any feedback is greatly appreciated.



void blur(int height, int width, RGBTRIPLE image[height][width])
{
    RGBTRIPLE image_copy[height][width];

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            if (i == 0 && j == 0) // Upper left corner pixel
            {
                int blue = round((image[i][j].rgbtBlue + image[i][j+1].rgbtBlue + image[i+1][j].rgbtBlue + image[i+1][j+1].rgbtBlue) / 4.0);
                int green = round((image[i][j].rgbtGreen + image[i][j+1].rgbtGreen + image[i+1][j].rgbtGreen + image[i+1][j+1].rgbtGreen) / 4.0);
                int red = round((image[i][j].rgbtRed + image[i][j+1].rgbtRed + image[i+1][j].rgbtRed + image[i+1][j+1].rgbtRed) / 4.0);

                image_copy[i][j].rgbtBlue = blue;
                image_copy[i][j].rgbtGreen = green;
                image_copy[i][j].rgbtRed = red;
            }

            if ( i == 0 && j == width) //Upper right corner pixel
            {
                int blue = round((image[i][j].rgbtBlue + image[i][j-1].rgbtBlue + image[i+1][j-1].rgbtBlue + image[i+1][j].rgbtBlue) / 4.0);
                int green = round((image[i][j].rgbtGreen + image[i][j-1].rgbtGreen + image[i+1][j-1].rgbtGreen + image[i+1][j].rgbtGreen) / 4.0);
                int red = round((image[i][j].rgbtRed + image[i][j-1].rgbtRed + image[i+1][j-1].rgbtRed + image[i+1][j].rgbtRed) / 4.0);

                image_copy[i][j].rgbtBlue = blue;
                image_copy[i][j].rgbtGreen = green;
                image_copy[i][j].rgbtRed = red;
            }

            if ( i == height && j == 0) // Bottom left pixel
            {
                int blue = round((image[i][j].rgbtBlue + image[i-1][j].rgbtBlue + image[i-1][j+1].rgbtBlue + image[i][j+1].rgbtBlue) / 4.0);
                int green = round((image[i][j].rgbtGreen + image[i-1][j].rgbtGreen + image[i-1][j+1].rgbtGreen + image[i][j+1].rgbtGreen) / 4.0);
                int red = round((image[i][j].rgbtRed + image[i-1][j].rgbtRed + image[i-1][j+1].rgbtRed + image[i][j+1].rgbtRed) / 4.0);

                image_copy[i][j].rgbtBlue = blue;
                image_copy[i][j].rgbtGreen = green;
                image_copy[i][j].rgbtRed = red;
            }

            if ( i == height && j == width) // Bottom right pixel
            {
                int blue = round((image[i][j].rgbtBlue + image[i-1][j].rgbtBlue + image[i-1][j-1].rgbtBlue + image[i][j-1].rgbtBlue) / 4.0);
                int green = round((image[i][j].rgbtGreen + image[i-1][j].rgbtGreen + image[i-1][j-1].rgbtGreen + image[i][j-1].rgbtGreen) / 4.0);
                int red = round((image[i][j].rgbtRed + image[i-1][j].rgbtRed + image[i-1][j-1].rgbtRed + image[i][j-1].rgbtRed) / 4.0);

                image_copy[i][j].rgbtBlue = blue;
                image_copy[i][j].rgbtGreen = green;
                image_copy[i][j].rgbtRed = red;
            }

            if ( i < height && i > 0 && j == 0) // Left column of pixels not including corners.
            {
                int blue = round((image[i][j].rgbtBlue + image[i-1][j].rgbtBlue + image[i-1][j+1].rgbtBlue + image[i][j+1].rgbtBlue + image[i+1][j+1].rgbtBlue + image[i+1][j].rgbtBlue) / 6.0);
                int green = round((image[i][j].rgbtGreen + image[i-1][j].rgbtGreen + image[i-1][j+1].rgbtGreen + image[i][j+1].rgbtGreen + image[i+1][j+1].rgbtGreen + image[i+1][j].rgbtGreen) / 6.0);
                int red = round((image[i][j].rgbtRed + image[i-1][j].rgbtRed + image[i-1][j+1].rgbtRed + image[i][j+1].rgbtRed + image[i+1][j+1].rgbtRed + image[i+1][j].rgbtRed) / 6.0);

                image_copy[i][j].rgbtBlue = blue;
                image_copy[i][j].rgbtGreen = green;
                image_copy[i][j].rgbtRed = red;
            }

            if ( i < height && i > 0 && j == width) // Right column of pixels not including corners.
            {
                int blue = round((image[i][j].rgbtBlue + image[i-1][j].rgbtBlue + image[i-1][j-1].rgbtBlue + image[i][j-1].rgbtBlue + image[i+1][j-1].rgbtBlue + image[i+1][j].rgbtBlue) / 6.0);
                int green = round((image[i][j].rgbtGreen + image[i-1][j].rgbtGreen + image[i-1][j-1].rgbtGreen + image[i][j-1].rgbtGreen + image[i+1][j-1].rgbtGreen + image[i+1][j].rgbtGreen) / 6.0);
                int red = round((image[i][j].rgbtRed + image[i-1][j].rgbtRed + image[i-1][j-1].rgbtRed + image[i][j-1].rgbtRed + image[i+1][j-1].rgbtRed + image[i+1][j].rgbtRed) / 6.0);

                image_copy[i][j].rgbtBlue = blue;
                image_copy[i][j].rgbtGreen = green;
                image_copy[i][j].rgbtRed = red;
            }

            if (i == 0 && j > 0 && j < width)  // Top row of pixels not including corners.
            {
                int blue = round((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 + image[i][j+1].rgbtBlue) / 6.0);
                int green = round((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 + image[i][j+1].rgbtGreen) / 6.0);
                int red = round((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 + image[i][j+1].rgbtRed) / 6.0);

                image_copy[i][j].rgbtBlue = blue;
                image_copy[i][j].rgbtGreen = green;
                image_copy[i][j].rgbtRed = red;
            }

            if ( i == height && j > 0 && j < width)  // Bottom row of pixels not including corners.
            {
                int blue = round((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 + image[i][j+1].rgbtBlue) / 6.0);
                int green = round((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 + image[i][j+1].rgbtGreen) / 6.0);
                int red = round((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 + image[i][j+1].rgbtRed) / 6.0);

                image_copy[i][j].rgbtBlue = blue;
                image_copy[i][j].rgbtGreen = green;
                image_copy[i][j].rgbtRed = red;
            }

            if (i > 0 && i < height && j > 0 && j < width) //Center pixels with 8 pixels surrounding 1 pixel -->  Total 9 pixels
            {
                int blue = round((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);
                int green = round((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);
                int red = round((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);

                image_copy[i][j].rgbtBlue = blue;
                image_copy[i][j].rgbtGreen = green;
                image_copy[i][j].rgbtRed = red;
            }
        }
    }

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            image[i][j] = image_copy[i][j];
        }
    }

}

1 Answer 1

0

It's notable that all the bad pixels seem to be located on the upper limits of width and height.

Let's look at one of them:

if ( i == 0 && j == width)

Note the limit on j. Wouldn't setting j = width be outside the limits of the array, by 1?

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

1

You must log in to answer this question.

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