Sign in to follow this  

Getting the size of a .png image?

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 . 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]; (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; (char*) &length, 4 );
length = ByteSwap( length );

unsigned char header[4]; (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." );
} (char*) &width, 4 ); (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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this