Results 1 to 5 of 5

Thread: extracting tga image from vtd

  1. #1

    extracting tga image from vtd

    I tried to extract a tga image from a vtd-file. It works, but the alpha channel values are incorrect. here's my code:
    [pascal]
    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;
    [/pascal]

  2. #2

    extracting tga image from vtd

    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?

  3. #3

    extracting tga image from vtd

    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.

  4. #4

    extracting tga image from vtd

    okay, sorry that I could not be of help then.. ops:

    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?

  5. #5

    extracting tga image from vtd

    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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •