1

I'd really appreciate any help that can be offered here. My code seems to output a blurred image perfectly, but fails every check50 check and I can't work out why...

void blur(int height, int width, RGBTRIPLE image[height][width])
{
    
    //Copy image over to new RGBTRIPLE
    
    RGBTRIPLE image_copy[height][width];
    for (int a = 0; a < height; a++)
    {
        for (int b = 0; b < width; b++)
        {
            image_copy[a][b] = image[a][b];
        }
    }
    
    //Cycle through each pixel of image
    
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            //Define 3x3 pixel array
            RGBTRIPLE arr[3][3];
            
            int i_proxy = i - 1;
            int j_proxy = j - 1;
            
            int null_count = 0;
            
            //Populate 3x3 array 
        
            for (int k = 0; k < 3; k++)
                {
                    for (int l = 0; l < 3; l++)
                    {
                        if (i_proxy < 0 || i_proxy == height || j_proxy < 0 || j_proxy == width)
                        {
                            arr[k][l].rgbtBlue = 0;
                            arr[k][l].rgbtGreen = 0;
                            arr[k][l].rgbtRed = 0;
                            null_count++;
                            j_proxy++;
                        }
                        else
                        {
                            arr[k][l] = image_copy[i_proxy][j_proxy];
                            j_proxy++;
                        }
                    }
                    i_proxy++;
                }
            int sum_red = 0, sum_blue = 0, sum_green = 0;
            for (int m = 0; m < 3; m++)
            {
                for (int n = 0; n < 3; n++)
                {
                    sum_red += arr[m][n].rgbtRed;
                    sum_blue += arr[m][n].rgbtBlue;
                    sum_green += arr[m][n].rgbtGreen;
                }
            }
            int pixel_type = 9 - null_count;
            image[i][j].rgbtBlue = round((float) (sum_blue / pixel_type));
            image[i][j].rgbtGreen = round((float) (sum_green / pixel_type));
            image[i][j].rgbtRed = round((float) (sum_red / pixel_type));
        }
    }
    return;
}

1 Answer 1

2

Please note I haven't validated this with testing and have only glanced at your code, but one thing that stands out to me:

round((float) (sum_blue / pixel_type))

Remember operator precedence here: the division will be performed first (as it is inside parentheses). At this point both values are integers so integer division will be performed and the remainder will be discarded. The result will then be cast to a float, but now it's irrelevant. Effectively the result will be exactly the same as:

sum_blue / pixel_type

If you remove the parentheses and cast one of the values to float, you will get the desired floating point division, and a meaningful result to perform round() on:

round((float) sum_blue / pixel_type)

You must log in to answer this question.

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