TGA (Truevision TGA, Targa)
| Full name | Truevision TGA (Truevision Graphics Adapter), also called Targa |
|---|---|
| Extensions | .tga; rarely .icb, .vda, .vst |
| MIME type | none registered; image/x-tga and image/x-targa are used |
| Type | raster image, uncompressed or lossless run-length encoding (RLE) |
| Starts with | no magic number. TGA 2.0 files end with the 18 bytes TRUEVISION-XFILE. + 00 |
| Specification | Truevision TGA File Format Specification, Version 2.0 (unofficial PDF copy) |
| Maintained by | nobody (Truevision is defunct); the 2.0 specification is frozen |
| License | open, 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#
- Use when a game engine, texture pipeline or 3D tool expects TGA.
- Use for render output and frame sequences where simple, fast, lossless writing matters more than file size.
- Use when you need straightforward 8-bit alpha that every graphics tool reads the same way.
- Avoid for the web: browsers don’t display TGA. Use PNG, WebP or AVIF.
- Avoid when file size matters: RLE only helps with flat areas; PNG is usually 2-5 times smaller, losslessly.
- Avoid for high bit depth, HDR, color profiles or rich metadata: TGA tops out at 8 bits per channel. Use PNG, TIFF or OpenEXR.
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.
Header#
| 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:
- High bit set: run-length packet. The low 7 bits plus 1 (1-128) is how many times the single pixel value that follows repeats.
- High bit clear: raw packet. The low 7 bits plus 1 is the number of literal pixel values that follow.
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.
Footer and extension area (TGA 2.0)#
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#
- Truevision TGA File Format Specification, Version 2.0: clean PDF conversion of the original Truevision document
- TGA format description at the Library of Congress: versions, structure, footer signature
- Paul Bourke: TGA file format: compact description of the header and RLE with sample code
- Truevision TGA on Wikipedia
Software that opens TGA files#
| Software | Windows | macOS | Linux | Web | Can | Notes |
|---|---|---|---|---|---|---|
| SumatraPDF | ✓ | view | free, open source; also opens TGA inside archives and comic books | |||
| macOS Preview | ✓ | view, convert | built into macOS | |||
| GIMP | ✓ | ✓ | ✓ | view, edit, create, convert | free, open source; RLE and origin options on export | |
| Krita | ✓ | ✓ | ✓ | view, edit, create | free, open source | |
| Paint.NET | ✓ | view, edit, create, convert | free | |||
| Adobe Photoshop | ✓ | ✓ | view, edit, create, convert | commercial | ||
| Blender | ✓ | ✓ | ✓ | view, create, convert | free, open source; textures and render output | |
| IrfanView | ✓ | view, convert | free for non-commercial use | |||
| XnView MP | ✓ | ✓ | ✓ | view, convert | free for private use | |
| XnConvert | ✓ | ✓ | ✓ | convert | batch conversion, free for private use | |
| ImageMagick | ✓ | ✓ | ✓ | convert, create | command line, open source | |
| FFmpeg | ✓ | ✓ | ✓ | convert, create | command line; TGA frame sequences to and from video | |
| Photopea | ✓ | view, edit, create, convert | free, 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 |
| Java | TwelveMonkeys |
| Go | ftrvxmtrx/tga |
| Python | Pillow |
| Ruby | MiniMagick |
| Rust | image, tinytga |
| PHP | GD, Imagick |
Related formats#
- PNG: lossless with alpha, much better compression, works in browsers
- BMP: the other simple uncompressed raster format, common on Windows
- DDS: GPU-compressed textures, the usual replacement for TGA in shipped games
- TIFF: lossless, high bit depths, layers and rich metadata
- PSD: Photoshop documents with layers, the usual source of exported TGA textures