PNG file format endianness?

Marlon picture Marlon · Mar 5, 2010 · Viewed 7.3k times · Source

Im not sure if endian is the right word but..

I have been parsing through a PNG file and I have noticed that all of the integer values are in big endian. Is this true?

For example, the width and height are stored in the PNG file as 32bit unsigned integers. My image is 16x16 and in the file its stored as:

00 00 00 10

when it should be:

10 00 00 00

Is this true or is there something I am missing?

Answer

ja. picture ja. · Mar 5, 2010

Yes, according to the specification, integers must be in network byte order (big endian):

All integers that require more than one byte shall be in network byte order: the most significant byte comes first, then the less significant bytes in descending order of significance (MSB LSB for two-byte integers, MSB B2 B1 LSB for four-byte integers). The highest bit (value 128) of a byte is numbered bit 7; the lowest bit (value 1) is numbered bit 0. Values are unsigned unless otherwise noted. Values explicitly noted as signed are represented in two's complement notation.

http://www.w3.org/TR/2003/REC-PNG-20031110/#7Integers-and-byte-order