An XBM file is C source text: a few #define lines and one array. The X
library’s XReadBitmapFile defines what a reader accepts, and
XWriteBitmapFile what a writer produces. This page follows the libX11
code for both.
Defines
Each define has the form #define name_suffix value,
with a decimal integer value. Only the part after the last underscore matters to the
reader; the name before it is ignored, and other defines are skipped.
| Suffix | Required | Meaning |
|---|---|---|
_width | yes | the width in pixels; must not be 0 |
_height | yes | the height in pixels; must not be 0 |
_x_hot | no | the hotspot column, for a cursor |
_y_hot | no | the hotspot row |
The width and height must come before the array. Without the hotspot defines, Xlib
reports the hotspot as −1, −1. Xlib’s writer decides on x_hot alone: if it is not
−1 it writes both hotspot defines, otherwise neither. The two defines are read
independently, so a reader can end up with one of them still −1.
The bits array
The array’s name must end in bits, conventionally
name_bits. Its declaration says which form the file is:
| Declaration | Form | Each value |
|---|---|---|
static unsigned char name_bits[] = { | X11 | 8 bits |
static char name_bits[] = { | X11 | 8 bits |
static short name_bits[] = { | X10 | 16 bits |
The values are hexadecimal, normally written 0x followed by two digits
(four in X10), separated by commas and white space, and the array ends with
};. Xlib reads only the first bits array in a file. Extra values after
the last row are ignored; too few make the file invalid.
Xlib’s writer puts 12 values on each line, each written 0x%02x, after
three spaces. It takes the name prefix from the file name it is given, minus any
directory (the text after the last /). It does not remove an extension,
so icon.xbm gives icon.xbm_width.
Rows and bit order
The rows run from the top of the image to the bottom, with no row count or separator. In the X11 form each row is ceil(width / 8) bytes. The first byte holds the leftmost 8 pixels, and within each byte the least significant bit, bit 0, is the leftmost pixel. A 1 bit is a foreground pixel, which X usually draws in black; 0 is background. Bits past the right edge in a row’s last byte are padding and are ignored.
This is the opposite bit order to most other one-bit formats, such as MacPaint and BMP, where the most significant bit is leftmost. Some third-party descriptions get it wrong; libX11 builds its image with the bit order set to least significant bit first.
For pixel (x, y) in the X11 form: byte number y × ceil(width / 8) + floor(x / 8) of the array, bit x mod 8.
The X10 form
X version 10 wrote the array as 16-bit short values. Each row is a whole
number of them, ceil(width / 16), and each value holds 16 pixels: its low
byte is the left 8 pixels and its high byte the next 8, again least significant bit
first. So a reader can split each value into low byte then high byte, and drop the
last high byte of a row when the width needs only an odd number of bytes. Xlib still
reads this form but writes only the X11 form.
How Xlib reads the file
From libX11 src/RdBitF.c, read as raw text. A reader that follows these
rules accepts what Xlib accepts. Other programs may be stricter or looser.
Before the array: line by line
- The file is read one line at a time into a buffer of 255 bytes, which holds at most 254 characters. A line that fills the buffer makes the file invalid. This check applies only to the lines read before the array; the lines of values are read character by character and have no length limit.
- A define is recognised only if the line starts with
#define, then a name, then a decimal integer. It must be at the start of its own line. The name is split at its last underscore, and the part after it is compared withwidth,heightorhot. A name with no underscore is compared whole, so#define width 8counts. Forhot, the two characters before the underscore decidex_hotory_hot. Other defines are skipped. - The array declaration must be on one line and start at the beginning of it, in one of
the three forms
static short,static unsigned charorstatic char, then a name, then= {. The text after the name must end inbits[]after its last underscore (or be exactlybits[]); any other array is skipped. Anything on the declaration line after the{is lost, so the values must begin on the next line. - Declarations with
constor withoutstaticdo not match, so Xlib skips them and reports the file invalid if no other array exists. - Only the first bits array is read. Reading stops once all the values it needs have been read.
In the array: character by character
- A value is a run of hexadecimal digits (
0–9,a–f,A–F). It ends at the first space, comma,}, tab or newline. Delimiters before the first digit are skipped. - Any other character is ignored and does not end a value. That includes the
xof0x, so0xffreads as the digits 0, f, f. It also includes a carriage return, which is not a delimiter. - Each digit shifts the value left by four bits. Only the low 8 bits are stored in the X11 form (the low 16 in the X10 form, as two bytes), so extra digits are silently dropped from the left.
- There is no decimal form:
255is read as the hexadecimal 0x255. - Comments are not skipped. A comment inside the array that contains letters
atofor digits is read as values. - The array may hold more values than needed; the extra ones are not read. If the file ends before enough values are read, the file is invalid.
Limits and errors
The format sets no maximum size. Xlib (RdBitF.c) returns
BitmapFileInvalid for a line before the bits array that fills its
254-character buffer (see the reading rules below), a missing or zero width or height
when the bits array is reached, a file with no bits array, and an array that runs out of
file before all its values are read. It has no error for a malformed value: characters
it does not understand are ignored (see below).
| Xlib result | Meaning |
|---|---|
BitmapSuccess | the file was read |
BitmapOpenFailed | the file could not be opened |
BitmapFileInvalid | the file is not valid bitmap data |
BitmapNoMemory | not enough memory |
Example
A 10 × 3 image. Row 0 has pixels 0 and 9 set, row 1 is fully set, row 2 has only pixel 1 set. Each row takes 2 bytes:
#define tiny_width 10
#define tiny_height 3
static unsigned char tiny_bits[] = {
0x01, 0x02, 0xff, 0x03, 0x02, 0x00};
| Row | Bytes | Pixels 0 to 9 |
|---|---|---|
| 0 | 0x01, 0x02 | #........# |
| 1 | 0xff, 0x03 | ########## |
| 2 | 0x02, 0x00 | .#........ |
In row 0, 0x02 in the second byte is bit 1, which is pixel 8 + 1 = 9.
The same image in the X10 form is static short tiny_bits[] = { 0x0201, 0x03ff,
0x0002 };.