S3TC
S3 Texture Compression (S3TC) is a family of image compression algorithms developed by S3 for their Savage 3D line of graphics cards. It is also known as DXTn or DXTC.
Introduction
S3TC is a lossy compression algorithm that transforms blocks of 4x4 RGBA32 pixels to either 64 or 128 bits. The different compression schemes are often denoted by their Microsoft assigned FourCC.
DXT1
16 input pixels are encoded as follows:
uint16_t color0; uint16_t color1; uint32_t pixels;
color0 and color1 are RGB565 color values. pixels are 16 2-bit pixels, referring to color0..3. Each component of color2 and color3 is calculated by:
if (color0 > color1) c2 = (2 * c0 + c1) / 3 c3 = (2 * c1 + c0) / 3 else c2 = (c0 + c1) / 2 c3 = 0
The pixels are stored in raster scan order and start at the least significant two bits of pixels. The alpha channel of each pixel is considered fully opaque, except when color0 <= color1. In that case, the alpha channel of color3 is considered fully transparent.
DXT2/3
16 input pixels are encoded as follows:
uint64_t alpha; uint16_t color0; uint16_t color1; uint32_t pixels;
alpha contains the alpha channel, 4 bits for each pixel. The colors and pixels are encoded similar to DXT1, with the exception that the components of color2 and color3 are always calculated by:
c2 = (2 * c0 + c1) / 3 c3 = (2 * c1 + c0) / 3
DXT2 considers the pixel values premultiplied by alpha. DXT3 does not.
DXT4/5
16 input pixels are encoded as follows:
uint8_t alpha0; uint8_t alpha1; uint48_t alpha; uint16_t color0; uint16_t color1; uint32_t pixels;
color0, color1 and pixels are similar to DXT2/3. The alpha channel is encoded in alpha as 16 3-bit values, referring to alpha0..7. alpha2 to alpha7 are calculated by:
if (alpha0 > alpha1) alpha2 = (6 * alpha0 + 1 * alpha1) / 7 alpha3 = (5 * alpha0 + 2 * alpha1) / 7 alpha4 = (4 * alpha0 + 3 * alpha1) / 7 alpha5 = (3 * alpha0 + 4 * alpha1) / 7 alpha6 = (2 * alpha0 + 5 * alpha1) / 7 alpha7 = (1 * alpha0 + 6 * alpha1) / 7 else alpha2 = (4 * alpha0 + 1 * alpha1) / 5 alpha3 = (3 * alpha0 + 2 * alpha1) / 5 alpha4 = (2 * alpha0 + 3 * alpha1) / 5 alpha5 = (1 * alpha0 + 4 * alpha1) / 5 alpha6 = 0 alpha7 = 255
DXT4 considers the pixel values premultiplied by alpha. DXT5 does not.
Containers
S3TC compressed textures can be found in the following containers:
- DirectDraw Surface (DDS) files
- TXD Renderware TeXture Dictionaries