I am trying to extract the size of an PNG image from a datastream
Consider the starting data of the stream
137 80 78 71 13 10 26 10 0 0 0 13 73 72 68 82 0 0 2 84 0 0 3 74 8 2 0 0 0 195 81 71 33 0 0 0 ...
^ ^ ^ ^ ^ ^
which contains the following information
- signature:
137 80 78 71 13 10 26 10
- IHDR chunk of:
- length
0 0 0 13
- type
73 72 68 82
- data
0 0 2 84 0 0 3 74 8 2 0 0 0
- crc:
195 81 71 33
- length
- then a new chunk start.
The information about the size of the image are encoded in the 8 bytes of the data chunk:
- width
0 0 2 84
or in bytesb'x00x00x02T'
- height
0 0 3 74
or in bytesb'x00x00x03J'
.
I know that the image has a width of 596
px and a height of 842
px but I cannot figure out how to compute the actual size of the image.
PS the values are given in Python and the here the datastream in binary form
b'x89PNGrnx1anx00x00x00rIHDRx00x00x02Tx00x00x03Jx08x02x00x00x00xc3QG!x00x00x00tpHY'
4
You can think of each byte as a base-256 digit of the respective dimension. So 0 * 256^3 + 0 * 256^2 + 2 * 256 + 84 = 596, and 0 * 256^3 + 0 * 256^2 + 3 * 256 + 74 = 842.
The next two bytes are important as well, where 8 is the bit depth, and 2 is the color type. 8 means 8 bits per component, and 2 means three components per pixel: red, green, and blue.
1