Generate PNG Image with Transparency From IDirect3DTexture9










0















I'm pretty new to Direct3D and I try to generate a png image file to disk from a already loaded initialized image using LPDIRECT3DTEXTURE9 interface. Note that I want to keep the transparency of the loaded image.



Basically I'm creating a function that get's an std::string for the file destination path and get the pixels from the existing LPDIRECT3DTEXTURE9 object and save them to disk.



I though I should add a sample code of how I do it with BitMap format to get the idea of what I'm trying to do.



Note that this code works, but I try to do something similar with PNG format.



int Width = 128;
int Height = 128;
LPDIRECT3DTEXTURE9 m_lpTexture;
LPDIRECT3DDEVICE9 s_lpD3DDev = NULL;
bool SaveToBitmapFile(const std::string & szPath)

LPDIRECT3DSURFACE9 lpSurfSrc = NULL;
LPDIRECT3DSURFACE9 lpSurfDst = NULL;

m_lpTexture->GetSurfaceLevel(0, &lpSurfSrc);
s_lpD3DDev->CreateOffscreenPlainSurface(Width,
Height, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &lpSurfDst, NULL);

if (D3D_OK !=
D3DXLoadSurfaceFromSurface(lpSurfDst, NULL, NULL, lpSurfSrc,
NULL, NULL, D3DX_FILTER_TRIANGLE, 0))

lpSurfDst->Release(); lpSurfDst = NULL;
lpSurfSrc->Release(); lpSurfSrc = NULL;


CBitMapFile myBmp;
myBmp.Create(Width, Height);

D3DLOCKED_RECT LR;
lpSurfDst->LockRect(&LR, NULL, 0);
for (int y = 0; y < Height; y++)

BYTE* pPixelsSrc = ((BYTE*)LR.pBits) + y * LR.Pitch;
BYTE* pPixelsDst = (BYTE*)(myBmp.Pixels(0, y));
for (int x = 0; x < Width; x++)

pPixelsDst[0] = pPixelsSrc[0];
pPixelsDst[1] = pPixelsSrc[1];
pPixelsDst[2] = pPixelsSrc[2];

pPixelsSrc += 4;
pPixelsDst += 3;


lpSurfDst->UnlockRect();

lpSurfDst->Release(); lpSurfDst = NULL;
lpSurfSrc->Release(); lpSurfSrc = NULL;

