PocketMatrix forums

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

Question regarding alpha blending / anti-aliasing

Maybe Mr. Bikker can help me here :)

I've asked this before, but since I haven't received an answer that I wanted/needed, I will ask it again...

So I've got this image coming out of a 3d rendering program. It's in a format that supports alpha-channels (TIFF, to be exact).

I want to use this image in a game environment. Using simple masks (ie logical operations) produced an ugly 'outline' of near-white or to be more exact near-render background pixels. Rendering an image w/out antialiasing produced 'jagged edge' effect.

So, following suggestions on this board (those that made some sense :p) I rendered to an image format that supports alpha-channels. Now I can open that image (in Photoshop) and view it's alpha channel in all it's glory - nice! But, uh..what now? How do I go from having an image with an alpha channel to alpha blending it w/ some other image? Should I make the a-channel  into a totally seperate image and load both as resources/BITMAP structures? If yes, what then?

Please help. If anyone can do it, I know you can, Jacco ;)
Stop flathering me. It won't work. :)

Anyway, about the alpha stuff: Suppose you loaded your alpha mask and your image to two equally sized buffers, and you want to plot pixel (u,v). So you fetch the alpha like this:

int a = *(alphamap + x + y * image_width);

and the pixel like this:

PIXEL p = *(bitmap + x + y * imagewidth);

Now for an alpha value of zero, you don't want to plot the pixel. For an alpha value of 255 (or whatever the resolution of the alpha values is), you want to replace the pixels that are already in your framebuffer by the pixel 'p'. All values inbetween should cause the pixel to be scaled. Scaling of one color component (in case of a 24bit image) goes like this (with 8-bit alpha mask):

red = (red * alpha) / 256;

or, more efficiently:

red = (red * alpha) >> 8;

The scaled color has to be added to the pixel that is already in the framebuffer. Together, the frame buffer pixel and the sprite pixel should never exceed '100%', so you also need to scale the pixel in the frame buffer by (255 - a).

For 16bit values, scaling of colors has been discussed in the general coding forum. It can be done quite efficiently.

BTW, Fredrik Malmer (RIP) used alpha masks in Argentum. I'm sure he or Carp can help you also.

- Jacco.
So how much different would the routines be for 16 bit bitmaps? How about something even simpler than that - 8 bit (256 colours)?
256 indexed color bitmaps rock for alpha blending. Good thing is that you can scale colors very easily; just create (at load time) 16 palettes with 16 intensities of the original palette. Now you can scale the color with a simple lookup with a 4-bit precision. Of course, you'll need to use the 16bit scaling code for the background, since you'll probably want to use a palette per sprite.

Scaling a color in 16bit mode (copied from the general development forum):

inline unsigned short scalecolor( unsigned short c, int mul )
{
unsigned int p = ((((c&0xF81F)<<11)|((c&0x7E0)>>5))*mul)>>5;
return (((p>>16)&0xF81F)|(p&0x7E0));
}

This returns the scaled color.

- Jacco.
WWWWWWWWhy does the first character of each post disappear? I'm very sure I typed '256', I verified using my back button.

Anyway, it's not a big deal.
Only seems to happen with numbers and sometimes it doesn't even happen--such as when i wish to be demonstrative.
Thanks for the code, I'm sure it will come in handy. Here are the next couple of questions ;)

First, I'm wondering which methods to implement for alpha blending. There's the option of loading two images (sprite and its alpha), scanning the alpha, trying to create a alpha value map on the run and then use it against another bitmap (background) to produce the desired effect. This approach is the most straightforward, if not simplistic. Another way to go about this process would be to pre-process the alpha bitmap and create (before runtime) a table of locations and their alpha values. This would save time since I won't have to scan through the alpha bitmap @ runtime as well reduce the amount of memory used (one less bitmap to load). The problem is creating this table of values, I'm not sure how to go about that.
Second, optimisations. I've heard some really good ideas in regards to optimising the alpha-blending code. One of them mentioned doing calculations and transformation of the loaded bitmaps (eg. for sprites) before the actual rendering loop kicks in, as it only leaves you to add the current framebuffer to the background to produce the desired effect (if anyone could elaborate on that, perhaps in a more practical fashion, I would appreciate it). Also, I think it was on your (Jacco) page that I saw some ideas on how to avoid using floating point variables in the calculations to increase the speed. Finally, I've seen some code for 86x asm that performs alpha blending, and was wondering if anyone's knowledgeble enough to port it into ARM asm. I personally only dealt with 86x and Z80 asm and currently am studying MIPS, so ARM is the grey area for me...

Keep up the good comments, I really appreciate the input!