0

I'm having trouble getting my head around the pointers needed for the reflection filter.

I have got to here and can't get the syntax right. Any tips would be much appreciated.

Thanks.

void reflect(int height, int width, RGBTRIPLE image[height][width])
{
     for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            int w = width - 1;
            int *OR, *OG, *OB, *RR, *RG, *RB, tempRed, tempGreen, tempBlue;
            
            OR = &image[i][j].rgbtRed;
            OG = &image[i][j].rgbtGreen;
            OB = &image[i][j].rgbtBlue;
            
            RR = &image[i][w].rgbtRed;
            RG = &image[i][w].rgbtGreen;
            RB = &image[i][w].rgbtBlue;
            
            tempRed = *RR;
            tempGreen = *RG;
            tempBlue = *RB;
            
            *RR = *OR;
            *RG = *OG;
            *RB = *OB;
            
            *OR = tempRed;
            *OG = tempGreen;
            *OB = tempBlue;
            
            w--;
        }
        
    }
    return;
}

I've made some improvements (it now compiles):

void reflect(int height, int width, RGBTRIPLE image[height][width])
{
    
     for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            
            int xR, xG, xB, yR, yG, yB,*aR, *aG, *aB, *bR, *bG, *bB, tempRed, tempGreen, tempBlue;

            xR = image[i][j].rgbtRed;
            xG = image[i][j].rgbtGreen;
            xB = image[i][j].rgbtBlue;

            yR = image[i][width - j - 1].rgbtRed;
            yG = image[i][width - j - 1].rgbtGreen;
            yB = image[i][width - j - 1].rgbtBlue;

            aR = &xR;
            aG = &xG;
            aB = &xB;

            bR = &yR;
            bG = &yG;
            bB = &yB;

            tempRed = *bR;
            tempGreen = *bG;
            tempBlue = *bB;

            *bR = *aR;
            *bG = *aG;
            *bB = *aB;

            *aR = tempRed;
            *aG = tempGreen;
            *aB = tempBlue;

        }

    }
    return;
}

The image isn't reflected at all, even when it would appear so in debug50?

1

For the change of pixels, you have to write on the image at some point, and right now, you're not doing it.

You don't need all those variables and pointers to do the swap of the pixels. You can do something like this: Copy the first pixel on the left into a temp variable. Copy the first pixel on the right into the first on the left. Copy the temp variable into the first pixel on the right. Then the second, the third and so on.

Now think about what will happen when you reach the middle point.

1
  • Thank you for your suggestion, I will see what I can do. Jul 8 '20 at 7:48

You must log in to answer this question.

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