1

我正在编写一个 DX9 渲染器,目前正在研究播放 AVI 电影文件的能力。我已经能够使用 检索任何指定的帧AVIStreamGetFrame(),它返回一个打包的 DIB,并且从那里我希望能够将该位图数据复制到一个已经存在的IDirect3DTexture9 *.

我的问题是对位图文件格式缺乏了解,并且不知道如何将给定的像素数据转换为可以解释BITMAPINFOHEADER的格式。IDirect3DTexture9

我首先像这样创建我的 DX9 纹理:

LPBITMAPINFO bmpInfo = m_pVideoData->GetVideoFormat();
D3DXCreateTexture(LtGEngine::GetInstance()->GetDevice(), 
                  bmpInfo->bmiHeader.biWidth, 
                  bmpInfo->bmiHeader.biHeight, 
                  D3DX_DEFAULT, 
                  0, 
                  D3DFMT_A8R8G8B8,   // <- DETERMINE HOW?
                  D3DPOOL_MANAGED,   // <- OR D3DPOOL_SYSTEMMEM? 
                  &m_pD3DTexture);

我在这里的问题列在上面的评论中。当我得到 BITMAPINFO 并且例如它读取 bmpInfo.bmiHeader.biBitCount = 8 (或 16 等)时,这是否意味着我需要相应地更改 D3DFMT_*?

稍后当我得到一个LPBITMAPINFOHEADER我想要渲染的帧时,我不知道如何处理pBitsIDirect3DTexture9::LockRect()函数返回的内容。这是我到目前为止所拥有的:

// Retrieve a frame from the video data as a BITMAPINFOHEADER
LPBITMAPINFOHEADER pBmpInfoHeader;
m_pVideoData->GetVideoFrame(0, 0, &pBmpInfoHeader);

D3DLOCKED_RECT rect;
if(FAILED(m_pD3DTexture->LockRect(0, &rect, NULL, 0)))
{
    m_pD3DTexture->UnlockRect(0);
}
DWORD* pDest = (DWORD*)rect.pBits;
// Now what to copy from pBmpInfoHeader?

是否有任何我没有见过的 API 调用可以为我执行此操作?或者有人知道比这更简单的方法吗?感谢阅读/帮助。

4

1 回答 1

1

让它工作!

需要考虑的几个注意事项。我的 AVI 文件(在本例中为单帧/位图)是 16 位格式,因此我必须使用 D3DFMT_X1R5G5B5 创建目标纹理。此外,位图是倒置存储的,所以我不得不反转指针并向后读取每一行。

这是代码:

// Retrieve a frame from the video data as a BITMAPINFOHEADER
LPBITMAPINFOHEADER pBmpInfoHeader;
m_pVideoData->GetVideoFrame(0, 0, &pBmpInfoHeader);

// Get dimentions
long nWidth = pBmpInfoHeader->biWidth;
long nHeight = pBmpInfoHeader->biHeight;

// Bitmap width correction (might not be needed...)
if (nWidth % 4 != 0)
    nWidth = nWidth + (4 - nWidth%4);

// Get Pixel data (should be after the header in memory)
WORD bitCount = pBmpInfoHeader->biBitCount;
DWORD size = nWidth * nHeight * bitCount/8;
BYTE *pPixelSrc = (BYTE *)pBmpInfoHeader + sizeof(pBmpInfoHeader);

// Lock the texture so we can write this frame's texel data
D3DLOCKED_RECT lock;
if(FAILED(m_pD3DTexture->LockRect(0, &lock, NULL, 0)))
{
    m_pD3DTexture->UnlockRect(0);
    return;
}

int iNumBytesPerRowSrc = pBmpInfoHeader->biWidth * (pBmpInfoHeader->biBitCount/8);  
int iNumBytesPerRowDst = lock.Pitch;
int iNumBytesToCopyPerRow = min(iNumBytesPerRowSrc, iNumBytesPerRowDst);

// Bitmap data is stored upside down
// Start at the end and work backwards
pPixelSrc += (iNumBytesPerRowSrc * nHeight);

// Store a pointer to the texture pixel data and write new data
BYTE* ucTexDst = (BYTE *)lock.pBits;
for(int y = 0; y < nHeight; ++y)
{
    pPixelSrc -= iNumBytesPerRowSrc;
    memcpy(ucTexDst, pPixelSrc, iNumBytesToCopyPerRow);
    ucTexDst += iNumBytesPerRowDst;
}

// Unlock texture so gfx card can resume its business
m_pD3DTexture->UnlockRect(0);
于 2011-06-23T20:31:08.413 回答