PocketMatrix forums

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

Alpha Blending (Transparency) Examples.

Hi Everyone,

I've been using a "hacked up" EasyCE library for all my games up until now, and it's still doing what I want, so I don't really want to have to jump to another library. I've effectivly build my own library from Jackos old one.

Anyone, the one thing that the library does not support is good alpha channel transparency. Up until now, I've just been using a transparency color, but this tends to give quite a harsh look.

Are there any really good examples anywhere on how to implement transparency? could someone share a TGA file example. I have alomost no idea, if someone would like to spend a little time explaining the process from Photoshop to code to get the final product I'd be very thankful.

I'm happy to share what my library currently has, in order to support it etc. I'm all for educationing the PDA Dev community.

-thanks.
For the blending of pixels I used Tala's suggestion from this thread:

http://www.pocketmatrix.com/forums/view ... tala+alpha

I prefer to work with PNG files because Photoshop and Inkscape support them and they have the 8bit alpha. Because I use PocketHAL which abstracts the display always as 16bit 565 I make sure all images are stored in that format and have built a little cmd line utility that converts PNG files into a custom format which is in 16-565 format and includes the alpha channel as a 5 bit value (see post above). By doing this there is no need to convert anything at runtime on the ppc - it's just straight blit to surface from there.
Ok, I've read the posts... and I'm completly lost....

heres what I've got so far:
Code:

// Get destenation colors
dcolr = dest[calcd] >> 11;
dcolg = dest[calcd] >> 5;
dcolb = dest[calcd];
// seperate them out
dcolg &= 63;
dcolb &= 31;
// Get source colors
scolr = src[calcs] >> 11;
scolg = src[calcs] >> 5;
scolb = src[calcs];
//seperate them out
scolg &= 63;
scolb &= 31;
//get alpha colors
acolr = alpha[calcs] >> 11;
acolg = alpha[calcs] >> 5;
acolb = alpha[calcs];
//seperate them out
acolg &= 63;
acolb &= 31;
//now try and place the src onto dest using the alpha....
dcolr = scolr - (31 - acolr);
dcolg = scolg - (63 - acolg);
dcolb = scolb - (31 - scolb);
//make sure all the figures a good.
if (dcolr < 0) dcolr = 0;
if (dcolg < 0) dcolg = 0;
if (dcolb < 0) dcolb = 0;
//assign new value to dest.
dest[calcd] = (dcolr << 11) | (dcolg << 5) | dcolb;


The alpha picture is just a grey scale pic. It's doing whats expected atm, placing the src picture onto the background from normal color wheres theres white in the alpha to black, where there is black in the alpha, now, the question is, how do I modify the function to convert the black sections of the alpha to the origional destination data?

-B
That routine is broken, and it is not performing alpha blending. The destination (original) color is completely ignored. The color values are separated out, but then they are overwritten. What this routine is currently doing is applying an inverse intensity to the source. When the alpha channel is white the output is black. As the alpha channel approaches black the source approaches its original intensity.

Alpha blending requires multiplication, to combine together the source and destination pixels, while using the alpha channel as the weight.

Going by the post j.edwards referenced, you want something like this:
Code:

unsigned int mask=0x07e0f81f;
unsigned int source1=dest[calcd];
unsigned int source2=src[calcs];
unsigned int alpha=alpha[calcs]&0x1f;

//spread colors
source1 = (source1 | (source1<<16)) & mask;
source2 = (source2 | (source2<<16)) & mask;

//do alpha op
dest = ((((source1-source2)*alpha)>>5) + source2)&mask;

//recombine colors
dest |= dest >> 16;


Note that in your code you are using an RGB alpha channel. Normally the alpha channel is at most a byte, which is applied to all three channels, instead of having an alpha value for each color component. The code from the other thread appears to work with a 5 bit (32 level) alpha channel, so I'm masking your alpha value by 0x1f to get just one 5 bit component.

Dan East
Yer, I figured since it was greyscale I could use one, but decided to use the KISS principle until I got it working.

Hopefully this will work... :)

OK, almost...

just a few variable conflicts and pumping the result back to dest[calcd]

:)
Ok, All working, sweet as. Next issue.

I've been playing around with photoshop for quite a while, I can create the alpha channel, it all looks sweet etc, but I can't save only the alpha channel as a tga.... talk about frustrating.....

how do you guys do it, just save the alpha as part of the tga, and recode the tga load algo?

-B
For some reason, saving alpha channels in TGA files is really tough. Randall and I worked on this for a few hours to get a technique in place. We ended up using Paint Shop Pro (version 6 at that time) to save the alpha channel to TGA. It was a fairly convoluted process that I can't really remember the specifics of. He never could get a method worked out with Photoshop. I guess saving alpha channel to TGA isn't a very important feature, so it is pretty much an implementation afterthought.

Dan East
I've been having some success by going to channels, only displaying the alpha, selecting all, then copying it, then pasting that into a new layer. It seems to work ok, bit of a long process, particually if your trying to tweek the image. I mite try to "automate" it.

