Wednesday, May 21, 2014

BMP Images

BMP, known as Bitmap Image. This format is widely used in Windows. It is capable of storing 2D digital images of arbitrary width, height & resolution, both monochrome & color, in various color depths.

The FILE Organization is as follows:
  • BMP Header
  • BitMap Header
  • Color Palette
  • IMAGE Data
The FILE structure is as follows:

BMP Header:

struct _WinBMPFileHeader{
            WORD FileType;
            DWORD FileSize;
            WORD Reserved1;
            WORD Reserved2;
            DWORD BitmapOffset;
}

FileType holds a 2-byte magic value used to identify the file type. It is always 4D42h or “BM” in ASCII.

FileSize is the total size of the BMP file in bytes & should agree with the file size reported by the filesystem.

Reserved1 & Reserved2 don’t contain useful data & are usually set to zero.

BitmapOffset is the starting offset of the bitmap data from the beginning of the file  in bytes.


BitMap Header:

struct _WinBitMapHeader{
            DWORD Size;
            LONG Width;
            LONG Height;
            WORD Planes;
            WORD BitsPerPixel;
            DWORD Compression;
            DWORD SizeOfBitmap;
            LONG HorzResolution;
            LONG VertResolution;
            DWORD ColorsUsed;
            DWORD ColorsImportant;
}

Size is the size of the header in bytes. This value is always 40.

Width is the width of image in pixels.

Height is the height of the image in pixels. If Height is a positive number, then the image is a "bottom-up" bitmap with the origin in the lower-left corner. If Height is a negative number, then the image is a "top-down" bitmap with the origin in the upper-left corner.

Planes is the no. of color planes used to represent the Bitmap data. BMP files contain only one plain, so this value is always 1.

BitsPerPixel is the number of bits in each pixel. This value is in the range 1 to 24; the values 1, 4, 8, and 24 are the only values considered legal.

Compression indicates the type of encoding method used to compress the bitmap data. 0 indicates that the data is uncompressed; 1 indicates that the 8-bit RLE algorithm was used; 2 indicates that the 4-bit RLE algorithm was used.

SizeOfBitmap is the size of the stored bitmap in bytes. This value is typically 0 when the bitmap data is uncompressed.

HorzResolution and VertResolution are the horizontal and vertical resolutions of the bitmap in pixels per meter. These values are used to help a BMP reader choose a proper resolution when printing or displaying a BMP file.

ColorsUsed is the number of colors present in the palette. If this value is 0, and the value of  
BitsPerPixel is less than 16, then the number of entries is equal to the maximum size possible for the colormap. BMP files with a BitsPerPixel value of 16 or greater will not have a color palette. This value is calculated by using the value of the BitsPerPixel field:

ColorsUsed = 1 << BitsPerPixel

ColorsImportant is the number of significant colors in the palette, determined by their frequency of appearance in the bitmap data; the more frequent the occurrence of a color, the more important it is.


COLOR Palette:

struct _WinPalette{
            BYTE Blue;
            BYTE Green;
            BYTE Red;
            BYTE Reserved;
}

Blue, Green, and Red hold the color component values for a pixel; each is in the range 0 to 255.
Reserved pads the structure to end on an even-byte boundary and is always zero.

IMAGE Data:
The actual image raster data starts from here. It contains the values for all the pixels.
Eg. A sample BMP Image (10 x 10) 

ZOOMED 

 Values for various BMP Headers:
BMP Header:
FileType                     =          ‘BM’
FileSize                      =          454
Reserved1                 =          0
Reserved2                 =          0
BitmapOffset              =          54

BitMap Header:

Size                             =          40
Width                           =          10
Height                          =          10
Planes                         =          1
BitsPerPixel                 =          32
Compression               =          0
SizeOfBitmap               =          0
HorzResolution            =          3780
VertResolution             =          3780
ColorsUsed                  =          0
ColorsImportant           =          0

Color Palette:

None, since images having a Bit Count greater than 8 stores colors through pixels only.

IMAGE Data:

(255 0 0 255) (255 0 0 255) (255 0 0 255) (255 0 0 255) (255 0 0 255)
(0 0 255 255) (0 0 255 255) (0 0 255 255) (0 0 255 255) (0 0 255 255)
(255 0 0 255) (255 0 0 255) (255 0 0 255) (255 0 0 255) (255 0 0 255)
(0 0 255 255) (0 0 255 255) (0 0 255 255) (0 0 255 255) 0 0 255 255 ()
(255 0 0 255) (255 0 0 255) (255 0 0 255) (255 0 0 255) (255 0 0 255)
(0 0 255 255) (0 0 255 255) (0 0 255 255) (0 0 255 255) (0 0 255 255)
(255 0 0 255) (255 0 0 255) (255 0 0 255) (255 255 255 255) (255 255 255 255)
(255 255 255 255) (255 255 255 255) (0 0 255 255) (0 0 255 255) (0 0 255 255)
(255 0 0 255) (255 0 0 255) (255 0 0 255) (255 255 255 255) (255 255 255 255)
(255 255 255 255) (255 255 255 255) (0 0 255 255) (0 0 255 255) (0 0 255 255)
(0 0 255 255) (0 0 255 255) (0 0 255 255) (255 255 255 255) (255 255 255 255)
(255 255 255 255) (255 255 255 255) (255 0 0 255) (255 0 0 255) (255 0 0 255)
(0 0 255 255) (0 0 255 255) (0 0 255 255) (255 255 255 255) (255 255 255 255)
(255 255 255 255) (255 255 255 255) (255 0 0 255) (255 0 0 255) (255 0 0 255)
(0 0 255 255) (0 0 255 255) (0 0 255 255) (0 0 255 255) (0 0 255 255)
(255 0 0 255) (255 0 0 255) (255 0 0 255) (255 0 0 255) (255 0 0 255)
(0 0 255 255) (0 0 255 255) (0 0 255 255) (0 0 255 255) (0 0 255 255)
(255 0 0 255) (255 0 0 255) (255 0 0 255) (255 0 0 255) (255 0 0 255)
(0 0 255 255) (0 0 255 255) (0 0 255 255) (0 0 255 255) (0 0 255 255)
(255 0 0 255) (255 0 0 255) (255 0 0 255) (255 0 0 255) (255 0 0 255)

                 The Image Data originates from the lower-left corner of the image. The value 255 is present in every PIXEL & that denotes the reserved byte in Color Palette used for padding.