I am trying to implement my idea on Blur function. My main assumption is to get this done using nested 4 for loops as follow:

void blur(int height, int width, RGBTRIPLE image[height][width])
    //list of int's used in the code
    int counter, counter1, sumred = 0, sumblue = 0, sumgreen = 0, t = 0, y = 0;
    RGBTRIPLE copy[height][width];
    for (int i = 0; i < height; i++)
        for (int b = 0; b < width; b++)
            //copy of image to read
            copy[i][b] = image[i][b];
            //counters reset for next pixel
            counter = 0;
            counter1 = 0;
            for (int c = i - 1; c < i + 2; c++)
                for (int z = b - 1; z < b + 2; z++)
                    //program has to check 9 pixels (adjacent to actual one including this one)
                    //boundary check - ignore pixels beyond the image width and height
                    if (c < 0 || c > height || z <  0 || z > height)
                    //count number of valid pixels
                    //sum colour value of each checked pixel
                    sumred += image[c][z].rgbtRed;
                    sumgreen += image[c][z].rgbtGreen;
                    sumblue += image[c][z].rgbtBlue;
                    //after check of all (valid and nonvalid) pixels find average
                    if (counter1 == 9)
                        copy[i][b].rgbtRed = round(sumred/counter);
                        copy[i][b].rgbtGreen = round(sumgreen/counter);
                        copy[i][b].rgbtBlue = round(sumblue/counter);
                    //copy blurred pixels into original image
                    image[i][b] = copy[i][b];


Unortunately output is way beyond expected. What is more while compiling code it returns following errors:

helpers.c:147:46: runtime error: 370 is outside the range of representable values of type 'unsigned char'
helpers.c:148:48: runtime error: 359 is outside the range of representable values of type 'unsigned char'
helpers.c:149:47: runtime error: 286 is outside the range of representable values of type 'unsigned char'

Check50 returns one condition correctly: :) blur correctly filters pixel in corner

What are your advices or hints for me in this case? I start to wonder if I can squeeze it all just in 4 nested for loops with boundary check conditions. Does compiler return error related to used types of data? Should I use double or long instead of int?

1 Answer 1


I few things, you should not update any pixel of the original image in any of the 4 for loops. You can use the values from the original image, do the calculations and store the results in the copy image, then the last thing to do is to copy the copy image to the original image. Hope this helps

You must log in to answer this question.

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