Alpha Channel Problem

Blitz3D Forums/Blitz3D Programming/Alpha Channel Problem

jfk EO-11110(Posted 2005) [#1]
Hi.

I have problems with the Alpha Channel of a Texture.

When I load the texture as a TGA with Alpha Channel, it works nicely.

But the TGA is 4 Megs, and even zipped still pretty huge. So I decided to save the RGB Channels as a JPG, and the Alpha Channel as a seperate JPG. To prevent lossy JPG behaviour on the Alpha Channel, I set all texels Alphabytes to zero if they are less than 10. This way the 4 Megs shrink down to less than 100 kB.

Now there's a problem, some brighter areas of the texture don't seem to be alphaed correctly. It looks like there is some kind of Bitwise overflow that makes a certain brightness half as transparent as slightly darker texels.

When I load the TGA and then apply the alpha channel as I did with the JPG, then it works perfectly. It seems setting the Alpha Bytes taken from the Alpha Channel JPG works ok. It must be something with the RGB JPG. Tho, this JPG was saved with a relative high quality, so it isn't a Lossy-Compression Problem.

Is there anyone who had the same Problem?


jfk EO-11110(Posted 2005) [#2]
Ok, fixed it. It was a stupid bug:

rgb1=readpixelfast(x,y,buffer1)
rgb2=readpixelfast(x,y,buffer2) and $FFFFFF
rgb3=rgb1 or ((rgb2 and $FF)shl 24)
writepixelfast x,y,rgb3,buffer1

should be
rgb1=readpixelfast(x,y,buffer1) and $FFFFFF
rgb2=readpixelfast(x,y,buffer2) and $FFFFFF
rgb3=rgb1 or ((rgb2 and $FF)shl 24)
writepixelfast x,y,rgb3,buffer1


Reading the RGB Pixel from Buffer1 without to mask out the Alpha Channel, and then ORing the Alphabyte of buffer2 to it gave me these strange Alpha values. Kind of a typo.


Barliesque(Posted 2005) [#3]
Sounds like a good idea. Will you add a little sample of this trick to the code archives?


Ice9(Posted 2005) [#4]
Why not PNG


jfk EO-11110(Posted 2005) [#5]
Ok, added to the code archives:
http://www.blitzbasic.com/codearcs/codearcs.php?code=1286

Ice9 - yes, for the Alpha Channel I use a 8-Bit PNG now.

Problem with a 32 Bit PNG with Alpha Channel is: since it's non-lossy you still got enormous filesizes with big textures (eg: 1024%1024, may be 1MB in 32Bit PNG or 100KB in JPG)