1

Below is my Blur function, which does actually work, yet still returns 9 runtime errors all relating to the right hand side of the image (or the width-value edge).

I added a couple of printfs to check width (600) and height (400). I also used check50 to try to understand where i'm going wrong, but actually made the errors harder to understand. Check50 told me that I'm trying to access location 600 of the array, and that this is past the end of the array (although my printf told me that the second dimension of the array (width) was 600 items long.

Please have a look at my code and let me know if you spot something I missed. I have noted the errors (lines 114 - 116, 122 - 124, 130 - 132)

Function:

// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
    RGBTRIPLE image2[height][width];
    printf("width: %i\nheight: %i\n", width, height); // how big is this img?

    for (int h = 0; h < height; h++) // Y axis
    {
        for (int w = 0; w < width; w++) // X axis
        {

            long aveRed = 0;
            long aveGreen = 0;
            long aveBlue = 0;
            long divider = 0;
            if (h > 0 && w > 0) // edge case 1: N W
            {
                // record top left
                aveRed += image[h - 1][w - 1].rgbtRed;
                aveGreen += image[h - 1][w - 1].rgbtGreen;
                aveBlue += image[h - 1][w - 1].rgbtBlue;
                divider++;
            }
            if ((h > 0) && (h > 0) && (w < width)) // edge case 2: N
            {
                // record top
                aveRed += image[h - 1][w].rgbtRed;
                aveGreen += image[h - 1][w].rgbtGreen;
                aveBlue += image[h - 1][w].rgbtBlue;
                divider++;
            }
            if ((h > 0) && (w < width)) // edge case 3: N E
            {
                // record top right
                aveRed += image[h - 1][w + 1].rgbtRed; // line 114
                aveGreen += image[h - 1][w + 1].rgbtGreen;
                aveBlue += image[h - 1][w + 1].rgbtBlue;
                divider++;
            }
            if ((w < width) && (h > 0) && (h < height)) // edge case 4: E
            { 
                // record right
                aveRed += image[h][w + 1].rgbtRed; // line 122
                aveGreen += image[h][w + 1].rgbtGreen;
                aveBlue += image[h][w + 1].rgbtBlue;
                divider++;
            }
            if ((w < width) && (h < height)) // edge case 5: S E
            {
                // record bottom right
                aveRed += image[h + 1][w + 1].rgbtRed; // line 130
                aveGreen += image[h + 1][w + 1].rgbtGreen;
                aveBlue += image[h + 1][w + 1].rgbtBlue;
                divider++;
            }
            if ((h < height) && (w > 0) && (w < width)) // edge case 6: S
            {
                // record bottom
                aveRed += image[h + 1][w].rgbtRed;
                aveGreen += image[h + 1][w].rgbtGreen;
                aveBlue += image[h + 1][w].rgbtBlue;
                divider++;
            }
            if ((h < height) && (w > 0)) // edge case 7: S W
            {
                // record bottom left
                aveRed += image[h + 1][w - 1].rgbtRed;
                aveGreen += image[h + 1][w - 1].rgbtGreen;
                aveBlue += image[h + 1][w - 1].rgbtBlue;
                divider++;
            }
            if ((h < height) && (w < 0)) // edge case 8: W
            {
                // record left
                aveRed += image[h][w - 1].rgbtRed;
                aveGreen += image[h][w - 1].rgbtGreen;
                aveBlue += image[h][w - 1].rgbtBlue;
                divider++;
            }
            // record current position
            aveRed += image[h][w].rgbtRed;
            aveGreen += image[h][w].rgbtGreen;
            aveBlue += image[h][w].rgbtBlue;
            divider++;
            // divide by total, average, record
            image2[h][w].rgbtRed = round(aveRed / divider);
            image2[h][w].rgbtGreen = round(aveGreen / divider);
            image2[h][w].rgbtBlue = round(aveBlue / divider);
        }
    }
    for (int x = 0; x < height; x++)
    {
        for (int y = 0; y < width; y++)
        {
            image[x][y] = image2[x][y];
        }
    }
    return;
}

terminal returned:

~/pset4/filter/ $ help50 ./filter -b images/yard.
bmp byard.bmp                                    
width: 600
height: 400
helpers.c:130:27: runtime error: index 600 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:131:29: runtime error: index 600 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:132:28: runtime error: index 600 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:114:27: runtime error: index 600 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:115:29: runtime error: index 600 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:116:28: runtime error: index 600 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:122:27: runtime error: index 600 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:123:29: runtime error: index 600 out of bounds for type 'RGBTRIPLE [width]'
helpers.c:124:28: runtime error: index 600 out of bounds for type 'RGBTRIPLE [width]'

Asking for help...

helpers.c:130:27: runtime error: index 600 out of bounds for type 'RGBTRIPLE [width]'

Looks like you're to access location 600 of an array on line 130 of helpers.c, but that 
location is past the end of the array.```
0

Let's look at one case, line 130. The rest will follow.

        if ((w < width) && (h < height)) // edge case 5: S E
        {
            // record bottom right
            aveRed += image[h + 1][w + 1].rgbtRed; // line 130

If w = width - 1, which is possible in this for loop, what is w + 1? The error happens because it's really trying to access image[h + 1][width]. And we all know that the array range runs from 0 to width-1, right?

1
  • Oh right! I did try adjusting width by +1 and -1 but it left a border, so I added the printfs to check the width. I probably had it right already at one point! Thanks Cliff!
    – JoeH
    Aug 8 '20 at 7:20

You must log in to answer this question.

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