Home / Alt manpages / pbmtozinc(1)

  • pbmtozinc(1)
  • User command
  • linux

Convert PBM Images to Zinc Bitmaps with pbmtozinc

Still maintaining code built on the Zinc Interface Library, pbmtozinc turns a PBM image into the bitmap declaration that library expects. The installed command is Netpbm 11.5.2, from Debian package netpbm 2:11.05.02-1.1build1. A small conversion and a basic check take about ten minutes.

You need a readable PBM file and a writable destination directory. The conversion itself is unprivileged: do not reach for sudo just because the command lives in an image-handling package. Elevation only matters if the permissions on the input or destination genuinely demand it.

1. Check the installed command

Confirm which executable will run and note the local Netpbm version. This is read-only and needs no elevated privileges:

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

Version output carries build details alongside the version number. On a Debian-based machine you can cross-check the package version too:

$ dpkg-query -W -f='${Package} ${Version}\n' netpbm
netpbm 2:11.05.02-1.1build1

Checkpoint

If command -v resolves to a different executable than you expected, stop and confirm you are testing the installation you think you are.

2. Confirm the input is a PBM

pbmtozinc takes one optional input file, or reads standard input if you give it none. It does not resize, threshold or redraw anything, it converts the PBM data exactly as received. A quick header check catches a wrong file before you waste a conversion on it:

$ head -n 3 /path/to/input.pbm
P1
8 4
0 1 0 1 0 1 0 1

P1 is the plain PBM form; a binary PBM may start with P4 instead, and the installed Netpbm reader handles both the usual ways. If another program generated the file, keep a copy of the original while you test.

3. Convert to a Zinc source file

Pass the PBM path and redirect standard output to a new file:

$ pbmtozinc /path/to/input.pbm > /path/to/output.zinc
$ printf 'exit status: %s\n' "$?"
exit status: 0

The output is source text, not a PNG or another Netpbm raster. A small 8 by 4 input comes out looking like this:

USHORT /path/to/input[] = {
  8
  4
 0x5500,0xaa00,0x3300,0xcc00};

The declaration name comes from the input filename. The first two values give the image dimensions, followed by the Zinc bitmap data itself. Every pixel in the PBM feeds into those values, so do not hand-edit the generated words to patch a source image: fix the PBM and convert it again instead.

Checkpoint

Confirm the destination exists, is non-empty, and starts with the declaration you expect:

$ test -s /path/to/output.zinc && echo 'output is non-empty'
output is non-empty
$ sed -n '1,5p' /path/to/output.zinc
USHORT /path/to/input[] = {
  8
  4
 0x5500,0xaa00,0x3300,0xcc00};

4. Use standard input when a pipeline is clearer

Leave off the file operand to read PBM data from standard input, useful when another trusted Netpbm command produces the PBM, or when you actually want the converter's fallback name:

$ pbmtozinc < /path/to/input.pbm > /path/to/piped.zinc
$ sed -n '1p' /path/to/piped.zinc
USHORT noname[] = {

With no filename on standard input, the declaration comes out named noname. If the code that consumes this source expects a particular identifier, either name your input file or rename the declaration deliberately after checking what got generated. Keep the input and output redirections separate, so the shell cannot feed a freshly truncated, empty output file straight back into the command.

5. Protect an existing output

Shell > truncates an existing destination before pbmtozinc even runs, which is a destructive overwrite of the output file even though the PBM input stays untouched. For a safer replacement, convert into a temporary file in the same directory, check it, then move it into place:

$ tmp=$(mktemp /path/to/pbmtozinc.XXXXXX)
$ pbmtozinc /path/to/input.pbm > "$tmp" && test -s "$tmp"
$ sed -n '1,4p' "$tmp"
$ mv -- "$tmp" /path/to/output.zinc

That final mv only replaces the old output after a successful conversion and a non-empty check. If conversion fails instead, remove the temporary file and the old output is left exactly as it was:

$ rm -- "$tmp"

Only run that cleanup while tmp still names the temporary file you just created. If a replacement turns out to be a mistake, restore the old file from your normal backup before converting anything else: the command itself has no undo.

6. Diagnose a failed conversion

A missing or unreadable path normally shows up as a non-zero exit status. Check it without changing any permissions:

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

If the input is not actually a Netpbm image, the program exits with status 1 and names the problem directly:

pbmtozinc: bad magic number 0x6e6f - not a PPM, PGM, PBM, or PAM file

A zero exit status is not proof the picture looks right. Check the dimensions and look at the Zinc source in whatever will actually compile or load it. Keep the original PBM around until that later step succeeds.

Done means

  • Version checked. The installed command and Netpbm version match what you expected.
  • Input verified. The PBM is readable and the original is still there.
  • Conversion succeeded. pbmtozinc returned status 0 and wrote non-empty Zinc bitmap source.
  • Output inspected. The generated dimensions and declaration name have been checked.
  • Old output protected. Truncation was avoided or the replacement was deliberately reviewed first.