codefail
codefail

Reputation: 381

Correct flip/mirror of pixels of an image?

http://tinypic.com/r/fwubzc/5

That shows what a flip should be and what a mirror should be.

Code for both types of mirrors:

void mirrorLeftRight()
{
    for (int x = 0; x < width/2; x++) {
            for (int y = 0; y < height; y++) {
                int temp = pixelData[x][y];
                pixelData[x][y]=pixelData[width-x][y]
                pixelData[width-x][y]=temp;
            }
    }
}

void mirrorUpDown()
{
    for (int x = 0; x < width; x++) {
            for (int y = 0; y < height/2; y++) {
                int temp = pixelData[x][y];
                pixelData[x][y]=pixelData[x][height-y]
                pixelData[x][height-y]=temp;
            }
    }
}

Does this seem right for mirrors?

And for flip, just a matter of using width and height w/o dividing by 2?

Upvotes: 0

Views: 25434

Answers (3)

Adriaan Stander
Adriaan Stander

Reputation: 166346

The code above seems more like the correct way to Flip, not mirror.

Mirror I would guess that you not switch the pixels, but rather copy from one side to the other.

With mirror I would guess that you need to change

int temp = pixelData[x][y]; 
pixelData[x][y]=pixelData[width-x][y] 
pixelData[width-x][y]=temp;

to something like this only

pixelData[x][y]=pixelData[width-x][y] 

Upvotes: 1

Jack
Jack

Reputation: 133567

It shouldn't work since you are swapping pixels while you just have to override the right part of the image with the left part. Same thing applies to the mirrorUpDown.

If you swap them you obtain a flip, if you overwrite them you obtain a mirror.

  • mirrorLeftRight: take pixels from left half and use them to overwrite right part
  • mirrorUpDown: take pixels from upper part and use them to overwrite lower one
  • flip: in this case you don't overwrite but you swap pixels (source half it's not influent in this case)

Upvotes: 2

interjay
interjay

Reputation: 110069

You need to use width-1-x instead of width-x, and height-1-y instead of height-y. Otherwise for x==0 you'll try to index [width], which is outside the array.

Upvotes: 5

Related Questions