• Advertisement
Sign in to follow this  

Loading PNG files

This topic is 1780 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 have searched google and I can't seem to find anything relevant. I am trying to load simple PNG images into my directx program. I have DDS images working and they become ID3D11ShaderResourceView when loaded. How can I load PNGs?

 

I am targeting the windows app store and D3D 11.1, but I would also prefer something more backwards compatible with windows 7.

Share this post


Link to post
Share on other sites
Advertisement

Excuse my ignorance (I never had the opportunity to use D3D11 since I'm stuck with XP for the time being), but wouldn't there be a D3DX function that loads various texture formats including .png files?  I know it's good not to rely on D3DX when possible either way.

 

Shogun.

Share this post


Link to post
Share on other sites

Excuse my ignorance (I never had the opportunity to use D3D11 since I'm stuck with XP for the time being), but wouldn't there be a D3DX function that loads various texture formats including .png files?  I know it's good not to rely on D3DX when possible either way.

 

Shogun.

D3DX11CreateTextureFromFile  

 

However it has been depreciated in windows 8 and therefore the future.  Might as well learn the 6 or 7 lines it takes to load image data using FreeImage, and the few more to send it into directx.  This allows you future proof and you could switch to openGL as well if you wanted.

 

Also rolling your own .bmp and/or .tga loader is a piece of cake if you can find the right header info for your image exporter.  This will be a great learning experience, and as long as you are just learning and/or using small projects the extra size of the image files won't really matter.

Edited by EddieV223

Share this post


Link to post
Share on other sites

Excuse my ignorance (I never had the opportunity to use D3D11 since I'm stuck with XP for the time being), but wouldn't there be a D3DX function that loads various texture formats including .png files?  I know it's good not to rely on D3DX when possible either way.

 

Shogun.

D3DX11CreateTextureFromFile  

 

However it has been depreciated in windows 8 and therefore the future.  Might as well learn the 6 or 7 lines it takes to load image data using FreeImage, and the few more to send it into directx.  This allows you future proof and you could switch to openGL as well if you wanted.

 

Ah, that makes sense.  Writing your own image loader or using an open source one has always been my choice for portability and such, especially when working with consoles (those of us who get that far).

 

Shogun.

Share this post


Link to post
Share on other sites



Excuse my ignorance (I never had the opportunity to use D3D11 since I'm stuck with XP for the time being), but wouldn't there be a D3DX function that loads various texture formats including .png files?  I know it's good not to rely on D3DX when possible either way.
 
Shogun.

D3DX11CreateTextureFromFile  
 
However it has been depreciated in windows 8 and therefore the future.  Might as well learn the 6 or 7 lines it takes to load image data using FreeImage, and the few more to send it into directx.  This allows you future proof and you could switch to openGL as well if you wanted.


 
Ah, that makes sense.  Writing your own image loader or using an open source one has always been my choice for portability and such, especially when working with consoles (those of us who get that far).
 
Shogun.


yep, agreed.

though funny is I have usually written my own loaders (and other code as well), but admitting to this is one of those things which often causes people to start making accusations of "reinventing the wheel" and "making a standard of non-standard" and so on.

but, I am not really afraid of writing my own code, and consider it a reasonable tradeoff to avoid the annoyance of having lots of external dependencies (more so when targeting multiple OS's is involved).

granted, not all file-formats are equally simple, like it is a bit simpler to write a loader for something like TGA or BMP than for something like PNG, or JPEG (*1).

ultimately, there is little really "special" about file-formats, mostly just specific ways of representing various kinds of data, and code generally doesn't really care whose code generates or processes the data, so long as it basically matches the expected format.


*1: for example, TGA and BMP are basically a header followed by a glob of raw pixel data (may be RLE compressed for TGA though).
in contrast, PNG is a TLV format (sort of like a WAV or an AVI), with data-lumps for headers and image data, and with the image data stored using Deflate compression and with per-scanline filtering and similar. and, JPEG is a beast not easily explained in a short summary (basically, it is a stream of escape-coded markers representing headers and image-data, which consists mostly of Huffman-compressed DCT blocks representing typically 3 planes in the YCbCr colorspace, ...). (worse yet is video codecs...).

but, usually, most of this complexity has its reasons (such as smaller file-sizes, ...), and by playing around with things, one may discover why things are the way they are.

as far as complexity goes, PNG isn't really all that bad though. the most complicated bit is probably the Deflate compression, but this part is (usually) handled by Zlib, unless of course one also goes and writes their own Deflate code (...).

as in everything, there are pros and cons involved...

Share this post


Link to post
Share on other sites

You can use a PNG decoding library, such as below, and then pass the resulting array of pixels to D3D:

http://nothings.org/stb_image.c

http://www.libpng.org/pub/png/libpng.html

 

I like the look for stb_image because I also want to access the raw pixel data. Now the part I have to find out is how to put that into a resource for D3D (which shouldn't be hard).

Share this post


Link to post
Share on other sites

If you're feeling like doing it yourself, you can have a look at the PNG format specification.It'll probably take some time to familiarize yourself with it, but the documentation appears exhaustive.

Share this post


Link to post
Share on other sites

I have now loaded the png file using stb_image.c but the image is not showing.

 

int x, y, n;
	unsigned char *data = stbi_load(charFilename, &x, &y, &n, 0);

	D3D11_TEXTURE2D_DESC desc;
	desc.Width = x;
	desc.Height = y;
	desc.MipLevels = 1;
	desc.ArraySize = 1;
	desc.Format = DXGI_FORMAT_R8G8B8A8_UINT;
	desc.SampleDesc.Count = 1;
	desc.SampleDesc.Quality = 0;
	desc.Usage = D3D11_USAGE_DEFAULT;
	desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
	desc.CPUAccessFlags = 0;
	desc.MiscFlags = 0;

	D3D11_SUBRESOURCE_DATA subres;
	subres.pSysMem = data;
	subres.SysMemPitch = x * n;
	subres.SysMemSlicePitch = 0;

	ID3D11Texture2D *texture2D = 0;

	HRESULT result = device->CreateTexture2D(&desc, &subres, &texture2D);
	if (FAILED(result))
	{
		DebugConsole::writeLine("Failed to create Texture2D");
	}

	result = device->CreateShaderResourceView(texture2D, NULL, &m_texture);
	if (FAILED(result))
	{
		DebugConsole::writeLine("Failed to create resource view");
	}

I have checked the HRESULT of both calls and they are both S_OK. I have also checked the filename and it is correct. If I change the extension to .dds (I have both files in the directory) then it works using the dds loading code (it checks for the .dds extension). Have I made a small mistake or am I going at it all wrong?


 

Share this post


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

  • Advertisement