Detecting .png alpha channel in Windows/D3D
I'm loading a texture from .png using D3DXCreateTextureFromFile(). How can my program kn开发者_StackOverflow社区ow if the image file contains an alpha channel?
This isn't too hard to do by simply examining the file.
A PNG file consists of:
- A file header
- One or more 'chunks'
The file header is always 8 bytes and should be skipped over.
Each chunk begins with 4 bytes indicating its length, and 4 bytes indicating its type. The first chunk should always be 13 bytes and have the type IHDR
. This contains the information about the image.
The tenth byte in the header contains the exact information you're looking for. It will be equal to 6
if the PNG file is RGBA.
More information can be found here.
Call IDirect3DTexture9::GetSurfaceLevel
and then call IDirect3DSurface9::GetDesc
. The D3DSURFACE_DESC.Format member will tell you.
精彩评论