0

I am working on the cs50 pset4 blur function, but I seem to be getting runtime error index out of bounds for the top and bottom pixels. Here is the code:

// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
    RGBTRIPLE copy[height][width];

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            if (i == 0 && (0 < j < width-1))//top, no corner
            {
                int blurred = round((image[i][j+1].rgbtRed + image[i+1][j+1].rgbtRed + image[i+1][j].rgbtRed + image[i+1][j-1].rgbtRed + image[i][j-1].rgbtRed + image[i][j].rgbtRed)/6);
                int blurgreen = round((image[i][j+1].rgbtGreen + image[i+1][j+1].rgbtGreen + image[i+1][j].rgbtGreen + image[i+1][j-1].rgbtGreen + image[i][j-1].rgbtGreen + image[i][j].rgbtGreen)/6);
                int blurblue = round((image[i][j+1].rgbtBlue + image[i+1][j+1].rgbtBlue + image[i+1][j].rgbtBlue + image[i+1][j-1].rgbtBlue + image[i][j-1].rgbtBlue + image[i][j].rgbtBlue)/6);
                copy[i][j].rgbtRed = blurred;
                copy[i][j].rgbtGreen = blurgreen;
                copy[i][j].rgbtBlue = blurblue;
                
            }
            
            else if (0 < i < height-1 && j == 0)//side left, no corner
            {
                int blurred = round((image[i-1][j].rgbtRed + image[i-1][j+1].rgbtRed + image[i][j+1].rgbtRed + image[i+1][j+1].rgbtRed) + image[i+1][j].rgbtRed + image[i][j].rgbtRed/6);
                int blurgreen = round((image[i-1][j].rgbtGreen + image[i-1][j+1].rgbtGreen + image[i][j+1].rgbtGreen + image[i+1][j+1].rgbtGreen + image[i+1][j].rgbtGreen + image[i][j].rgbtGreen)/6);
                int blurblue = round((image[i-1][j].rgbtBlue + image[i-1][j+1].rgbtBlue + image[i][j+1].rgbtBlue + image[i+1][j+1].rgbtBlue + image[i+1][j].rgbtBlue + image[i][j].rgbtBlue )/6);
                copy[i][j].rgbtRed = blurred;
                copy[i][j].rgbtGreen = blurgreen;
                copy[i][j].rgbtBlue = blurblue;
            }
            
            else if (i == height-1 && 0 < j < width-1)//bottom, no corner
            {
                int blurred = round((image[i][j-1].rgbtRed + image[i-1][j-1].rgbtRed + image[i-1][j].rgbtRed + image[i-1][j+1].rgbtRed + image[i][j+1].rgbtRed + image[i][j].rgbtRed)/6);
                int blurgreen = round((image[i][j-1].rgbtGreen + image[i-1][j-1].rgbtGreen + image[i-1][j].rgbtGreen + image[i-1][j+1].rgbtGreen + image[i][j+1].rgbtGreen + image[i][j].rgbtGreen)/6);
                int blurblue = round((image[i][j-1].rgbtBlue + image[i-1][j-1].rgbtBlue + image[i-1][j].rgbtBlue + image[i-1][j+1].rgbtBlue + image[i][j+1].rgbtBlue + image[i][j].rgbtBlue)/6);
                copy[i][j].rgbtRed = blurred;
                copy[i][j].rgbtGreen = blurgreen;
                copy[i][j].rgbtBlue = blurblue;
            }
            
            else if (0 < i < height-1 && j == width-1)//right, no corner
            {
                int blurred = round((image[i+1][j].rgbtRed + image[i+1][j-1].rgbtRed + image[i][j-1].rgbtRed + image[i-1][j-1].rgbtRed + image[i-1][j].rgbtRed + image[i][j].rgbtRed)/6);
                int blurgreen = round((image[i+1][j].rgbtGreen + image[i+1][j-1].rgbtGreen + image[i][j-1].rgbtGreen + image[i-1][j-1].rgbtGreen + image[i-1][j].rgbtGreen + image[i][j].rgbtGreen)/6);
                int blurblue = round((image[i+1][j].rgbtBlue + image[i+1][j-1].rgbtBlue + image[i][j-1].rgbtBlue + image[i-1][j-1].rgbtBlue + image[i-1][j].rgbtBlue + image[i][j].rgbtBlue)/6);
                copy[i][j].rgbtRed = blurred;
                copy[i][j].rgbtGreen = blurgreen;
                copy[i][j].rgbtBlue = blurblue;
            }
            
            else if (i == 0 && j == 0)//top left corner
            {
                int blurred = round((image[i][j+1].rgbtRed + image[i+1][j].rgbtRed + image[i+1][j+1].rgbtRed + image[i][j].rgbtRed)/4);
                int blurgreen = round((image[i][j+1].rgbtGreen + image[i+1][j].rgbtGreen + image[i+1][j+1].rgbtGreen + image[i][j].rgbtGreen)/4);
                int blurblue = round((image[i][j+1].rgbtBlue + image[i+1][j].rgbtBlue + image[i+1][j+1].rgbtBlue + image[i][j].rgbtGreen)/4);
                copy[i][j].rgbtRed = blurred;
                copy[i][j].rgbtGreen = blurgreen;
                copy[i][j].rgbtBlue = blurblue;
            }
            
            else if (i == height-1 && j == width-1)//bottom right corner
            {
                int blurred = round((image[i][j].rgbtRed + image[i][j-1].rgbtRed + image[i-1][j].rgbtRed + image[i-1][j-1].rgbtRed)/4);
                int blurgreen = round((image[i][j].rgbtGreen + image[i][j-1].rgbtGreen + image[i-1][j].rgbtGreen + image[i-1][j-1].rgbtGreen)/4);
                int blurblue = round((image[i][j].rgbtBlue + image[i][j-1].rgbtBlue + image[i-1][j].rgbtBlue + image[i-1][j-1].rgbtGreen)/4);
                copy[i][j].rgbtRed = blurred;
                copy[i][j].rgbtGreen = blurgreen;
                copy[i][j].rgbtBlue = blurblue;;
            }
            
            else if (i == height-1 && j == 0)//bottom left corner
            {
                int blurred = round((image[i-1][j].rgbtRed + image[i-1][j+1].rgbtRed + image[i][j+1].rgbtRed + image[i][j].rgbtRed)/4);
                int blurgreen = round((image[i-1][j].rgbtGreen + image[i-1][j+1].rgbtGreen + image[i][j+1].rgbtGreen + image[i][j].rgbtGreen)/4);
                int blurblue = round((image[i-1][j].rgbtBlue + image[i-1][j+1].rgbtBlue + image[i][j+1].rgbtBlue + image[i][j].rgbtGreen)/4);
                copy[i][j].rgbtRed = blurred;
                copy[i][j].rgbtGreen = blurgreen;
                copy[i][j].rgbtBlue = blurblue;
            }
            
            else if (i == 0 && j == width-1)//top right corner
            {
                int blurred = round((image[i+1][j].rgbtRed + image[i+1][j-1].rgbtRed + image[i][j-1].rgbtRed + image[i][j].rgbtRed)/4);
                int blurgreen = round((image[i+1][j].rgbtGreen + image[i+1][j-1].rgbtGreen + image[i][j-1].rgbtGreen + image[i][j].rgbtGreen)/4);
                int blurblue = round((image[i+1][j].rgbtBlue + image[i+1][j-1].rgbtBlue + image[i][j-1].rgbtBlue + image[i][j].rgbtGreen)/4);
                copy[i][j].rgbtRed = blurred;
                copy[i][j].rgbtGreen = blurgreen;
                copy[i][j].rgbtBlue = blurblue;
            }
            
            else//inside
            {
                int blurred = round((image[i][j].rgbtRed + image[i][j-1].rgbtRed + image[i-1][j-1].rgbtRed + image[i-1][j].rgbtRed) + image[i-1][j+1].rgbtRed + image[i][j+1].rgbtRed + image[i+1][j+1].rgbtRed + image[i+1][j].rgbtRed + image[i+1][j-1].rgbtRed/9);
                int blurgreen = round((image[i][j].rgbtGreen + image[i][j-1].rgbtGreen + image[i-1][j-1].rgbtGreen + image[i-1][j].rgbtGreen + image[i-1][j+1].rgbtGreen + image[i][j+1].rgbtGreen + image[i+1][j+1].rgbtGreen + image[i+1][j].rgbtGreen + image[i+1][j-1].rgbtGreen)/9);
                int blurblue = round((image[i][j].rgbtBlue + image[i][j-1].rgbtBlue + image[i-1][j-1].rgbtBlue + image[i-1][j].rgbtGreen + image[i-1][j+1].rgbtGreen + image[i][j+1].rgbtGreen + image[i+1][j+1].rgbtGreen + image[i+1][j].rgbtGreen + image[i+1][j-1].rgbtGreen)/9);
                copy[i][j].rgbtRed = blurred;
                copy[i][j].rgbtGreen = blurgreen;
                copy[i][j].rgbtBlue = blurblue;
            }
        }
    }
    
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            image[i][j].rgbtRed = copy[i][j].rgbtRed;
            image[i][j].rgbtGreen = copy[i][j].rgbtGreen;
            image[i][j].rgbtBlue = copy[i][j].rgbtBlue;
        }
    }
    return;
}

