Targa (TGA) Plug-in Library

From the original Xaede archive. This is historical writing, preserved in a new reading edition. Technical advice, tools and project status reflect the time of publication.

Software links in this reading edition point to the current GitHub repository.

I found that the TGA file format is quite useful in game development, and is used extensively by a number of modern game-engines, including Unreal Engine 3. There are a number of online examples and tutorials describing how to bang out some C or C++ code to load a TGA file so that it can be used by a DirectX or OpenGL application. However, I wanted to create my own library that solves the following problems:

  • It can load the file into a graphics-library compatible format in a single pass (thus being much faster than the online examples I’ve read).
  • Handles arbitrary bit-length encoding.
  • Handles run-length encoded (RLE) Targa files.
  • Automatically converts any TGA file to my desired format – 32-bit RGBA. If an alpha channel is missing, a default one is automatically created. It also converts from black & white encoding.
  • Allows me to apply color-channel masking or replacement by modifying the serialized RGBA value, allowing me to manipulate the image prior to creation as a graphics-library texture.

Given that I’ve never found a real use for Targa color mappings, I dropped support for using them in my reference library. The reference library I created is written in C, and compiles natively on Windows, Linux and Mac OS X. Source code and documentation are available in the GitHub repository.