-B
Is there any reason why you have to use TGA's? I would recommend using PNGs.
No other reason other than I'd have to find a png library and then rewrite some code to support png loading and saving.

Do you still load your png's into a buffer in uncompressed format? (like tga) how do you handle the alpha stuff? 2 seperate images?

my tga's are gzipped, so their quite small, but I'd be keen to swap over the png, as I can see the benifits, atm I'm focusing more on game development.

by all means please let me know how it's done easily :)

Also, could the code be spead up with a check to see if the alpha colour is black or white not to do the calcs, just leave dest alone or change it to the source? after all you only need to do the calcs if it's a shade of grey?

-B
In my code I check for alpha == 0 (ignore that pixel and move to next), and alpha == 31 - just copy src pixel directly as no alpha necessary. Haven't done any performance tests, but am assuming it's quicker because most images I use have a large chunk of alpha=31 but with edge transparency.

Png can store a variety of pixel formats but I just use the simple 32bit - 8r, 8g, 8b, 8a - because it's easy to convert. Wouldn't be surprised if you can store in a 16bit 565 format and have an alpha channel as well though.

For png I used libpng (which also required zlib), which took a bit to get building, and I could never get it to build for ppc, which wasn't a big problem for my purposes because most of my images are icons so I convert them on desktop to a simple run-length encoding that isn't as well compressed as png obviously. if you need large images then you will need to spend the time to get it building for ppc so you can store them as png on the client. There are posts in this forum about using jpegs as well which compress better than pngs because png are lossless.

Depending on how each image is used I choose to keep it as run-length encoded in memory or expanded out to a full surface - if it can be drawn without dropping framerate as runlength then that is done to reduce memory, but if there are clipping issues or need maximum performance then expand them out.

It was a bit of work to get it all working, but has made life so much easier since. Now I can just work with the graphics tools like photoshop or inkscape, save as png, double click a batch file that converts all png images in the folder, package them as part of that process, F5 and go. If you are going to get back into ppc development seriously, it's probably worth your while getting it sorted out to start with.
TGA supports RLE compression.

Dan East
yep, but the way my app is coded it's not much use, because I just unzip the tga directly into a buffer. It may be useful if you unpack it in RLE, but then it would make mem allocation a nightmare.

-B
I just got my image loader up and running using the IImagingFactory thats part of WindowsCE. It's quite nice as it loads PNG, JPG, GIF and BMP. It isn't done yet as it only loads from file but once I get it working so that it also loads a BMP from the resources and image decoding from memory, I'll post the code here.

If your just after PNG loading, I found this library which could be adapted in all sorts of ways:

http://homepage3.nifty.com/ysflight/png ... odere.html

As for alpha channels, I stick to PNG's for the compression. Photoshop will allow you to create and save images with alpha channel in TGA and PNG format. While in Photoshop, go to the Windows menu and select layers or hit F7. IN that new window you'll select the channels tab, from there you'll see the RGB channel layout.

If there is great demand for how alpha channels are created and saved in photoshop, I can post a tutorial.
I'm getting some very random results....

1/2 the time it works, the other 1/2 it does not. I can't seem to consistantly create a system to produce an alpha channel with anything other than either black or white..

Maybe it would be easier to get a png library to load a png into a 565 buffer with an 8bit transparency buffer or 565(8) format.

Has anyone does this, feel like sharing?

-B
In my current game engine, I've got my own surface class where I have the 565 layer and alpha layer separate. But I also store the layers as separate PNG images as well. I use the SHLoadImageFile function to load the PNGs into a HBITMAP object, and then a variation of PocketFrog's code to make sure it's in 565 format before transferring the data to my surface class.

The surface class has the following member variables:
int m_iWidth, m_iHeight;
BYTE *m_pPixels, *m_pAlpha;
So the m_pPixels variable is sized based on m_iWidth*m_iHeight*2 and m_pAlpha is only half that size.

I've also got my own blit function that takes into account if the image has an alpha layer, and then uses Tala's single-multiply blending function, which is linked to earlier in this thread.

My new game engine is still evolving, but it works incredibly well for this stuff, and almost all of the little tidbits I use can be found scattered in the forums here. :)
I've extend the PocketHal surface class (called TSurface) and added methods that operate directly on the encapsulated surface. So I can blit another TSurface object into this one, I can draw lines, points, triangles, get the byte pointer and what not.

I too store my pixel and alpha data seperatly. Alpha data is a byte array in the range of 0-31. So the blit alpha routines look to that array for per-pixel operations.
You want to use 0-32 as the range, not 0-31. Otherwise you can't represent fully opaque. Consider this:

With alpha = 31, you get:

(31 red * 31 alpha) >> 5 == 30 red
(63 green * 31 alpha) >> 5 == 61 green

This is not good.

With alpha = 32, you get:

(31 red * 32 alpha) >> 5 == 31 red
(63 green * 32 alpha) >> 5 == 63 green

This is what you want. The original code by Tala does specify the input alpha to be from 0 to 32.
:) My bad you are correct!