Introduction
XPM is a colour image format for icons and small pictures in the X Window System. Like XBM, an XPM file is C source code that a program can compile in, but it holds colours and transparency. Each pixel is written as one or more characters, so the picture can be read, and even drawn, in a text editor.
Arnaud Le Hors wrote the format and its library, libXpm, at Groupe Bull. It was widely used for the icons of Unix desktops and programs, and many toolkits still read it.
Structure
The file starts with the comment /* XPM */, then declares an array of
strings. The first string gives the width, height, number of colours and characters
per pixel, optionally followed by a hotspot. One string per colour follows, giving the
characters and the colour for each display type: c for colour,
g for greyscale, m for mono and s for a
symbolic name. The colour None marks transparent pixels. The remaining
strings are the rows of pixels, from the top, and optional extension strings can
follow them. See XPM 3 syntax; the older forms
are on XPM 1 and XPM 2.
/* XPM */
static char *dot[] = {
"5 5 2 1",
" c None",
". c #FF0000",
" ... ",
".....",
".....",
".....",
" ... "};
Not described
The XPM 3 and versions pages follow the libXpm source and the XPM manual.
Known, not yet covered
- Reference: the writer’s file layout is not described (libXpm WrFFrI.c and scan.c): the variable name is the file’s base name with each
.and-changed to_(image_namewhen there is no name); the header is/* XPM */thenstatic char * name[] = {; each colour line is the code, then a tab before each key and its value, keys in the orders,m,g4,g,c; the transparent colour comes first asc None; unnamed colours are written as#RRRRGGGGBBBBin upper-case hex (a colour name instead when an rgb.txt file is supplied and matches exactly); the cpp used is the smallest that fits (92 codes per character), with codes taken in index order from the exact 92-character string.XoO+@#$%&*=-;:>,<1234567890qwertyuipasdfghjklzxcvbnmMNBVCZASDFGHJKLPIUYTREWQ!~^/()_`'][{}|; comments are written as/*text*/before the values, colours and pixels; the last string has no comma; extensions are written as"XPMEXT name", its lines, then"XPMENDEXT"; the file ends with};. (NEW) - Reference: colour-name handling is not described. libXpm reads
#values and names through XlibXParseColor, so the X server’s colour database decides; the case and space rules and how 1 to 4 hex digits are scaled belong to Xlib and were not checked against raw source. libXpm’s own rgb.txt reader (rgb.c) is used only by the writer, to turn an exact RGB match back into a name: it is given a file name by the program and does not search for one, reads lines of three decimal numbers 0 to 255 and a name (the rest of the line), skips bad lines silently, lower-cases names, scales values by 257, and keeps at most 1024 entries. (NEW) - Reference: reader rules that make a file valid or invalid are not stated (parse.c, misc.c): numbers are decimal digits only, with no sign, and are unsigned 32-bit values that wrap silently; width or height of 0 is invalid; a pixel string shorter than width × cpp, a missing pixel string, a code with no colour, and a colour string with no key and value are invalid, and extra characters at the end of a pixel string are ignored; cpp 1 allows at most 256 colours and a NUL code is invalid; cpp of BUFSIZ or more, or a colour value text of BUFSIZ or more, is invalid; width × height of 2^30 or more, or too many colours for memory, gives
XpmNoMemory; with duplicate colour codes the result depends on the code path (the last wins for cpp 1 and 2, the first for longer codes). (NEW) - Reference: colour-string and values-string detail is missing (parse.c): a word straight after a key is always its value even if it is itself a key word; the words of a value are joined with single spaces; a repeated key replaces the earlier one; in the values string a fifth number that is not
XPMEXTis the x hotspot and an incomplete hotspot is ignored, not an error; extension strings are read only when the values string ends withXPMEXT. (NEW) - Reference: extension reading is described wrongly or incompletely (parse.c 599-706): for the one-string form
XPMEXT name datalibXpm does not split the data off, so the whole remainder of the string becomes the name and there are no lines; strings before the firstXPMEXTare skipped; any string that begins withXPMEXTorXPMENDEXT(a prefix match) ends the data of the current extension, so data lines must not begin with those words. (NEW) - Reference: comment and word rules of the XPM 2 syntaxes are incomplete (data.c 409-415): C comments are
/* */, Lisp comments run from;to the end of the line, plain comments from!; libXpm’s word separator is any Cisspacecharacter (including new line, carriage return, vertical tab and form feed), wider than the manual’s space and tab; a comment longer than BUFSIZ characters loses its start; and when a named file is missing and libXpm was built with STAT_ZFILE it also tries the name with.Zand then.gzadded. (NEW) - Viewer: there is no XPM plugin yet.
Links
Software
- GIMP – free image editor that opens and saves XPM (Windows, macOS, Linux)
- ImageMagick – command-line tool that converts images to and from XPM (Windows, macOS, Linux)
Developers
- libXpm – the reference library that reads and writes XPM (C)
Reference
- XPM Manual – Arnaud Le Hors’s manual for the format and libXpm, published by X.Org (PDF)