16

I have search in google. I found something here, http://paulbourke.net/dataformats/bmp/ , but i can't understand the size of header.

Reading some webpages i got confused. http://en.wikipedia.org/wiki/JPEG

Can anyone give the correct information of header about all these format?

cola
  • 12,198
  • 36
  • 105
  • 165

2 Answers2

21

PNG file contains 8-bytes header.

JPEG file (see specification) contains 2-bytes header (SOI) followed by series of markers, some markers can be followed by data array. Each type of marker has different header format. The bytes where the image is stored follows SOF0 marker (10-bytes length). However, between JPEG header and SOF0 marker there can be other segments.

BMP file contains 14-bytes header.

GIF file contains at least 14 bytes in its header.

Dai
  • 141,631
  • 28
  • 261
  • 374
Pavel Strakhov
  • 39,123
  • 5
  • 88
  • 127
0

I do not know about jpg/jpeg, gif, or others, but I do know about BMP images.

The file header size is 14 bytes. The first two bytes are the header field, composed of two chars (usually ‘B’, and then ‘M’). Then comes the total size of the BMP file (as a 4-byte integer), the next two fields are reserved and are both 2 bytes (so can both be set to 0), and finally, the last part of the file header is a 4-byte integer of the offset at which the pixel array will start.

After the file header comes the information header. There are various different ones in use, but among the most common is the BITMAPINFOHEADER.

This header consists of: the header size in bytes - which is 40 (4 bytes), then the width of the pixel array in pixels (4 bytes), then the height of the pixel array in pixels (4 bytes), then the number of colour panes - usually 1 (2 bytes), then the bits per pixel - often 24 (2 bytes), and finally 6 more fields which are each 4 bytes large and can be set to 0 (will not go into detail to keep the answer short).

After this you can start writing the pixel array.

If you choose 24 bits per pixel, then each pixel will take 3 bytes of data, each byte representing an unsigned char (from 0 to 255) corresponding to red, green or blue. BMP images use the BGR convention, so be sure to write the colour bytes in that order.

Finally, you will need to ensure that each row terminates at a 4-byte boundary. In other words, if the number of bytes in a row of the pixel array is not a multiple of 4, you will have to add in a corresponding amount of ‘padding’ bytes (usually just zeros) - either 1, 2 or 3 bytes.

And there, you’ve got yourself a BMP image!

If you would like any more details, I’d be happy to oblige.