[~] BACK



~~ XPM ~~

** FUNNY FILE FORMATS **

[COD] | [20200708] | [0] | [0x17]




In my latest version of XMobar, there is a small green space invader in the top left corner. This can be done with XMobar using a *.xpm file.
That's why today there is "fun with unusual file formats". XPM stands for "X PixMap" and is an ASCII text format. It is used to display raster graphics. It is mostly used for icons. XPM was developed in 1989 by Daniel Dardailler and Colas Nahaboo. The latter is perhaps known for the GWM window manager for X-Window systems.

The *.xpm format was defined so that an .xpm file is always valid C source code. This means that *.xpm files can be included directly in C code using #include.


The syntax of a .xpm file is straightforward and easy to understand.

Here is my SpaceInvader:

space_invader.xpm
/* XPM */
static char * spaceinvader_xpm[] = {
"20 20 2 1 XPMEXT",
"       c None",
".      c #afd75f",
"                    ",
"    ..        ..    ",
"    ..        ..    ",
"      ..    ..      ",
"      ..    ..      ",
"    ............    ",
"    ............    ",
"  ....  ....  ....  ",
"  ....  ....  ....  ",
"....................",
"....................",
".. .............. ..",
".. .............. ..",
".. ..          .. ..",
".. ..          .. ..",
"     ....  ....     ",
"     ....  ....     ",
"                    ",
"                    ",
"                    ",
"XPMEXT author PHØ   ",
"mailto:post@nerdbude.com",
"XPMENDEXT           "};


The *.xpm file consists of seven parts:

- header line
- declaration and beginning of assignment line
- values
- cColors
- pixels
- extensions
- end of assignment


header line

The header line consists of a C comment.

/* XPM */
This comment is considered a "magic number" and defines that it is a .xpm file.

declaration and beginning of assignment line

A C variable is declared in the declaration line. The name of the variable can be freely chosen, but must be a valid C identifier. Usually the image name is simply used here.

static char * spaceinvader_xpm[] = {

values

The values ​​contain 4 or 6 decimal numbers (in the example only 4)

20 20 2 1

20 - width of the image in px
20 - height of the image in px
2 - number of colors in the image
1 - number of characters per pixel value


The last two characters (5 + 6) are values ​​that indicate so-called hotspots, for example the point where the cursor is pointing.
For normal images, however, you can safely ignore these and don't need them.
If extensions are used in the file, the 4/6 numbers are followed by XPMEXT (xpm extension).


cColors

The number of colors has already been determined in the values ​​(2).
Now it's time to define the colors.

" c None", ". c #afd75f",

Each color definition consists of a character code. Different color definitions can be specified here for different types of display. This means that the *.xpm can be optimized for different color depths and does not need to be scaled down to b/w every time. The following options are available:

c - Color
g - more than four shades of gray
g4 - four shades of gray
m - Monochrome
s - symbolic value (such as foreground or background)

These options are followed by the actual color definition.
Either in HEX (#afd75f) or HSV (%80, 55.8, 84.3). Transparency is simply set with "None".


pixels

Now comes the actual image, or rather the information about which pixels get color and which remain transparent.
One pixel line in the image also corresponds to one line in the *.xpm file. Great for ASCII art and there are no limits to your imagination.

If you use *.xpm files in C code, you can already see the image in the source code.

extensions

Last but not least, our *.xpm file has the extensions. The extensions should be used in the following format:

"XPMEXT extension_name extension_value"

In the case of Space Invaders, this is how it looks:

"XPMEXT author PHØ" "mailto:post@nerdbude.com", "XPMENDEXT"};

The XPMENDEXT at the end closes the extensions. And "};" completes the C declaration.

So why have *.jpg and *.gif when you can have *.xpm.
This is what the little guy looks like:


and you can find him on Github:

Link (Github)
[~] BACK