PDA

View Full Version : How to convert GL_RGB pixel data format (image) to GL_RGBA format



Coloro.cl
02-12-2002, 06:16 AM
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 Coloro.cl (edited 02-12-2002).]

Andrea Rossini
02-12-2002, 06:32 AM
I think this will help:
allocate an array or a matrix of these dimension:

Widht*height*3/4

(bytes)
(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:
(123,31,200)--------->(123,31,200,255)

this works at least in my test programs...

Hope be useful

Andrea Rossini
02-12-2002, 06:34 AM
Sorry Sorry Sorry

the right formula is:

w * h * 4/3

Sorry Sorry Sorry

Coloro.cl
02-12-2002, 07:25 AM
thanks andrea, i'll test the code idea...

tcobbs
02-12-2002, 02:59 PM
Originally posted by Andrea Rossini:
Sorry Sorry Sorry

the right formula is:

w * h * 4/3

Sorry Sorry Sorry

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

Coloro.cl
02-12-2002, 05:27 PM
Here is a code from Pythian project:

//-------------------------------------------
procedure TTexture.GenTexFromBMP(BMP: TFastDIB;
useTransparency: boolean; transparentColor: TFColor);
var
pd :PPixelData;
x,y:integer;
c:TFColor;
pxWidth, pxHeight,os:integer;

function XYToOffset(ox,oy:integer):integer;
var pxoffset:integer;
begin
{
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
begin
pxoffset := pxoffset + (oy*(PxWidth));
end;
pxoffset := pxoffset * 4; // move into position for RGBA data
Result := pxoffset;
end;

begin
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
begin
for x := 0 to pxWidth-1 do
begin
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;
end;
end;

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

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...(?) http://www.opengl.org/discussion_boards/ubb/confused.gif

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

Andrea Rossini
02-12-2002, 09:30 PM
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

Gavin
02-13-2002, 03:12 AM
Load stuff into records/structs it is easier. Gl accepts images in structure/record format for textures, drawpixels etc...

Coloro.cl
02-13-2002, 05:01 AM
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 Coloro.cl (edited 02-13-2002).]