0

When I tried running this code under Check50, the value returned was completely off for some reason but I can't seem to figure out why. I then reduced the code to only calculating the rgbtBlue value but I still am not able to find out the logical error in this code. Really appreciate it if someone can point out to me what's wrong over here.

void blur(int height, int width, RGBTRIPLE image[height][width])
{
    RGBTRIPLE temp[height * width];
    *\\looking at the [a][b] pixel*
    for (int a = 0, k = 0; a < height; a++)
    {
        for (int b = 0; b < width; b++)
        {
            RGBTRIPLE t;
            t.rgbtBlue = 0;
            t.rgbtGreen = 0;
            t.rgbtRed = 0;
            int counter = 0;
            *\\calculating the average value for blue of the targetted pixel*
            for (int i = a - 1; i < a + 2; i++)
            {
                for (int j = b - 1; j < b + 2; j++)
                {
                    if (i >= 0 && j >= 0 && i < height && j < width)
                    {
                        t.rgbtBlue += image[i][j].rgbtBlue;
                        counter ++;
                    }
                }
            }
            temp[k].rgbtBlue = round((float)t.rgbtBlue / counter);
            k++;
        }
    }
    for (int x = 0, k = 0; x < height; x++)
    {
        for (int z = 0; z < width; z++)
        {
            image[x][z].rgbtBlue = temp[k].rgbtBlue;
            k++;
        }
    }
    return;
}

--------------------------------------UPDATE-------------------------------------

Hi guys I managed to fix it but I have no idea how it works. instead of declaring a RGBTRIPLE t, by changing it to 3 diff ints the code now works perfectly fine. I am thinking it is a problem with storing a BYTE data type into another BYTE data type. Can someone please explain to me why?

void blur(int height, int width, RGBTRIPLE image[height][width])
{
    RGBTRIPLE temp[height * width];
    for (int a = 0, k = 0; a < height; a++)
    {
        for (int b = 0; b < width; b++)
        {
            int red, green, blue;
            red = 0;
            green = 0;
            blue = 0;
            float counter = 0;
            for (int i = a - 1; i < a + 2; i++)
            {
                for (int j = b - 1; j < b + 2; j++)
                {
                    if (i >= 0 && j >= 0 && i < height && j < width)
                    {
                        red += image[i][j].rgbtRed;
                        blue += image[i][j].rgbtBlue;
                        green += image[i][j].rgbtGreen;
                        counter ++;
                    }
                }
            }
            temp[k].rgbtRed = round (red / counter);
            temp[k].rgbtGreen = round (green / counter);
            temp[k].rgbtBlue = round (blue / counter);
            k++;
        }
    }
    for (int x = 0, k = 0; x < height; x++)
    {
        for (int z = 0; z < width; z++)
        {
            image[x][z].rgbtRed = temp[k].rgbtRed;
            image[x][z].rgbtGreen = temp[k].rgbtGreen;
            image[x][z].rgbtBlue = temp[k].rgbtBlue;
            k++;
        }
    }
    return;
}
0

The RGBTRIPLE struct only can store 3 values from 0 to 255. When you add 4, 6 or 9 pixels together you'll problably get more than that.

2
  • Oh I see! No way I could have thought of this, thank you so much! – tangolin Jun 4 '20 at 10:20
  • The data type 'int' can store 2 or 4 bytes depending of the computer architecture. – Tritum Jun 4 '20 at 10:32

You must log in to answer this question.

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