1

I have two structs, and a function. I want to be able to pass back the expression in the function to my main program. But it is a multi-dimensional array... since I am trying to print it out...

//Setting the struct up for the pixel's
struct pixel
{
   unsigned char red;
   unsigned char green;
   unsigned char blue;
};

//Setting the struct up for the Image Type and scanning in the pixels into an array
struct ImageType
{
   char ppImage[3];
   char comment[256];
   char newlinechar;
   int width, height;
   int maxColor;
   struct pixel image[256][256];
};

My function

//Function in order to flip the image, going from the left most pixel flipping with the    right most
void MirrorVertical(struct ImageType imgur)
{
   int x, y;

   for(x=0; x < imgur.width; x++)
      {
         for(y=0; y < imgur.height; y++)
            {
               imgur.image[x][y]=imgur.image[(imgur.width*imgur.width)-x-1][y];
            }
      }
}

@nhgrif I originally had it as this

    for(x=0; x < imgur.width; x++)
      {
         for(y=0; y < imgur.height/2; y++)
            {
               temp = imgur.image[x][y];
               imgur.image[x][y] = imgur.image[x][imgur.height-y-1];
               imgur.image[x][imgur.height-y-1] = temp;
            }
      }
}

Where temp was the defined as the struct pixel temp; as a place to hold it.

Dave Boz
  • 49
  • 8
  • `x < imgur.width/2` is probably better... – nhgrif Dec 08 '13 at 02:50
  • Why not change the function to return the struct instead of void return...? – nhgrif Dec 08 '13 at 02:51
  • @nhgrif ah because I am mirroring the image, I am ultimately going to blend two images together, I am just trying to get it to mirror on the side, otherwise I would use width/2. If I was going to return the struct, what would I substitute void for, if I returned the struct. – Dave Boz Dec 08 '13 at 03:00
  • Then I doubt you're going to get the expected results. You're copying the left-side into the right... then expecting to copy the right back into the left... but the right already contains what was on the left (since you copied left into right), so the right half of the image is just completely overwritten. – nhgrif Dec 08 '13 at 03:03
  • @nhgrif so in essence you're saying I literally just a copy of the image, moved it left from right, then right back again, so I'm outputting the same image? – Dave Boz Dec 08 '13 at 03:06
  • No. You're outputting the left half of the image on the left half, and the reverse of the left half of the image on the right half. (Only the right half is actually changed) Are you intending to output the entire image reversed? – nhgrif Dec 08 '13 at 03:07
  • If your intent is to only change the right half, you can stop at `x < imgur.width/2`, but if your intent is to change the entire image, then you need to fix your logic. – nhgrif Dec 08 '13 at 03:08
  • Basically... if your original image were this: `012345`, then what you're currently doing is changing it to look like this: `012210`. If that's intended, stop at `x < imgur.width/2`. If you instead intend to produce `543210` then you need to fix your logic. – nhgrif Dec 08 '13 at 03:11
  • @nhgrif Yes I'm trying to take say the image > and output ><. I'm trying to mirror the image, from left to right on a Y Axis. – Dave Boz Dec 08 '13 at 03:12
  • Then you have an image like this: `012345`, and you need to create `543210` so you can merge the two together and create `012345543210`? – nhgrif Dec 08 '13 at 03:14
  • @nhgrif Exactly. so that's why I was reading in the [x] as 256 and [y] as 256(for each pixel, rgb, and then for every one to the left pixel, copy it and move it to the right while flipping it. – Dave Boz Dec 08 '13 at 03:17
  • @nhgrif which is why I kept y the same, because you are going down each row by -1 and flipping it with the one before and moving by each column by 1 – Dave Boz Dec 08 '13 at 03:18
  • So, your current code will take `012345` and turn it into `012210`, and when you merge the two, you'll get `012345012210`, which isn't what you intend. The standard swap algorithm looks like this `temp = a; a = b; b = temp;` (and you can still stop at `x < imgur.width/2`) – nhgrif Dec 08 '13 at 03:19
  • @nhgrif Why would I use x < imgur.width/2 if I am not going to cut the image in half? Wont that only display half of that image for me? I originally had that, but it would be overwritten in the very first iteration of the loop. – Dave Boz Dec 08 '13 at 03:28
  • If you **swap** the left-most with the right-most pixel, then you've swap everything by the time you get to the middle. – nhgrif Dec 08 '13 at 03:33
  • @nhgrif Yea, but I want the copy on the other side, so like you said, 12345 should be 1234554321. – Dave Boz Dec 08 '13 at 03:40
  • Again, your function is going to take an image `012345` and return `012210`. If you try to append this image to the original, the final image will be `012345012210`. You need to swap `0` and `5`, so the first swap turns `012345` into `512340`, then the second turns `512340` into `542310`, and the third (and final) swap turns `542310` into `543210`, so that THIS can be appended and create `012345543210`. – nhgrif Dec 08 '13 at 03:43
  • What you're currently doing is this: `012345` -> `012340` -> `012310`, `012210`, the you crossover the midpoint. You're trying to copy the 4th element (which used to be `3`) into the 3rd position. But since the 4th element is no longer `3` (you changed it to `2`), the image stays as `012210`. And the same logic will apply to every other attempted swap after the midpoint. – nhgrif Dec 08 '13 at 03:45
  • What you NEED to do is use a `temp` variable. So given `012345`, do this: `temp = 0`, then first = last, so `512345`, now last = temp, so `512340`. And repeat until the midpoint (or else you swap everything twice getting back to original orientation). So you'll end up with `543210`, which you can append to the original image, and get `012345543210`. – nhgrif Dec 08 '13 at 03:46
  • @nhgrif 'temp = image[x][y]; image[x][y]=image[width-x][y] image[width-x][y]=temp;' Where am I hitting the mid-point in my code? Sorry... I am still trying to understand, I honestly don't have the best comprehension in the world, thanks for sticking with me. – Dave Boz Dec 08 '13 at 03:56
  • you need `x < imgur.width/2` in the outer loop and `y < imgur.height` in the inner loop – nhgrif Dec 08 '13 at 04:01
  • Wow.... I really am idiotic.... the imgur.width/2 is used to say stop at the mid-point of the picture because after that its just repition and I have already copied it....I am so sorry... Thanks for your help man I really appreciate it. @nhgrif – Dave Boz Dec 08 '13 at 04:11
  • No problem. But for a laugh, look at the very first comment. – nhgrif Dec 08 '13 at 04:12
  • @nhgrif I know I went back and re-read..... I really deserve a slap in the face...But truthfully by making you yell at me through the keyboard and really explaining it I understand now what I did wrong, for some reason I couldnt see why I would divide it by 2 if I was copying it... and then it hit me that I'm not printing it here.... – Dave Boz Dec 08 '13 at 04:13
  • By the way, while figuring this out was a good exercise and will be useful at some point in the future, @codnodder's solution is what you should be using (the second code snippet) – nhgrif Dec 08 '13 at 04:15

