Dxt Image Compression

Source: Internet
Author: User

During the past two days, decode the data in the DDS format.Program. DDS is an image format developed by Microsoft for DirectX. On msdn, you can find its file format description: DDS file reference.

The dxt image compression format is now supported by the vast majority of 3D graphics card hardware. (It uses a Lossy Image Compression Algorithm invented by S3.Algorithm. BTW, mentioned in my book p232 ). The dxt format is also called s3tc. Currently, only. Dds files support this compression in image formats that can be directly displayed by popular image watching software. For ease of development, our engine also supports loading. Dds files.

Our engine colleagues hope that even when the hardware does not support it, we can load and use textures normally, so we have the need for dxt soft decoding.

Fortunately, I have studied some data before and it is easy to write.

Dxt1 supports a 1-bit alpha channel. This is actually optional. Each 4x4 block can have or does not have this transparent channel as needed. When the alpha channel is not required, each block can have four colors (two of which are obtained by interpolation). When the alpha channel is required, there can be only three colors, 11. It is reserved for describing transparent points. To identify whether a channel is used, the two high-color values starting from each block must be used:Color_0AndColor_1. If the color_0 value is greater than the value (treated as an unsigned short integer ),Color_1There is no channel.

Unfortunately, there is no description in the file header of the DDS file: whether at least one block of the entire image contains the alpha channel. However, in a 3D program, you need to know this information so that the program can run more efficiently.

For soft decoding programs, you need to know this information. Because when there is a channel, we need to decode the data into the rgba5551 format. If there is no channel, we need to decode it into the rgb565 format.

At first I thought I needed to scan the entire data segment and check whether there was at least one blockColor_0Less than or equalColor_1. I saw several DDS files generated by tools before I found myself wrong. When NVIDIA dxt tools compresses dxt1 images, you must manually specify whether a 1-bit channel is required. If you do not specify a channel, the order of the color values of the two color palette on each 4x4 data block is irrelevant (due to the difference in the interpolation scheme, it is possible to get a better image quality ). That is to say, only the person who compresses the image knows whether there is a channel on the image, but there is no record on the file header.

Dxt3 adds a 4-bit alpha channel based on dxt1. Each 4x4 uses 64-bit to store the alpha channel information. (When data is stored, the alpha channel information is placed before the color information in each data block)

Dxt5 improves the storage of Alpha channels. It is worth mentioning. It still uses 64 bit to store 16 alpha information. The first two bytes (16 bit) Save the maximum Alpha and minimum Alpha values of the current block. In the next 48 bits, each pixel occupies 3 bits, which exactly describes 4x4 pixels.

WhenAlpha_0GreaterAlpha_1The 3bit behind us can represent 8-level interpolation; otherwise, we retain 110 and 111 respectively indicating that Alpha is 0 and 255, and there can be 6 levels of excessive interpolation in the middle.

For the dxt3 and dxt5 compression algorithms, you can also find detailedArticle: Textures with Alpha Channels

Dxt2 and dxt4 are rarely used in practice. In terms of data compression algorithms, they are completely equivalent to dxt3 and dxt5. The difference is only that the color data is pre-multiplied by Alpha.

since we can use a dedicated 4-byte identifier in the DDS file for Alpha pre-multiplication, I'm surprised that, the important channel information in dxt1 is not displayed in the DDS file header. I really don't know what people think about the design file format. >_

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.