A TGA file has no signature at the start. It begins with an 18-byte header, followed by an optional image ID, an optional colour map, and then the image data. Numbers of two bytes are little-endian, low byte first.
Layout
| Offset | Size | Type | Name | Description |
|---|---|---|---|---|
| 0 | 1 | u8 | ID length | The length of the image ID, 0 to 255. |
| 1 | 1 | u8 | colour map type | 0 none, 1 a colour map follows. 2 to 127 are reserved; 128 to 255 are for developers. |
| 2 | 1 | u8 | image type | How the image is stored. |
| 3 | 2 | u16 | first entry | The index of the first colour map entry stored. |
| 5 | 2 | u16 | colour map length | The number of entries stored. |
| 7 | 1 | u8 | colour map entry size | The bits in each entry: 15, 16, 24 or 32. |
| 8 | 2 | u16 | X origin | The image’s lower-left corner on the screen, horizontally. |
| 10 | 2 | u16 | Y origin | The image’s lower-left corner on the screen, vertically. |
| 12 | 2 | u16 | width | The width of the image in pixels. |
| 14 | 2 | u16 | height | The height of the image in pixels. |
| 16 | 1 | u8 | pixel depth | The bits a pixel takes: 8, 15, 16, 24 or 32. |
| 17 | 1 | u8 | image descriptor | Alpha bits and the direction of the rows. |
Image type
| Value | Meaning |
|---|---|
| 0 | No image data |
| 1 | Colour-mapped, uncompressed |
| 2 | True-colour, uncompressed |
| 3 | Greyscale, uncompressed |
| 9 | Colour-mapped, run-length encoded |
| 10 | True-colour, run-length encoded |
| 11 | Greyscale, run-length encoded |
| 32 | Colour-mapped, compressed with Huffman, delta and run-length coding |
| 33 | The same, with a four-pass quadtree-type process |
Bit 3 of the value (the 8) marks run-length encoding, so types 9, 10 and 11 are types 1, 2 and 3 encoded. Values 0 to 127 belong to Truevision and 128 to 255 are left to developers. Types 32 and 33 are listed in descriptions of the first Truevision manual (Truevision Technical Guide, 1989), for colour-mapped images; the 2.2 specification of 1991 does not mention them, and no decoder, document or sample file giving their layout has been found (see Not described). The explorer names them, reads their header and colour map, and shows the compressed bytes without a picture.
| Type | Colour map type | Pixel depth | What a pixel is |
|---|---|---|---|
| 1, 9 | 1 | 8, 15, 16 | A number of a colour map entry; 8 or 16 bits. |
| 2, 10 | 0 (some programs store a palette here anyway) | 15, 16, 24, 32 | The colour itself, in the layout under Pixels. |
| 3, 11 | 0 | 8 (16 for grey with alpha) | A grey level. |
Colour map fields
The three fields at offsets 3 to 7 describe the colour map. The first entry is the
colour number of the first colour stored, so pixels point to colours from that number
on; the pixel numbers of other entries have no colour (drawn black). Some files (19 found, from one
game-art set) are colour-mapped with a colour map of length 0 and no palette at all; the
specification does not say what that means, and the explorer reports it and shows each
pixel number as a grey level. The entry size can differ from
the pixel depth: a common layout is 24-bit entries for an 8-bit image. When the colour
map type is 0 these five bytes should be zero. Each of the red, green and blue fields
is min(entry size ÷ 3, 8) bits, so a 24 or 32-bit entry has 8 bits per colour and a
15 or 16-bit one 5 bits; spare bits are attribute bits (alpha, overlay or interrupt). A 15-bit
entry is still read from two bytes, ignoring the top bit.
Image descriptor
| Bits | Name | Meaning |
|---|---|---|
| 3–0 | alpha bits | The alpha or attribute bits in each pixel (on TrueVista boards alpha bits, on ICB and TARGA boards overlay bits). The attributes type of the extension area says what they hold. |
| 4 | right to left | 1 if pixels in a row run from right to left. |
| 5 | top to bottom | 1 if the first row in the file is the top one; otherwise it is the bottom one. |
| 7–6 | interleaving | Must be 0 in version 2.0. The original specification used them for interleaved rows: 1 two-way, 2 four-way, 3 reserved. The 2.2 specification does not describe the row order. FFmpeg’s decoder reads them as follows: two-way stores rows 0, 2, 4 and so on, then 1, 3, 5 and so on (counting from the first row in the file’s own order); four-way stores rows 0, 4, 8, …, then 1, 5, 9, …, then 2, 6, …, then 3, 7, …. The explorer follows that. stb_image and Pillow ignore the bits, and no sample file found uses them, so this layout rests on FFmpeg alone. |
| Bit 5 | Bit 4 | Origin of the first pixel stored |
|---|---|---|
| 0 | 0 | Bottom left (the usual case) |
| 0 | 1 | Bottom right |
| 1 | 0 | Top left |
| 1 | 1 | Top right |
Most TGA files are stored bottom-up, with the bottom row first and bit 5 zero, the default of the format.
Telling a TGA from other bytes
An original (version 1) file has no signature, so a reader can only check that the header fields make sense together. The explorer calls a file a TGA when all of these hold; a file that fails one is shown as unknown (and a plain text file, whose first bytes are letters, fails at once, because letters are far above the allowed colour map type):
- It is at least 18 bytes long.
- The colour map type is 0 or 1.
- The image type is 0, 1, 2, 3, 9, 10, 11, 32 or 33.
- With an image: width and height are not zero; the pixel depth suits the type (8, 15 or 16 for colour-mapped and greyscale, 15, 16, 24 or 32 for true-colour); colour-mapped types have colour map type 1; a colour map's entry size is 8, 15, 16, 24 or 32.
- Strictly, the pixels are all there: a raw image has at least
width × height × bytesof data, run-length packets reach the last pixel. A file that otherwise passes but is cut short is still shown, with the damage reported.
A version 2.0 file additionally ends in the footer
with TRUEVISION-XFILE. and a zero byte, which settles it.
Image ID
Straight after the header, the number of bytes given by the ID length field, up to 255. The format does not say what it holds; programs use it for a name or a comment.
Colour map
After the image ID, when the colour map type is 1. Each entry takes as many whole bytes as its entry size needs:
| Entry size | Bytes | Layout |
|---|---|---|
| 15 or 16 | 2 | Bits as A R R R R R G G G G G B B B B B, least significant byte first |
| 24 | 3 | Blue, green, red |
| 32 | 4 | Blue, green, red, alpha |