Home / Alt manpages / rasttopnm(1)

  • rasttopnm(1)
  • User command
  • linux

Convert Sun Rasterfiles to PNM Safely with rasttopnm

rasttopnm turns an old Sun rasterfile into a Netpbm image, and a careless redirect can wipe out a good file before the new one is ready. Here's a conversion that checks the result and keeps the original safe if anything fails, in about ten minutes.

You need a shell, a readable Sun rasterfile, and a directory to write into. The conversion is an ordinary unprivileged operation: don't use sudo unless filesystem permissions genuinely require it. The examples use rasttopnm from Netpbm 11.5.2, installed here as Debian package netpbm 2:11.05.02-1.1build1.

1. Check the installed command

Confirm the executable is the one you expect and inspect its local version:

$ command -v rasttopnm
/usr/bin/rasttopnm
$ rasttopnm --version
rasttopnm: Using libnetpbm from Netpbm Version: Netpbm 11.5.2
...

The version output carries build details alongside the Netpbm version, so the final lines can differ between distributions. What matters is that the command resolves and reports Netpbm 11.5.2 on this machine.

Its interface is rasttopnm [options] [rastfile]. Give it a filename and it reads that file; give it nothing and it reads a Sun rasterfile from standard input. The converted image always goes to standard output, which is why a redirect or a pipeline is normally part of the command.

2. Convert to a new output file

Replace the two placeholder paths with your own files:

$ rasttopnm /path/to/input.ras > /path/to/output.pnm
rasttopnm: writing PGM file

That status line tells you which output family got picked from the input: black-and-white produces PBM, greyscale produces PGM, colour produces PPM. The file name has no say in that choice, which is why .pnm is a safer extension than assuming every result is a PPM.

Check both the exit status and the file itself:

$ printf 'exit status: %s\n' "$?"
exit status: 0
$ file /path/to/output.pnm
/path/to/output.pnm: Netpbm image data, size = 4 x 2, rawbits, greymap

Your file description will reflect the actual dimensions and image type. For a more direct header check, run head on just the text header, never on the whole binary image:

$ head -n 1 /path/to/output.pnm
P5

P4, P5 and P6 are the binary PBM, PGM and PPM signatures Netpbm commonly produces. If the output feeds into another Netpbm command, pass the file through unchanged and let that command read the signature itself.

3. Keep a useful output while testing

Shell redirection with > truncates an existing destination before rasttopnm has even finished running: a destructive overwrite of whatever was there before. Write to a temporary name in the same directory instead, and only replace the destination after verification:

$ rasttopnm /path/to/input.ras > /path/to/output.pnm.new
$ status=$?
$ if [ "$status" -eq 0 ] && [ -s /path/to/output.pnm.new ]; then
>     mv -- /path/to/output.pnm.new /path/to/output.pnm
> else
>     printf 'conversion failed; old output was kept\n' >&2
>     rm -f -- /path/to/output.pnm.new
>     exit "$status"
> fi

The temporary file is deliberately removed on failure. The rasterfile is only ever read, and the previous PNM stays exactly where it was unless the conversion succeeds and the final mv actually runs. Stop before that point and you can clean up the leftover .new file later once you're sure it isn't needed.

Checkpoint

Run file /path/to/output.pnm after the move and confirm the dimensions and type match what you expected. If the converter prints an error, keep the original rasterfile and go investigate the input path or format rather than retrying over a good destination.

4. Use standard input and quiet mode

The optional input argument is what makes pipelines possible. This reads a rasterfile and sends it to rasttopnm through standard input:

$ cat /path/to/input.ras | rasttopnm -quiet > /path/to/output.pnm
$ printf 'exit status: %s\n' "$?"
exit status: 0

-quiet only suppresses the normal message announcing which PNM type is being written. It doesn't touch the image data on standard output, and it doesn't turn a real error into success. In scripts, keep standard output reserved for the image and rely on the exit status for control flow.

For a plain file conversion, naming the input directly is easier to review than routing it through cat. Save the pipeline for when another program is already producing a raster stream.

5. Know when you actually need -index

Most users should leave -index alone. A colormapped Sun rasterfile stores a colour map plus pixel data as indices into it. Without this option, rasttopnm uses the colours the map describes.

-index tells the converter to use the map indices themselves as the pixel values. That's useful when a greyscale raster originally had matching values but its colour map was changed afterwards, say to equalise or otherwise alter the displayed colours. Skip -index and you get the colours from the modified map; use it and you recover the image represented by the original indices.

$ rasttopnm -index /path/to/colormapped.ras > /path/to/index-values.pnm
rasttopnm: writing PGM file

This only changes how the input colour map is interpreted. It never edits the rasterfile or the colours stored on disk. It arrived in Netpbm 10.56, so it's present here in 11.5.2, but check the target package before relying on it against older installations.

Common traps

If the command can't open the input, check the path and read permission without touching the file:

$ ls -l /path/to/input.ras
$ test -r /path/to/input.ras && printf '%s\n' 'input is readable'
  • Result looks wrong? Verify the dimensions and PNM type before blaming the viewer. A grayscale image doesn't become a colour PPM just because you named the destination picture.ppm. Rename it to match the detected type if another tool cares about the extension, or keep the neutral .pnm name.
  • Pipeline fails partway? Never treat an empty or partial destination as a valid image. Check the producer's exit status as well as rasttopnm's, and use the temporary-file pattern from step 3. Keep the original until the new image opens correctly in whatever comes next.
  • No elevated access needed. No service restart, configuration edit or root privilege applies here. The only state-changing steps above create, replace or remove image files, so review the paths before running them, especially the final mv and the cleanup rm -f.

Done means

  • Version known: rasttopnm is installed and its version confirmed on this system.
  • Original intact: the source Sun rasterfile remains untouched and readable.
  • Output checked: the result exits successfully, is non-empty, and has the expected dimensions and PBM, PGM or PPM type.
  • -index used correctly: only when the colour-map indices, not the mapped colours, are what you actually want.
  • No silent overwrite: a failed conversion can never clobber a previously useful output.