HANDLE hFile = ::CreateFile(szPath.c_str(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

DWORD dwRWC = 0;

WriteFile(hFile, &myBmp.m_Header, sizeof(myBmp.m_Header), &dwRWC, NULL);
WriteFile(hFile, &myBmp.m_InfoHeader, sizeof(myBmp.m_InfoHeader), &dwRWC, NULL);

int iWidth = myBmp.Pitch();
for (int y = myBmp.m_InfoHeader.biHeight - 1; y >= 0; y--)
WriteFile(hFile, (BYTE*)myBmp.m_pPixels + y * iWidth, iWidth, &dwRWC, NULL);

CloseHandle(hFile);



From my understanding, I'll need to use



D3DSURFACE_DESC sd;
m_lpTexture->GetLevelDesc(0, &sd);


And generate the headers of the png myself?



Any help would be much appreciated!



Thanks in advance!










share|improve this question

















  • 1





    You can use the deprecated D3DX9 helper library function D3DXSaveTextureToFile to write out PNG files when using legacy Direct3D 9. That said, you should probably be using Direct3D 11 instead. See Living without D3DX

    – Chuck Walbourn
    Nov 13 '18 at 19:19












  • Wow @ChuckWalbourn, you are seriously a legend! I was way over-complicating it, starting to manually generate headers and copying each pixel. Mind adding it as an answer so I can mark it as accepted answer please? Thank you so much! I was also going through the work you've done with DirectXTex which really helped me understanding these images formats, so thank you all the work you do!

    – Gilad Reich
    Nov 13 '18 at 19:53















0















I'm pretty new to Direct3D and I try to generate a png image file to disk from a already loaded initialized image using LPDIRECT3DTEXTURE9 interface. Note that I want to keep the transparency of the loaded image.



Basically I'm creating a function that get's an std::string for the file destination path and get the pixels from the existing LPDIRECT3DTEXTURE9 object and save them to disk.



I though I should add a sample code of how I do it with BitMap format to get the idea of what I'm trying to do.



Note that this code works, but I try to do something similar with PNG format.



int Width = 128;
int Height = 128;
LPDIRECT3DTEXTURE9 m_lpTexture;
LPDIRECT3DDEVICE9 s_lpD3DDev = NULL;
bool SaveToBitmapFile(const std::string & szPath)

LPDIRECT3DSURFACE9 lpSurfSrc = NULL;
LPDIRECT3DSURFACE9 lpSurfDst = NULL;

m_lpTexture->GetSurfaceLevel(0, &lpSurfSrc);
s_lpD3DDev->CreateOffscreenPlainSurface(Width,
Height, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &lpSurfDst, NULL);

if (D3D_OK !=
D3DXLoadSurfaceFromSurface(lpSurfDst, NULL, NULL, lpSurfSrc,
NULL, NULL, D3DX_FILTER_TRIANGLE, 0))

lpSurfDst->Release(); lpSurfDst = NULL;
lpSurfSrc->Release(); lpSurfSrc = NULL;


CBitMapFile myBmp;
myBmp.Create(Width, Height);

D3DLOCKED_RECT LR;
lpSurfDst->LockRect(&LR, NULL, 0);
for (int y = 0; y < Height; y++)

BYTE* pPixelsSrc = ((BYTE*)LR.pBits) + y * LR.Pitch;
BYTE* pPixelsDst = (BYTE*)(myBmp.Pixels(0, y));
for (int x = 0; x < Width; x++)

pPixelsDst[0] = pPixelsSrc[0];
pPixelsDst[1] = pPixelsSrc[1];
pPixelsDst[2] = pPixelsSrc[2];

pPixelsSrc += 4;
pPixelsDst += 3;


lpSurfDst->UnlockRect();

lpSurfDst->Release(); lpSurfDst = NULL;
lpSurfSrc->Release(); lpSurfSrc = NULL;

HANDLE hFile = ::CreateFile(szPath.c_str(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

DWORD dwRWC = 0;

WriteFile(hFile, &myBmp.m_Header, sizeof(myBmp.m_Header), &dwRWC, NULL);
WriteFile(hFile, &myBmp.m_InfoHeader, sizeof(myBmp.m_InfoHeader), &dwRWC, NULL);

int iWidth = myBmp.Pitch();
for (int y = myBmp.m_InfoHeader.biHeight - 1; y >= 0; y--)
WriteFile(hFile, (BYTE*)myBmp.m_pPixels + y * iWidth, iWidth, &dwRWC, NULL);

CloseHandle(hFile);



From my understanding, I'll need to use



D3DSURFACE_DESC sd;
m_lpTexture->GetLevelDesc(0, &sd);


And generate the headers of the png myself?



Any help would be much appreciated!



Thanks in advance!










share|improve this question

















  • 1





    You can use the deprecated D3DX9 helper library function D3DXSaveTextureToFile to write out PNG files when using legacy Direct3D 9. That said, you should probably be using Direct3D 11 instead. See Living without D3DX

    – Chuck Walbourn
    Nov 13 '18 at 19:19












  • Wow @ChuckWalbourn, you are seriously a legend! I was way over-complicating it, starting to manually generate headers and copying each pixel. Mind adding it as an answer so I can mark it as accepted answer please? Thank you so much! I was also going through the work you've done with DirectXTex which really helped me understanding these images formats, so thank you all the work you do!

    – Gilad Reich
    Nov 13 '18 at 19:53













0












0








0








I'm pretty new to Direct3D and I try to generate a png image file to disk from a already loaded initialized image using LPDIRECT3DTEXTURE9 interface. Note that I want to keep the transparency of the loaded image.



Basically I'm creating a function that get's an std::string for the file destination path and get the pixels from the existing LPDIRECT3DTEXTURE9 object and save them to disk.



I though I should add a sample code of how I do it with BitMap format to get the idea of what I'm trying to do.



Note that this code works, but I try to do something similar with PNG format.



int Width = 128;
int Height = 128;
LPDIRECT3DTEXTURE9 m_lpTexture;
LPDIRECT3DDEVICE9 s_lpD3DDev = NULL;
bool SaveToBitmapFile(const std::string & szPath)

LPDIRECT3DSURFACE9 lpSurfSrc = NULL;
LPDIRECT3DSURFACE9 lpSurfDst = NULL;

m_lpTexture->GetSurfaceLevel(0, &lpSurfSrc);
s_lpD3DDev->CreateOffscreenPlainSurface(Width,
Height, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &lpSurfDst, NULL);

if (D3D_OK !=
D3DXLoadSurfaceFromSurface(lpSurfDst, NULL, NULL, lpSurfSrc,
NULL, NULL, D3DX_FILTER_TRIANGLE, 0))

lpSurfDst->Release(); lpSurfDst = NULL;
lpSurfSrc->Release(); lpSurfSrc = NULL;


CBitMapFile myBmp;
myBmp.Create(Width, Height);

D3DLOCKED_RECT LR;
lpSurfDst->LockRect(&LR, NULL, 0);
for (int y = 0; y < Height; y++)

BYTE* pPixelsSrc = ((BYTE*)LR.pBits) + y * LR.Pitch;
BYTE* pPixelsDst = (BYTE*)(myBmp.Pixels(0, y));
for (int x = 0; x < Width; x++)

pPixelsDst[0] = pPixelsSrc[0];
pPixelsDst[1] = pPixelsSrc[1];
pPixelsDst[2] = pPixelsSrc[2];

pPixelsSrc += 4;
pPixelsDst += 3;


lpSurfDst->UnlockRect();

lpSurfDst->Release(); lpSurfDst = NULL;
lpSurfSrc->Release(); lpSurfSrc = NULL;

HANDLE hFile = ::CreateFile(szPath.c_str(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

DWORD dwRWC = 0;

WriteFile(hFile, &myBmp.m_Header, sizeof(myBmp.m_Header), &dwRWC, NULL);
WriteFile(hFile, &myBmp.m_InfoHeader, sizeof(myBmp.m_InfoHeader), &dwRWC, NULL);

int iWidth = myBmp.Pitch();
for (int y = myBmp.m_InfoHeader.biHeight - 1; y >= 0; y--)
WriteFile(hFile, (BYTE*)myBmp.m_pPixels + y * iWidth, iWidth, &dwRWC, NULL);

CloseHandle(hFile);



From my understanding, I'll need to use



D3DSURFACE_DESC sd;
m_lpTexture->GetLevelDesc(0, &sd);


And generate the headers of the png myself?



Any help would be much appreciated!



Thanks in advance!










share|improve this question














I'm pretty new to Direct3D and I try to generate a png image file to disk from a already loaded initialized image using LPDIRECT3DTEXTURE9 interface. Note that I want to keep the transparency of the loaded image.



Basically I'm creating a function that get's an std::string for the file destination path and get the pixels from the existing LPDIRECT3DTEXTURE9 object and save them to disk.



I though I should add a sample code of how I do it with BitMap format to get the idea of what I'm trying to do.



Note that this code works, but I try to do something similar with PNG format.



int Width = 128;
int Height = 128;
LPDIRECT3DTEXTURE9 m_lpTexture;
LPDIRECT3DDEVICE9 s_lpD3DDev = NULL;
bool SaveToBitmapFile(const std::string & szPath)

LPDIRECT3DSURFACE9 lpSurfSrc = NULL;
LPDIRECT3DSURFACE9 lpSurfDst = NULL;

m_lpTexture->GetSurfaceLevel(0, &lpSurfSrc);
s_lpD3DDev->CreateOffscreenPlainSurface(Width,
Height, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &lpSurfDst, NULL);

if (D3D_OK !=
D3DXLoadSurfaceFromSurface(lpSurfDst, NULL, NULL, lpSurfSrc,
NULL, NULL, D3DX_FILTER_TRIANGLE, 0))

lpSurfDst->Release(); lpSurfDst = NULL;
lpSurfSrc->Release(); lpSurfSrc = NULL;


CBitMapFile myBmp;
myBmp.Create(Width, Height);

D3DLOCKED_RECT LR;
lpSurfDst->LockRect(&LR, NULL, 0);
for (int y = 0; y < Height; y++)

BYTE* pPixelsSrc = ((BYTE*)LR.pBits) + y * LR.Pitch;
BYTE* pPixelsDst = (BYTE*)(myBmp.Pixels(0, y));
for (int x = 0; x < Width; x++)

pPixelsDst[0] = pPixelsSrc[0];
pPixelsDst[1] = pPixelsSrc[1];
pPixelsDst[2] = pPixelsSrc[2];

pPixelsSrc += 4;
pPixelsDst += 3;


lpSurfDst->UnlockRect();

lpSurfDst->Release(); lpSurfDst = NULL;
lpSurfSrc->Release(); lpSurfSrc = NULL;

HANDLE hFile = ::CreateFile(szPath.c_str(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

DWORD dwRWC = 0;

WriteFile(hFile, &myBmp.m_Header, sizeof(myBmp.m_Header), &dwRWC, NULL);
WriteFile(hFile, &myBmp.m_InfoHeader, sizeof(myBmp.m_InfoHeader), &dwRWC, NULL);

int iWidth = myBmp.Pitch();
for (int y = myBmp.m_InfoHeader.biHeight - 1; y >= 0; y--)
WriteFile(hFile, (BYTE*)myBmp.m_pPixels + y * iWidth, iWidth, &dwRWC, NULL);

CloseHandle(hFile);



From my understanding, I'll need to use



D3DSURFACE_DESC sd;
m_lpTexture->GetLevelDesc(0, &sd);


And generate the headers of the png myself?



Any help would be much appreciated!



Thanks in advance!







c++ textures direct3d directx-9






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 18:18









Gilad ReichGilad Reich

346210




346210







  • 1





    You can use the deprecated D3DX9 helper library function D3DXSaveTextureToFile to write out PNG files when using legacy Direct3D 9. That said, you should probably be using Direct3D 11 instead. See Living without D3DX

    – Chuck Walbourn
    Nov 13 '18 at 19:19












  • Wow @ChuckWalbourn, you are seriously a legend! I was way over-complicating it, starting to manually generate headers and copying each pixel. Mind adding it as an answer so I can mark it as accepted answer please? Thank you so much! I was also going through the work you've done with DirectXTex which really helped me understanding these images formats, so thank you all the work you do!

    – Gilad Reich
    Nov 13 '18 at 19:53












  • 1





    You can use the deprecated D3DX9 helper library function D3DXSaveTextureToFile to write out PNG files when using legacy Direct3D 9. That said, you should probably be using Direct3D 11 instead. See Living without D3DX

    – Chuck Walbourn
    Nov 13 '18 at 19:19












  • Wow @ChuckWalbourn, you are seriously a legend! I was way over-complicating it, starting to manually generate headers and copying each pixel. Mind adding it as an answer so I can mark it as accepted answer please? Thank you so much! I was also going through the work you've done with DirectXTex which really helped me understanding these images formats, so thank you all the work you do!

    – Gilad Reich
    Nov 13 '18 at 19:53







1




1





You can use the deprecated D3DX9 helper library function D3DXSaveTextureToFile to write out PNG files when using legacy Direct3D 9. That said, you should probably be using Direct3D 11 instead. See Living without D3DX

– Chuck Walbourn
Nov 13 '18 at 19:19






You can use the deprecated D3DX9 helper library function D3DXSaveTextureToFile to write out PNG files when using legacy Direct3D 9. That said, you should probably be using Direct3D 11 instead. See Living without D3DX

– Chuck Walbourn
Nov 13 '18 at 19:19














Wow @ChuckWalbourn, you are seriously a legend! I was way over-complicating it, starting to manually generate headers and copying each pixel. Mind adding it as an answer so I can mark it as accepted answer please? Thank you so much! I was also going through the work you've done with DirectXTex which really helped me understanding these images formats, so thank you all the work you do!

– Gilad Reich
Nov 13 '18 at 19:53





Wow @ChuckWalbourn, you are seriously a legend! I was way over-complicating it, starting to manually generate headers and copying each pixel. Mind adding it as an answer so I can mark it as accepted answer please? Thank you so much! I was also going through the work you've done with DirectXTex which really helped me understanding these images formats, so thank you all the work you do!

– Gilad Reich
Nov 13 '18 at 19:53












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53287237%2fgenerate-png-image-with-transparency-from-idirect3dtexture9%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53287237%2fgenerate-png-image-with-transparency-from-idirect3dtexture9%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3

Museum of Modern and Contemporary Art of Trento and Rovereto