The pixels start at the offset given by bfOffBits in the
file header. Without compression they are
stored as rows, each padded to a multiple of 4 bytes, normally from the bottom of the
picture to the top. This page describes uncompressed pixels;
compression covers the rest.
00000036 FF 00 00 00 FF 00 00 00 00 00 FF FF FF FF 00 00
Row size
Each row is rounded up to a whole number of 4-byte words. The number of bytes in a row, often called the stride, is:
const stride = Math.floor((bitCount * width + 31) / 32) * 4;
The padding bytes should be zero and are ignored when reading. The size of the pixel
data is stride × |height|, which is what biSizeImage holds when
it is set.
| Bit count | Width | Bits of pixels | Stride | Padding |
|---|---|---|---|---|
1 | 10 | 10 | 4 bytes | 2 bytes and 6 bits |
4 | 5 | 20 | 4 bytes | 1 byte and 4 bits |
8 | 5 | 40 | 8 bytes | 3 bytes |
16 | 3 | 48 | 8 bytes | 2 bytes |
24 | 3 | 72 | 12 bytes | 3 bytes |
32 | 3 | 96 | 12 bytes | none, ever |
Row order
When the height in the DIB header is positive, the first row in the file is the bottom row of the picture and the last row is the top. When the height is negative, the rows run from the top down, like most other formats. Pixels within a row always run from left to right.
To find the row that is drawn at line y from the top of a bottom-up image,
read row height − 1 − y of the data.
Pixels at each bit depth
| Bit count | Each pixel is | Packing |
|---|---|---|
1 | an index into a 2-colour 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 |
16 | a colour, split by bit masks | a little-endian 16-bit number; 5-5-5 by default |
24 | a colour | 3 bytes: blue, green, red |
32 | a colour, split by bit masks | a little-endian 32-bit number; by default the bytes are blue, green, red, unused |
Worked examples
1 bit, 10 pixels wide
The pixel values 1 0 1 1 0 0 1 1 1 0 fill the first byte with
10110011 and start the second with 10. The rest of the second
byte and two more bytes pad the row to 4.
B3 80 00 00
4 bits, 5 pixels wide
The palette indexes 1 2 3 4 5 pack two to a byte, high nibble first. The
last byte holds the fifth pixel and a zero nibble, and one zero byte pads the row to
4.
12 34 50 00
16 bits, 5-5-5
Pure red is 0 11111 00000 00000 in binary, the number
0x7C00. Stored little-endian, the low byte comes first.
00 7C
24 bits, 2 by 2
The example at the top of this page holds a 2 by 2 image. The bottom row comes first:
blue (FF 00 00) and green (00 FF 00), then 2 bytes of padding.
The top row follows: red (00 00 FF) and white (FF FF FF), then
its padding. The overview shows the whole 70-byte
file.