How to convert GL_RGB pixel data format (image) to GL_RGBA format

the problem as follows:

in delphi 3, i use the glaux dll to load a bmp in to memory, and i want to use some transparent color to make background visible. I have downloaded a lots of tutorials (nehe, Pythian project) but is painfull to do this code run.

i think that must exist the way to do this transformation in a simple subproc or function, reading the information loaded in memory and allocating it with the alpha feature in another memory address.

[This message has been edited by (edited 02-12-2002).]

I think this will help:
allocate an array or a matrix of these dimension:


(i assume black is your trans color)
do a for loop inside the returned bitmap.
read 3 bytes at once. If the 3 bytes are 0,0,0 send them in the new allocated buffer as: 0,0,0,0.
If you read a different color ie blue(0,0,255) you must send it in the second buffer like: (0,0,255,255)
another ex:

this works at least in my test programs…

Hope be useful

Sorry Sorry Sorry

the right formula is:

w * h * 4/3

Sorry Sorry Sorry

thanks andrea, i’ll test the code idea…

Originally posted by Andrea Rossini:
[b]Sorry Sorry Sorry

the right formula is:

w * h * 4/3

Sorry Sorry Sorry[/b]

Umm, actually it’s:

w * h * 4 bytes

(RGBA is 4 bytes per pixel.) I don’t know what the glaux bitmap loader does, but BMP files pad each line to be an even multiple of 4 bytes, so a 3-pixel wide line would be 12 bytes instead of 9 (assuming 24 bit RGB). Make sure to take this into account when reading the BMP data if glaux doesn’t strip out the padding. Of course, if you’re using this file as a texture, it’s highly likely that your lines will already be an even multiple of 4 bytes.

–Travis Cobbs

Here is a code from Pythian project:

procedure TTexture.GenTexFromBMP(BMP: TFastDIB;
useTransparency: boolean; transparentColor: TFColor);
pd :PPixelData;
pxWidth, pxHeight,os:integer;

function XYToOffset(ox,oy:integer):integer;
var pxoffset:integer;
0 1 2 3 4 5 6 7 8 9 PxWidth=10
0 X X X X X X X X X X PxHeight=3
1 X X X X X X X X X X
2 X X X X X X X X X X }
oy := (PxHeight-1) - oy; // now y is OK
pxoffset := ox;
if oy > 0 then
pxoffset := pxoffset + (oy*(PxWidth));
pxoffset := pxoffset * 4; // move into position for RGBA data
Result := pxoffset;

if bmp.Bpp <> 24 then raise Exception.Create(‘TTexture.GenTexFromBMP: Don’‘t support non 24bit pixel formats! (image is ‘+IntToStr(bmp.bpp)+’)’);
pxWidth := bmp.Width;
pxHeight := bmp.Height;
// allocate memory for it, assume RGBA data (4 components)
pd := AllocMem( (bmp.Width*bmp.Height)*4 );
for y := 0 to pxHeight-1 do
for x := 0 to pxWidth-1 do
c := bmp.Pixels24[y,x];
os := XYToOffset(x,y);

  pd^[ os + 0 ] := c.r;
  pd^[ os + 1 ] := c.g;
  pd^[ os + 2 ] := c.b;
  // alpha transparency here
  if useTransparency = false then
    pd^[ os + 3 ] := 255
  else if (c.r = transparentColor.r) and
          (c.g = transparentColor.g) and
          (c.b = transparentColor.b) then
       pd^[ os + 3 ] := 0 else pd^[ os + 3 ] := 255;


// now we have pixel data generate the texture
Pointer(FImage) := pd;
FWidth := bmp.Width;
FHeight := bmp.Height;

it looks good, but again, is hard to do these units works, may be i check how the glaux load a bmp into memory and give this as parameter of the procedure above…(?)

[This message has been edited by (edited 02-12-2002).]

Yes it’s not 3/4 it’s just 4 (sorry again, when I got home I took an eye on my code and I said “woops!it’s 4 not 3/4”)…

I don’t know delphi but your algo seems the same as mine

Load stuff into records/structs it is easier. Gl accepts images in structure/record format for textures, drawpixels etc…

I downloaded the glBMP-D3 delphi unit from the home page of this site. I check it…may be works…

[This message has been edited by (edited 02-13-2002).]