0

When trying to compile and run blur one of two things happens, even if I don't change the code at all:

I either get a warning saying my int red, green, or blue variables are not used. Which I think is related to something very wrong with my if statement in the second for loop. If I add a print statement to give me the counter values, just to see what's happening, I get endless values from 1-9 printed. If I add a print statement to any of the color counters I get an endless print out of random numbers. If I remove the print statements and I somehow do not get the int not used warning, the image is output but no pixel was altered.

I've tried the inverse of the if/else in the for loop, making putting continue under the if, if the byte falls out of bounds. I've also tried using different math with the inner for loops. I can't figure it out.

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];
        }
    }

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        { //saying these are not used
            int blue = 0;
            int green = 0;
            int red = 0;
            float counter = 0.00;
            //for (int k = 0; k < 3; k++) --> same for l in second for loop
            for (int k = -1; k < 2; k++)
            {

                for (int l = -1; l < 2; l++)
                {

                    //use with k,l < 3? if (i - k + 1 < 0 || i - k + 1 >= height || j - l + 1 < 0 || j - l + 1 >= width)
                    if (i + k >= 0 && i + k <= height && j + l >= 0 && j + l <= width)
                    {
                        counter++;
                        //printf("counter: %f\n", counter);
                        blue += copy[i][j].rgbtBlue;
                        green += copy[i][j].rgbtGreen;
                        //printf("green: %i\n", green);
                        red += copy[i][j].rgbtRed;
                    }

                }
            }

            copy[i][j].rgbtBlue = round(blue / counter);
            copy[i][j].rgbtGreen = round(green / counter);
            copy[i][j].rgbtRed = round(red / counter);
        }
    }

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

    return;
}

1 Answer 1

0

First of all, this code does compile correctly. I can't explain why you're having trouble compiling it. Are you editing one file and compiling another maybe?

Second, there's a major flaw in the code. Look at the following lines from the core of the nested for loops (extraneous code removed):

if (i + k >= 0 && i + k <= height && j + l >= 0 && j + l <= width)      
{
    counter++;
    blue += copy[i][j].rgbtBlue;
    green += copy[i][j].rgbtGreen;
    red += copy[i][j].rgbtRed;
}

Think about what is being added to the color values on each pass through the loop. Is the code adding all the pixels around the target pixel, or just adding the target pixel over and over n times and then dividing by n later? Did you notice that the result image is identical to the original all the time?

There are at least two other bugs lying in wait, but you deserve to have the enjoyment of finding them yourself. ;-)

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

0

You must log in to answer this question.