Deep Dive

Alpha and RLE: How TGA Works Inside

TGA is one of the simplest image formats around, which makes it a great excuse to look "under the hood" without fear of drowning in detail. We'll go byte by byte: the 18-byte header, types 2 and 10, the RLE algorithm in plain terms, how transparency is stored, and what that cryptic TRUEVISION-XFILE signature is doing at the end of the file.

Translucent crystals with colored highlights — a visual metaphor for the alpha channel and TGA's data structure
Transparency and a simple structure are the two pillars the TGA format stands on. Photo: Pexels

File anatomy: three parts

Open a .tga in a hex editor and you'll see the format is surprisingly logical. Any TGA file (in the modern 2.0 version) consists of three big parts:

  1. The header — a short block at the start describing the image.
  2. The image data — the pixels themselves, compressed or not.
  3. The footer — an optional "tail" at the end with metadata and the format signature.

No per-block checksums like PNG, and no entropy magic like JPEG. That simplicity is TGA's signature trait; if you want to understand where it came from historically, see "What Is TGA".

Don't fear the bytes

There will be specific offsets and values ahead, but the idea is simple: the header says what the image is, the data holds the pixels, and the footer adds metadata. You can grasp the format with no programming.

The heart of the format is a compact header of exactly 18 bytes. It packs everything a decoder needs to read the pixels correctly. Here are the key fields:

Byte 0 — ID Length
Length of the text description after the header (0–255)
Byte 1 — Color Map Type
Whether there's a palette (0 — no, 1 — yes)
Byte 2 — Image Type
Image type and compression (2, 10, etc.)
Bytes 8–11 — Origin
Coordinates of the starting corner (X, Y)
Bytes 12–15 — Size
Image width and height
Byte 16 — Pixel Depth
Bits per pixel: 8, 16, 24 or 32
Byte 17 — Descriptor
Alpha bits and row order

A curious quirk: there's no "magic" signature at the very start of the file. Most formats begin with identifying bytes (PNG with ‰PNG, JPEG with FF D8), but TGA starts straight away with the description length. So you can't reliably identify a TGA by its opening — you either look at the footer signature (see below) or trust the extension.

Image types: 2, 10 and the rest

The header's second byte — Image Type — is arguably the most important field. It decides exactly how pixels are stored. There are several values:

TypeWhat it isCompression
1Color-mapped (palette)None
2True-colorNone
3GrayscaleNone
9Color-mapped (palette)RLE
10True-colorRLE
11GrayscaleRLE

In practice you'll almost always meet two values: type 2 (uncompressed true-color) and type 10 (the same true-color but RLE-compressed). The other types are a legacy of the 1980s, when memory was expensive and images were often stored via a palette. Notice the symmetry: types 9, 10 and 11 are exactly types 1, 2 and 3 plus RLE.

A grid of blue cubes — pixels that TGA stores either back to back or as compressed RLE packets
TGA pixels: type 2 writes them back to back, type 10 groups identical ones into RLE packets. Photo: Pexels

RLE compression, simply put

RLE (Run-Length Encoding) in TGA is wonderfully simple to picture. The image is encoded not pixel by pixel but in packets. Each packet starts with one control byte, and its top bit decides what comes next:

1

Top bit = 1: run packet

The rest of that byte holds a count. Then comes a single pixel and an instruction to repeat it that many times — so "blue, 40 times" takes just a couple of bytes.

2

Top bit = 0: raw packet

The remaining 7 bits give N. Then come N+1 distinct pixels in a row, each written as-is — for areas where everything is varied.

3

Repeat to the end

The decoder reads packet after packet until it has the whole row (and then the whole image).

The clever part is that the decoder never has to "guess": the first bit of each packet tells it directly whether this is a run or a set of distinct pixels. That's why RLE decodes instantly. The downside is that it's only effective where long runs of identical pixels exist: icons, logos, flat backgrounds. On a photograph, where almost every pixel is unique, RLE can even grow the size slightly (because of the control bytes). For more on how this affects file weight, see the comparison "TGA vs PNG".

