将 BITMAPINFOHEADER 图像数据写入 IDirect3DTexture9

发布于 2024-11-16 21:03:05 字数 1412 浏览 0 评论 0原文

我正在编写 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 时,我不知道如何处理从 IDirect3DTexture9::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 调用可以为我执行此操作?或者有谁知道比这更简单的方法?感谢您的阅读/帮助。

I'm writing a DX9 renderer and currently working on the ability to play AVI movie files. I've been able to retrieve any specified frame using AVIStreamGetFrame(), which returns a packed DIB, and from there I want to be able to copy that bitmap data to an already existing IDirect3DTexture9 *.

My issue is a lack of understanding of the bitmap file format and knowing how to convert the pixel data given from a BITMAPINFOHEADER to a format that IDirect3DTexture9 can interpret.

I first create my DX9 texture like this:

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);

Questions I have here are listed as comments above. When I get the BITMAPINFO and for instance it reads bmpInfo.bmiHeader.biBitCount = 8 (or 16, etc.) does this mean I need to change the D3DFMT_* accordingly?

Later on when I get a LPBITMAPINFOHEADER for the frame I want to render, I'm lost on what to do with pBits returned from the IDirect3DTexture9::LockRect() function. Here is what I have so far:

// 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?

Are there any API calls that do this for me that I haven't seen? Or does anyone know of an easier way than this? Thanks for reading/helping.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

划一舟意中人 2024-11-23 21:03:05

可以开始工作了!

需要考虑的几个注意事项。我的 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);

Got it to work!

Couple notes to consider. My AVI file (a single frame/bitmap in this case) was in a 16 bit format, therefore I had to create my destination texture with D3DFMT_X1R5G5B5. Also, bitmaps are stored upside down, so I had to reverse my pointer and read each row backwards.

Here's the code:

// 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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文