DXT Compression
DXT Compression is considered a "lossless" compression because almost no pixel data is lost in the compression process. Some subtle colour detail may disappear as the colours are stored as 16 bit RGB 5,6,5 rather than 24 bit RGB 8,8,8. The following are the DXT formats used in the Trainz (N3V) binary textures.
Contents |
DXT1 Compression
DXT1 stores 16 input pixels in 64 bits of output, consisting of two 16-bit RGB 5:6:5 colour values and a 4x4 two bit lookup table.
If the first colour value (c0) is numerically greater than the second colour value (c1) then the two other colours are calculated, such that
c2 = 2/3*c0 + 1/3*c1 and c3=1/3*c0 + 2/3*c1
Otherwise if c0 <= c1 then c2 = 1/2*c0 + 1/2*c1 and c3 is transparent.
The lookup table is then consulted to determine the colour value of each pixel, with a value of 0 corresponding to c0 and a value of 3 corresponding to c3. DXT1 does not store alpha data enabling higher compression ratio of 8:1. DXT1 can be used for masked alpha textures.
DXT3 Compression
DXT3 converts 16 input pixels to 128 bits of output, consisting of 64 bit of alpha channel data (four bits for each pixel) followed by 64 bits of colour data encoded the same way as DXT1 (with the exception that the 4 colour version of DXT1 algorithm is always used instead of deciding which version to use based on the relative values of c0 and c1).
Typically DTX3 is well suited to images with sharp alpha transitions, between translucent and opaque areas. Compression ratio is 4:1.
DXT5 Compression
DXT5 converts 16 input pixels to 128 bits of output, consisting of 64 bits of alpha channel data (two 8 bit alpha values and a 4x4 3 bit lookup table) followed by 64 bits of colour data (encoded the same way as DXT3). If a0 > a1, then six other alpha values are calculated, such that
a2 =(6a0 + a1)/7, a3 =(5a0 + 2a1)/7, a4 =(4a0 + 3a1)/7, a5 =(3a0 + 4a1)/7, a6 =(2a0 + 5a1)/7, a7 = (a0 + 6a1)/7
Otherwise, if a0 <= a1, four other alpha values are calculated as
a2 =(4a0 + a1)/5, a3 =(3a0 + 2a1)/5, a4 =(2a0 + 3a1)/5, a5 =(a0 + 4a1)/5, with a6 = 0, and a7 = 255.
The lookup table is then consulted to determine the alpha values for each pixel, with a value of 0 corresponding to a0 and a value of 7 corresponding to a7.
Because DXT5 uses an interpolated alpha scheme it generally produces superior results for alpha (transparency) gradients than DXT3.
Some consider DXT5 to be the most flexible general purpose compression codec. Compression ratio is 4:1.
DTX Compression Support
Most modern graphics cards support "on the fly" DXT decoding, meaning that DXT compressed textures can be loaded directly by the graphics processor without any need to decode them before loading.