DDS

From SimsWiki
Jump to: navigation, search
This article is imported from the old MTS2 wiki. You can help Sims2Wiki by cleaning it up. It's original page with comments can be found at http://old_wiki.modthesims2.com/DDS

DDS Format

Ok, was tinkering with DXT stuff. Some information that might be helpful when writing our own decoders. DXT is the DirectX Texture compression technology, and there are currently 5 different formats defined:

DXT1 - Color + 1bit alpha DXT2 - Color + premultiplied alpha (type 1) DXT3 - Color + alpha (type 1) DXT4 - Color + premultiplied alpha (type 2) DXT5 - Color + alpha (type 2)

DXT Color compression

Color information is stored the same for all DXT formats, except in the case of one-bit transparency, or masked transparency (it either is or isn't). Color information is stored in a 64bit block of data as follows:

WORD WORD BYTE BYTE BYTE BYTE

Where the two WORDs are color_1 and color_2, respectively, and the four BYTEs are bytes, divided into 2bit pairs, in a 4x4 grid representing a 4x4 block if pixels. Color information is stored in color_1 and color_2, where color_1 is the highest-valued color, and color_2 is the lowest-valued color in the 4x4 block of pixels.

The determination of color_1 and color_2 is done by a color quantization algorithm, which I think can be arbitrary, its not predefined. If color_1 is a lower-valued color than color_2, one-bit alpha storage (DXT1 format) of the pixel block is used, providing a total of 3 colors and 1 transparency value. If color_1 is a higher-valued color than color_2, non-alpha storage (DXT2-5) of the pixel block is used, providing a total of 4 colors.

When two colors are stored, up to 4 colors can be determined through interpoloation. In non-alpha storage the four possible colors are:

bit pair color 00 color_1 01 color_2 10 2/3 color_1 + 1/3 color_2 11 1/3 color_1 + 2/3 color_2

In a 4x4 block of 16 pixels, reducing color information to 4 possible colors provides huge gains in compression, and barely noticable loss in image quality.

In one-bit alpha storage, the three possible colors and transparency are defined as follows:

bit pair color 00 color_1 01 color_2 10 1/2 color_1 + 1/2 color_2 11 Transparent pixel

When decompressing a DXT color block, read the two WORDS, then read one byte at a time and interpolate color values. Write the resulting fill 24bit color values to the image, keeping in mind that the remaining 3 bytes are for pixels on the 3 following rows of the image, at the appropriate starting column.

DXT Alpha compression

Alpha information is stored for DXT2-5 format color blocks (where color_1 is a higher-value color than color_2). There are two possible ways to store alpha information in the 64bit additional block. The first method is used with DXT2/3 format images, and the second method is used with DXT4/5 format images.

In DXT2/3, alpha pixels are stored in a 64bit block of 4 WORDS. Alpha values are stored in 4bit nibbles, a total of 16 values, in a 4x4 grid that corrosponds to the color pixels of the associated color block. The alpha information is read directly, without interpolation, and is on a scale 1/17th the full 0-255 (256 value) standard for 8bit color. A value of 15 is multiplied by 17 to get 255, where a value of 9 is multiplied by 17 to get 153, etc. This is the simplest and more accurate method of storing alpha information for DXT compressed images.

In DXT4/5, alpha pixels are stored in a 64bit block of data structured similar to that of color data. Two 16bit alpha values are stored in WORDs alpha_1 and alpha_2. Alpha pixel information is stored in 3bit values, and interpolates between the two 16bit alpha values. The structure of this block is more complex, and I'll have to describe it later. Suffice to say, its complex compared to DXT2/3.

Encoding DXT information is quite easy, as you just store it following the schema defined above. The hard part is finding an accurate and quick color quantization algorithm for finding the highest-value and lowest value pixels in the 4x4 block of raw image data. There are numerous ways to quantitize color data, and I'm not very familiar with any of them.

Personal tools
Namespaces

Variants
Actions
Navigation
game select
Toolbox