View Full Version : texture alpha from GIF transparency

02-02-2006, 05:18 PM
I have some code that loads GIF files as texture maps. It all works well, but I can't get the transparency information to come through to the final image.

Here's some code extracts...

// Load image - OleLoadPicturePath supports most common picture formats...
HRESULT hr = OleLoadPicturePath(wszPath, 0, 0, 0, IID_IPicture, (void**)&pPicture);

... in here, I do a bit of scaling to get 2^x dimensions for m_iWidth and m_iHeight

// Now Create A Temporary Bitmap
BITMAPINFO bi = {0};
bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bi.bmiHeader.biBitCount = 32;
bi.bmiHeader.biWidth = m_iWidth;
bi.bmiHeader.biHeight = m_iHeight;
bi.bmiHeader.biCompression = BI_RGB;
bi.bmiHeader.biPlanes = 1;

hbmpTemp = CreateDIBSection(hdcTemp, &bi, DIB_RGB_COLORS, (void**)&pBits, 0, 0);

// Render GIF image onto bitmap
SelectObject(hdcTemp, hbmpTemp);
pPicture->Render(hdcTemp, 0, 0, m_iWidth, m_iHeight, 0, lHeight, lWidth, -lHeight, 0);

// .. then do a bit of
for(long i = 0; i < m_iWidth * m_iHeight; i++) {
pPixel = (BYTE *)&amp;(pBits[i]);
temp = pPixel[0];
pPixel[0] = pPixel[2];
pPixel[2] = temp;
pPixel[3] = 255; // Set The Alpha Value
}So, in a nutshell, it's pretty straightforward. But, I think the problem is that Render doesn't set the alpha values, because the final bitmap always comes out with alpha values of zero. The GIF transparency is definitely loaded, because when I force the alpha to 255, the texture shows black in the transparent sections of the GIF.

Is there a simple way to get the GIF transparency to carry through to the alpha channel of the bitmap and texture?

Perhaps by defining the bitmap a different way, the Render() will correctly render the alpha values as well as the colours?

Otherwise, I can manually test the pixel colour when setting the alpha values, and get the transparency that way, but I'd rather do it properly if I can.


02-02-2006, 05:27 PM
I don't think your transparency is there, black with full alpha is still black. There's a lot of code missing there and I'm going to bet that either your loading function isn't really reading in the transparency or your rendering function is discarding it.

What's the class behind pPicture?

02-02-2006, 05:46 PM
Actually, there's almost no code missing from that. The only bit I cut out is the calculation of m_iWidth and m_iHeight, the creation of hdcTemp, which is just CreateCompatibleDC(GetDC(0)). I also chopped out the error checking at each stage. But as far as the important code goes, that's all there is.

OleLoadPicturePath() is a standard function in VC. pPicture is of class IPicture, derived from IUnknown. I don't really know much about this class. Perhaps there is a way to get the bits, with transparency, direct out of IPicture without rendering it onto another bitmap.

OleLoadPicturePath() is getting the transparency OK, but once it's Render()ed into the Bitmap, the transparency information is not there. The alpha values for the bitmap are all zero, regardless of what Render() does, but you can see from the results of the Render that it is correctly applying the alpha values from the GIF image. If pBits is initialised to white, the background of the bitmap stays white. If pBits is initialised to black, the background of the bitmap stays black.

02-03-2006, 07:27 PM
I've got it sorted another way now. Instead of OLE, I'm now using Gdiplus to load the image. This lets me pull the pixels right out of the loaded Bitmap, and is even less code than the Ole version. Still a few bugs to iron out, but I've loaded PNG files with full alpha channels, and the resultant textures look great! Now I have to sort out depth ordering of the rendering to get the full benefit of partial transparency. :)