0

I'm confused about the reason I can't pass this test, if anyone can notice what's the cause I would be grateful :) here is the log :

:( blur correctly filters 3x3 image

Expected Output:
70 85 95 80 95 105 90 105 115 117 130 140 127 140 149 137 150 159 163 178 188 170 185 194 178 193 201
Actual Output:
70 85 95 80 95 105 90 105 115 117 130 140 127 140 149 137 150 159 163 200 188 170 185 194 178 193 201

{ RGBTRIPLE copy[height][width];

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

for (int i = 0; i < height; i++)
{
    for (int j = 0; j < width; j++)
    {
        if(i == 0 && j == 0)
        {
            // top left corner
            float red = (copy[i][j].rgbtRed + copy[i][j+1].rgbtRed + copy[i+1][j].rgbtRed + copy[i+1][j+1].rgbtRed) / 4.0;
            float green = (copy[i][j].rgbtGreen + copy[i][j+1].rgbtGreen + copy[i+1][j].rgbtGreen + copy[i+1][j+1].rgbtGreen) / 4.0;
            float blue = (copy[i][j].rgbtBlue + copy[i][j+1].rgbtBlue + copy[i+1][j].rgbtBlue + copy[i+1][j+1].rgbtBlue) / 4.0;

            image[i][j].rgbtRed = (int)round(red);
            image[i][j].rgbtGreen = (int)round(green);
            image[i][j].rgbtBlue = (int)round(blue);
        }
        else if(i == 0 && j == width - 1)
        {
            // top right corner
            float red = (copy[i][j].rgbtRed + copy[i][j-1].rgbtRed + copy[i+1][j].rgbtRed + copy[i+1][j-1].rgbtRed) / 4.0;
            float green = (copy[i][j].rgbtGreen + copy[i][j-1].rgbtGreen + copy[i+1][j].rgbtGreen + copy[i+1][j-1].rgbtGreen) / 4.0;
            float blue = (copy[i][j].rgbtBlue + copy[i][j-1].rgbtBlue + copy[i+1][j].rgbtBlue + copy[i+1][j-1].rgbtBlue) / 4.0;

            image[i][j].rgbtRed = (int)round(red);
            image[i][j].rgbtGreen = (int)round(green);
            image[i][j].rgbtBlue = (int)round(blue);
        }
        else if(i == 0 && j > 0 && j < width - 1)
        {
            // top edge
            float red = (copy[i][j].rgbtRed + copy[i][j-1].rgbtRed + copy[i][j+1].rgbtRed + copy[i+1][j-1].rgbtRed + copy[i+1][j].rgbtRed + copy[i+1][j+1].rgbtRed) / 6.0;
            float green = (copy[i][j].rgbtGreen + copy[i][j-1].rgbtGreen + copy[i][j+1].rgbtGreen + copy[i+1][j-1].rgbtGreen + copy[i+1][j].rgbtGreen + copy[i+1][j+1].rgbtGreen) / 6.0;
            float blue = (copy[i][j].rgbtBlue + copy[i][j-1].rgbtBlue + copy[i][j+1].rgbtBlue + copy[i+1][j-1].rgbtBlue + copy[i+1][j].rgbtBlue + copy[i+1][j+1].rgbtBlue) / 6.0;

            image[i][j].rgbtRed = (int)round(red);
            image[i][j].rgbtGreen = (int)round(green);
            image[i][j].rgbtBlue = (int)round(blue);
        }
        else if (i == height - 1 && j == 0)
        {
            // bottom left corner
            float red = (copy[i - 1][j].rgbtRed + copy[i - 1][j+1].rgbtRed + copy[i][j].rgbtRed + copy[i][j+1].rgbtRed) / 4.0;
            float green = (copy[i - 1][j].rgbtGreen + copy[i][j+1].rgbtGreen + copy[i][j].rgbtGreen + copy[i][j+1].rgbtGreen) / 4.0;
            float blue = (copy[i - 1][j].rgbtBlue + copy[i - 1][j+1].rgbtBlue + copy[i][j].rgbtBlue + copy[i][j+1].rgbtBlue) / 4.0;

            image[i][j].rgbtRed = (int)round(red);
            image[i][j].rgbtGreen = (int)round(green);
            image[i][j].rgbtBlue = (int)round(blue);
        }
        else if(i == height - 1 && j == width - 1)
        {
            // bottom right corner
            float red = (copy[i][j].rgbtRed + copy[i][j-1].rgbtRed + copy[i-1][j].rgbtRed + copy[i-1][j-1].rgbtRed) / 4.0;
            float green = (copy[i][j].rgbtGreen + copy[i][j-1].rgbtGreen + copy[i-1][j].rgbtGreen + copy[i-1][j-1].rgbtGreen) / 4.0;
            float blue = (copy[i][j].rgbtBlue + copy[i][j-1].rgbtBlue + copy[i-1][j].rgbtBlue + copy[i-1][j-1].rgbtBlue) / 4.0;

            image[i][j].rgbtRed = (int)round(red);
            image[i][j].rgbtGreen = (int)round(green);
            image[i][j].rgbtBlue = (int)round(blue);

        }
        else if(i == height - 1 && j > 0 && j < width - 1)
        {
            // bottom edge
            float red = (copy[i][j].rgbtRed + copy[i][j-1].rgbtRed + copy[i][j+1].rgbtRed + copy[i-1][j-1].rgbtRed + copy[i-1][j].rgbtRed + copy[i-1][j+1].rgbtRed) / 6.0;
            float green = (copy[i][j].rgbtGreen + copy[i][j-1].rgbtGreen + copy[i][j+1].rgbtGreen + copy[i-1][j-1].rgbtGreen + copy[i-1][j].rgbtGreen + copy[i-1][j+1].rgbtGreen) / 6.0;
            float blue = (copy[i][j].rgbtBlue + copy[i][j-1].rgbtBlue + copy[i][j+1].rgbtBlue + copy[i-1][j-1].rgbtBlue + copy[i-1][j].rgbtBlue + copy[i-1][j+1].rgbtBlue) / 6.0;

            image[i][j].rgbtRed = (int)round(red);
            image[i][j].rgbtGreen = (int)round(green);
            image[i][j].rgbtBlue = (int)round(blue);
        }
        else if(i != 0 && i != height - 1 && j == 0)
        {
            // left edge
            float red = (copy[i][j].rgbtRed + copy[i][j+1].rgbtRed + copy[i-1][j].rgbtRed + copy[i-1][j+1].rgbtRed + copy[i+1][j].rgbtRed + copy[i+1][j+1].rgbtRed) / 6.0;
            float green = (copy[i][j].rgbtGreen + copy[i][j+1].rgbtGreen + copy[i-1][j].rgbtGreen + copy[i-1][j+1].rgbtGreen + copy[i+1][j].rgbtGreen + copy[i+1][j+1].rgbtGreen) / 6.0;
            float blue = (copy[i][j].rgbtBlue + copy[i][j+1].rgbtBlue + copy[i-1][j].rgbtBlue + copy[i-1][j+1].rgbtBlue + copy[i+1][j].rgbtBlue + copy[i+1][j+1].rgbtBlue) / 6.0;

            image[i][j].rgbtRed = (int)round(red);
            image[i][j].rgbtGreen = (int)round(green);
            image[i][j].rgbtBlue = (int)round(blue);
        }
        else if(i != 0 && i != height - 1 && j == width - 1)
        {
            // right edge
            float red = (copy[i][j].rgbtRed + copy[i][j-1].rgbtRed + copy[i-1][j].rgbtRed + copy[i-1][j-1].rgbtRed + copy[i+1][j].rgbtRed + copy[i+1][j-1].rgbtRed) / 6.0;
            float green = (copy[i][j].rgbtGreen + copy[i][j-1].rgbtGreen + copy[i-1][j].rgbtGreen + copy[i-1][j-1].rgbtGreen + copy[i+1][j].rgbtGreen + copy[i+1][j-1].rgbtGreen) / 6.0;
            float blue = (copy[i][j].rgbtBlue + copy[i][j-1].rgbtBlue + copy[i-1][j].rgbtBlue + copy[i-1][j-1].rgbtBlue + copy[i+1][j].rgbtBlue + copy[i+1][j-1].rgbtBlue) / 6.0;

            image[i][j].rgbtRed = (int)round(red);
            image[i][j].rgbtGreen = (int)round(green);
            image[i][j].rgbtBlue = (int)round(blue);

        }
        else
        {
            // 3*3


            float red = (copy[i][j].rgbtRed + copy[i][j-1].rgbtRed + copy[i][j+1].rgbtRed + copy[i-1][j].rgbtRed + copy[i-1][j-1].rgbtRed + copy[i-1][j+1].rgbtRed + copy[i+1][j-1].rgbtRed + copy[i+1][j].rgbtRed + copy[i+1][j+1].rgbtRed) / 9.0;
            float green = (copy[i][j].rgbtGreen + copy[i][j-1].rgbtGreen + copy[i][j+1].rgbtGreen + copy[i-1][j].rgbtGreen + copy[i-1][j-1].rgbtGreen + copy[i-1][j+1].rgbtGreen + copy[i+1][j-1].rgbtGreen + copy[i+1][j].rgbtGreen + copy[i+1][j+1].rgbtGreen) / 9.0;
            float blue = (copy[i][j].rgbtBlue + copy[i][j-1].rgbtBlue + copy[i][j+1].rgbtBlue + copy[i-1][j].rgbtBlue + copy[i-1][j-1].rgbtBlue + copy[i-1][j+1].rgbtBlue + copy[i+1][j-1].rgbtBlue + copy[i+1][j].rgbtBlue + copy[i+1][j+1].rgbtBlue) / 9.0;

            image[i][j].rgbtRed = (int) round(red);
            image[i][j].rgbtGreen = (int) round(green);
            image[i][j].rgbtBlue = (int) round(blue);


        }

    }
}

}

1 Answer 1

1

I found the problem after I took the log:

Actual Output:

70 85 95

80 95 105

90 105 115

117 130 140

127 140 149

137 150 159

163 200 188

170 185 194

178 193 201

made it like the actual grid of the image :

70 85 95 // 80 95 105 // 90 105 115

117 130 140 // 127 140 149 // 137 150 159

163 200 188 // 170 185 194 // 178 193 201

this way, it was easy to know that the error was coming from the bottom corner block of code and only the Green in particular, it was only a typo, and took me the whole evening to settle :(, this happens so I hope someone facing the same issue finds this helpful.

I hope this helps someone to examine his code in the future by taking the log and making it look like pixels as rows and columns.

3
  • [chuckle] These are the kind of things that have been driving programmers insane since the beginning! Welcome to the club!
    – Cliff B
    Commented Jan 26 at 3:50
  • really good job of altering visualization of your output data to solve your own problem! well done and upvoted!
    – UpAndAdam
    Commented Jan 26 at 17:39
  • thanks for the comments guys :) Commented Jan 27 at 11:04

You must log in to answer this question.

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