View Full Version : bitmap: adding alpha channel

06-05-2004, 05:20 AM
hi, i want to load a bitmap and then
add an alpha channel to enable blending later.
heres my code:


isize = timage[0]->sizeX * timage[0]->sizeY; //get size

*timage[1] = (struct _AUX_RGBImageRec*)malloc(sizeof(struct _AUX_RGBImageRec)); //reserver memory
timage[1]->sizeX = timage[0]->sizeX; //copy width and height
timage[1]->sizeY = timage[0]->sizeY;

timage[1]->data = (unsigned char*)malloc((isize + (isize/3))); //reserve memory for data, old size + 1 channel

for(i=0, h=0 ; i<=isize ; i+=3, h+=4) //loop for copying and adding bytes
timage[1]->data[h] = timage[0]->data[i];
timage[1]->data[h+1] = timage[0]->data[i+1];
timage[1]->data[h+2] = timage[0]->data[i+2];

if(timage[0]->data[i]==0 &amp;&amp; timage[0]->data[i+1]==0 &amp;&amp; timage[0]->data[i+2]==255) //if color is blue
timage[1]->data[h+3] = 0; //set alpha to zero
timage[1]->data[h+3] = 255;

glGenTextures(1, &amp;texture[0]);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, timage[1]->sizeX, timage[1]->sizeY, 0, GL_RGBA, GL_UNSIGNED_BYTE, timage[1]->data); //here it crashes :(((but it crashes at the glTexImage2D call and i dont know why ? if i load the RGB bitmap it works perfectly, where is my mistake ?

06-05-2004, 06:57 AM
the third parameter of glTexImage2D has to be 1,2,3 or 4. In your case, try to change it to 4.

06-05-2004, 07:03 AM
that doesnt change anything.
did anybody do this before and could give me his code ?

06-05-2004, 07:18 AM
did it crash without you adding alpha ?

btw the 1,2,3,4 thing is obsolete (still working so), GL since 1.2 allows quite some more internal formats.

06-05-2004, 07:24 AM
yes it does

06-05-2004, 07:37 AM
i think i located the error
isize = timage[0]->sizeX * timage[0]->sizeY * 3;

i forgot the "* 3", but there are 3 bytes per pixel so its necessary (sorry please dont kill me im new to opengl and programming)

thanks for all who posted