Re: 90 Rotation in one buffer only



> what could be the BEST Memory Optimal way of rotating an image or
> matrix by 90
> degrees using input buffer only? i.e. Input buffer only is the output
> memory buffer.
>
> Say, we can use small amount of memory locations (say 30-40 bytes extra
> considering 1 pixel/byte) for temporary storage of any block/blocks in
> the image but not the whole of image.

all you need is only one tmp pixel:

//assume for simplicity that pixels are integers:
int tmp;

//image width and height
int w, h;

//only first quadrant
int w2 = w / 2;
int h2 = h / 2;

//loop through pixels
for(int j = 0; j < h2; j++) {
for(int i = 0; i < w2; i++) {
tmp = getPixel(i, j);
setPixel(i, j, getPixel(i, h - j));
setPixel(i, h - j, getPixel(w - i, h - j));
setPixel(w - i, h - j, getPixel(w - i, j));
setPixel(w - i, j, tmp);
}
}

//get value of pixel at coordinates x, y
int getPixel(int x, int y);

//set value of pixel at coordinates x, y
int setPixel(int x, int y, int value);

HTH

--
Andrey Kuznetsov
http://uio.imagero.com Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities


.



Relevant Pages


Loading