Home / Alt manpages / pbmtoatk(1)

  • pbmtoatk(1)
  • User command
  • linux

Convert a PBM bitmap to an Andrew Toolkit raster with pbmtoatk

You will convert a monochrome Netpbm PBM image into an Andrew Toolkit raster object, either in a file or in a pipeline. The command writes the converted object to standard output, so the main safety issue is choosing the output destination deliberately. This takes about five minutes if you already have a PBM file.

What you need

  1. A Linux system with Netpbm installed. This guide was checked with Debian package netpbm 2:11.05.02-1.1build1, providing Netpbm 11.5.2.
  2. A readable PBM input file. PBM is a two-colour bitmap format; its pixels represent black and white rather than greyscale or colour.
  3. Write permission in the directory where you will create the raster object. No command in this guide needs root privileges.

Checkpoint

Confirm that the program you will run is the expected one.

command -v pbmtoatk
pbmtoatk -version

On the checked installation, the first command prints /usr/bin/pbmtoatk. The second prints Netpbm build information, including Netpbm Version: Netpbm 11.5.2, and exits successfully. It does not convert an image when used this way.

1. Check the PBM input

Before converting, inspect the file type and dimensions. Replace the placeholder with your own path. The filename extension is only a convention; the contents must be PBM.

INPUT='/path/to/input.pbm'
file "$INPUT"
pnmfile "$INPUT"

A valid file should be identified as Netpbm image data, and pnmfile should report a PBM image with a width and height. If pnmfile is unavailable, file is still a useful first check. Do not use a file you do not trust: conversion reads it, but does not make an unsafe source safe.

PBM has raw and plain forms. Raw PBM normally starts with P4; plain PBM starts with P1. Both are valid PBM inputs. A PBM image is strictly monochrome, so a source with shades of grey or colour must be converted to PBM first with an appropriate Netpbm tool.

2. Convert to an Andrew Toolkit raster file

Run pbmtoatk with the PBM path and redirect standard output to a new file. The program has no options specific to this conversion.

INPUT='/path/to/input.pbm'
OUTPUT='/path/to/output.rast'
pbmtoatk "$INPUT" > "$OUTPUT"

The output filename has no required suffix. .rast is a clear local convention, but the data format is Andrew Toolkit's raster object, not a PBM file. On success, the command is silent and returns exit status zero. There is no elevated-privilege step here; use a directory you own instead of reaching for sudo.

Checkpoint

Verify that the file exists and begins like an Andrew Toolkit raster object.

test -s "$OUTPUT" && sed -n '1,6p' "$OUTPUT"
printf 'exit status: %s\n' "$?"

A small successful result starts with lines similar to these:

\begindata{raster,1}
2 0 65536 65536 0 0 8 4
bits 1 8 4

The dimensions in the raster header should match the PBM dimensions. The remaining lines encode the bitmap rows for the Andrew Toolkit reader. Do not edit those lines by hand unless you are deliberately working with the raster format.

3. Use standard input in a pipeline

The optional pbmfile argument means input can also come from standard input. This is useful when another Netpbm command produces PBM and you do not need an intermediate file.

pnmfile "$INPUT"
cat "$INPUT" | pbmtoatk > "$OUTPUT"

The explicit cat is intentionally easy to replace with a producer. For example, keep the final conversion shape and substitute a command that emits a valid PBM stream:

some-pbm-producing-command | pbmtoatk > "$OUTPUT"

Do not put diagnostic text into the same stream as the raster object. Standard output is the converted object; a shell redirection such as > saves it exactly. If you want to retain diagnostics separately, redirect standard error independently:

pbmtoatk "$INPUT" > "$OUTPUT" 2>pbmtoatk.err

Common failure modes

Wrong path or permissions

A missing input file produces an error naming the path and a non-zero exit status. Check the path and read permission, then rerun the conversion. If the output file was created by a failed redirection, inspect it before reusing the name. A safe retry is to write a new temporary output and replace the old result only after success:

tmp_output=$(mktemp "\${OUTPUT}.XXXXXX")
if pbmtoatk "$INPUT" > "$tmp_output"; then
    mv "$tmp_output" "$OUTPUT"
else
    status=$?
    rm -f "$tmp_output"
    exit "$status"
fi

This changes state only at the final mv. If the conversion fails, the previous $OUTPUT remains untouched. The rm removes only the temporary file created by this command.

Input is not PBM

If Netpbm cannot parse the input, stop and identify its real format. Do not rename a PNG, JPEG or PGM file to make it look like PBM. Convert it with a format-appropriate tool, then run pnmfile again. A successful file-type check does not replace checking the image dimensions and appearance in your normal image workflow.

Output looks empty or corrupt

Check the exit status and the first lines of the output before opening it elsewhere. Also check that the source dimensions are sensible and that the output was not overwritten by a second command. Because the result is a textual Andrew Toolkit object with encoded bitmap data, a text editor may show readable headers but is not an image viewer.

Done means

  • pbmtoatk -version identifies the installed Netpbm build.
  • The source is a readable PBM image with expected dimensions.
  • pbmtoatk "$INPUT" > "$OUTPUT" exits zero.
  • The output is non-empty and begins with an Andrew Toolkit raster header.
  • You kept the original source and can reproduce the conversion from the recorded command.