The pixels follow the colour map, or
the header and image ID if there is none. There are width × height pixels
of a fixed size. Types 1, 2 and 3 store them in full; types 9, 10 and 11 pack them in
run-length packets.
Pixels
Each row holds width pixels, and the rows come bottom row first unless bit
5 of the descriptor says otherwise. A pixel takes as many whole bytes as its depth
needs:
| Depth | Bytes | True-colour pixel |
|---|---|---|
| 15 | 2 | Bits x R R R R R G G G G G B B B B B, least significant byte first |
| 16 | 2 | The same, with one attribute bit in the top bit: alpha, or on old boards an overlay or interrupt bit. The attributes type says which; without an extension area the explorer treats the bit as an overlay bit and shows the picture opaque. |
| 24 | 3 | Blue, green, red |
| 32 | 4 | Blue, green, red, alpha |
A colour-mapped pixel is a colour number, 8 or 16 bits: it points to the colour map entry of that number, counting from the first entry given in the header. A greyscale pixel of 8 bits is the grey level; at 16 bits it is the grey level followed by 8 bits of alpha.
Whether the alpha bits are alpha is decided in this order, and the explorer follows it:
- If the extension area gives an attributes type, 0, 1 and 2 mean the alpha field is not usable (the picture is shown opaque; for 1 and 2 an option below can change that), 3 means useful alpha, and 4 means pre-multiplied alpha: each colour has already been multiplied by alpha, so a viewer that blends straight alpha divides it out first. With 3 or 4 the top bit of a 16-bit true-colour pixel is alpha too, and a greyscale or 32-bit pixel's alpha is read whatever the descriptor says.
- With no extension area, alpha is read when the descriptor's alpha bit count is not zero (the fourth byte of a 32-bit pixel, the second byte of a 16-bit grey pixel, the alpha byte of a 32-bit colour map entry). A 32-bit file with a count of 0 leaves its fourth byte meaningless and is shown opaque; so is a 16-bit true-colour file with no attributes type, since the top bit was an overlay bit on the boards that wrote it.
The bytes cannot say which reading a file whose extension area has no attributes type (or has type 1 or 2) wants, and the programs differ, so the explorer shows an option at the top of the window, Alpha without an attributes type, with four readings. The first is the default.
- Truevision’s specification: the descriptor’s alpha bit count decides, as in the list above.
- ImageMagick (
coders/tga.c): alpha is used for any 32-bit image or when the descriptor’s count is not zero; the top bit of a 16-bit true-colour pixel is then alpha, 1 opaque and 0 transparent. It ignores the attributes type. - Pillow (
TgaImagePlugin.py,Unpack.c): the fourth byte of a 32-bit pixel and the second byte of a 16-bit grey pixel are always alpha, and so is the top bit of a 16-bit true-colour pixel, read inverted (BGRA;15Z: 1 transparent, 0 opaque). Only an attributes type of 0 makes the picture opaque; types 1 and 2 are read as alpha. Pillow also expands 5-bit colours by truncating (v × 255 / 31) where the explorer rounds, so a colour can differ by 1. - Ignore alpha (stb_image reads 15 and 16-bit pixels as RGB with no alpha; FFmpeg reads them as RGB 5-5-5): the picture is opaque.
An attributes type of 0 (opaque), 3 (alpha) or 4 (pre-multiplied) decides by itself, whatever the option says. Sources: Truevision TGA File Format Specification (field 24, attributes type), and the program sources named above.
Run-length packets
Types 9, 10 and 11 replace the rows with packets. Each packet starts with a one-byte header:
| Header bit | Name | Meaning |
|---|---|---|
| 7 | type | 1 a run, 0 a raw packet. |
| 6–0 | count | The pixel count minus one: 0 to 127, for 1 to 128 pixels. |
A run packet is followed by one pixel, to be repeated count times; a raw
packet by count pixels as they stand. Pixels are in the layout above.
83 00 00 FF 01 00 FF 00 FF 00 00
Packets and rows
Version 2.0 of the specification says a packet should not hold pixels from more than one row, even when the end of one row matches the start of the next. Version 1 allowed packets to run across rows, and a reader must still accept such files. The explorer reads on across rows and reports a packet that crosses one. A run packet that has more pixels than the image still needs is reported and the extra pixels are dropped; a packet that the data does not finish is reported, and the bytes of it are shown as left over. The explorer’s packet table lists every packet with its offset, kind, pixel count, first pixel, and the row and column its first pixel lands on.