0

I have an issue with the reflect filter, it just reflects half of the image so what is the issue with my code here?!

void swap(RGBTRIPLE *pixel1, RGBTRIPLE *pixel2)
{
    RGBTRIPLE temp = *pixel1;
    *pixel2 = *pixel1;
    *pixel1 = temp;
}

void reflect(int height, int width, RGBTRIPLE image[height][width])
{
    for(int i = 0; i < height; i++)
    {
        for(int j = 0; j < width / 2; j++)
        {
            swap(&image[i][j], &image[i][width - 1 - j]);
        }
    }
}

Reflected image

4
  • please post your swap function. – Cliff B Mar 15 at 8:53
  • It's already there at the top @CliffB – Mohamed Shawky Mar 15 at 9:16
  • Sorry, missed that. It's late. ;-) – Cliff B Mar 15 at 9:31
  • No worries man. – Mohamed Shawky Mar 15 at 9:33
0

Look very carefully at what's happening:

RGBTRIPLE temp = *pixel1;
*pixel2 = *pixel1;
*pixel1 = temp;

The code saves pixel1 in temp, then copies it to pixel2, then copies temp back to pixel1. In other words, everything ends up with the contents of pixel1.

The value in p2 needs to be saved before overwriting it.

Try saving pixel2 in temp instead. ;-)

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

1

You must log in to answer this question.

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