Sign in to follow this  
Programmer16

Setting the alpha channel of a file

Recommended Posts

I made a file that takes a bitmap, generates a bitmask and then saves them. I then open DirectX Texture Tool and open the bitmask on to the regular bitmaps alpha channel and then save it to a file. What I'd like to do is skip the use of DirectX Texture Tool (not because I don't like it, just because it will make my life easier), by applying the bitmask to the textures alpha channel and then saving the texture to a PNG file. I've been researching, but I can't find much (other than how to setup alpha blending and such. I'm doing this in C++ and DirectX9 and the texture is 32 bit format (D3DFMT_A8R8G8B8.) Thanks, Donald Beals

Share this post


Link to post
Share on other sites
I've tried setting the parts that are supposed to be transparent to 0x00, 0x00000000, D3DCOLOR_ARGB(0, iR, iG, iB) it retains the color but with no alpha), D3DCOLOR_ARGB(0, 0, 0, 0) same as 0x00000000 AFAIK, loaded a png file with a transparent color and copy the bitinfo from that. Nothing seems to work.

Share this post


Link to post
Share on other sites
Okay, I've figured out how to do it (for those that would like to know, you take the regular bitmap and the bitmask and AND thier values together.)

My problem now is saving to a file. When I do the operation, it renders fine in my app, but when I try to load it in another app (same paramters, 32 bit format, png file, alphablending on) it still renders the transparent parts (although they are now black.) Any help would be apreciated!

Share this post


Link to post
Share on other sites
I load up the image and get it displaying the texture with an alpha channel (that part works fine.) But, when I save it as a PNG file (with D3DXIFF_PNG and .png extension) the alpha channel seems to disappear (when I load it into another app, the transparent parts are rendered.) Its a 32bit D3DFMT_A8R8G8B8 texture. Saving it as a DDS file works fine, but I'd rather use png files.

So my question is, to save it as a PNG file, should I try using DevIL or libpng or something?

Here's my code
Load the texture
D3DXCreateTextureFromFileEx(g_pDevice, FileName, D3DX_DEFAULT, D3DX_DEFAULT,
D3DX_DEFAULT, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, D3DX_FILTER_NONE,
D3DX_FILTER_NONE, 0, 0, 0, &g_pBaseImage);


This creates the bitmask, and then uses the bitmask to set the alpha channel.

IDirect3DSurface9* pSrc = 0;
IDirect3DSurface9* pDst = 0;
IDirect3DSurface9* pFinal = 0;

g_pBaseImage->GetSurfaceLevel(0, &pSrc);
g_pBaseImage->GetLevelDesc(0, &g_SurfaceDesc);

SAFERELEASE(g_pBitmask);
g_pDevice->CreateTexture(g_SurfaceDesc.Width, g_SurfaceDesc.Height, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &g_pBitmask, 0);
g_pDevice->CreateTexture(g_SurfaceDesc.Width, g_SurfaceDesc.Height, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &g_pFinal, 0);
g_pBitmask->GetSurfaceLevel(0, &pDst);
g_pFinal->GetSurfaceLevel(0, &pFinal);

D3DLOCKED_RECT SrcRect;
g_pBaseImage->LockRect(0, &SrcRect, 0, 0);
unsigned int* pSrcBits = (unsigned int*)SrcRect.pBits;
unsigned int nSrcPitch = SrcRect.Pitch;

D3DLOCKED_RECT DstRect;
g_pBitmask->LockRect(0, &DstRect, 0, 0);
unsigned int* pDstBits = (unsigned int*)DstRect.pBits;

for(unsigned int nY = 0; nY < g_SurfaceDesc.Height; ++nY)
{
for(unsigned int nX = 0; nX < g_SurfaceDesc.Width; ++nX)
{
int iSector = (nY * (SrcRect.Pitch / 4) + nX);
unsigned int nSrcColor = pSrcBits[iSector];
if(pSrcBits[iSector] == g_nChosenColor)
pDstBits[iSector] = 0x00000000;
else
pDstBits[iSector] = 0xffffffff;
}
}

D3DLOCKED_RECT FinalRect;
g_pFinal->LockRect(0, &FinalRect, 0, 0);
unsigned int* pFinalBits = (unsigned int*)FinalRect.pBits;

for(nY = 0; nY < g_SurfaceDesc.Height; ++nY)
{
for(unsigned int nX = 0; nX < g_SurfaceDesc.Width; ++nX)
{
int iSector = (nY * (SrcRect.Pitch / 4) + nX);
pFinalBits[iSector] = pDstBits[iSector] & pSrcBits[iSector];
}
}
g_pBaseImage->UnlockRect(0);
g_pBitmask->UnlockRect(0);
g_pFinal->UnlockRect(0);

pFinal->Release();
pDst->Release();
pSrc->Release();
return 0;






Save the new texture
D3DXSaveTextureToFile(FileName, D3DXIFF_PNG, g_pFinal, 0);


As I said, this works perfectly when saving to a DDS file, but not to a PNG.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Well, unfortunately, the D3DXSaveTextureToFile don't seem to be able to save the alpha channel with the png file format (for some weird reason). I have discovered that it _does_ store the alpha channel with the tga format, but of course, some graphics programs (like Paint Shop Pro, as I am using) does not support the alpha channel with tga. :-(

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this