PDA

View Full Version : extracting tga image from vtd



Nightmare_82
02-08-2004, 05:07 PM
I tried to extract a tga image from a vtd-file. It works, but the alpha channel values are incorrect. here's my code:

function TVTDbManager.extractTgaImage(const p_strFileName: String; const p_strOutputFileName : String): Boolean;
VAR
pSource, pRead, pDest: Pointer;
iSourceSize, iTexWidth, iTexHeight, iTexCount, iRes,
iSig, i, j: Integer;
oBmp: TBitmap;
strKey : TRecordKey;
oResultTga : TBitmapEx;
iPatternWidth : Integer;
iPatternHeight : Integer;
begin
Result:= false;
strKey:= FileNameToKey(p_strFileName);
if m_oVTDb.RecordNum[strKey] <> -1 then
begin
iRes:= m_oVTDb.ReadRecord(strKey, pSource, iSourceSize);
if (iRes <> 0) then
begin
Exit;
end;
end;

pRead:= pSource;

// read signature
iSig:= ReadInt(pRead); // record signature
if (iSig <> $58464741) then
begin
FreeMem(pSource);
Exit;
end;

iTexWidth:= ReadInt(pRead); // texture width
iTexHeight:= ReadInt(pRead); // texture height
iPatternWidth:= ReadInt(pRead); // pattern width
iPatternHeight:= ReadInt(pRead); // pattern height
iTexCount:= ReadInt(pRead); // texture count

if (iTexCount < 1) then
begin
FreeMem(pSource);
Exit;
end;

// create preview bitmap
oResultTga:= TBitmapEx.Create();
oResultTga.Width:= iPatternWidth * iTexCount;
oResultTga.Height:= iPatternHeight;
oResultTga.PixelFormat:= pf32bit;

// render all textures to the same image
for i:= 0 to iTexCount - 1 do
begin
oBmp:= TBitmap.Create();
oBmp.Width:= iPatternWidth;
oBmp.Height:= iPatternHeight;
oBmp.PixelFormat:= pf32bit;

// render scanlines
for j:= 0 to iPatternHeight - 1 do
begin
// source image has 24 byte header (which we skip)
// each scanline has 32-bit pixels
pRead:= Pointer(Integer(pSource) + 24 + (j * iTexWidth * 4) + (i * iTexWidth * iTexHeight * 4));
pDest:= oBmp.ScanLine[j];
// move the pixel data to temporary bitmap
Move(pRead^, pDest^, iPatternWidth * 4);
end;

// draw temporary bitmap on preview bitmap
oResultTga.Canvas.Draw(i * iPatternWidth, 0, oBmp);
oBmp.Free();
end;

oResultTga.SaveToFile(p_strOutputFileName);

oResultTga.Free;
Result:= true;

FreeMem(pSource);
end;

snorga
02-08-2004, 07:27 PM
Not sure guessing:
You are copy from the VTDB to a BMP and then copy the BMP to the TGA image.

But BMP dont have alpha channel, so I would guess that is where the alpha is lost/corrupted.

Could that be it?

Nightmare_82
03-08-2004, 07:53 AM
i also tried TBitmapEx instead of TBitmap, but it didn't work.
The alpha channel is correct for the pixels which are fully transparent and it is also correct for a whole picture which is not transparent. But when I extract a transparent picture, all non-transparent pixels have an incorrect alpha value.

snorga
03-08-2004, 08:16 PM
okay, sorry that I could not be of help then.. :oops:

Maybe the alpha is only stored as 4 bit or something like that?
But that does not make much sense... Do you know what format the textures are stored as?

Nightmare_82
05-08-2004, 12:16 PM
Good idea.
I looked at the source code but the images are converted to format A8R8G8B8 before saving to vtdb.
I use format A4R4G4B4 for transparent and R5G6B5 for non-transparent images.