tags:

views:

79

answers:

2

I am working on the implementation of functions for an already written image processing program. I am given explanations of functions, but not sure how they are designating pixels of the image.

In this case, I need to flip the image horizontally, i.e., rotates 180 degrees around the vertical axis

Is this what makes the "image" i am to flip?

    void Image::createImage(int width_x, int height_y)
{
    width = width_x;
    height = height_y;
    if (pixelData!=NULL)
        freePixelData();
    if (width <= 0 || height <= 0) {
        return;
    }

    pixelData = new Color* [width];  // array of Pixel*
    for (int x = 0; x < width; x++) {
        pixelData[x] = new Color [height];  // this is 2nd dimension of pixelData
    }
}

I do not know if all the functions I have written are correct. Also, the Image class calls on a Color class

So to re-ask: what am I "flipping" here?

Prototype for function is:

void flipLeftRight();

As there is no input into the function, and I am told it modifies pixelData, how do I flip left to right?

A: 

well, the simplest approach would be to read it 1 row at a time into a temporary buffer the same size as 1 row.

Then you could use something like std::reverse on the temporary buffer and write it back.

You could also do it in place, but this is the simplest approach.

EDIT: what i;ve described is a mirror, not a flip, to mirror you also need to reverse the order of the rows. Nothing too bad, to do that I would create a buffer the same size as the image, copy the image and then write it back with the coordinates adjusted. Something like y = height - x and x = width - x.

Evan Teran
+1  A: 
Michael Dorgan
I would upvote you except you used the XOR swap - http://en.wikipedia.org/wiki/XOR_swap_algorithm.
Ron Warholic
Hadn't seen that article before. Thanks for the link!
Michael Dorgan
Your code as written won't work against the `pixelData` array as initialized above. The `Color` components in the original post aren't allocated contiguously - you *have* to use double-indirection to access each item.
Dathan
as I understand, the 2dim array is 'pixelData[x][y]', and this is what i am to modify in my function, correct?
codefail