PocketMatrix forums

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

Simple AlphaBlit

Hey everyone,

I came up with this AlphaBlit function for my new game, and it seems to be pretty quick (no slowdowns on my Axim X5 at least), but I was wondering if you might see some ways to improve it... Right now I'll state that it doesn't do any clipping, so you can run into problems if you try to GetPixel or SetPixel off the surfaces.

Code:
Pixel cWhite = Color(255,255,255);
Pixel cBlack = Color(0,0,0);

// AlphaBlit - John Till 2004
void AlphaBlit(int _iX, int _iY, Surface *_pSource, Surface *_pAlpha, Rect *_rRect)
{
int iRow, iCol, iAlpha, iR1, iR2, iG1, iG2, iB1, iB2;
Rasterizer *r1, *r2;
Pixel p1, p2, p3, pMask;

r1 = pDisplay->CreateRasterizer(_pSource);
r2 = pDisplay->CreateRasterizer(_pAlpha);

pMask = _pSource->GetColorMask();

for(iRow = _rRect->top; iRow < _rRect->bottom; iRow++)
for(iCol = _rRect->left; iCol < _rRect->right; iCol++)
{
p1 = r1->GetPixel(iCol, iRow);
if(p1 != pMask)
{
p2 = r2->GetPixel(iCol, iRow);
if(p2 == cWhite)
{
pDisplay->SetPixel(_iX+iCol-_rRect->left, _iY+iRow-_rRect->top, p1);
}
else if(p2 != cBlack)
{
iAlpha = ((p2 & BLUE_MASK) << (8 - BLUE_BITS));

iR1 = ((p1 & RED_MASK) >> (RED_SHIFT - (8 - RED_BITS)));
iG1 = ((p1 & GREEN_MASK) >> (GREEN_SHIFT - (8 - GREEN_BITS)));
iB1 = ((p1 & BLUE_MASK) << (8 - BLUE_BITS));

p3 = pDisplay->GetPixel(_iX+iCol-_rRect->left, _iY+iRow-_rRect->top);

iR2 = ((p3 & RED_MASK) >> (RED_SHIFT - (8 - RED_BITS)));
iG2 = ((p3 & GREEN_MASK) >> (GREEN_SHIFT - (8 - GREEN_BITS)));
iB2 = ((p3 & BLUE_MASK) << (8 - BLUE_BITS));

pDisplay->SetPixel(_iX+iCol-_rRect->left, _iY+iRow-_rRect->top,
Color((iR1 * iAlpha + iR2 * (255-iAlpha)) >> 8,
(iG1 * iAlpha + iG2 * (255-iAlpha)) >> 8,
(iB1 * iAlpha + iB2 * (255-iAlpha)) >> 8));
}
}
}
}


void AlphaBlit(int _iX, int _iY, Surface *_pSource, Surface *_pAlpha)
{
AlphaBlit(_iX, _iY, _pSource, _pAlpha, &Rect(0,0,_pSource->GetWidth(),_pSource->GetHeight()));
}

I've posted a very simple test below... Anyway, feel free to use it if you want to. And if you have ideas on how to improve it, let me know. :)

-John
Attachments:
alphablit_test.png
I see you're using CreateRasterizer... (see post about depricated functions).

Anyway, the alpha RGB code can be sped up a lot using this trick. The code was posted a while back in this thread http://www.pocketmatrix.com/forums/view ... ight=alpha
Code:

int m_alpha = ((*AlphaPixel & GREEN_MASK)>>(GREEN_SHIFT-2+3 )) ;
if(m_alpha)
{
const unsigned long mask = RED_MASK | BLUE_MASK | (GREEN_MASK << 16);

unsigned long d = *src;
unsigned long s = *dest;

//split into xGxRxB
d = (d | (d<<16)) & mask;
s = (s | (s<<16)) & mask;

//do alpha op
d = ((((d-s)*(m_alpha))>>5) + s)&mask;

//recombine into RGB
d |= d >> 16;
*dest = (Pixel)d;
}


If I recall, it's only a 32 level alpha, but getting it down to a single multiply is worth it.

I thing using surface->Lock would speed things up too, since you wouldn't be calculating an offset into the surfave each pixel.
Great news ! Alphablit is what I'm prying for Pocket PC ;-)
It's the best news I've expected for so long time. Alpha Blit is vital for my !PYROMANIA! project.
Great job!
Please post the final code :wink: