Code archives/File Utilities/Load TGA (Targa) With Alpha Map

This code has been declared by its author to be Public Domain code.

Download source code

Load TGA (Targa) With Alpha Map by Streaksy2010
I've been tired of loading textures and then applying alpha channel using a seperate image file since forever. It's been more than a little inconvenient. I got tired and looked up TGA format and it's pretty straight-forward. (.PNG format wasn't so straightforward...)

I basically `reversed the polarity' of Skidracer's SaveTGA function to make it load instead of save, checking http://local.wasp.uwa.edu.au/~pbourke/dataformats/tga/ for the TGA specs just incase. I tried this and it worked immediately with a TGA I saved using The Gimp. There are probably some types of TGA it won't load but I estimate that they're the minority.

Check out Skidracer's SaveTGA, also in the code archives.
Function LoadTGA(fn$)
fil=ReadFile(fn$)
idlength=ReadByte(fil)
colourmaptype=ReadByte(fil) 
imagetype=ReadByte(fil)
colourmapindex=ReadShort(fil)
colourmapentries=ReadShort(fil)
colourmapsize=ReadByte(fil)
xorigin=ReadShort(fil)
yOrigin=ReadShort(fil)
width=ReadShort(fil)
height=ReadShort(fil)
bitsperpixel=ReadByte(fil)
attributes=ReadByte(fil)
tex=CreateTexture(width,height,1+2+16+32)
b=TextureBuffer(tex)
LockBuffer(b)
For y=height-1 To 0 Step -1
For x=0 To width-1
col=ReadInt(fil)
WritePixelFast(x,y,col,b)
Next
Next
UnlockBuffer(b)
CloseFile fil
Return tex
End Function

Comments

_PJ_2010
I basically `reversed the polarity' of Skidracer's SaveTGA function

"The engines cannae trake it, Cap'n - we need more dilithium crystals!"

:D

Up till now, I'd been using some other TGA Loading functions from the archives, but they did seem a little like overkill with all the messing about with bits and bytes for alpha channels and colour depth, it was just a tad too much for my needs. This on the other hand, is much more straightforwards, and so far has worked perfectly for me! Thanks, Streaksy!


Streaksy2010
In retrospect I should have allowed texture flags to be passed to the function instead of assuming 1+2+16+32. Easily added.


Code Archives Forum