Introduction
This is the bitmap format of OS/2 2.x and later. A file holds a device independent
bitmap (DIB): a 14-byte file header, a header of 16 to 64 bytes describing the image, an
optional palette and the pixels. It starts with BM, like a Windows
bitmap.
The header, BITMAPINFOHEADER2 (also known as
OS22XBITMAPHEADER), lines up with the first 40 bytes of the Windows header,
but it is a different format. Compression 3 means Huffman 1D and 4 means RLE24, where
Windows uses them for bit masks and JPEG. The header can be cut short, with the missing
fields taken as zero, and it adds OS/2’s own fields for units, halftoning and colour
encoding. Files use the extension .bmp.
Structure
The file has four parts, in this order. The colour table is there at 8 bits per pixel and below. All numbers are little-endian.
0File headerBM, the file size and the offset of the pixels14 bytes14Header its size, then as many fields as that size covers16 to 64 bytes…Colour table the palette, 4 bytes per entryup to 1024 bytes…Pixel data rows padded to 4 bytes, bottom row first, or compressed; at the offset in the file headervaries
File header
The file starts with the 14-byte BITMAPFILEHEADER2. It has no padding, so
the fields after the first are not aligned to 4 bytes.
| Offset | Size | Type | Name | Description |
|---|---|---|---|---|
| 0 | 2 | char[2] | usType | The signature BM (42 4D), the constant BFT_BMAP. |
| 2 | 4 | u32 | cbSize | IBM defines this as the size of the header structure, 14 plus cbFix. Many programs write the size of the whole file instead, so readers should not depend on it. |
| 6 | 2 | i16 | xHotspot | Used by icons and pointers; ignored in a bitmap and usually zero. |
| 8 | 2 | i16 | yHotspot | Used by icons and pointers; ignored in a bitmap and usually zero. |
| 10 | 4 | u32 | offBits | The offset from the start of the file to the pixel data. |
The same file header begins every other format in the family: BFT_ICON
(IC), BFT_COLORICON (CI),
BFT_POINTER (PT) and BFT_COLORPOINTER
(CP), and the array header BFT_BITMAPARRAY (BA).
Header
The header follows at offset 14. Its first field, cbFix, gives its length.
A header can stop after any field: the fields it holds are the ones that fit in
cbFix bytes, and every field after that is taken as zero. The full header is
64 bytes; the shortest, 16 bytes, ends after the bit count. A length that ends part-way
through a field is not a valid cut, and the explorer reports it. Offsets in this table
are from the start of the header; add 14 for the offset in the file.
| Offset | Size | Type | Name | Description |
|---|---|---|---|---|
| 0 | 4 | u32 | cbFix | The header size, 16 to 64. |
| 4 | 4 | u32 | cx | The width in pixels. |
| 8 | 4 | u32 | cy | The height in pixels. It is unsigned: the rows are stored bottom row first. |
| 12 | 2 | u16 | cPlanes | The number of planes, 1. |
| 14 | 2 | u16 | cBitCount | Bits per pixel, usually 1, 4, 8 or 24. A 16-byte header ends here. |
| 16 | 4 | u32 | ulCompression | How the pixels are stored: one of the compression values, 0 for none. |
| 20 | 4 | u32 | cbImage | The size of the pixel data in bytes. May be 0 for an uncompressed image. |
| 24 | 4 | u32 | cxResolution | Horizontal resolution of the target device, in usUnits. |
| 28 | 4 | u32 | cyResolution | Vertical resolution of the target device, in usUnits. |
| 32 | 4 | u32 | cclrUsed | The number of entries in the colour table. 0 means the most the bit count allows. |
| 36 | 4 | u32 | cclrImportant | The number of colours needed to show the image. 0 means all of them. |
| 40 | 2 | u16 | usUnits | The resolution units. BRU_METRIC (0), pixels per metre, is the only value defined. |
| 42 | 2 | u16 | usReserved | Reserved. |
| 44 | 2 | u16 | usRecording | The row order. BRA_BOTTOMUP (0), bottom row first, is the only value defined. |
| 46 | 2 | u16 | usRendering | The halftoning algorithm used, if any: see halftoning. |
| 48 | 4 | u32 | cSize1 | The first halftoning parameter: see halftoning. |
| 52 | 4 | u32 | cSize2 | The second halftoning parameter: see halftoning. |
| 56 | 4 | u32 | ulColorEncoding | The colour encoding. BCE_RGB (0), each colour table entry an RGB2, is the only value defined. |
| 60 | 4 | u32 | ulIdentifier | Free for the application’s own use. The full header ends here, at 64 bytes. |
Because a missing field counts as zero, a 16-byte header means an uncompressed image whose colour table has the full number of entries.
Halftoning
usRendering says how the bitmap was halftoned, and
cSize1 and cSize2 are the algorithm’s parameters. The
header records the method only; a reader draws the stored pixels as they are.
| Value | Name | Algorithm | cSize1 | cSize2 |
|---|---|---|---|---|
| 0 | BRH_NOTHALFTONED | Not halftoned | Unused | Unused |
| 1 | BRH_ERRORDIFFUSION | Error diffusion, or damped error diffusion | The error damping, as a percentage from 0 to 100 | Ignored |
| 2 | BRH_PANDA | Processing Algorithm for Non-coded Document Acquisition | The pattern’s width in pixels | The pattern’s height in pixels |
| 3 | BRH_SUPERCIRCLE | Super Circle | The pattern’s width in pixels | The pattern’s height in pixels |
Colour table
At 1, 4 and 8 bits per pixel the colour table follows the header, and each pixel is
a number that picks one of its entries. It holds cclrUsed entries, or 2, 16
or 256 when cclrUsed is 0 or missing. A 24-bit image normally has none.
Each entry is an RGB2 of 4 bytes:
| Offset | Size | Type | Name | Description |
|---|---|---|---|---|
| 0 | 1 | u8 | bBlue | Blue, 0 to 255. |
| 1 | 1 | u8 | bGreen | Green, 0 to 255. |
| 2 | 1 | u8 | bRed | Red, 0 to 255. |
| 3 | 1 | u8 | fcOptions | Entry options for OS/2’s palette manager: PC_RESERVED (1) and PC_EXPLICIT (2). Zero in a stored bitmap. |
The table is 4 × entries bytes long, so a full 256-colour table takes 1024
bytes. It can hold any colours in any order, so always look colours up in it. The
pixels start at offBits, which may leave a gap after the table.
Pixel data
Uncompressed pixels are stored as rows from the bottom of the picture to the top, and pixels within a row run from left to right. Each row is padded with zero bytes to a multiple of 4 bytes:
const stride = Math.floor((bitCount * width + 31) / 32) * 4;
| Bit count | Each pixel is | Packing |
|---|---|---|
1 | an index into a 2-colour table | 8 pixels per byte; the highest bit is the leftmost pixel |
4 | an index into a 16-colour table | 2 pixels per byte; the high nibble is the left pixel |
8 | an index into a 256-colour table | 1 pixel per byte |
24 | a colour | 3 bytes: blue, green, red |
To find the row drawn at line y from the top, read row
height − 1 − y of the data.
Compression
ulCompression says how the pixel data is stored. When the header is too
short to hold it, it is 0. A compressed image’s cbImage gives the size of
the compressed data. Each method has one bit count, given in the table; the explorer
reports a file whose bit count does not match its method, or a value above 4.
| Value | Name | Bit count | Meaning |
|---|---|---|---|
| 0x00 (0) | BCA_UNCOMP | any | Uncompressed. |
| 0x01 (1) | BCA_RLE8 | 8 | Run-length encoded 8-bit indexes. |
| 0x02 (2) | BCA_RLE4 | 4 | Run-length encoded 4-bit indexes. |
| 0x03 (3) | BCA_HUFFMAN1D | 1 | Huffman 1D coding of a 1-bit image. |
| 0x04 (4) | BCA_RLE24 | 24 | Run-length encoded 24-bit colours. |
RLE8 data is read two bytes at a time. In encoded mode the first byte is a count from 1 to 255 and the second is a palette index to repeat that many times. A first byte of zero is an escape, and the second byte says what it means:
| Second byte | Meaning |
|---|---|
| 0x00 (0) | End of line: go to the start of the next row. |
| 0x01 (1) | End of bitmap: the image is complete. |
| 0x02 (2) | Delta: the next two bytes move the position right and on by that many pixels and rows. Skipped pixels are left undefined. |
| 0x03 (3) to 0xFF (255) | Absolute mode: that many literal palette indexes follow, padded with a zero byte to an even length. |
RLE4 works the same way with 4-bit indexes. In encoded mode the count is a number of pixels and the second byte holds two indexes, used in turn: high nibble, low nibble, high nibble and so on. In absolute mode the count is a number of pixels, packed two to a byte, and the bytes are padded to an even length.
RLE24 works the same way with 24-bit colours. In encoded mode the count is a number of pixels, followed by three bytes, the blue, green and red of one colour, repeated that many times. The escapes are those of RLE8. In absolute mode the count is a number of bytes, a multiple of 3, each 3 bytes being one pixel’s blue, green and red, padded with a zero byte to an even length. That the count is in bytes is stated by David Charlap’s article in Dr. Dobb’s Journal. The one published example that reads otherwise, in Encyclopedia of Graphics File Formats, shows a literal run of count 1, which cannot be right, as a count of 1 is the end-of-bitmap escape.
| Bytes | Meaning |
|---|---|
03 10 20 30 | Three pixels of the colour red 30, green 20, blue 10. |
00 06 01 02 03 04 05 06 | Literal pixels: two pixels, blue 01, green 02, red 03, then blue 04, green 05, red 06. |
00 03 07 08 09 00 | One literal pixel, with a padding byte to make the length even. |
00 00 | End of line. |
00 01 | End of bitmap. |
Huffman 1D
BCA_HUFFMAN1D applies only to 1-bit images. IBM calls it modified Huffman
encoding and never published how OS/2 frames it inside a bitmap; the code itself is the
one-dimensional coding of ITU-T T.4, the Group 3 fax standard. Each row is a series of
runs that start with white and alternate between white and black. A run shorter than
64 is one terminating code. A longer one is one or more make-up codes for its
multiples of 64 and then a terminating code for the rest.
Where IBM’s reference is silent, the explorer takes the most natural reading and
accepts all of what T.4 allows: bits are read from the high bit of each byte down; rows
go bottom row first, like every other OS/2 bitmap; end-of-line codes,
000000000001 with any fill zeros before it, may come before a row and are
skipped; rows may follow on bit by bit or each start on a byte or a 4-byte boundary,
whichever decodes every row; and a white run is pixel value 0 and a black run pixel
value 1, whose colours come from the colour table. The tables below give the code
for each run length.
| Run | White | Black |
|---|---|---|
| 0 | 00110101 | 0000110111 |
| 1 | 000111 | 010 |
| 2 | 0111 | 11 |
| 3 | 1000 | 10 |
| 4 | 1011 | 011 |
| 5 | 1100 | 0011 |
| 6 | 1110 | 0010 |
| 7 | 1111 | 00011 |
| 8 | 10011 | 000101 |
| 9 | 10100 | 000100 |
| 10 | 00111 | 0000100 |
| 11 | 01000 | 0000101 |
| 12 | 001000 | 0000111 |
| 13 | 000011 | 00000100 |
| 14 | 110100 | 00000111 |
| 15 | 110101 | 000011000 |
| 16 | 101010 | 0000010111 |
| 17 | 101011 | 0000011000 |
| 18 | 0100111 | 0000001000 |
| 19 | 0001100 | 00001100111 |
| 20 | 0001000 | 00001101000 |
| 21 | 0010111 | 00001101100 |
| 22 | 0000011 | 00000110111 |
| 23 | 0000100 | 00000101000 |
| 24 | 0101000 | 00000010111 |
| 25 | 0101011 | 00000011000 |
| 26 | 0010011 | 000011001010 |
| 27 | 0100100 | 000011001011 |
| 28 | 0011000 | 000011001100 |
| 29 | 00000010 | 000011001101 |
| 30 | 00000011 | 000001101000 |
| 31 | 00011010 | 000001101001 |
| 32 | 00011011 | 000001101010 |
| 33 | 00010010 | 000001101011 |
| 34 | 00010011 | 000011010010 |
| 35 | 00010100 | 000011010011 |
| 36 | 00010101 | 000011010100 |
| 37 | 00010110 | 000011010101 |
| 38 | 00010111 | 000011010110 |
| 39 | 00101000 | 000011010111 |
| 40 | 00101001 | 000001101100 |
| 41 | 00101010 | 000001101101 |
| 42 | 00101011 | 000011011010 |
| 43 | 00101100 | 000011011011 |
| 44 | 00101101 | 000001010100 |
| 45 | 00000100 | 000001010101 |
| 46 | 00000101 | 000001010110 |
| 47 | 00001010 | 000001010111 |
| 48 | 00001011 | 000001100100 |
| 49 | 01010010 | 000001100101 |
| 50 | 01010011 | 000001010010 |
| 51 | 01010100 | 000001010011 |
| 52 | 01010101 | 000000100100 |
| 53 | 00100100 | 000000110111 |
| 54 | 00100101 | 000000111000 |
| 55 | 01011000 | 000000100111 |
| 56 | 01011001 | 000000101000 |
| 57 | 01011010 | 000001011000 |
| 58 | 01011011 | 000001011001 |
| 59 | 01001010 | 000000101011 |
| 60 | 01001011 | 000000101100 |
| 61 | 00110010 | 000001011010 |
| 62 | 00110011 | 000001100110 |
| 63 | 00110100 | 000001100111 |
| 64 | 11011 | 0000001111 |
| 128 | 10010 | 000011001000 |
| 192 | 010111 | 000011001001 |
| 256 | 0110111 | 000001011011 |
| 320 | 00110110 | 000000110011 |
| 384 | 00110111 | 000000110100 |
| 448 | 01100100 | 000000110101 |
| 512 | 01100101 | 0000001101100 |
| 576 | 01101000 | 0000001101101 |
| 640 | 01100111 | 0000001001010 |
| 704 | 011001100 | 0000001001011 |
| 768 | 011001101 | 0000001001100 |
| 832 | 011010010 | 0000001001101 |
| 896 | 011010011 | 0000001110010 |
| 960 | 011010100 | 0000001110011 |
| 1024 | 011010101 | 0000001110100 |
| 1088 | 011010110 | 0000001110101 |
| 1152 | 011010111 | 0000001110110 |
| 1216 | 011011000 | 0000001110111 |
| 1280 | 011011001 | 0000001010010 |
| 1344 | 011011010 | 0000001010011 |
| 1408 | 011011011 | 0000001010100 |
| 1472 | 010011000 | 0000001010101 |
| 1536 | 010011001 | 0000001011010 |
| 1600 | 010011010 | 0000001011011 |
| 1664 | 011000 | 0000001100100 |
| 1728 | 010011011 | 0000001100101 |
Runs of 1792 and over use the same make-up codes for both colours:
| Run | Code |
|---|---|
| 1792 | 00000001000 |
| 1856 | 00000001100 |
| 1920 | 00000001101 |
| 1984 | 000000010010 |
| 2048 | 000000010011 |
| 2112 | 000000010100 |
| 2176 | 000000010101 |
| 2240 | 000000010110 |
| 2304 | 000000010111 |
| 2368 | 000000011100 |
| 2432 | 000000011101 |
| 2496 | 000000011110 |
| 2560 | 000000011111 |
A complete file
This 2 by 2 pixel, 24-bit image has the shortest header, 16 bytes, and is 46 bytes long. The header is marked. The pixels start at offset 30: the bottom row, blue and green, then the top row, red and white, each 6 bytes padded to 8.
00000000 42 4D 2E 00 00 00 00 00 00 00 1E 00 00 00 10 00
00000010 00 00 02 00 00 00 02 00 00 00 01 00 18 00 FF 00
00000020 00 00 FF 00 00 00 00 00 FF FF FF FF 00 00
Telling it apart
The explorer’s parser for this format checks two things: the file starts with
BM (42 4D), and the header size, the four bytes at offset 14,
is from 16 to 64. A file that fails either check is turned down, and the explorer tries
the parsers for the other BMP formats.
Sizes of 40, 52 and 56 bytes are also Windows header sizes, and a file with one of them
is read as a Windows bitmap, unless its compression and bit count mean something only
OS/2 defines: compression 3 at 1 bit per pixel (Huffman 1D, where Windows bit masks need
16 or 32) or compression 4 at 24 bits per pixel when the pixels do not start with a
JPEG’s FF D8 (RLE24).
At 40 bytes the two headers then mean the same. At 52 and 56 bytes they do not: bytes
40 to 55 are usUnits, usReserved, usRecording,
usRendering, cSize1 and cSize2 to OS/2, but the
red, green, blue and alpha masks to Windows. A 52- or 56-byte header without bit masks
(compression 0, 1 or 2) is read as OS/2 when those bytes hold OS/2 values with a
halftoning algorithm set: units, reserved and recording 0, and rendering 1, 2 or 3. As
Windows masks those would be a red mask of zero and a green mask of one or two stray
bits. When the bytes are all zero both readings say the same, and the file is read as
Windows, whose view gives the OS/2 meaning of each of those fields beside it. A Windows
2 and OS/2 1.x bitmap has a 12-byte header.
Variants
- Windows bitmap (BM) – Windows 3.0 and later: a 40- to 124-byte header
- Windows 2 and OS/2 1.x bitmap (BM) – the first bitmaps, with a 12-byte header
- OS/2 2.x bitmap (BM) – a 16- to 64-byte header, with Huffman 1D and RLE24
- OS/2 bitmap array (BA) – several OS/2 bitmaps, icons or pointers for different screens, chained in one file
- OS/2 icon (IC) – a black and white OS/2 icon: AND and XOR masks in one 1-bit bitmap
- OS/2 colour icon (CI) – a colour OS/2 icon: a 1-bit mask bitmap followed by a colour image
- OS/2 pointer (PT) – a black and white OS/2 mouse pointer, with its hotspot in the header
- OS/2 colour pointer (CP) – a colour OS/2 mouse pointer: masks, a colour image and a hotspot
Links
Software
Reference
- BITMAPINFO2 – IBM’s description of the header, with every constant
- BITMAPFILEHEADER2 – IBM’s description of the file header and its type codes
- ITU-T T.4 – the fax standard that defines the Huffman 1D run-length codes
- The BMP File Format, Part 2 – David Charlap, Dr. Dobb’s Journal, 1995, on RLE24 and Huffman