本程序只关注对Gif纹理的实现。不对GIF加解密进行详细的说明。
GIF文档解析采用gif89a.h, gif89a.cpp实现。
gif89a 代码下载
详细纹理生成代码:
bool Cmenu::CreateTextureFromGif()
{
HRESULT hr;
CGif89a* pGif = new CGif89a();
BOOL bopen = pGif->open("110743081.gif",true);
int index = 0;
LPCFRAME pFrame = pGif->getFrame(index);
if (!pFrame)
return false;
BYTE* pColorTable = pGif->getColorTable(index);
if (!pColorTable)
return false;
//create empty Texture.
hr = D3DXCreateTexture(m_pDevice,pFrame->imageWidth,pFrame->imageHeight,0,0,D3DFMT_X8B8G8R8,D3DPOOL_MANAGED,&m_pTexture[0]);
if(FAILED(hr))
return false;
D3DSURFACE_DESC textureDesc;
m_pTexture[0]->GetLevelDesc(0,&textureDesc);
if(textureDesc.Format != D3DFMT_X8B8G8R8)
return false;
D3DLOCKED_RECT locketrect;
hr = m_pTexture[0]->LockRect( 0,&locketrect,0,0 );
if(FAILED(hr))
return false;
BYTE* pBytes = (BYTE*)locketrect.pBits;
DWORD lPitch = locketrect.Pitch;
int idx_trs = pFrame->ctrlExt.trsFlag ? pFrame->ctrlExt.trsColorIndex : -1;
int x=0,y=0;
int sx, sy;
if(x<0)
sx = -x;
else
sx = 0;
if(y<0)
sy = -y;
else
sy = 0;
DWORD width = pFrame->imageWidth;
pBytes += lPitch*sy;
for( DWORD h=sy; y+h<pFrame->imageHeight; h++ )
{
DWORD* pDstData32 = ((DWORD*)pBytes) + sx;
WORD* pDstData16 = ((WORD*)pBytes) + sx;
BYTE* pPixel;
BYTE* pIdx = pFrame->dataBuf + (pFrame->imageWidth*(((y>0)?y:0)+h) + ((x>0)?x:0));
for( DWORD w=sx; w<width; w++ )
{
pPixel = pColorTable + (*pIdx)*3;
if (*pIdx==idx_trs)
{
//if( 32 == ddsd.ddpfPixelFormat.dwRGBBitCount )
// pDstData32[w] = 0;
//else
// pDstData16[w] = 0;
}
else
{
DWORD m_nRShiftL = 0; DWORD m_nRShiftR = 0;
DWORD m_nGShiftL = 0; DWORD m_nGShiftR = 0;
DWORD m_nBShiftL = 0; DWORD m_nBShiftR = 0;
DWORD m_nAShiftL = 0; DWORD m_nAShiftR = 0;
DWORD dr = ((DWORD(pPixel[0])>>(m_nRShiftL))<<m_nRShiftR);
DWORD dg = ((DWORD(pPixel[1])>>(m_nGShiftL))<<8);
DWORD db = ((DWORD(pPixel[2])>>(m_nBShiftL))<<16);
DWORD da = ((0xff>>(m_nAShiftL))<<24);
pDstData32[w] = (DWORD)(dr+dg+db+da);
}
pIdx ++;
}
pBytes += lPitch;
}
m_pTexture[0]->UnlockRect(0);
return true;
}
如有任何疑问请留言。交流~~
posted on 2009-04-04 01:05
Only Soft 阅读(813)
评论(0) 编辑 收藏 引用 所属分类:
D3D