0

This code works in Check50 for all tests except the 3x3 and 4x4 image. If anyone has any suggestions, I would appreciate it. Thank you!

// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
    //my code begins here

    //initiate variables
    float red = 0;
    float green = 0;
    float blue = 0;
    float avg_red = 0;
    float avg_green = 0;
    float avg_blue = 0;
    int starth = 0;
    int endh = 0;
    int startw = 0;
    int endw = 0;
    float count = 0;

    RGBTRIPLE temp[height][width];

    //loop through each row
    for (int h = 0; h < height; h++)
    {
        //loop through the width of each row
        for (int w = 0; w < width; w++)
        {
            //if statement for top left corner: h == 0, w ==0
            if (h == 0 && w == 0)
            {
                starth = h;
                endh = h + 2;
                startw = w;
                endw = w + 2;
            }
            //if statement for top row: h == 0, w > 0
            if (h == 0 && w > 0)
            {
                starth = h;
                endh = h + 2;
                startw = w - 1;
                endw = w + 2;
            }
            //if statement for top right corner: h == 0, w == width
            if (h == 0 && w == width)
            {
                starth = h;
                endh = h + 2;
                startw = w - 1;
                endw = w + 1;
            }
            //if statement for left edge: h > 0, w == 0
            if (h > 0 && w == 0)
            {
                starth = h - 1;
                endh = h + 2;
                startw = w;
                endw = w + 2;
            }
            //if statement for right edge: h > 0, w == width
            if (h > 0 && w == width)
            {
                starth = h - 1;
                endh = h + 2;
                startw = w - 1;
                endw = w + 1;
            }
            //if statement for lower left corner: h == height, w == 0
            if (h == height && w == 0)
            {
                starth = h - 1;
                endh = h + 1;
                startw = w;
                endw = w + 2;
            }
            //if statement for bottom row: h == height, w > 0
            if (h == height && w > 0)
            {
                starth = h - 1;
                endh = h + 1;
                startw = w - 1;
                endw = w + 2;
            }
            //if statement for lower right corner: h == height, w == width
            if (h == height && w == width)
            {
                starth = h - 1;
                endh = h + 1;
                startw = w - 1;
                endw = w + 1;
            }
            //if statement for middle of the image, h > 0 && h < height && w > 0 && w < width, middle of the image
            if (h > 0 && h < height && w > 0 && w < width)
            {
                starth = h - 1;
                endh = h + 2;
                startw = w - 1;
                endw = w + 2;
            }


            red = 0.0;
            green = 0.0;
            blue = 0.0;
            count = 0.0;


            for (int x = starth; x < endh; x++)
            {
                for (int y = startw; y < endw; y++)
                {
                    red = red + image[x][y].rgbtRed;
                    green = green + image[x][y].rgbtGreen;
                    blue = blue + image[x][y].rgbtBlue;
                    count++;
                }
            }

            avg_red = round((float)(red / count));
            avg_green = round((float)(green / count));
            avg_blue = round((float)(blue / count));

            temp[h][w].rgbtRed = (int)avg_red;
            temp[h][w].rgbtGreen = (int)avg_green;
            temp[h][w].rgbtBlue = (int)avg_blue;

        }

    }
    //loop through image[height][width] here and set each pixel to temp[height][width]
    //loop through each row
    for (int h = 0; h < height; h++)
    {
        //loop through the width of each row
        for (int w = 0; w < width; w++)
        {
            image[h][w].rgbtRed = temp[h][w].rgbtRed;
            image[h][w].rgbtGreen = temp[h][w].rgbtGreen;
            image[h][w].rgbtBlue = temp[h][w].rgbtBlue;
        }

    }
    //my code ends here
    return;
}

1 Answer 1

0

For your if conditions, remember that arrays start with 0 index, and the last pixel is not width/height, but width - 1/height - 1.

1
  • Thank you very much. That seems to have fixed the problem. Simple, but important to remember! Thanks! Jun 4, 2020 at 3:00

You must log in to answer this question.

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