1

This is the output and input image that my code generates: https://i.sstatic.net/Y2Gpg.jpg

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

    RGBTRIPLE newImage[height][width];

    for (int h = 0; h < height; h++)
    {
        for (int w = 0; w < width; w++)
        {
            BYTE redAvg = 0;
            BYTE greenAvg = 0;
            BYTE blueAvg = 0;
            double count = 0.0;
        
            for (int y = -1; y <= 1; y++)
            {
                for (int x = -1; x <= 1; x++)
                {
                    int h_1 = h + y;
                    int w_1 = w + x;

                    if (h_1 >= 0 && h_1 < height && w_1 >= 0 && w_1 < width)
                    {
                        RGBTRIPLE img = image[h_1][w_1];
                        count++;
                        redAvg += img.rgbtRed;
                        greenAvg += img.rgbtGreen;
                        blueAvg += img.rgbtBlue;
                    }
                }
            }
        
            newImage[h][w].rgbtRed = round(redAvg/count);
            newImage[h][w].rgbtGreen = round(greenAvg/count);
            newImage[h][w].rgbtBlue = round(blueAvg/count);
        }
    }

    for (int x = 0; x < height; x++)
    {
        for (int y = 0; y < width; y++)
        {
            image[x][y] = newImage[x][y];
        }
    }

    return;
}

2 Answers 2

4

Got the solution. Using the BYTE type to store the color values resulted in overflow. Solving by using int or double for redAvg, greenAvg, and blueAvg.

2
  • can you elaborate this? I had the exact issue and this comment solved my code. I don't understand why using BYTE results to overflow when BYTE is just a uint8_t?
    – PalaDean
    Commented Aug 28, 2022 at 12:59
  • @PalaDean uint8_t has a maximum value of 255. During this part of the code redAvg += img.rgbtRed; greenAvg += img.rgbtGreen; blueAvg += img.rgbtBlue; the value of, say, greenAvg goes well beyond 255, resulting in the overflow. During which, the value will be wrapped around to 0, resulting in the darker image.
    – user39598
    Commented Jul 11, 2023 at 17:31
0

why did you store the averages for each colour in a new image, why not just store it in image[h][w]?

1
  • blur looks at pixels surrounding each pixel. If you modify a pixel and then analyze an adjacent pixel you will be looking at incorrect values. Commented Apr 16, 2020 at 4:39

You must log in to answer this question.

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