0

I'm having an issue with the counter variable of my code. I wrote a printf to see if it returns 4, 6, or 9. Which is what it should do, if my IF conditions are correct. Unfortunately it does not return 4, 6, or 9. What am I doing wrong?

Here is my code:

void blur(int height, int width, RGBTRIPLE image[height][width])

{
int counter = 0;
               
        for (int row = 0; row < height; row++)
    {
         for (int col = 0; col < width; col++)
        {

            int OrgRed = image[height][width].rgbtRed;
            int OrgGreen = image[height][width].rgbtGreen;
            int OrgBlue = image[height][width].rgbtBlue;
        }
    }
            
    for (int i = 0; i < height; i++) //loop through neighbor pixels
        {
            for (int j = 0; j < height; j++)
            {
                int sumRed = image[i][j].rgbtRed;
                int sumGreen =image[i][j].rgbtGreen;
                int sumBlue =image[i][j].rgbtBlue;
                
                if ((i - 1 >= 0) && (j - 1 >= 0)) // 9 conditions in total: middle, four corners, four edges
                 {        //upper left corner 
                    sumRed += image[i - 1][j - 1].rgbtRed;
                    counter++;  
                 }
                if ((i - 1 >= 0) && (j < width)) //top edge
                {
                    sumRed += image[i - 1][j].rgbtRed;
                    counter++;
                }
                if ((i - 1 >= 0 ) && ( j + 1 < width)) // upper right corner
                {
                    sumRed += image[i - 1][j + 1].rgbtRed;
                    counter++; 
                }

                if ((j - 1 >= 0) && (i < height)) //left edge
                {
                    sumRed += image[i][j - 1].rgbtRed;
                    counter++;
                }
                if ((j - 1 >= 0) && (i + 1 < height )) //bottom left corner
                {
                    sumRed += image[i + 1][j + 1].rgbtRed;
                    counter++;
                }
                if ((i + 1 < height) && (j + 1 < width)) // bottom right corner 
                {
                    sumRed += image[i + 1][j + 1].rgbtRed;
                    counter++;
                }
                if ((i + 1 < height) && (j < width)) // bottom edge
                {
                    sumRed += image[i + 1][j].rgbtRed;
                    counter++;
                }
                if ((j + 1 < width) && (i < height)) // right edge
                {
                    sumRed += image[i][j + 1].rgbtRed;
                    counter++;
                }
            
                if (counter == 4)
                {
                  printf("%i is 4\n", counter); 
                }
                else if (counter == 6)
                {
                  printf("%i is 6\n", counter); 
                }
                else if (counter == 9)
                {
                  printf("%i is 9\n", counter); 
                }
                else
                {
                printf("this is pooop\n");
                }
                
            
            
        }
    }   
0

You declare the variable counter outside of the loop, so it will retain its value and continuously increment over time.

The top left pixel at (0, 0) will have 3 neighbouring pixels, then next pixel at (0, 1) will have 5 neighbouring pixels. Consequently, counter will go from 3, to 8, and then scale beyond any of your test conditions that might print a result.

You must log in to answer this question.

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