All formats

TGA (Truevision TGA, Targa)

Full nameTruevision TGA (Truevision Graphics Adapter), also called Targa
Extensions.tga; rarely .icb, .vda, .vst
MIME typenone registered; image/x-tga and image/x-targa are used
Typeraster image, uncompressed or lossless run-length encoding (RLE)
Starts withno magic number. TGA 2.0 files end with the 18 bytes TRUEVISION-XFILE. + 00
SpecificationTruevision TGA File Format Specification, Version 2.0 (unofficial PDF copy)
Maintained bynobody (Truevision is defunct); the 2.0 specification is frozen
Licenseopen, no patents

TGA stores raster images either as raw pixels or with simple run-length compression. It supports palette (color-mapped), true-color and grayscale images with 8, 15, 16, 24 or 32 bits per pixel, and a full 8-bit alpha channel in 32-bit files. The format is so simple that a reader fits in a few hundred lines of code, which is why many game engines, 3D programs and texture tools still read and write it.

Today TGA is mostly used for game textures and sprites, frame sequences rendered from 3D programs (Blender, Maya, 3ds Max) and video tools, and as a lossless exchange format between graphics programs. It has no web browser support and weak compression, so it’s not a format for distributing images.

When to use TGA#

File structure#

A TGA file has 5 parts. The last two exist only in TGA 2.0 files:

Part Size Description
Header 18 bytes image type, color map description, size, pixel depth, origin
Image ID 0-255 bytes free-form identification, length given in the header
Color map variable palette entries, only when the header says there is one
Image data variable pixels, raw or RLE-compressed
Extension and developer areas optional metadata, thumbnail, color correction (TGA 2.0)
Footer 26 bytes offsets of the extension and developer areas and the signature (TGA 2.0)

All multi-byte integers are little-endian. Width and height are 16-bit, so the maximum image size is 65535 x 65535.

Offset Size Field Notes
0 1 ID length length of the image ID field
1 1 Color map type 0: no palette, 1: palette present
2 1 Image type see the table below
3 2 First color map entry index of the first palette entry
5 2 Color map length number of palette entries
7 1 Color map entry size 15, 16, 24 or 32 bits
8 2 X origin screen position, rarely used
10 2 Y origin screen position, rarely used
12 2 Width in pixels
14 2 Height in pixels
16 1 Pixel depth 8, 15, 16, 24 or 32 bits per pixel
17 1 Image descriptor bits 0-3: alpha bits per pixel; bit 4: right-to-left; bit 5: top-to-bottom; bits 6-7: must be 0

Image types:

Type Meaning
0 no image data
1 color-mapped (palette), uncompressed
2 true-color, uncompressed
3 grayscale, uncompressed
9 color-mapped, RLE
10 true-color, RLE
11 grayscale, RLE

Pixels and orientation#

True-color pixels are stored as BGR (24-bit) or BGRA (32-bit) bytes. 15 and 16-bit pixels pack 5 bits per channel into a little-endian 16-bit value: ARRRRRGG GGGBBBBB, with the top bit as 1-bit alpha in 16-bit files.

By default rows are stored bottom to top (origin in the lower-left corner). Bit 5 of the image descriptor set (0x20) means the first row is the top row. Many files are written top-down, so a reader must check this bit; ignoring it is the most common TGA bug (upside-down images).

A typical 32-bit file with alpha has pixel depth 32 and descriptor 0x08 (8 alpha bits, bottom-left origin) or 0x28 (top-left origin).

A 2 x 2 uncompressed 24-bit image (blue and green in the bottom row, red and white on top):

00                 ID length: 0
00                 color map type: none
02                 image type 2: true-color, uncompressed
00 00 00 00 00     color map specification (unused)
00 00 00 00        X origin, Y origin
02 00 02 00        width 2, height 2
18                 pixel depth: 24
00                 descriptor: no alpha, origin bottom-left
FF 00 00  00 FF 00 bottom row: blue, green (B G R order)
00 00 FF  FF FF FF top row: red, white
00 00 00 00        footer: extension area offset (none)
00 00 00 00        developer directory offset (none)
54 52 55 45 56 49 53 49 4F 4E 2D 58 46 49 4C 45 2E 00   "TRUEVISION-XFILE." + 0

RLE compression#

Image types 9, 10 and 11 split pixel data into packets. Each packet starts with one byte:

The 2.0 specification says a packet should not cross a scan line, but some writers do it anyway; a robust decoder accepts it. RLE helps with flat colors, masks and sprites, and can make noisy photos slightly larger than raw.

A TGA 2.0 file ends with a 26-byte footer: the 4-byte offset of the extension area, the 4-byte offset of the developer directory (0 when absent), and the signature TRUEVISION-XFILE. followed by a zero byte. Files without the signature are TGA 1.0; they are still valid.

The extension area is 495 bytes: author name, comments, date and time, job name, software name and version, key color, pixel aspect ratio, gamma, offsets to a color correction table, a thumbnail (“postage stamp”) and a scan-line table, and an attributes type byte. Attributes type tells how to treat the alpha channel: 0 no alpha, 1 or 2 undefined data, 3 normal alpha, 4 premultiplied alpha.

Detecting TGA files#

There are no magic bytes at the start, so programs identify TGA by the .tga extension, by the footer signature, or by checking that the header fields are consistent (valid image type, pixel depth 8/15/16/24/32, color map type 0 or 1). Many files in the wild are TGA 1.0 with no footer.

Specifications and documentation#

Software that opens TGA files#

SoftwareWindowsmacOSLinuxWebCanNotes
SumatraPDF✓viewfree, open source; also opens TGA inside archives and comic books
macOS Preview✓view, convertbuilt into macOS
GIMP✓✓✓view, edit, create, convertfree, open source; RLE and origin options on export
Krita✓✓✓view, edit, createfree, open source
Paint.NET✓view, edit, create, convertfree
Adobe Photoshop✓✓view, edit, create, convertcommercial
Blender✓✓✓view, create, convertfree, open source; textures and render output
IrfanView✓view, convertfree for non-commercial use
XnView MP✓✓✓view, convertfree for private use
XnConvert✓✓✓convertbatch conversion, free for private use
ImageMagick✓✓✓convert, createcommand line, open source
FFmpeg✓✓✓convert, createcommand line; TGA frame sequences to and from video
Photopea✓view, edit, create, convertfree, runs in the browser

Libraries#

Open-source libraries that read and write TGA. See all TGA libraries for a comparison.

C/C++Magick++, stb_image
C#ImageSharp, Magick.NET, Pfim
JavaTwelveMonkeys
Goftrvxmtrx/tga
PythonPillow
RubyMiniMagick
Rustimage, tinytga
PHPGD, Imagick