Thursday, March 27, 2008

BMP - Bitmap Files

The BMP file format, sometimes called Bitmap, is an image file format used to store bitmap digital images. BMP stores uncompressed(raw) pixel values. Each BMP file consists of 54 byte file header followed by CLUT(Color Look Up Table), if bitcount <= 8, followed by pixel values. Pixels are stored in the BMP files in BGR format. Blue Pixel value is stored first followed by Green pixel value followed by Red pixel value. The first pixel values(BGR) in the BMP file corresponds to the pixel in the bottom left of the Image. Then the second pixel values(BGR) in the BMP file corresponds to the pixel next to the bottom left of the Image. This goes on till the bottom right of the Image. That is raster scanning from the bottom left to bottom right. The next pixel values in the BMP file corresponds to the above line of the bottom line(immediate top line). In uncompressed BMP files, and many other BMP file formats, image pixels are stored with a color depth of 1, 4, 8, 16, 24, or 32 bits per pixel. Images of 8 bits and fewer can be either grayscale or indexed color. Indexed color requires Color Look Up Table. Bitmap files are stored with the name extension .BMP and occasionally we also see bitmap files stored in a device-independent bitmap form with the name extension .DIB. Device-independent bitmap files are simply a list of pixels with values for the red, green and blue components and omit the header information associated with size and other descriptors. The bitmap image format originated in early versions of the Microsoft Windows and has stayed ever since through packages such as Paintbrush/Paint. While the Windows operating system supports images with 1, 4, 8, 16, 24 and 32 bits per pixel we shall largely focus on the use of monochrome and 24-bit colour here. Bitmap File Structure

The bitmap file structure is very simple and consists of a bitmap-file header, a bitmap-information header, a colour table, and an array of bytes that define the bitmap image.
The file has the following form:

  • File Header Information
  • Image Header Information
  • CLUT(if present)
  • Pixel Values
BITMAPFILEHEADER

The bitmap file header contains information about the type, size, and layout of a bitmap file and permits a check as to the type of file the user is reading. The first two bytes of the file contain the ASCII character “B” followed by an “M” to identify the file type.

The next four bytes of the header contain the file size with the least significant bit first. The next four bytes are unused and set to zero.

The final four bytes are an offset to the start of the image pixel data from the header and measured in bytes. Formally the structure is of the form:

BITMAPFILEHEADER {
2 bytes file type
4 bytes file size in bytes
2 bytes reserved
2 bytes reserved
4 bytes offset to data in bytes
} BITMAPFILEHEADER;

BITMAPINFOHEADER

The bitmap-information header specifies the dimensions, compression type, and colour format for the bitmap. The first four bytes are the header size, usually 40 bytes, followed by the width and height of the image measured in pixels. The next two bytes contain 1 which is the number of planes. The next two bytes store the number of bits used to represent the colour intensities of a pixel, which in this text is usually 24 (referred to as true colour) as we frequently use such images. Twenty-four bit colour has over the years become more prevalent as memory has become cheaper and processor speeds have increased. The next four bytes store the compression (0 for 24 bit RGB) followed by the Image size (may be 0 if not compressed). The next eight bytes store the X and Y resolution (pixels/meter). The final entries in the bitmap information section are the number of colour map entries and the number of significant colours. Formally this is written as:

BITMAPINFOHEADER {
4 bytes needed for BITMAPINFOHEADER structuresize
4 bytes bitmap width in pixels
4 bytes bitmap height in pixel
2 bytes 1
2 bytes bits/pixel (1 = monochrome)
4 bytes compression 0, 8, 4
4 bytes image size in bytes (may be 0 for monochrome)
4 bytes pixels/metre
4 bytes pixels/metre
4 bytes number of colour indexes used by bitmap in colour table
4 bytes number of colour indexes considered important
} BITMAPINFOHEADER;

COLOUR LOOK UP TABLE(CLUT)

The colour table is not present for bitmaps with 24 bit files because each pixel is represented by the 8-bit blue-green-red (BGR) values in the actual bitmap data area.

IMAGE DATA

The bitmap data immediately following the colour table consist of BYTE values representing consecutive rows (scan lines) of the bitmap image in left-to-right order. A scan line must be zero-padded to end on a 32-bit boundary or rounded up to a multiple of four bytes. The scan lines in the bitmap are stored from bottom to the top of the image. This means that the first byte in the file represents the pixels in the lower-left corner or origin of the bitmap and the last byte represents the pixels in the upper-right corner.

The format of the file depends on the number of bits used to represent each pixel with the most significant bit field corresponding to the leftmost pixel.

There are different types of BMP files. Some of them are:
  • RGB 24-bit
  • RGB 565
  • RGB 8-bit (or) RGB 256 Color
  • RGB 4-bit (or) RGB 16 Color
RGB 24-bit (or) RGB 888

Most BMP files are of this type. In the 24-bits, each 8-bits are given for red, green and blue. This is the uncompressed file format. This does not require CLUT. The size of the file will be 54 + (width * height * 3) bytes. First 54 is for the BMP file header. 3 is for Red, Green and Blue.


No comments: