0

I'm currently working on filter.c, and I'm struggling with the blur segment of it. I tried to write a code (I'm aware it's a mess of hard-code, but it's an attempt!), but I keep getting this error:

helpers.c:236:9: runtime error: index 600 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:237:9: runtime error: index 600 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:238:9: runtime error: index 600 out of bounds for type 'RGBTRIPLE [width]'

My code is this:

void blur(int height, int width, RGBTRIPLE image[height][width])
{
    //For every row
    for (int i = 0; i < height; i++)
    {
        int sum = 0;
        int count = 0;
        int j = 0;
        //For every pixel in the row
        for (j = 0; j < width; j++)
        {
            //First row
            if (i == 0)
            {
                //Left corner
                if (j == 0)
                {
                    //Pixel to the right
                    sum += image[0][1].rgbtRed + image[0][1].rgbtGreen + image[0][1].rgbtBlue;
                    //Pixel right-bottom
                    sum += image[1][1].rgbtRed + image[1][1].rgbtGreen + image[1][1].rgbtBlue;
                    count = 4;
                }

  

          //Right corner
            else if (j == (width-1))
            {
                //Pixel to the left
                sum += image[0][width-2].rgbtRed + image[0][width-2].rgbtGreen + image[0][width-2].rgbtBlue;
                //Pixel left-bottom
                sum += image[1][width-2].rgbtRed + image[1][width-2].rgbtGreen + image[1][width-2].rgbtBlue;
                count = 4;
            }

            //In the middle
            else
            {
                //Pixel left
                sum += image[0][j-1].rgbtRed + image[0][j-1].rgbtGreen + image[0][j-1].rgbtBlue;
                //Pixel right
                sum += image[0][j+1].rgbtRed + image[0][j+1].rgbtGreen + image[0][j+1].rgbtBlue;
                //Bottom left
                sum += image[1][j-1].rgbtRed + image[1][j-1].rgbtGreen + image[1][j-1].rgbtBlue;
                //Bottom right
                sum += image[1][j+1].rgbtRed + image[1][j+1].rgbtGreen + image[1][j+1].rgbtBlue;
                count = 6;
            }
            //Pixel itself
            sum += image[0][j].rgbtRed + image[0][j].rgbtGreen + image[0][j].rgbtBlue;
            //Pixel right under
            sum += image[1][j].rgbtRed + image[1][j].rgbtGreen + image[1][j].rgbtBlue;
        }

        //Last row
        else if (i == (height - 1))
        {
            //Left corner
            if (j == 0)
            {
                //Pixel to the right
                sum += image[height-1][1].rgbtRed + image[height-1][1].rgbtGreen + image[height-1][1].rgbtBlue;
                //Pixel right-top
                sum += image[height-2][1].rgbtRed + image[height-2][1].rgbtGreen + image[height-2][1].rgbtBlue;
                count = 4;
            }
            //Right corner
            else if (j == (width-1))
            {
                //Pixel to the left
                sum += image[height-1][width-2].rgbtRed + image[height-1][width-2].rgbtGreen + image[height-1][width-2].rgbtBlue;
                //Pixel left-up
                sum += image[height-2][width-2].rgbtRed + image[height-2][width-2].rgbtGreen + image[height-2][width-2].rgbtBlue;
                count = 4;
            }
            //In the middle
            else
            {
                //Pixel left
                sum += image[height-1][j-1].rgbtRed + image[height-1][j-1].rgbtGreen + image[height-1][j-1].rgbtBlue;
                //Pixel right
                sum += image[height-1][j+1].rgbtRed + image[height-1][j+1].rgbtGreen + image[height-1][j+1].rgbtBlue;
                //Top left
                sum += image[height-2][j-1].rgbtRed + image[height-2][j-1].rgbtGreen + image[height-2][j-1].rgbtBlue;
                //Top right
                sum += image[height-2][j+1].rgbtRed + image[height-2][j+1].rgbtGreen + image[height-2][j+1].rgbtBlue;
                count = 6;
            }
            //Pixel itself
            sum += image[height-1][j].rgbtRed + image[height-1][j].rgbtGreen + image[height-1][j].rgbtBlue;
            //Pixel right above
            sum += image[height-2][j].rgbtRed + image[height-2][j].rgbtGreen + image[height-2][j].rgbtBlue;
        }

        //Not first row or last row
        else
        {
            //If first column
            if (j == 0)
            {
                //Right-top pixel
                sum += image[i-1][j+1].rgbtRed + image[i-1][j+1].rgbtRed + image[i-1][j+1].rgbtBlue;
                //Right-bottom pixel
                sum += image[i+1][j+1].rgbtRed + image[i+1][j+1].rgbtRed + image[i+1][j+1].rgbtBlue;
                //Right pixel
                sum += image[i][j+1].rgbtRed + image[i][j+1].rgbtRed + image[i][j+1].rgbtBlue;
                count = 6;
            }
            //If last column
            else if (j == (width-1))
            {
                //Left-top pixel
                sum += image[i-1][j-1].rgbtRed + image[i-1][j-1].rgbtRed + image[i-1][j-1].rgbtBlue;
                //Left-bottom pixel
                sum += image[i+1][j-1].rgbtRed + image[i+1][j-1].rgbtRed + image[i+1][j-1].rgbtBlue;
                //Left pixel
                sum += image[i][j-1].rgbtRed + image[i][j-1].rgbtRed + image[i][j-1].rgbtBlue;
                count = 6;
            }
            //If middle
            else
            {
                //Pixel right
                sum += image[i][j+1].rgbtRed + image[i][j+1].rgbtRed + image[i][j+1].rgbtBlue;
                //Pixel left
                sum += image[i][j-1].rgbtRed + image[i][j-1].rgbtRed + image[i][j-1].rgbtBlue;
                //Top right
                sum += image[i-1][j+1].rgbtRed + image[i-1][j+1].rgbtRed + image[i-1][j+1].rgbtBlue;
                //Bottom right
                sum += image[i+1][j+1].rgbtRed + image[i+1][j+1].rgbtRed + image[i+1][j+1].rgbtBlue;
                //Top left
                sum += image[i-1][j-1].rgbtRed + image[i-1][j-1].rgbtRed + image[i-1][j-1].rgbtBlue;
                //Bottom left
                sum += image[i+1][j-1].rgbtRed + image[i+1][j-1].rgbtRed + image[i+1][j-1].rgbtBlue;
                count = 9;
            }
            //Pixel above
            sum += image[i-1][j].rgbtRed + image[i-1][j].rgbtRed + image[i-1][j].rgbtBlue;
            //Pixel under
            sum += image[i+1][j].rgbtRed + image[i+1][j].rgbtRed + image[i+1][j].rgbtBlue;
            //Pixel itself
            sum += image[i][j].rgbtRed + image[i][j].rgbtRed + image[i][j].rgbtBlue;
        }
    }
    float average = sum/count;
    //Setting the values of them to the average
    image[i][j].rgbtRed = (int) round(average);
    image[i][j].rgbtGreen = (int) round(average);
    image[i][j].rgbtBlue = (int) round(average);
}
return;
}

I'm not sure why it goes to index 600 if I've set the loop to stay <width and <height, any help is appreciated, thank you in advance!

1

Question: When this code calculates blur values for each pixel, are those values stored in a temporary array, or stored back in the original image array as it goes along?

Every pixel's blurred values must be calculated on the original values in the surrounding cells. If the cells are updated, then the cells that follow will be calculated on blurred values, not the original values.

Further, the vars sum and average are simultaneously being used for all three colors, R, B and G. Each color requires it's own sum and average. The code can't use one var to concurrently calculate all three color blur values!

There may be more issues, but this will get you started.

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

2
  • Thank you!! I actually was going to bed and then woke up mid sleep thinking about that last point, the sum part being completely off. I have no idea why I even did it that way originally. As for the first part, I see my mistake now! The last part of the inner loop writes over the image[i][j] and that messes it up! But then the question is, do I then make another array of size [i][j] that stores those values temporarily and then overwrite at the end? Thank you so much!!! – Hana Ali Jul 29 '20 at 1:13
  • 1
    I think you already know the answer to your question. ;-) – Cliff B Jul 29 '20 at 3:19

You must log in to answer this question.

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