0

I get the eror only for the 4x4 image, the others are correct. Seems like the values are swapped between each other, but I cant find a way to fix it. Something in the inner loop (I think) is swapping the pixel values so they are in an incorrect order.

The Code

// Reflect image horizontally
void reflect(int height, int width, RGBTRIPLE image[height][width])
{
   RGBTRIPLE temp;
   for (int i = 0; i < width; i++)
   {
       for (int j = 0; j < height; j++)
       {
           int size = width - 1;
           int reversed_pixel = 0;
           while (reversed_pixel <= size)
           {
               temp = image[i][reversed_pixel];
               image[i][reversed_pixel] = image[i][size];
               image[i][size] = temp;
               reversed_pixel++;
               size--;
           }
       }
   }
   return;
}

And this is the error I keep getting:

Expected Output:
100 110 120
70 80 90
40 50 60
10 20 30
140 160 170
130 150 160
120 140 150
110 130 140
245 254 253
225 234 243
205 214 223
195 204 213
85 85 85
255 255 255
0 0 0
50 28 90

Actual Output:
10 20 30
40 50 60
70 80 90
100 110 120
110 130 140
120 140 150
130 150 160
140 160 170
195 204 213
205 214 223
225 234 243
245 254 253
50 28 90
0 0 0
255 255 255
85 85 85

Any help is really appreciated. Thanks!

2

This is interesting! ;-)

I'll bet that the code works perfectly with any image that has an odd number of rows and produces the original image when there's an even number of rows!

The reversal code, the inner for loop, is working perfectly. The problem is that there is an outer for loop. All it is doing is re-executing the mirroring code on every pass. So, if it executed once, it mirrors the image. Twice and it puts it back to the original. A third pas reverses it again, and so on!

Remember, a mirror image is only reversed in one dimension (left to right), not two.

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

4
  • Thanks!, it helped a lot. Cant say I know how to fix it yet, but at least I know in wich direction to look. – Sebastian Lares Jun 15 '20 at 22:18
  • The fix is simple. Remove the outer loop. – Cliff B Jun 16 '20 at 4:38
  • You are right, indeed that worked. I had in my mind that the while loop would run once for each j number of the loop, not for only one until it was true and then move to the next one. I tought the outer loop was necesary so the two loops could itinerate every pixel Width and Height. I was already implementing two different algorithms, one for even odd number of rows and the other for even rows lol. Thank you! – Sebastian Lares Jun 16 '20 at 22:46
  • Sounds like you got it right. To be honest, I was slightly wrong. It needed to have one loop removed and parts of each loop setup used - i and height. ;-) In any case, it's a single for loop process the way you did it. To be most accurate, it really is a two loop process, but you had three. – Cliff B Jun 16 '20 at 23:24

You must log in to answer this question.

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