1 Answers1

1

Why not use a pointer?

//Function in order to flip the image, going from the left most pixel
// flipping with the right most
void MirrorVertical(struct ImageType *imgur)
{
  int x, y;
  for (x = 0; x < imgur->width; x++) {
    for(y = 0; y < imgur->height; y++) {
      imgur->image[x][y] = imgur->image[(imgur->width * imgur->width)-x-1][y];
    }
  }
}

MirrorVertical(&img);

EDIT:

This assumes you are trying to modify the original struct in place.

MORE EDIT:

This version creates a mirrored copy of pixel array, leaving the original object unmodified.

// Function in order to flip the image, going from the left most pixel
// flipping with the right most
void MirrorVertical(struct ImageType imgur, struct pixel (*mirror)[256])
{
  int x, y1, y2;
  for (x = 0; x < imgur.width; x++)
    for (y1 = imgur.height-1, y2 = 0; y1 >= 0; y1--, y2++)
      mirror[x][y2] = imgur.image[x][y1];
}

struct pixel mirror[256][256];
MirrorVertical(imgur, mirror);

/* use mirror[x][y].red, ... */
codnodder
  • 1,674
  • 9
  • 10
  • So in the main function I would call MirrorVertical(&img); and then how would I go about printing that then? – Dave Boz Dec 08 '13 at 03:01
  • Also why is it imgur->width not imgur.width, as goes for height? – Dave Boz Dec 08 '13 at 03:02
  • I thought that if you created a global structure, that the structure would never change, even if called in a function, but a copy is used within the function and that is what ends up being outputted. – Dave Boz Dec 08 '13 at 03:04
  • '.' is for objects, '->' is for pointers. See http://stackoverflow.com/questions/5844972/using-arrow-and-dot-opporators-together-in-c?rq=1 – codnodder Dec 08 '13 at 03:04
  • `imgur->width` is the same as `(*imgur).width` In this solution, `imgur` is a pointer and must be dereferenced. – nhgrif Dec 08 '13 at 03:04
  • @codnodder Yes I'm trying to return the mutli-dimensional array, because I am printing a ppm image to, where it mirrors what is inputted within the main program. So if the image is > it will return > – Dave Boz Dec 08 '13 at 03:14
  • @codnodder thats awesome thanks. Why are you using y1 and y2? And I have the pixel mirror[256][256] ironically already set. Also why declare a second struct within the prototype of the function, not just the body? – Dave Boz Dec 08 '13 at 04:07
  • @user3078582 Use whatever subscript arrangement works for you. The one you had seemed funky at first glance, so I substituted a more conventional array flipping algorithm. To be honest I did not really look at yours all that closely so maybe it's fine. – codnodder Dec 08 '13 at 04:11
  • @user3078582 Re second struct... just demonstrating usage. – codnodder Dec 08 '13 at 04:13
  • @codnodder Okay, cool I understand. If I decided to use the first one, which I tried, which is with my original alogirthm, which I edited because it was incorrect thanks to nhgrif, but when I try to compile it, it gives me a message, saying "error: request for member 'ppImage' in something not a strucutre or a union, in my main program, which is for everything that I have labeled in my main program with a imageA. which is defied through my structure in my main program – Dave Boz Dec 08 '13 at 04:26
  • Sounds completely unrelated. – codnodder Dec 08 '13 at 04:30
  • @codnodder gotcha...back to playing around hah – Dave Boz Dec 08 '13 at 04:33