tags:

views:

70

answers:

1

I have some working code that uses IImgCtx to load images, but I can't work out how to get at the alpha channel. For images like .gif's and .png's there are transparent pixels, but using anything other than a 24-bit bitmap as a drawing surface doesn't work.

For reference on the interface: http://www.codeproject.com/KB/graphics/JianImgCtxDecoder.aspx

My code looks like this:

IImgCtx *Ctx = 0;
HRESULT hr = CoCreateInstance(CLSID_IImgCtx, NULL, CLSCTX_INPROC_SERVER, IID_IImgCtx, (LPVOID*)&Ctx);
if (SUCCEEDED(hr))
{
    GVariant Fn = Name;
    hr = Ctx->Load(Fn.WStr(), 0);
    if (SUCCEEDED(hr))
    {
        SIZE  Size = { -1, -1 };
        ULONG State = 0;

        while (true)
        {
            hr = Ctx->GetStateInfo(&State, &Size, false);
            if (SUCCEEDED(hr))
            {
                if ((State & IMGLOAD_COMPLETE) || (State & IMGLOAD_STOPPED) || (State & IMGLOAD_ERROR))
                {
                    break;
                }
                else
                {
                    Sleep(1);
                }
            }
            else break;
        }

        if (Size.cx > 0 &&
            Size.cy > 0 &&
            pDC.Reset(new GMemDC))
        {
            if (pDC->Create(Size.cx, Size.cy, 32))
            {
                HDC hDC = pDC->StartDC();
                if (hDC)
                {
                    RECT rc = { 0, 0, pDC->X(), pDC->Y() };
                    Ctx->Draw(hDC, &rc);
                    pDC->EndDC();
                }
            }
            else pDC.Reset();
        }
    }

    Ctx->Release();

Where "StartDC" basically wraps CreateCompatibleDC(NULL) and "EndDC" wraps DeleteDC, with appropriate SelectObjects for the HBITMAPS etc. And pDC->Create(x, y, bit_depth) calls CreateDIBSection(...DIB_RGB_COLORS...). So it works if I create a 24 bits/pixel bitmap but has no alpha to speak of, and if I pass a 32 bits/pixel bitmap it fills out the RGB colour channels and leaves the alpha channel 0x00. If I OR the alpha channel with 0xff to set the image to fully opaque then it looks the same as the 24 bpp option. Now this interface apparently is used by Internet Explorer to load images, and obviously THAT supports transparency, so I believe that it's possible to get some level of alpha out of the interface. The question is how?

(I also have fall back code that will call libpng/libjpeg/my .gif loader etc)

+1  A: 

GDI does not support alpha channels. When Draw method is called for PNGs and GIFs some internal alpha blending method make its work, it does not touch alpha channel of bitmap selected in DC, it only combines color channels using internally stored alpha channel data of the PNG\GIF image.

For 32-bit BMPs situation is quite different, there is no alpha blending, all bits are just copied into DC. If bit depth of bitmap selected in DC and bit depth of bitmap to be drawn on DC are matched then drawing on DC is just like a memcpy call. That is why sometimes there is an illusion that GDI supports alpha channels, but in fact that is just a dumb copying of bytes and copied alpha channel do not make any difference in resulting image.

In other words, do not use GDI or any library based on it for work with alpha channels. Use GDI+, about 10 years elapsed since its release and it may be found almost on all Windows computers.

actual