0

I'm currently stuck on cs50's pset4 filter (less comfortable). The program compiles perfectly. I have tried out the first three parts of the code (grayscale, sepia & reflection) and their output has been as desired; however, whenever I try out blur, I keep getting hit with a seg fault. Could someone please help me identify where the error(s) lies?

void blur(int height, int width, RGBTRIPLE image[height][width])
{
    for (int i = 0; i <height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            int c = i + 1;
            int d = j + 1;
            int e = i + 2;
            int f = j + 2;

            float blurred_blue = 0;
            float blurred_green = 0;
            float blurred_red = 0;
            int z = 0;

            if (height - i == 1 && width - j >= 2)
            {
                for (int a = i - 1; a < c; a++)
                {
                    for (int b = j - 1; b < f; b++)
                    {
                        blurred_blue = blurred_blue + image[a][b].rgbtBlue;
                        blurred_green = blurred_green + image[a][b].rgbtGreen;
                        blurred_red = blurred_red + image[a][b].rgbtRed;
                        z++;
                    }
                }
            }
            else if (width - j == 1 && height - i >= 2)
            {
                for(int a = i - 1; a < e; a++)
                {
                    for(int b = j - 1; b < d; b++)
                    {
                        blurred_blue = blurred_blue + image[a][b].rgbtBlue;
                        blurred_green = blurred_green + image[a][b].rgbtGreen;
                        blurred_red = blurred_red + image[a][b].rgbtRed;
                        z++;
                    }
                }
            }
            else
            {
                for(int a = i - 1; a < e; a++)
                {
                    for(int b = j - 1; b < f; b++)
                    {
                        blurred_blue = blurred_blue + image[a][b].rgbtBlue;
                        blurred_green = blurred_green + image[a][b].rgbtGreen;
                        blurred_red = blurred_red + image[a][b].rgbtRed;
                        z++;
                    }
                }
            }
            image[i][j].rgbtBlue = round (blurred_blue / z);
            image[i][j].rgbtGreen = round (blurred_green / z);
            image[i][j].rgbtRed = round (blurred_red / z);
        }
    }
    return;
}
Enis Arik
  • 665
  • 10
  • 24
  • Likely that you try to read `image[-1][-1]` which is not allowed and may cause segmentation fault. This happens in the last for loop. `a` and `b` are assigned to `-1` in there. You want them to start from `0`? – Enis Arik Jun 15 '21 at 07:33
  • `if (height - i == 1 && width - j >= 2)` <<-- strange condition. Which values of height (and i and j) would evaluate as true ? – wildplasser Jun 15 '21 at 11:15
  • Thank you very much, Enis Arik. I've fixed that bug but there still appears to be a problem. I'll keep on working on the program and see if I can figure it out. – Hamster1303 Jun 16 '21 at 07:43
  • wildplasser, the idea on using that condition is that if height - i == 1 and width - j >= 2 then we would in the last row but not the last column. – Hamster1303 Jun 16 '21 at 07:46

0 Answers0