The first bit of every RLE packet asks "repeat, or read on?" The whole trick of TGA compression fits into a single bit.

Alpha channel: 8 bits for transparency

Now to transparency. How many bits go to the alpha channel depends on the pixel's color depth:

  • 32-bit: 8 bits for red, 8 for green, 8 for blue and 8 for alpha. That's 256 levels of transparency — smooth semi-transparent shadows, glass, smoke.
  • 16-bit: 5 bits per color and just 1 bit of alpha — a pixel is either fully visible or fully transparent, no in-between.
  • 24-bit: color only, no alpha channel.

Exactly how many bits go to transparency is recorded in the low bits of byte 17 (the Image Descriptor). The same byte holds the row-order flag — which is what causes the classic "flipped TGA" problem: old files store rows bottom-to-top, and if a decoder ignores this field, the image ends up upside down.

A translucent green 3D structure — a visual for layers and the levels of alpha-channel transparency
An 8-bit alpha channel gives 256 levels of transparency — from fully visible to invisible. Photo: Pexels

In 1989, version 2.0 of the format arrived and added an optional 26-byte footer to the end of the file. That's the line between old TGA 1.0 and modern 2.0. The footer stores:

  • An offset to the extension area (4 bytes) — which holds the date, author name, gamma, and a thumbnail preview (the "postage stamp").
  • An offset to the developer area (4 bytes) — a place for arbitrary data from a specific program.
  • The signature — the ASCII string TRUEVISION-XFILE. with a trailing null byte (18 bytes).

That TRUEVISION-XFILE signature is TGA 2.0's "ID badge." Since there's no signature at the start of the file, programs use this string at the end to confirm they're looking at a genuine TGA version 2.0 rather than a truncated file or an older version.

18bytes in the header
26bytes in the 2.0 footer
1989year of version 2.0

Why this matters in practice

Understanding TGA's inner workings isn't just trivia. It helps in real situations:

  • Deliberate export. Knowing about types 2 and 10, you understand that the "RLE" checkbox on save is a choice between "smaller size" and "no compression at all" — and both are lossless.
  • 32 vs 24 bit. Need transparency — save 32-bit (with alpha). Don't — 24-bit saves space.
  • A "flipped" image. Now you know the culprit is the row-order flag in the descriptor, and the fix is a vertical flip.

And if your task is simpler — just to open or publish a TGA — there's no need to dig into the bytes: convert the file to PNG and the decoder handles all those headers, types and footers for you.

Don't want to wrangle bytes? Just open the TGA

The free FormatZ converter reads the header, unpacks RLE and keeps the alpha channel — and hands you a finished PNG in a couple of seconds.

Convert TGA to PNG

Need to build a TGA from an image at a specific depth and compression? Use PNG → TGA. The full list of conversions is on the formats page. And to see where all this anatomy is put to use, read "TGA in games and graphics".

These are the value of the header's second byte, which says how pixels are stored. Type 2 is an uncompressed true-color image. Type 10 is the same thing but compressed with the RLE algorithm. There are other types too: 1 and 9 for color-mapped images, 3 and 11 for grayscale.
RLE encodes rows of pixels in packets. Each packet starts with a control byte: if the top bit is 1, it's a 'run' — the next pixel is repeated N+1 times; if it's 0, it's a 'raw' packet — N+1 distinct pixels follow in a row. That stores identical pixels compactly while staying lossless.
In a 32-bit TGA the alpha channel gets 8 bits — that's 256 levels of transparency. In the 16-bit variant alpha gets just 1 bit (a pixel is either visible or not). The number of transparency bits is recorded in the low bits of the 'image descriptor' field in the header.
The footer is an optional 26-byte 'tail' at the end of the file, added in TGA version 2.0 (1989). It holds offsets to the extension area and the developer area, and ends with the signature 'TRUEVISION-XFILE.' plus a null byte. That signature is how you tell a TGA 2.0 from the older 1.0.
It helps you choose export settings deliberately (RLE compression or not, 24 or 32 bit), understand where transparency comes from and why the file is so large, and diagnose issues like a flipped image. But to simply open a TGA, converting it to PNG is enough.