Search code examples
c++winapigdi

How to get the pixelformat layout using the Windows API


Given an HDC, I would like to figure out not only how many bits of Red, Green, Blue and Alpha there are (using DescribePixelFormat), but even their layout (BGR(A) or RGB(A)) so that I can perform pixel and color component level manipulation.

Should I always assume they're in BGR(A) order? Is that a reasonable assumption?


Solution

  • http://msdn.microsoft.com/en-us/library/dd183449(VS.85).aspx

    That's the docs for COLORREF which is stored as 0x00BBGGRR.

    According to that which is used by most of WINAPI, I'd have to assume that everything on Windows is actually stored in BGRA format.. AFAIK, DIBs are in BGRA format.

    You can try painting a window red. Create a DIB section and copy the pixels into a buffer. If the first byte is 0xFF, it is RGBA otherwise it is BGRA.

    GDI32 images always stores as BGRA on my machine. GDI24 images are always BGR. I have released an Image handling API and of all the users, I haven't met a single one that has not had the same format. I have yet to see a DC or default backbuffer format in RGBA instead of BGRA.

    What is the reason you really need to know the format of a DC? I never found myself wanting to know.

    When reading an image, you figure out the bit count from the BitmapInfoHeader.biBitCount field.