If it helps, I have some code that creates uncompressed .tga files from a buffer in memory...

It expects a image pixel buffer in bgr format like so as default

Code:
TSomeBuffer = packed record
  b,g,r : Byte;
End;
but you can use the ASwapPixels param to use r,g,b instead.

You can also make it output the pixel rows in reverse (AFlipVertically) if you need to, if the image ends upside down.

[pascal]Unit tga_unit;
{$IFDEF fpc}
{$MODE Delphi}
{$ENDIF}
{$H+}
Interface

Type
PByteArray = ^TByteArray;

Function WriteTGA(Const AFileName : AnsiString;
Const AImageWidth,AImageHeight : Integer;
Const AImageBuffer : PByteArray;
Const ASwapPixels : Boolean;
Const AFlipVertically : Boolean) : Boolean;

implementation

Uses
Classes;

Function WriteTGA(Const AFileName : AnsiString;
Const AImageWidth,AImageHeight : Integer;
Const AImageBuffer : PByteArray;
Const ASwapPixels : Boolean;
Const AFlipVertically : Boolean) : Boolean;
Const
TGAHeader : Packed Array[0..12 - 1] Of Byte = (0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0);

Var
TGAFile : TFileStream;
Header : Packed Array[0..6 - 1] Of Byte;
Bits : Byte;
ColorMode : Integer;
TempColor : Byte;
index : Integer;
Row : Integer;
Begin
// Open file for output.
TGAFile := TFileStream.Create(AFileName,fmCreate);

// Set the color mode, and the bit depth.
ColorMode := 3;
Bits := 24;

Header[0] := AImageWidth Mod 256;
Header[1] := AImageWidth Div 256;
Header[2] := AImageHeight Mod 256;
Header[3] := AImageHeight Div 256;
Header[4] := Bits;
Header[5] := 0;

TGAFile.Write(TGAHeader,SizeOf(tgaHeader));
TGAFile.Write(Header ,SizeOf(Header));

If ASwapPixels Then
Begin
// Now switch image from RGB to BGR.
index := 0;
While index < (AImageWidth * AImageHeight * ColorMode) Do
Begin
TempColor := AImageBuffer^[index + 0];
AImageBuffer^[index + 0] := AImageBuffer^[index + 2];
AImageBuffer^[index + 2] := TempColor;
Inc(index,ColorMode);
End;
End;

// Finally write the image.
If Not AFlipVertically Then
TGAFile.Write(AImageBuffer^,AImageWidth * AImageHeight * ColorMode)
Else
// Write pixels in reverse row order
Begin
For Row := AImageHeight - 1 Downto 0 Do
TGAFile.Write(AImageBuffer^[Row * AImageWidth * ColorMode],AImageWidth * ColorMode);
End;

If ASwapPixels Then
Begin
// Now switch image from BGR back to RGB.
index := 0;
While index < (AImageWidth * AImageHeight * ColorMode) Do
Begin
TempColor := AImageBuffer^[index + 0];
AImageBuffer^[index + 0] := AImageBuffer^[index + 2];
AImageBuffer^[index + 2] := TempColor;
Inc(index,ColorMode);
End;
End;

// close the file.
TGAFile.Free;
Result := True;
End;

end.
[/pascal]

cheers,
Paul