# Setting the alpha channel of a file

This topic is 4776 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 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 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 on other sites
Well screw it. I'm tired of working on this damn thing and if I save in DDS file format it works fine (and is 10x smaller than a PNG for some reason.) So, I'll just stick with DDS for now.

##### Share on other sites
Ok, I've found someone else that could benefit from this app, but we'd both rather that it save in png format. D3DX is supposed to support it, but the alpha channel isn't saved. How would I go about saving it to the file outside of D3DX?

Thanks!

##### Share on other sites
Can you please rephrase your question? I know how to use PNGs with D3D but I don't know exactly where you are going wrong.

##### 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 textureD3DXCreateTextureFromFileEx(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 textureD3DXSaveTextureToFile(FileName, D3DXIFF_PNG, g_pFinal, 0);

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

##### Share on other sites
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 on other sites
Wow, I didn't think anybody else was going to reply to this. I had to postpone finishing this so that I could finish my 4E4 entry. I guess I'll just have to use libpng or something.

1. 1
2. 2
frob
15
3. 3
4. 4
5. 5

• 14
• 13
• 14
• 69
• 22
• ### Forum Statistics

• Total Topics
632138
• Total Posts
3004319

×