Jump to content
  • Advertisement
Sign in to follow this  

Getting the size of a .png image?

This topic is 4776 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 need a function or something that returns the height and width of a .png image file. The smaller the better because I don't really need any other functions. Anybody know?

Share this post

Link to post
Share on other sites
I've never done it before, but you can find the spec at http://www.libpng.org/pub/png/spec/1.0/PNG-Contents.html . It looks like you just need to search for the first IHDR chunk. Shouldn't be too hard.

Share this post

Link to post
Share on other sites
This seems to work ok. I threw it together quickly but it should be fine.

#include <fstream>
#include <algorithm>

//Endian stuff
template<typename Type>
Type ByteSwap( const Type& Obj )
Type NewVal;
const char* Src = reinterpret_cast<const char*>( &Obj );
std::reverse_copy( Src, Src + sizeof(Obj), reinterpret_cast<char*>( &NewVal ) );
return NewVal;

void FindPngSize( std::ifstream& fileStream, unsigned int& width, unsigned int& height )
unsigned char signature[8];
fileStream.read( (char*) signature, 8 );
//Check the PNG signature
if( signature[0] != 137 || signature[1] != 80 || signature[2] != 78 ||
signature[3] != 71 || signature[4] != 13 || signature[5] != 10 ||
signature[6] != 26 || signature[7] != 10 )
throw std::runtime_error( "PNG signature incorrect." );

unsigned int length;
fileStream.read( (char*) &length, 4 );
length = ByteSwap( length );

unsigned char header[4];
fileStream.read( (char*) header, 4 );
//Check the IHDR chunk type
if( header[0] != 73 || header[1] != 72 || header[2] != 68 || header[3] != 82 )
throw std::runtime_error( "First PNG chunk does not appear to be an IHDR chunk." );

fileStream.read( (char*) &width, 4 );
fileStream.read( (char*) &height, 4 );
width = ByteSwap( width );
height = ByteSwap( height );

Notice that the values do need to be byte swapped on x86, as they're big endian values in the file. Feel free to use my byte swapping function, or write your own, or use somebody else's (e.g. SDL).

Share this post

Link to post
Share on other sites
Guest Anonymous Poster
when I get the time

This is very ungrateful!
I hate it when people ask a question, someone takes their time to find a solution and then the OP doesn't even bother to comment/try the solution!
This is why I'm starting to spend less and less time coming up with answers and solutions.
If you don't get any feedback, what's the point?

I think the moderators should monitor users that just spews out questions without hardly reading the answers.

What if I start to copy all questions from my math tutorials etc, I can probably come up with 100 questions an hour!

I'm more then willing to spend time helping out if I know that my results will come to use!
Otherwise it's just a waste of my time, time I could have spent helping someone else that has a REAL problem that needs to be solved.

My 2c.

PS. I don't expect to get feedback on this, since this isn't a direct question, so I'll not be disapointed if nobody comments.

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!