XPM 3, from 1991, is the form almost every XPM file uses. It is a C array of strings in four parts: values, colours, pixels and, optionally, extensions. This page follows the XPM Manual and libXpm, the reference library.
The file
/* XPM */
static char * name[] = {
values
colours
pixels
extensions
};
The first line must be the comment /* XPM */: libXpm reads the second
word of the file, and XPM there marks an XPM 3 file in C syntax. Every
part after it is a C string in double quotes, and the strings are separated by commas.
C comments may appear between strings and are kept by libXpm as the file’s comments.
libXpm writes the declaration as static char *; many programs write
static const char *, which readers accept because they skip to the first
string.
Values
The first string holds four or more numbers in decimal, separated by spaces:
| Field | Required | Meaning |
|---|---|---|
| width | yes | the width in pixels |
| height | yes | the height in pixels: the number of pixel strings |
| ncolors | yes | the number of colour strings |
| cpp | yes | characters per pixel: how many characters name one colour |
| x_hotspot y_hotspot | no | the hotspot, for a cursor; both or neither |
XPMEXT | no | the literal word, saying that extensions follow the pixels |
libXpm’s writer picks codes from 92 printable ASCII characters: all of them except the
double quote, which would end the string, the backslash, which C treats as an
escape, and ?, which could form a C trigraph. Its first choice is a
space. One character per pixel therefore covers up to 92 colours, and two cover up to
8,464. A reader should accept any byte other than the double quote.
Colours
Then come ncolors strings, one per colour. Each starts with exactly cpp characters, the code for this colour in the pixel strings, which may include spaces. After them come one or more pairs of a key and a value, separated by white space:
| Key | libXpm name | The value is the colour to use on |
|---|---|---|
c | XPM_COLOR | a colour display |
g | XPM_GRAY | a greyscale display with more than four shades |
g4 | XPM_GRAY4 | a four-shade greyscale display |
m | XPM_MONO | a black and white display |
s | symbolic | any display: a symbolic name, such as background, that the program can map to a colour of its choosing when it loads the image |
A colour string needs at least one pair; a key with no value makes the file invalid. A value
may be several words, such as light blue: it runs until the next key or
the end of the string. A reader uses the key for its own display and falls back on
another when that one is missing; most files today have only c.
Fall-back order when a key is missing
The XPM Manual (XpmReadFileToImage, pages 17 and 18) says that when no default exists
for the visual in use, a reader looks first for defaults nearer to the monochrome type
and then nearer to the colour type, and that a colour that cannot be parsed sends it
looking for another by the same rule. libXpm’s create.c does this with the
keys in the order s, m, g4, g,
c:
- If the program passed a colour for the symbolic name, try that.
- Try the wanted key, then each lower key in turn down to
m. The symbolic keysis not tried in this step. - Then try each higher key in turn up to
c. - If none gives a usable colour, reading fails with
XpmColorFailed.
For example, a greyscale display (g) with a colour entry for
c and m tries g, then g4, then
m, then c.
Colour values
| Value | Meaning |
|---|---|
None | transparent: these pixels are left out of the image’s mask. Any case |
# and hexadecimal digits | an RGB colour, with 1 to 4 digits per component: #F00, #FF0000, #FFF000000 or #FFFF00000000 |
| a colour name | a name from the X colour database, such as red, gray50 or light blue |
% and an HSV code | named in the XPM Manual beside # and colour names, and marked there “not implemented”. No format for the code is given |
libXpm passes every value except None to Xlib’s
XParseColor, so any colour form X understands works. A reader without X
needs the X colour names: most libraries carry a copy of the X rgb.txt
table.
Pixels
Then come height strings, one per row from the top. Each is width × cpp characters long: the colour code of each pixel from left to right, with no separators. A code that matches no colour string makes the file invalid.
Extensions
When the values string ends with XPMEXT, extension strings follow the
last row. Each extension starts with a string beginning XPMEXT and its
name. Its data is either the rest of that string or the strings that follow it, up to
the next XPMEXT. The string XPMENDEXT ends the extensions.
"XPMEXT author Jane Smith",
"XPMEXT notes",
"first line of notes",
"second line of notes",
"XPMENDEXT"
No extension names are standardised. libXpm hands them to the program as a name and a list of lines. The manual (page 9) says that, to avoid clashes in shared files, extension names should be prefixed with the name of the company.
Whitespace, comments and compressed files
- Whitespace. The manual (page 8) says words are separated by white space made of space and tab characters.
- Comments. A reader keeps only one comment per section (the manual, page 14, and XpmReadFileToXpmImage): if a section has several, only the last is stored. The sections are the hints (values), the colours and the pixels.
- Compressed files. The format itself has no compression. On systems
that support it, libXpm’s file readers (manual, pages 18 and 19) assume a file is
compressed when its name ends in
.Zor.gz, and read it through anuncompressorgzipprocess. If the name does not end that way the file is read as it stands. The writers compress the same way when given such a name. When reading, a NULL name means standard input.
Example
A 4 × 3 cursor with two characters per pixel, a hotspot at (1, 1) and a symbolic background colour:
/* XPM */
static char *cursor[] = {
/* width height ncolors cpp x_hot y_hot */
"4 3 3 2 1 1",
/* colours */
" c None",
"bl c #0000FF m black",
"wh c white m white s background",
/* pixels */
" blbl ",
"blwhwhbl",
" blbl "
};
The first colour’s code is two spaces. Each row is 4 × 2 = 8 characters. The middle row reads blue, white, white, blue.
libXpm results
| Value | Name | Meaning |
|---|---|---|
| 1 | XpmColorError | read, but one or more colours could not be parsed or allocated exactly |
| 0 | XpmSuccess | read |
| −1 | XpmOpenFailed | the file could not be opened |
| −2 | XpmFileInvalid | the file is not valid XPM |
| −3 | XpmNoMemory | not enough memory |
| −4 | XpmColorFailed | a colour could not be allocated at all |