Date: 02-16-2022
Return to Index
created by gbSnippets
'A BMP file contains a device independent bitmap (DIB).
'The BMP file format consists of 4 sections:
'Bitmap header
'Bitmap info
'Color palette
'Bitmap data
'A BMP file is loaded into memory as a DIB data structure, which is the same as the
'BMP file format but without the 14-byte BMP header.
'Bitmap Header (14 bytes)
0000h 2 bytes identifies the BMP file
* BM - Windows 3.1x, 95, NT, ... etc
* BA - OS/2 Bitmap Array
* CI - OS/2 Color Icon
* CP - OS/2 Color Pointer
* IC - OS/2 Icon
* PT - OS/2 Pointer
0002h 4 bytes 'size of BMP file (bytes)
0006h 2 bytes 'reserved
0008h 2 bytes 'reserved
000Ah 4 bytes 'offset (starting address) of byte where the bitmap data can be found
'Bitmap info (DIB Header)
'There are five header types, V1-V5.
'V3 (below, 40 bytes) is a common choice:
Eh 4 'this header size (40 bytes)
12h 4 'bitmap width (pixels, signed integer)
16h 4 'bitmap height (pixels, signed integer)
1Ah 2 'number of color planes (must be 1)
1Ch 2 'bits per pixel (color depth of image - 1, 4, 8, 16, 24 or 32)
1Eh 4 'code for compression method
22h 4 'image size (not the file size)
26h 4 'horizontal resolution (pixels per meter, signed integer)
2Ah 4 'vertical resolution (pixels per meter, signed integer)
2Eh 4 'number colors in the palette (0 if pallette not used)
32h 4 'generally ignored - number of important colors used (0 if all)
'Color Palette
'Not used for 16+ bit images - there are no palette bytes in those BMP files.
'Bitmap Data
'Pixel data, stored "upside-down" starting in the lower left corner, going left to right, then bottom row
'to top row. If height is negative, storage is top row to bottom row.
'Bits per pixel 1,4,8,24.
'Rgb Color (24-Bit) Pixel values are stored with bytes as Bgr (blue, green, red)
'Rgb Color (32-Bit) Pixel values are stored with bytes as Bgr (blue, green, red, alpha)
'Padding bytes are inserted in order to keep the line of data in multiples of four.
'For example, a bitmap with Width 1 would have 3 bytes of padding, Width 2 would
'have 2, Width 3 would have 1, and Width 4 would not have any at all.
'Example
'Here's the file content of a 2x2 pixel bitmap, with 24 Bits/Pixel encoding
'Colors are blue-green (top row) then red-white (bottom row)
Offset Size Hex Value Value Description
'---header--------------------------------------
0h 2 42 4D "BM" Magic Number (unsigned integer 66, 77)
2h 4 46 00 00 00 70 Bytes Size of the BMP file
6h 2 00 00 Unused Application Specific
8h 2 00 00 Unused Application Specific
Ah 4 36 00 00 00 54 bytes Offset to Bitmap Data
'---info (note 1st value is length of info data---------
Eh 4 28 00 00 00 40 bytes Number of bytes in the Header
12h 4 02 00 00 00 2 Pixels Width of the Bitmap in Pixels
16h 4 02 00 00 00 2 Pixels Height of the Bitmap in Pixels
1Ah 2 01 00 1 plane Number of Color planes being used.
1Ch 2 18 00 24 Bits The number of Bits/Pixel.
1Eh 4 00 00 00 00 0 BI_RGB, No compression used
22h 4 10 00 00 00 16 bytes Size of the raw BMP Data (after this Header)
26h 4 13 0B 00 00 2,835 Pixels/meter Horizontal resolution of the Image
2Ah 4 13 0B 00 00 2,835 Pixels/meter Vertical resolution of the Image
2Eh 4 00 00 00 00 0 colors Number of colors in the palette
32h 4 00 00 00 00 0 important colors Means all colors are important
'---palette (no bytes because image is 24 bits per pixel---
'---pixel data-------------------------------------
36h 3 00 00 FF 0 0 255 Red, Pixel (1,0)
39h 3 FF FF FF 255 255 255 White, Pixel (1,1)
3Ch 2 00 00 0 0 Padding For 4 byte alignment (Could be a value other than zero)
3Eh 3 FF 00 00 255 0 0 Blue, Pixel (0,0)
41h 3 00 FF 00 0 255 0 Green, Pixel (0,1)
44h 2
'note: pixel data is BGR position
'note: the first Line in the BMP file is actually the bottom line of the image.
'gbs_00415
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm