Home / Alt manpages / pbmtoxbm(1)

  • pbmtoxbm(1)
  • User command
  • linux

Convert PBM Images to XBM with pbmtoxbm

You will finish with an XBM source file generated from a PBM image, with its dimensions checked and its X11 or X10 format chosen deliberately. The examples use Netpbm 11.5.2, provided here by package version 2:11.05.02-1.1build1.

Allow about ten minutes. You need a shell, the netpbm package, and a readable PBM file. The conversion itself normally needs no elevated privileges. Do not use sudo unless the input or destination directory is genuinely inaccessible to your account.

1. Check the installed command

Confirm which executable will run and record the Netpbm version. These are ordinary read-only checks:

$ command -v pbmtoxbm
/usr/bin/pbmtoxbm
$ pbmtoxbm --version
pbmtoxbm: Using libnetpbm from Netpbm Version: Netpbm 11.5.2
pbmtoxbm: Built from source dated 2024-03-31 09:09:47

The version details after the first line can include build information. If command -v finds nothing, install Netpbm through your normal system package process, then repeat this check. Installing a package is an administrative change, so it is outside the conversion steps below.

Checkpoint

You should have a path to pbmtoxbm and a readable PBM file before creating any output.

2. Convert a PBM file to the default X11 format

The basic form is pbmtoxbm [options] [pbmfile]. Give the output a new name so that a failed run cannot destroy an existing XBM file:

$ pbmtoxbm input.pbm > output.xbm
$ file output.xbm
output.xbm: xbm image (8x4), ASCII text

Replace input.pbm with your file. The command writes XBM text to standard output, so the shell redirection creates output.xbm. The X11 form is the default. The explicit -x11 option is valid but does not change that result:

$ pbmtoxbm -x11 input.pbm > output-x11.xbm
$ file output-x11.xbm
output-x11.xbm: xbm image (8x4), ASCII text

The dimensions in the file result come from the input, so yours will differ. A non-zero exit status means the conversion failed; do not treat a partially written destination as a valid image.

3. Inspect the generated XBM source

XBM is C-like source, not a binary image container. Open the first few lines with a pager or sed:

$ sed -n '1,12p' output.xbm
#define input_width 8
#define input_height 4
static char input_bits[] = {
 0xa5,0x5a,0xff,0x00};

The identifier is normally based on the input file name. The output contains width and height definitions followed by the bitmap bytes. Do not edit the generated data merely to make it look like a normal image file; programs that consume XBM expect this source form.

If you pipe PBM data on standard input instead of naming a file, the conversion still works:

$ pbmtoxbm < input.pbm > from-stdin.xbm
$ file from-stdin.xbm
from-stdin.xbm: xbm image (8x4), ASCII text

With standard input, the installed command uses a generic identifier such as noname. Use a named input file when the generated C identifier matters to the code that includes it.

4. Choose X10 only for a compatible consumer

Use -x10 when the program receiving the result specifically requires the older X10 XBM representation:

$ pbmtoxbm -x10 input.pbm > output-x10.xbm
$ file output-x10.xbm
output-x10.xbm: xbm image (8x4), ASCII text
$ sed -n '1,5p' output-x10.xbm
#define input_width 8
#define input_height 4
static short input_bits[] = {

On this Netpbm build, X10 output uses a static short data array, while the default X11 output uses static char. The exact bytes depend on the PBM pixels. Do not select X10 because it sounds more compatible: X11 is the default and is the normal choice for current XBM consumers.

-x10 and -x11 are alternatives. Supplying both is an error:

$ pbmtoxbm -x10 -x11 input.pbm > rejected.xbm
$ printf 'exit status: %s\n' "$?"
exit status: 1

Remove the rejected file before reusing its name if it was created by the shell. A safer pattern is to write to a temporary name and rename only after checking the result:

$ pbmtoxbm input.pbm > output.xbm.new
$ file output.xbm.new
output.xbm.new: xbm image (8x4), ASCII text
$ mv output.xbm.new output.xbm

The final mv replaces an existing output.xbm. Only run it after you have checked the new file and are certain replacement is intended. If conversion fails, leave the old output in place and remove the incomplete .new file manually after inspecting it.

5. Diagnose input and output mistakes

For an input error, check the path and read permission without changing anything:

$ ls -l input.pbm
$ test -r input.pbm && echo readable
readable

A missing file or an unreadable file produces a non-zero exit status. Check the spelling and directory first. Running the converter as root does not repair a wrong path, and it can leave root-owned output behind.

Check that the destination is writable before a batch conversion:

$ test -w . && echo destination-directory-is-writable
destination-directory-is-writable

Shell redirection happens before pbmtoxbm runs. Therefore pbmtoxbm input.pbm > output.xbm truncates an existing output.xbm even if the input later proves invalid. Use a new destination or the .new and mv pattern above when the old file is valuable.

If file does not identify the result as XBM, check the exit status and inspect the first lines. An empty file, an error message captured by unusual redirection, or a non-PBM input needs correction before the XBM is passed to another program. Keep the original PBM until the consumer has accepted the conversion.

Done means

  • pbmtoxbm resolves to the intended Netpbm installation and its version is known.
  • The PBM input is readable and the XBM output is non-empty.
  • file reports the expected dimensions and XBM format.
  • X11 was left as the default unless a compatible consumer required -x10.
  • An existing output was not overwritten blindly, and the original PBM remains available for recovery.