Question

In my engine I have a need to be able to detect DXT1 textures that have texels with 0 alpha (e.g. a cutout for a window frame). This is easy for textures I compress myself, but I'm not sure about textures that are already compressed.

Is there an easy way to tell from the header whether a DDS image contains alpha?

Was it helpful?

Solution

As far as I know, there's no way to tell from the header. There's a DDPF_ALPHAPIXELS flag, but I don't think that will get set based on what's in the pixel data. You'd need to parse the DXT1 blocks, and look for colours that have 0 alpha in them (making sure to check that the colour is actually used in the block, too, I suppose).

OTHER TIPS

I agree with the accepted answer. Your job may be made a bit easier by using the "squish" library to decompress the blocks for you.

http://www.sjbrown.co.uk/?code=squish

DDS is a very poor wrapper for DXT (or BTC) data. The header will not help you. Plain original DXT1 did not have any alpha. I believe d3d nowadays does actually decode DXT1 with alpha though. Every DXT1 block looks like this: color1(16 bits) color2(16 bits) indices(32 bits). If the 16 bit color1 value is less than color2 (just a uint16 comparison, nothing fancy!) the block has no alpha. Otherwise it does. So to answer you question: Skip the header, read 16 bits a, read 16 bits b, if a>b there is alpha. otherwise skip 32 bits and repeat until eof. Other DXT formats like DXT5 always have alpha. It is very rare that people rely on the DXT1 alpha trick because some hw (intel..) does not support it reliably.

No, the DDS header only uses alpha flags for uncompressed images. I had a similar need to figure out if a DXT1 image was using 1-bit alpha and after a long search I came across this reference here: https://msdn.microsoft.com/en-us/library/windows/desktop/bb147243(v=vs.85).aspx

Basically if color_0 <= color_1 then there is a possibility the texture has 1-Bit alpha. To further verify it, you need to check the next 32-bits in 2-bit pairs if they are 11. Then continue this for every block if not found.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top