Adding alpha channel to textures

I’m curious if it’s possible to take a loaded texture (from an image with no alpha value) and in some relatively simple way specify one specific color in it to be transparent…?

I’d really like to be able to do this sort of thing without finding an image loader that is appropriate and works with my compiler(metrowerks codewarrior – none do), etc.


Yes, this should be pretty simple to do. Say for example you are using a 16-bit 565 rgb color format, and you want to make it so that one color is transparent, you could just resample your image to a 16-bit 1555 argb format, using an alpha of 1 for all colors but the one you want to be transparent (in which case you use an alpha of 0). For example:

unsigned short resample_565_to_1555(
unsigned short pixel,
unsigned short transparent_color)
float a, r, g, b;
const float one_over_31= 1.f/31.f;
const float one_over_63= 1.f/63.f;
unsigned short new_pixel;

a= (pixel == transparent_color) ? 0.f : 1.f;
r= ((pixel & 0xF800) >> 11) * one_over_31;
g= ((pixel & 0x07E0) >> 5) * one_over_63;
b= (pixel & 0x001F) * one_over_31;

new_pixel= (a > 0.f) ? 0x8000 : 0x0000;
new_pixel|= (unsigned short)(r * 31.f) << 10;
new_pixel|= (unsigned short)(g * 31.f) << 5;
new_pixel|= (unsigned short)(b * 31.f);

return new_pixel;

(I just whipped that out, but it should give you the general idea)

[This message has been edited by DJ Tricky S (edited 01-14-2001).]