Jump to content
  • Advertisement
Sign in to follow this  
JackFrost

Use DirectX to display image data from another source

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm writing an application that uses the OpenCV library for image manipulation and other stuff. The thing is that I'm using DirectX to display the images. Earlier versions of the program used DirectDraw to draw to a DDSurface, but we're bringing it up to DX9. The code that did this originally was like this:
BYTE *surfPtr = (BYTE*)(ddsd.lpSurface);

if(mirror == true)
{
    for(int i = 0; i < img->height; i++)
	for(int j = 0; j < img->width; j++)
	     memcpy(surfPtr + (ddsd.dwHeight - i - 1) * ddsd.lPitch + 4*j, img->imageData + (i*img->widthStep) + 3*j, 3);
}

Basically I'm wondering how to update this to DX9. Since DirectDraw is deprecated, what should I use? The D3DXSprite class? And how would I write the image data?

Share this post


Link to post
Share on other sites
Advertisement
Assuming your surface format is something like D3DFMT_A8R8G8B8 (32bpp and 8 bits per component) and that both the OpenCV image and the DX surface are the same dimensions, then use the following code snippet

D3DLOCKED_RECT lockedRect;
pTexture->LockRect(0, &lockedRect, NULL, 0);
unsigned char *pSurfPtr = static_cast<unsigned char*>(lockedRect.pBits);
for(int i = 0; i < img->height; i++)
{
DWORD dwRowStart = i*lockedRect.Pitch;
DWORD dwImgRowStart = i*img->widthStep;
for(int j = 0; j < img->width; j++)
{
DWORD dwColStart = j*4; // 4 channels per pixel in DX data.
DWORD dwImgColStart = j*3; // 3 channels per pixel in OpenCV data
memcpy( pSurfPtr + dwRotStart + dwColStart,
img->imageData + dwImgRowStart + dwImgColStart,
3*sizeof(unsigned char)
);
}
}
pTexture->UnlockRect(0);


Notice there is no error checking here for simplicity. Also, the row and column offsets are computed only when necessary to try to speed things up a bit.

Hope this helps,
neneboricua

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!