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