0

This is my second attempt at this problem, and a full rewrite; I tried once before to be as eloquent as I could, and it was just totally wrong and bizarre. So I just tried to make this one as readable as I possibly could, but the only things that work are the corners, which are a lot more hardcoded in than the rest of the function. The rest of the pixels are totally black.

So there's something wrong in the loops I have set up for the edges and center, but I can't figure out what. EDIT: I've cut it down to just the loops I'm having problems with. I'd appreciate any advice I could get; I know this function's a long one. Thanks very much for your time!

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

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            int tempRed = 0;
            int tempGreen = 0;
            int tempBlue = 0;
            float validPixels = 0.00;

            // corner functions were here

            else if (i == 0)
            {
                // top border
                for (int tempHorz = -1; tempHorz > 1; tempHorz++)
                {
                    for (int tempVert = 0; tempVert > 1; tempVert++)
                    {
                        // this might need to be its own function - I can see broader applications here
                        tempRed += image[(i + tempHorz)][(j + tempVert)].rgbtRed;
                        tempGreen += image[(i + tempHorz)][(j + tempVert)].rgbtGreen;
                        tempBlue += image[(i + tempHorz)][(j + tempVert)].rgbtBlue;
                    }
                }
                validPixels = 6;
            }
            else if (j == 0)
            {
                // left border
                for (int tempHorz = 0; tempHorz > 1; tempHorz++)
                {
                    for (int tempVert = -1; tempVert > 1; tempVert++)
                    {
                        tempRed += image[(i + tempHorz)][(j + tempVert)].rgbtRed;
                        tempGreen += image[(i + tempHorz)][(j + tempVert)].rgbtGreen;
                        tempBlue += image[(i + tempHorz)][(j + tempVert)].rgbtBlue;
                    }
                }
                validPixels = 6;
            }
            else if (i == (height - 1))
            {
                // bottom border
                for (int tempHorz = -1; tempHorz > 1; tempHorz++)
                {
                    for (int tempVert = -1; tempVert > 0; tempVert++)
                    {
                        tempRed += image[(i + tempHorz)][(j + tempVert)].rgbtRed;
                        tempGreen += image[(i + tempHorz)][(j + tempVert)].rgbtGreen;
                        tempBlue += image[(i + tempHorz)][(j + tempVert)].rgbtBlue;
                    }
                }
                validPixels = 6;
            }
            else if (j == (width - 1))
            {
                // right border
                for (int tempHorz = -1; tempHorz > 0; tempHorz++)
                {
                    for (int tempVert = -1; tempVert > 1; tempVert++)
                    {
                        tempRed += image[(i + tempHorz)][(j + tempVert)].rgbtRed;
                        tempGreen += image[(i + tempHorz)][(j + tempVert)].rgbtGreen;
                        tempBlue += image[(i + tempHorz)][(j + tempVert)].rgbtBlue;
                    }
                }
                validPixels = 6;
            }
            else
            {
                // the creamy middle
                for (int tempHorz = -1; tempHorz > 1; tempHorz++)
                {
                    for (int tempVert = -1; tempVert > 1; tempVert++)
                    {
                        tempRed += image[(i + tempHorz)][(j + tempVert)].rgbtRed;
                        tempGreen += image[(i + tempHorz)][(j + tempVert)].rgbtGreen;
                        tempBlue += image[(i + tempHorz)][(j + tempVert)].rgbtBlue;
                    }
                }
                validPixels = 9;
            }

            // put filtered results into tempArray so as not to affect original values with the blur
            tempArray[i][j][0] = round(tempRed / validPixels * 1.00);
            tempArray[i][j][1] = round(tempGreen / validPixels * 1.00);
            tempArray[i][j][2] = round(tempBlue / validPixels * 1.00);
        }
    }

    // transfers tempArray results over to the original image
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            image[i][j].rgbtRed = tempArray[i][j][0];
            image[i][j].rgbtGreen = tempArray[i][j][1];
            image[i][j].rgbtBlue = tempArray[i][j][2];
        }
    }
    return;
}

And the link to the problem: https://cs50.harvard.edu/x/2020/psets/4/filter/less/

0

This one's fun because the problems are hiding in plain sight!

The code has multiple bugs, but we're going to focus on the current one so that you can move on to the rest. I wouldn't want to deprive you of a chance to fix them yourself before you even see them!! ;-)

Let's start with a little diagnosis. The first clue is that the corner code works fine but the edge and center pixel code isn't working at all. It just gives black pixels. But what is black? It's 0x000000. Interesting! BTW, what were the three RGB values initialized to? Doesn't the code set them to 0 to start with?

Looking at the code, all the code for corner cases are written in one style, while all the code for edge and center cases are in a completely different style, which isn't working. Hmmm... another clue.

Did you actually run the code in the debugger and step through it to see just what it does? By this time, you should be comfortable doing exactly that. I didn't think so. If you had, you would have seen that none of the for loops in the broken cases are executing. So let's look at the code for the center pixels.

            // the creamy middle
            for (int tempHorz = -1; tempHorz > 1; tempHorz++)
            {
                for (int tempVert = -1; tempVert > 1; tempVert++)

Hmmm.... does a for loop execute while or until the test condition is met?

Here's one last hint for you, for future problems. If you have an image array represented by image[x][y], does x represent the horizontal or the vertical coordinate in this problem? ;-)

Happy coding!

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

| improve this answer | |
  • Well, this is what I get for stepping away from CS50 for a month! To be honest, I was so out of it that I put my original equation of Stack Overflow's main--over here. But I suspect these two probably have a lot of the same problems, so I'll work on this first. Thanks for the hints! – dubflicker Jul 10 at 5:54

You must log in to answer this question.

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