0

This is my first post; I hope I get the netiquette right. :-) I've been stuck on pset4 / filters / blur for days. The code compiles.

When I run it through check50, these two points are correct: :) blur correctly filters pixel on edge :) blur correctly filters pixel in corner

The rest are incorrect.

I've also been getting the same runtime errors at the same spot in the code for days and I can't figure out why.

  • helpers.c:145:85 (and every other "middle of loop" line where w = w +1): runtime error: index 600 out of bounds for type 'RGBTRIPLE [width]'
  • helpers.c:245:32 (always at round(green) and I can't fathom how that could be a negative number): runtime error: -2 is outside the range of representable values of type 'unsigned char'
  • The picture always comes out purple with dots of green (RGB green at 255, blue and red 0)

I would really appreciate any tips on where to look! I am completely flummoxed.

purple blur

void blur(int height, int width, RGBTRIPLE image[height][width])
{

    //make a temp image for storing values
    RGBTRIPLE imagetemp[height][width];
    //make a copy for reference
    RGBTRIPLE imagecopy[height][width];

    for (int h = 0; h < height; h++)
    {
        for (int w = 0; w < width; w++)
        {
            imagecopy[h][w] = image[h][w];
        }
    }

    for (int h = 0; h < height; h++)
    {
        for (int w = 0; w < width; w++)
        {
            BYTE avgred, avggreen, avgblue;
            float red, green, blue;

            //top edge case
           if (h == 0)
           {
                //left edge case
                if (w == 0)
                {
                    red = ((imagecopy[h][w].rgbtRed + imagecopy[h][w + 1].rgbtRed + imagecopy[h + 1][w].rgbtRed + imagecopy[h + 1][w + 1].rgbtRed) / 4);
                    green = ((imagecopy[h][w].rgbtGreen + imagecopy[h][w + 1].rgbtGreen + imagecopy[h + 1][w].rgbtGreen + imagecopy[h + 1][w + 1].rgbtGreen) / 4);
                    blue = ((imagecopy[h][w].rgbtBlue + imagecopy[h][w + 1].rgbtBlue + imagecopy[h + 1][w].rgbtBlue + imagecopy[h + 1][w + 1].rgbtBlue) / 4);
                }
                //right edge case
                if (w == width)
                {
                    red = ((imagecopy[h][w].rgbtRed + imagecopy[h][w - 1].rgbtRed + imagecopy[h + 1][w].rgbtRed + imagecopy[h + 1][w - 1].rgbtRed) / 4);
                    green = ((imagecopy[h][w].rgbtGreen + imagecopy[h][w - 1].rgbtGreen + imagecopy[h + 1][w].rgbtGreen + imagecopy[h + 1][w - 1].rgbtGreen) / 4);
                    blue = ((imagecopy[h][w].rgbtBlue + imagecopy[h][w - 1].rgbtBlue + imagecopy[h + 1][w].rgbtBlue + imagecopy[h + 1][w - 1].rgbtBlue) / 4);
                }
                //middle of image
                if (w > 0 && w < width)
                {
                    red = ((imagecopy[h][w].rgbtRed + imagecopy[h][w - 1].rgbtRed + imagecopy[h][w + 1].rgbtRed + imagecopy[h + 1][w].rgbtRed + imagecopy[h + 1][w + 1].rgbtRed + imagecopy[h + 1][w - 1].rgbtRed) / 6);
                    green = ((imagecopy[h][w].rgbtGreen + imagecopy[h][w - 1].rgbtGreen + imagecopy[h][w + 1].rgbtGreen + imagecopy[h + 1][w].rgbtGreen + imagecopy[h + 1][w + 1].rgbtGreen + imagecopy[h + 1][w - 1].rgbtGreen) / 6);
                    blue = ((imagecopy[h][w].rgbtBlue + imagecopy[h][w - 1].rgbtBlue + imagecopy[h][w + 1].rgbtBlue + imagecopy[h + 1][w].rgbtBlue + imagecopy[h + 1][w + 1].rgbtBlue + imagecopy[h + 1][w - 1].rgbtBlue) / 6);
                }
           }

            //bottom edge case
           if (h == height)
           {
                //left edge case
                if (w == 0)
                {
                    red = ((imagecopy[h][w].rgbtRed + imagecopy[h][w + 1].rgbtRed + imagecopy[h - 1][w].rgbtRed + imagecopy[h - 1][w + 1].rgbtRed) / 4);
                    green = ((imagecopy[h][w].rgbtGreen + imagecopy[h][w + 1].rgbtGreen + imagecopy[h - 1][w].rgbtGreen + imagecopy[h - 1][w + 1].rgbtGreen) / 4);
                    blue = ((imagecopy[h][w].rgbtBlue + imagecopy[h][w + 1].rgbtBlue + imagecopy[h - 1][w].rgbtBlue + imagecopy[h - 1][w + 1].rgbtBlue) / 4);
                }
                //right edge case
                if (w == width)
                {
                    red = ((imagecopy[h][w].rgbtRed + imagecopy[h][w - 1].rgbtRed + imagecopy[h - 1][w].rgbtRed + imagecopy[h - 1][w - 1].rgbtRed) / 4);
                    green = ((imagecopy[h][w].rgbtGreen + imagecopy[h][w - 1].rgbtGreen + imagecopy[h - 1][w].rgbtGreen + imagecopy[h - 1][w - 1].rgbtGreen) / 4);
                    blue = ((imagecopy[h][w].rgbtBlue + imagecopy[h][w - 1].rgbtBlue + imagecopy[h - 1][w].rgbtBlue + imagecopy[h - 1][w - 1].rgbtBlue) / 4);
                }

                if (w != 0 && w != width)
                {
                    red = ((imagecopy[h][w].rgbtRed + imagecopy[h][w - 1].rgbtRed + imagecopy[h][w + 1].rgbtRed + imagecopy[h - 1][w].rgbtRed + imagecopy[h - 1][w + 1].rgbtRed + imagecopy[h - 1][w - 1].rgbtRed) / 6);
                    green = ((imagecopy[h][w].rgbtGreen + imagecopy[h][w - 1].rgbtGreen + imagecopy[h][w + 1].rgbtGreen + imagecopy[h - 1][w].rgbtGreen - imagecopy[h - 1][w + 1].rgbtGreen + imagecopy[h - 1][w - 1].rgbtGreen) / 6);
                    blue = ((imagecopy[h][w].rgbtBlue + imagecopy[h][w - 1].rgbtBlue + imagecopy[h][w + 1].rgbtBlue + imagecopy[h - 1][w].rgbtBlue + imagecopy[h - 1][w + 1].rgbtBlue + imagecopy[h - 1][w - 1].rgbtBlue) / 6);
                }
           }
           if (h != 0 && h != height)
           // middle of image when 0 < h < height
           {
                //left edge case
                if (w == 0)
                {
                    red = ((imagecopy[h + 1][w].rgbtRed + imagecopy[h + 1][w + 1].rgbtRed + imagecopy[h][w].rgbtRed + imagecopy[h][w + 1].rgbtRed + imagecopy[h - 1][w].rgbtRed + imagecopy[h - 1][w + 1].rgbtRed) / 6);
                    green = ((imagecopy[h + 1][w].rgbtGreen + imagecopy[h + 1][w + 1].rgbtGreen + imagecopy[h][w].rgbtGreen + imagecopy[h][w + 1].rgbtGreen + imagecopy[h - 1][w].rgbtGreen + imagecopy[h - 1][w + 1].rgbtGreen) / 6);
                    blue = ((imagecopy[h + 1][w].rgbtBlue + imagecopy[h + 1][w + 1].rgbtBlue + imagecopy[h][w].rgbtBlue + imagecopy[h][w + 1].rgbtBlue + imagecopy[h - 1][w].rgbtBlue + imagecopy[h - 1][w + 1].rgbtBlue) / 6);

                }
                //right edge case
                if (w == width)
                {
                    red = ((imagecopy[h + 1][w].rgbtRed + imagecopy[h + 1][w - 1].rgbtRed + imagecopy[h][w].rgbtRed + imagecopy[h][w - 1].rgbtRed + imagecopy[h - 1][w].rgbtRed + imagecopy[h - 1][w - 1].rgbtRed) / 6);
                    green = ((imagecopy[h + 1][w].rgbtGreen + imagecopy[h + 1][w - 1].rgbtGreen + imagecopy[h][w].rgbtGreen + imagecopy[h][w - 1].rgbtGreen + imagecopy[h - 1][w].rgbtGreen + imagecopy[h - 1][w - 1].rgbtGreen) / 6);
                    blue = ((imagecopy[h + 1][w].rgbtBlue + imagecopy[h + 1][w - 1].rgbtBlue + imagecopy[h][w].rgbtBlue + imagecopy[h][w - 1].rgbtBlue + imagecopy[h - 1][w].rgbtBlue + imagecopy[h - 1][w - 1].rgbtBlue) / 6);

                }
                //middle of image
                if (w > 0 && w < width)
                {
                    red = ((imagecopy[h][w].rgbtRed + imagecopy[h][w - 1].rgbtRed + imagecopy[h][w + 1].rgbtRed + imagecopy[h - 1][w].rgbtRed + imagecopy[h - 1][w + 1].rgbtRed + imagecopy[h - 1][w - 1].rgbtRed + imagecopy[h + 1][w].rgbtRed + imagecopy[h + 1][w + 1].rgbtRed + imagecopy[h + 1][w - 1].rgbtRed) / 9);
                    green = ((imagecopy[h][w].rgbtGreen + imagecopy[h][w - 1].rgbtGreen + imagecopy[h][w + 1].rgbtGreen + imagecopy[h - 1][w].rgbtGreen - imagecopy[h - 1][w + 1].rgbtGreen + imagecopy[h - 1][w - 1].rgbtGreen + imagecopy[h + 1][w].rgbtGreen - imagecopy[h + 1][w + 1].rgbtGreen + imagecopy[h + 1][w - 1].rgbtGreen) / 9);
                    blue = ((imagecopy[h][w].rgbtBlue + imagecopy[h][w - 1].rgbtBlue + imagecopy[h][w + 1].rgbtBlue + imagecopy[h - 1][w].rgbtBlue + imagecopy[h - 1][w + 1].rgbtBlue + imagecopy[h - 1][w - 1].rgbtBlue + imagecopy[h + 1][w].rgbtBlue + imagecopy[h + 1][w + 1].rgbtBlue + imagecopy[h + 1][w - 1].rgbtBlue) / 9);
                }

           }

            avgred = round(red);
            avggreen = round(green);
            avgblue = round(blue);
            imagetemp[h][w].rgbtRed = avgred;
            imagetemp[h][w].rgbtGreen = avggreen;
            imagetemp[h][w].rgbtBlue = avgblue;
        }
    }
    for (int h = 0; h < height; h++)
    {
        for (int w = 0; w < width; w++)
        {
            image[h][w] = imagetemp[h][w];
        }
    }
    return;
}
1

Um, your code seems pretty hardcoded. It is good to improve the ability to think by trying to implement a new algorithm. I'm really sorry, I couldn't read it because its a lot. :( Anyways here's my code. I think it might help you. What I've basically done is I started from the top row then the relevant row and the row below.

For i being the row and j being the width(simple demo)

([i-1][j-1]) ([i-1][j]) ([i-1][j+1])

([i] [j-1]) ([i] [j]) ([i] [j+1])

([i+1][j-1]) ([i+1][j]) ([i+1][j+1])

Consider each pair a block in 3x3 cube

Here's the code

// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
    long temp[height][width][3];
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            long sumRed = 0, sumBlue = 0, sumGreen = 0;
            float pixCount = 0.000;
            //top row
            for (int k = j - 1; k <= j + 1; k++)
            {
                if (i - 1 >= 0 && (k >= 0 && k < width))
                {
                    sumRed += (long)image[i - 1][k].rgbtRed;
                    sumBlue += (long)image[i - 1][k].rgbtBlue;
                    sumGreen += (long)image[i - 1][k].rgbtGreen;
                    pixCount++;
                }
            }
            //relevant row
            for (int k = j - 1; k <= j + 1; k++)
            {
                if (i >= 0 && (k >= 0 && k < width))
                {
                    sumRed += (long)image[i][k].rgbtRed;
                    sumBlue += (long)image[i][k].rgbtBlue;
                    sumGreen += (long)image[i][k].rgbtGreen;
                    pixCount++;
                }
            }
            //bottom row
            for (int k = j - 1; k <= j + 1; k++)
            {
                if (i + 1 < height && (k >= 0 && k < width))
                {
                    sumRed += (long)image[i + 1][k].rgbtRed;
                    sumBlue += (long)image[i + 1][k].rgbtBlue;
                    sumGreen += (long)image[i + 1][k].rgbtGreen;
                    pixCount++;
                }
            }
            temp[i][j][0] = round(sumRed / pixCount);
            temp[i][j][1] = round(sumBlue / pixCount);
            temp[i][j][2] = round(sumGreen / pixCount);
        }
    }
    for (int i = 0; i < height; i++)
    {
         for (int j = 0; j < width; j++)
        {
            image[i][j].rgbtRed = temp[i][j][0];
            image[i][j].rgbtBlue = temp[i][j][1];
            image[i][j].rgbtGreen = temp[i][j][2];
        }
    }
    return;
}
| improve this answer | |

You must log in to answer this question.

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