Here are the errors:

helpers.c:93:112: runtime error: index -1 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:93:138: runtime error: index -1 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:94:120: runtime error: index -1 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:94:148: runtime error: index -1 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:95:116: runtime error: index -1 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:95:143: runtime error: index -1 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:93:38: runtime error: index 600 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:93:62: runtime error: index 600 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:94:40: runtime error: index 600 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:94:66: runtime error: index 600 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:95:39: runtime error: index 600 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:95:64: runtime error: index 600 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:114:112: runtime error: index 600 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:114:138: runtime error: index 600 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:115:120: runtime error: index 600 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:115:148: runtime error: index 600 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:116:116: runtime error: index 600 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:116:143: runtime error: index 600 out of bounds for type 'RGBTRIPLE [width]'

I am at a loss here, cannot figure out why it happens. Can anyone assist me?

1

Some of your conditions need to change. There's no 'between' syntax like in maths, so (0 < j < width-1) won't work in c.

You need to split this into something like (j > 0 && j < width - 1), and make similar changes to other conditions.

  • Thank you for the comment, I will try and comment as soon as possible. – Krasimir Yankov Kolev Aug 25 '20 at 18:00
  • I have just edited the conditions using && (i > 0 && i < height - 1 && j == width - 1), however the errors remain. Basically, the only pixels that are correctly blurred are the ones on the edges... – Krasimir Yankov Kolev Aug 25 '20 at 18:18

You must log in to answer this question.

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