开发者

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?


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.


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)


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] 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