PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Rotating image in memory by 90 degress

Hi, All!

I'm making subj now. Image is 320x240. After rotation it must be 240x320. The significant priority has code speed.
By now, I've wrote a simple piece of code (srcbuf & dstbuf are pointers to unsigned short):

Code:

for( x=0; x<320; x++ )
{
for( y=0; y<240; y++ )
{
//int32_t oldy = 320-x-1;
//int32_t oldx = y;
//dstbuf[240*x+y] = srcbuf[320*oldx+oldy];
dstbuf[240*x+y] = srcbuf[320*y+319-x];
}
}


Have 2 questions.

1. Does somebody know more efficient way to do this?

2. Will I get more speed, if I rewrite this code in arm assembler and call it from C++ function?

Will be glad for any answer. :)
Best regards, Nomad.
Hi Nomad,

the problem with your algorithm is that you'll get a cache miss for almost every pixel. Rotate the image in small blocks ("macro-blocks") such as 40x40 pixels large (which fit nicely into the cache) and you'll notice a speedup of ~500%... :D

Daniel
Hi, Daniel.

I can't understand though, why turning by small blocks will give performance gain.

2-dimentional array is stored as linear chain of WORDs. Turning by 40x40 for example means that I need to copy 40 times for 40 bytes everytime shifting source pointer by (1 line - 40 elements )

In my cycle I do copying one element after another, without jumping.

As I think you've seen performance gain in action, so maybe I'm wrong somewhere in my conclusions.

If is not too hard for you can you explain me more or maybe you have some links to places where it is explained?

Nomad.
The reason why working with small block is faster is simple: it is cache friendly!

Cache misses are one of the major performance problems on mobile phones, because main memory is very slow. To my feeling the performance drop is a lot heavier than on the PC.

So how does using small blocks help?

While you walk in 1 pixel steps through memory in the source image you do 1 line steps (image-width * 2 bytes) in the target image. The first column of target pixels will always result in cache misses. In case of a small block, the 2nd target column will result in hits, because those cache lines that were loaded for the first target column also hold the pixels for the 2nd target column. With a large block (full image) though, you already wrote into so many areas in memory that those cache lines that hold the first pixels of the first column are already caching other parts of memory (those pixels at the bottom of the image). So for the large image you will almost always have cache misses for every target column.

While this explanation might not be fully technically correct (please somebody correct me if I'm wrong) it should give you an idea what is going on. The general rule is: data locality is king.

In an extreme form you could store the complete image in small blocks, which is what modern graphics cards are doing.

Here's a good book on this topic:
http://www.amazon.com/Code-Optimization-Effective-Memory-Usage/dp/1931769249
Although the book is for PC programming, the general ideas hold also for mobile devices.

BTW: If you optimize your code well, you can even do filtered (sub-pixel accurate) rotations at any angle in real time. We recently did a project (http://studierstube.icg.tu-graz.ac.at/handheld_ar/signpost2007.php) were we needed to rotate and zoom a 2D map in real time. Without filtering it looks ugly but with some nice tricks filtering can be done almost as fast as without...

bye,
Daniel
Daniel,

Could you please explain some more how the filter and sub-pixel can be incorporated efficiently?
Thanks.

At the moment, my rotate at any angle function does it in 2 stages - it first draws lines around the outside (simple line drawing) and stores the u,v coord for left and right edges in a buffer and then in the 2nd stage runs through the buffer and interpolates the u,v from left to right. It works, but is not all that fast, but has been handy because it will work for any poly that is concave - or convex - whichever it is without overlaps :). Do you know any better way?
My code is inspired by the implementation of PocketFrog.

A major difference is that I use paletted images, which has several advantages:

It requires only one byte per pixel which is great for large images and improves data locality (the maps we are rendering are 2048x1024 pixels large). We simply store the images in 8-bit PNGs and keep the palette.

Using paletted images allows me storing the actual colors in a lookup table which is optimally prepared for blending.

I'm rotating in macro blocks (small sub blocks instead of the complete image at once), which greatly improves cache locality in worst cases (90° roations). Look into this very same thread for why this helps. The blocks are 16x16 pixels large which showed to be a good size. I also experimented with storing the image itself in macro blocks but didn't notice any pefromance differences. In case of 0° rotation with and without macro blocks yields same speed, but in worst case (90° rotation) it helps a lot. This is of course independent of filtering.

In the inner loop, this is what I do for the non-filtered case:

Code:

unsigned ix = blockU >> 16;
unsigned iy = blockV >> 16;
unsigned char idx = srcPixels[ix + (iy<<BITS_W)];

*blockDest = nPalette16[idx];

blockU += dudx;
blockV += dvdx;
++blockDest;

As can be seen this is pretty forward fixed-point interpolation. Here is the same code for filtered drawing:

Code:

unsigned ix = blockU >> 16;
unsigned iy = blockV >> 16;

unsigned char idxLT = srcPixels[ix + (iy<<BITS_W)],
idxRT = srcPixels[ix+1 + (iy<<BITS_W)],
idxLB = srcPixels[ix + ((iy+1)<<BITS_W)],
idxRB = srcPixels[ix+1 + ((iy+1)<<BITS_W)];

int blendLR = (blockU>>8)&0xff, // blend factor for left-right
blendTB = (blockV>>8)&0xff; // blend factor for up-down

unsigned int pixTop32 = blend32(nPalette32[idxRT], nPalette32[idxLT], blendLR),
pixBot32 = blend32(nPalette32[idxRB], nPalette32[idxLB], blendLR);
unsigned int pix32 = blend32(pixBot32, pixTop32, blendTB);

*blockDest = col32to16(pix32);

blockU += dudx;
blockV += dvdx;
blockDest;


As I said, the lookup-table is stores data in a form optimized for blending:

Code:

inline unsigned int blend32(unsigned int src, unsigned int dst, int alpha)
{
unsigned int diff = src-dst; // difference of all 3 color channels
unsigned int res = dst+ (diff*(alpha>>3)>>5); // blend all at once

res &= 0x03F07C1F; // cleanup unused area
return res;
}

inline unsigned short col32to16(unsigned int col32)
{
return ((col32&0x00007C00)<<1) | ((col32&0x03F00000)>>15) | (col32&0x0000001F);
}


Creating the lookup table (nMap->palette32) values is simply done with:

Code:

if(nMap->pixMap->getFormat()==FORMAT_PALETTED)
{
for(size_t i=0; i<nMap->pixMap->getPalette().size(); ++i)
{
const COLOR_TRIPLE& col = nMap->pixMap->getPalette()[i];
nMap->palette16[i] = ImageTool::convertRGB888_to_RGB565(col.red, col.green, col.blue);
nMap->palette32[i] = ((col.red>>3)<<10) | ((col.green>>2)<<20) | ((col.blue>>3)<<0);
}
}


On fast devices such as the Treo 700W this results in perfectly smooth scrolling, zooming and rotation. On slower devices such as the HTC Excalibur it is still ok.

bye,
Daniel