0

My code seems to work (when visually inspecting output files), but Check50 does not pass it. I'm struggling to understand what is causing this issue despite having repeatedly read over the code and pseudocode.

        int cap(int a)
    {
        if (a > 255)
        {
            return 255;
        }
        else
        {
            return a;
        }
    }

// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
    RGBTRIPLE temp[height][width];
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            temp[i][j] = image[i][j];
        }
    }

                float redVal = 0;
                float greenVal = 0;
                float blueVal = 0;
                int valid = 0;

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            valid = 0; 
            for (int di = -1; di <= 1; di++)
                {
                    for (int dj = -1; dj <= 1; dj++)
                        {
                            int pixelheight = i + di;
                            int pixelwidth = j + dj;
                            if ((i + di >= 0 && i + di <= height) && (j + dj >= 0 && j + dj <= width))
                                {
                                    redVal = redVal + temp[pixelheight][pixelwidth].rgbtRed;
                                    greenVal = greenVal + temp[pixelheight][pixelwidth].rgbtGreen;
                                    blueVal = blueVal + temp[pixelheight][pixelwidth].rgbtBlue;
                                    valid++;

                                }
                        }

                }
            redVal = cap(round(redVal / valid));
            greenVal = cap(round(greenVal / valid));
            blueVal = cap(round(blueVal / valid));

            temp[i][j].rgbtBlue = blueVal;
            temp[i][j].rgbtGreen = greenVal;
            temp[i][j].rgbtRed = redVal;
        }
    }

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            image[i][j] = temp[i][j];
        }
    }
    return;
}

Here is the Check50 output I'm getting

:( blur correctly filters middle pixel
    expected "127 140 149\n", not "171 189 201\n"
:( blur correctly filters pixel on edge
    expected "80 95 105\n", not "102 120 132\n"
:) blur correctly filters pixel in corner
:( blur correctly filters 3x3 image
    expected "70 85 95\n80 9...", not "70 85 95\n102 ..."
:( blur correctly filters 4x4 image
    expected "70 85 95\n80 9...", not "70 85 95\n102 ..."
1

Without doing a deep dive, I do see a major problem in the logic. The code copies all the pixels from image to temp array. But then, it does all the calculations using the temp array as the source while concurrently putting the blurred pixels back into temp and not image. Finally, it copies the end result back to the image array. This would be the same as not using a temp array at all.

Here's the problem. Every pixel in the transformed blurred image has to be calculated from the original pixels. By using the same array to hold the original and the blurred pixels, the code is using the altered pixels that preceded the current one and the original pixels that follow. This corrupts the calculations.

The code needs to use ALL ORIGINAL PIXELS to calculate the new pixels. In other words, it needs to leave one of the arrays totally unchanged for source data and put the blurred results in the other array. (There are two ways to do this: You can leave the original array unchanged and put the new data in temp, and then copy the entire temp array back to the image array after all the calculations have been done, OR you can copy the original to temp, use temp as the source, and put the blurred pixels directly back into array.)

BUT, you CANNOT use the same array as the source data and put the blurred pixels in it at the same time!

There may be other problems, but this is the main issue I see.

BTW, looking at the image and noting that it "seems" right may tell you if something is seriously wrong, but is usually never enough to tell if it's actually right. For instance, seeing that a reasonable image is produced is a good thing. At minimum, you have the image being output! But that doesn't mean there aren't issues buried in the image.

You should learn to actually validate the raw data. In this case, like check50 does, check to see that the exact numerical data is correct. For instance, you'd never detect something like an "off by 1" error by looking at the image. Always be prepared to make sure that the actual data is right. Another best practice is to produce test data to use, and determine what the correct result data should be before you actually run the test.

You'll pick up on this with experience. Happy Programming!!! ;-)

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

1
  • Thank you! I had initially tried to reference the image[] array, but had gotten a segmentation error. Turns out I had fixed the issue, but never changed temp[] back to image. Still not passing check50, but I believe this is a step in the right direction. Thanks for the advice! May 1 at 15:12

You must log in to answer this question.

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