reversing struct array's rows

Would this take the struct array in temp and reverse it into temp2, then finally flipping the original image in im.pic?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

for (int i = 0; i < im.rows; i++) {

        for (int j = 0; j < im.cols; j++) {

            temp2[i][j].r = temp[im.rows - (i + 1)][im.cols].r; // reversing
            temp2[i][j].g = temp[im.rows - (i + 1)][im.cols].g;
            temp2[i][j].b = temp[im.rows - (i + 1)][im.cols].b;

        }

   }

    for (int i = 0; i < im.rows/2; i++) { // idea is to store 0-349 with temp2

        for (int j = 0; j < im.cols/2; j++) {

            im.pic[i][j].r = temp2[i][j].r;
            im.pic[i][j].g = temp2[i][j].g;
            im.pic[i][j].b = temp2[i][j].b;

        }

    }
Last edited on
No.

What are you trying to do? Mirror an image?
My end goal is to flip a ppm image upside down, that's what the r, g, b 2d arrays are representing.

My thinking is, if I can have temp 2 as the rows reversed, I could set the first half of the image rows to the first half of temp2, then the second half of image to the first half of temp.

Giving me a 2d array where the rows have been flipped upside down.

btw, MAX is 700. In case the 699 is confusing.
Last edited on
You need a helper function:

void swap_row( pixel* a, pixel* b, int length );
Exchanges two rows, each with length pixels.

Now you are dealing with a simple loop over the rows, making this the same kind of problem as reversing a string.

Also, don’t hard-code the number of rows. What if you have an image with only 200 rows?

1
2
3
4
for (int n = 0; n < (image_height / 2); n++)
{
  swap_row( image_data[n], image_data[height - n], image_width );
}

Hope this helps.
Last edited on
It would certainly help if I could use an extra function. I'm bound by rules though.

Why doesn't this work?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
    for (int i = 0; i < im.rows; i++) {
        for (int j = 0; j < im.cols; j++) {


            temp2[i][j].r = temp[im.rows - (i + 1)][im.cols].r;
            temp2[i][j].g = temp[im.rows - (i + 1)][im.cols].g;
            temp2[i][j].b = temp[im.rows - (i + 1)][im.cols].b;

        }
    }
        for (int i = 0; i < im.rows/2; i++) {
        for (int j = 0; j < im.cols/2; j++) {


                im.pic[i][j].r = temp2[i][j].r;
                im.pic[i][j].g = temp2[i][j].g;
                im.pic[i][j].b = temp2[i][j].b;

        }
    }
You are bound by rules that forbid you to use a function?
I find that very hard to accept.

Also, you deleted your first post. That is exceedingly rude, and makes this thread useless to anyone but you...

...and I cannot review what you had posted originally, nor compare it to what you have now.



So... have you tested your current code? Does it do what you want? When you look at the flipped image, do you see where something goes wrong?


The solution I have demonstrated for you does not use another, temporary image. Again, it works very much like the standard “reverse a string” homeworks.

The solution you are currently proposing is:
  • create a temporary image
  • flip the content of the original into the temporary
  • copy the (now flipped) content of the temporary straight back into the original

This is a workable solution. Again, compile it, give it a run, and see how the modified image looks. Then look at your loop bounds.

Hope this helps.
My bad on the deletion. That was actually a mistake while trying to update it to an edit I had made. I'll fix that.

But yeah. It's a homework assignment, of course, and we have to stick to the functions she gives us.


My proposed solution just creates black lines that cut through the top half of the image horizontally.

*edit*
The original post is gone, but I modified best I could.
Last edited on
Don't fret. Just for future notice.

Sorry, my brain is fried right now, so... here you go.

I don't know what your pixel/RGB struct is called, so in the following I'm just calling it "Pixel".

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int ytop = 0;
int ybottom = image_height - 1;
while (ytop < ybottom)
{
  // swap two scan lines
  for (int x = 0; x < image_width; x++)
  {
    Pixel pixel = im.pic[ytop][x];
    im.pic[ytop][x] = im.pic[ybottom][x];
    im.pic[ybottom][x] = pixel;
  }
  // next
  ytop++;
  ybottom--;
}

This works by swapping the top and bottom rows, then the next two, and so on, until you meet in the middle, at which point the image will be upside-down.

Enjoy!
That's some sick code.

Thank you.
Topic archived. No new replies allowed.