Skip to content

FileFormats

divVerent edited this page Aug 2, 2011 · 2 revisions

S2TC File Formats

S2TC textures are normally stored in DDS (DirectDraw Surface) file format.

Notation & Conventions

  • A "block" is a group of 4x4 pixels. If the image width or height cannot be divided by 4, it gets rounded up for block division purposes. For the pixel data, any padding can be chosen.
  • All n-bit integers are stored in little-endian byte order.
  • Pixels are always read from top to bottom, then left to right.
  • Pixels are stored from least to most significant bit into a word.
  • The same reading order is used for blocks within an image, or for pixels within a block when encoded.

File structure

The file header is 128 bytes long, and contains the following fields, which are stored as 32-bit little-endian integers if not otherwise specified:

Byte position | Data
--------------+---------------------------------------------
0-3           | format identification: string "DDS "
4-7           | always 0x0000007C
8-11          | always 0x000A1007
12-15         | unpadded total image height
16-19         | unpadded total image width
20-23         | length of the first MIP level: (width+3)/4 * (height+3)/4 * blocksize
24-27         | zero
28-31         | number of MIP levels
32-75         | zero
76-79         | always 0x00000020
80-83         | 0x00000005, or 0x00000004 when all pixels are opaque
84-87         | the string "DXT1", "DXT3" or "DXT5", depending on the format
88-107        | zero
108-111       | always 0x00401008
112-127       | zero

After that, a sequence of mipmaps is stored, beginning with the first (largest) level. Each mipmap is encoded as a series of color blocks, possibly preceded by alpha blocks.

The DXT1 Format

In the DXT1 format, no alpha blocks are stored, thus blocksize is 8.

For each block, two colors c0, c1 are chosen; these are 16-bit values in RGB565 encoding (i.e. the 5 most significant bits are the red value, the next 6 bits the green value, and the 5 least significant bits are the blue value). It must always be that c1 >= c0.

The pixel data is encoded as a 32-bit word, of which each two bits represent one pixel. The two bits can have the following values:

Pixel value   | Meaning
--------------+------------
00            | c0
01            | c1
10            | reserved
11            | transparent

Using these values, a color block is encoded as follows:

Byte position | Data
--------------+-------------------------------
0-1           | c0 (16-bit word)
2-3           | c1 (16-bit word)
4-7           | pixel data (32-bit word, 2bpp)

The DXT3 Format

In the DXT3 format, an alpha block is stored before each color block, thus blocksize is 16.

An alpha block is simply a 64-bit value, of which each four bits represent one pixel's alpha value in 4bpp representation.

To encode a color block, two colors c0, c1 are chosen; these are 16-bit values in RGB565 encoding (i.e. the 5 most significant bits are the red value, the next 6 bits the green value, and the 5 least significant bits are the blue value). It must always be that c1 <= c0.

The color data itself is encoded as a 32-bit word, of which each two bits represent one pixel. The two bits can have the following values:

Pixel value   | Meaning
--------------+------------
00            | c0
01            | c1
10            | reserved
11            | reserved

Using these values, an alpha and color block are encoded as follows:

Byte position | Data
--------------+-------------------------------
0-7           | alpha data (64-bit word, 4bpp)
--------------+-------------------------------
8-9           | c0 (16-bit word)
10-11         | c1 (16-bit word)
12-15         | color data (32-bit word, 2bpp)

The DXT5 Format

In the DXT5 format, an alpha block is stored before each color block, thus blocksize is 16.

To encode an alpha block, two alpha values a0, a1 are chosen; these are 8-bit alpha values. It must always be that a1 >= a0.

The alpha data itself is encoded as a 48-bit word, of which each three bits represent one pixel. The three bits can have the following values:

Alpha value   | Meaning
--------------+------------
000           | a0
001           | a1
010           | reserved
011           | reserved
100           | reserved
101           | reserved
110           | transparent
111           | opaque

A color block is identically encoded as in DXT3.

Using these values, an alpha and color block are encoded as follows:

Byte position | Data
--------------+--------------------------------
0             | a0 (8-bit byte)
1             | a1 (8-bit byte)
2-7           | alpha data (48-bit value, 3bpp)
--------------+--------------------------------
8-9           | c0 (16-bit word)
10-11         | c1 (16-bit word)
12-15         | color data (32-bit word, 2bpp)
Clone this wiki locally