I changed this a few times, I tried to condense it, but now the NULL check gives me an error message // Blur image void blur(int height, int width, RGBTRIPLE image[height][width]) {

RGBTRIPLE copy[height][width];
int i = 0;
int j = 0;

copy[i][j] = image[i][j];

for (i = 0; i < height; i++)
    for (j = 0; j < width; j++)
        image[i][j] = copy[i][j];

        int blue = 0;
        int red = 0;
        int green = 0;

        float block = 1.0;

        //modify 3x3 block
        for(int n = -1; n < 1; n++)
            for(int m = -1; m < 1; m++)
                //check if block is valid
                if(copy[i+n][j+m] ==NULL)
                    copy[i+n][j+m].rgbtBlue =0;
                    copy[i+n][j+m].rgbtRed =0;
                    blue += copy[i+n][j+m].rgbtBlue;
                    red += copy[i+n][j+m].rgbtRed;
                    green += copy[i+n][j+m].rgbtGreen;

            //new pixel
            image[i][j].rgbtBlue = round((blue / block));
            image[i][j].rgbtRed = round((red / block));
            image[i][j].rgbtGreen = round((green / block));



1 Answer 1


I took a quick look at it and one thing jumped out at me. The limits for checking right and bottom sides have a problem. They're going off the edge of the array.

Let's take one of them. You should be able to apply it to the rest easily. Look at the following:

    //adding middle right
    if (j < width)
        d += image[i][j + 1].rgbtBlue;
        t += image[i][j + 1].rgbtRed;
        k += image[i][j + 1].rgbtGreen;

Say that you have a 3x3 pixel image, so the array dimensions can be from 0 to 2. For i=1 and j=2, this code will try to access image[1][3].rgbtBlue, etc. That's off the edge of the array.

You should be able to figure it all out from this. If not, leave a comment. ;-)

Programming tips:

DON'T use single letter var names (d,t,k)! What if you had to find every occurrence of the variable "t" in 1000 lines of code?!?!?!?! Use descriptive names. The only place to use single letters is in for loop counters, like i, j, k.

You can copy entire structures as a whole. You don't have to copy individual elements. For example, your code,

    image[i][j].rgbtBlue = copy[i][j].rgbtBlue;
    image[i][j].rgbtRed = copy[i][j].rgbtRed;
    image[i][j].rgbtGreen = copy[i][j].rgbtGreen;

can be replaced with one line: image[i][j] = copy[i][j] ;

happy programming!

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

  • I condensed it into a 2nd loop sinceI'm not sure how to turn it into a recursion. but It still gives me an error message when I try to check for NULL values in the image, I posted the edit
    – Stevek
    Aug 21, 2022 at 13:58

You must log in to answer this question.

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