Jump to content
  • Advertisement
Sign in to follow this  
Evil Steve

DXT1 Decompression

This topic is 2567 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts


I'm trying to decompress DXT1 format data to 32-bit ARGB, but the colours I'm getting disagree with those that Paint.Net and Photoshop (7 + NVidia DDS plugin) give.

The top left 4x4 pixel block shows up in Paint.Net / Photoshop as:
TRANS TRANS RGB(16, 239, 33) RGB(24, 233, 28)
TRANS RGB(16, 239, 33) RGB(24, 233, 28) RGB(24, 233, 28)
RGB(16, 239, 33) RGB(24, 233, 28) RGB(24, 233, 28) RGB(33, 227, 24)
RGB(24, 233, 28) RGB(24, 233, 28) RGB(33, 227, 24) RGB(33, 227, 24)

And the binary layout for this block is:
0x64 0x17 0x03 0x27 0x8f 0xa3 0x68 0x5a

I read that as:
colour_0 = 0x1764
colour_1 = 0x2703
mask = 0x5a68a38f

The mask then corresponds to:
11 11 00 10
11 00 10 10
00 10 10 01
10 10 01 01
Which seems correct - the top left corner is transparent at least.

colour_0 becomes RGB565(2, 59, 4) - which is RGB(16, 236, 32) when converted to 24-bit

Paint.NET seems to be getting RGB(16, 239, 33) for colour_0, which is similar but slightly different to what I get. However, How can it have got the value 239 from a 6-bit green value? 239 has the low bit set, which means it can't have been generated from a 6-bit value left shifted by two, surely?

Am I missing something obvious here? The thing that threw me was that Paint.Net and Photoshop get the exact same colours out, which are different from what I get...

Any help is appreciated!


Share this post

Link to post
Share on other sites
A typical (and more correct) method for expanding a value to more bits in this case (such as the 6 to 8 bit expansion you're asking about) is to do something like:

(v << 2) | ((v >> 4) & 0x3)

That ensures that your up-sampled values cover the full range of original inputs (with evenly spaced interpolants). (Otherwise, you can never 255 back, for example).

Share this post

Link to post
Share on other sites
I would call it linear scaling: y = (255 * x) / 63 - this will give you full range mapping [0..63] to [0..255]. Then green value would be (255 * 0x3B) / 63 = ~238.81 which rounds to 239.

If you would use y = x * 4 then 63 would turn 252, which obviously isn't maximum value for 8-bit channel.

More universally - if you have input range [a0..a1], and you want to map it to output range [b0..b1], then following formula will do the trick (it is equation for line through two points):

b = (b1 - b0) / (a1 - a0) * (a - a0) + b0.

In our case a0 = b0 = 0, b1 = 255, and a1 = 63.

Share this post

Link to post
Share on other sites
The technique is (also) known as bit replication; you replicate the higher order bits to the lower order bits after shifting. Just like Martins said indirectly; upscaling by the power-of-two difference results in zeros in the lower order bits, and you can never expand the source value into the destination value's full range. By replicating the bits, you approximate the linear scaling in the lower bits by the scaling in the higher order bits, which will linearly scale from all 0's up to all 1's.

The method in general is fairly accurate and differ most of the time only in the least significant bit.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!