SebastianAltomare
SebastianAltomare

Reputation: 11

Filter-Less Harvard CS50 Course Blur Function Trouble - What is the problem?

Here is what I currently have for my blur function. I keep trying to test cases in my mind and I can't find the issue with my logic. But obviously there's something, so can anyone else spot it?? Everytime I go through the corners and the egdes and the middle pixels, I make it through the logic and add up the correct pixel colors, where am I messing up?

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

    int redsum, greensum, bluesum;
    float counter;
    redsum = greensum = bluesum = counter = 0;

    for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++) {
            // itself
            redsum += copy[i][j].rgbtRed;
            greensum += copy[i][j].rgbtGreen;
            bluesum += copy[i][j].rgbtBlue;
            counter++;

            // one row above
            if ((i - 1) >= 0) {
                redsum += copy[i-1][j].rgbtRed;
                greensum += copy[i-1][j].rgbtGreen;
                bluesum += copy[i-1][j].rgbtBlue;
                counter++;

                // one row above + one column left
                if ((j - 1) >= 0) {
                    redsum += copy[i-1][j-1].rgbtRed;
                    greensum += copy[i-1][j-1].rgbtGreen;
                    bluesum += copy[i-1][j-1].rgbtBlue;
                    counter++;
                }

                // one row above + one column right
                if ((j + 1) < width) {
                    redsum += copy[i-1][j+1].rgbtRed;
                    greensum += copy[i-1][j+1].rgbtGreen;
                    bluesum += copy[i-1][j+1].rgbtBlue;
                    counter++;
                }
            }


            // one column left
            if ((j - 1) >= 0) {
                redsum += copy[i][j-1].rgbtRed;
                greensum += copy[i][j-1].rgbtGreen;
                bluesum += copy[i][j-1].rgbtBlue;
                counter++;

                // one column left + one row below
                if ((i + 1) <= height) {
                    redsum += copy[i+1][j-1].rgbtRed;
                    greensum += copy[i+1][j-1].rgbtGreen;
                    bluesum += copy[i+1][j-1].rgbtBlue;
                    counter++;
                }
            }

            // one column right
            if ((j + 1) <= width) {
                redsum += copy[i][j+1].rgbtRed;
                greensum += copy[i][j+1].rgbtGreen;
                bluesum += copy[i][j+1].rgbtBlue;
                counter++;

                // one column right + one row below
                if ((i + 1) <= height) {
                    redsum += copy[i+1][j+1].rgbtRed;
                    greensum += copy[i+1][j+1].rgbtGreen;
                    bluesum += copy[i+1][j+1].rgbtBlue;
                    counter++;
                }
            }

            if ((i + 1) <= height) {
                redsum += copy[i+1][j].rgbtRed;
                greensum += copy[i+1][j].rgbtGreen;
                bluesum += copy[i+1][j].rgbtBlue;
                counter++;
            }

            image[i][j].rgbtRed = roundf(redsum / counter);
            image[i][j].rgbtGreen = roundf(greensum / counter);
            image[i][j].rgbtBlue = roundf(bluesum / counter);
        }
    }
}

Weirdly, when I do check50, it says:

:( blur correctly filters pixel on edge
    expected "80 95 105\n", not "76 91 101\n"

The fact that they're all 4 less can't be coincidental.

Upvotes: 0

Views: 27

Answers (0)

Related Questions