Home / Alt manpages / pamtoavs(1)

  • pamtoavs(1)
  • User command
  • linux

Convert a Netpbm Image to AVS X with pamtoavs

Gnuplot cannot plot a raw PAM file, so pamtoavs converts PAM, PPM, PGM or PBM images into an AVS X file Gnuplot 4.2 and later reads as RGB. The command reads one image from a named file or standard input and writes the binary result to standard output. Allow about ten minutes when the source image is already available.

This guide follows the Netpbm package installed on this machine: Debian package netpbm version 2:11.05.02-1.1build1, linked with Netpbm 11.5.2. The local manual page is dated 7 February 2010, so the version check is worth keeping in scripts and support notes. The conversion itself needs no sudo.

1. Check the installed command

Confirm the executable is on your path and ask the common Netpbm option for its library version. These are read-only checks:

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

The remaining lines describe the build environment and can differ. What matters is that the command reports a Netpbm version and exits without attempting a conversion. The long form --version works too, through the common Netpbm option handling.

2. Inspect the input before conversion

pamtoavs expects a Netpbm image. Use pamfile or pnmfile if either is installed, and keep the input untouched:

$ pamfile /path/to/input.pam
/path/to/input.pam: PAM, 640 by 480, 3 channels, 8 bits per channel
$ test -r /path/to/input.pam && echo readable
readable

Your file may be PPM, PGM or PBM rather than PAM, and the diagnostic will name that format instead. A readable file is not necessarily a sensible picture, so check its dimensions and contents with an image tool before trusting the AVS output. If pamfile is missing, use whatever Netpbm inspection command your system has, or proceed with a known-good input.

3. Convert to a new AVS file

Redirect standard output to a new destination. Pick a name that does not already hold anything useful:

$ pamtoavs /path/to/input.pam > output.avs
$ test -s output.avs && echo 'AVS output is non-empty'
AVS output is non-empty

There is normally no progress text, because the image itself is the command's standard output; it is binary, not a text report. A successful exit status only means the conversion completed, not that the source had the dimensions or colours you intended.

Checkpoint

Keep the source image while checking output.avs. Shell redirection truncates an existing destination before pamtoavs even starts, so never aim > at a file you cannot afford to lose.

4. Verify the AVS header and size

AVS X starts with two four-byte dimensions, followed by RGB pixel bytes. For a two by two test image, the installed command produced a 24-byte file: eight bytes of dimensions and sixteen bytes of pixel data. Use od to inspect the start of the file rather than trying to read binary data as prose:

$ od -An -tx1 -N8 output.avs
 00 00 02 80 00 00 01 e0
$ wc -c < output.avs
921608

Those values describe a 640 by 480 image: 0x00000280 is 640 and 0x000001e0 is 480. The expected total is eight bytes plus three bytes per pixel, so this image should occupy 8 + (640 * 480 * 3) = 921608 bytes. Substitute your own input's dimensions. If the size does not match, stop before importing the file anywhere else and investigate the source or the conversion failure.

A small reproducible smoke test helps when diagnosing a pipeline. This creates a temporary PBM on standard output, converts it through a pipe, then checks the exact size, all without needing elevated privileges:

$ pbmmake -white 2 2 | pamtoavs > test.avs
$ wc -c < test.avs
24
$ od -An -tx1 -N8 test.avs
 00 00 00 02 00 00 00 02

The eight header bytes show width 2 and height 2. The remaining sixteen bytes are four RGB pixels, three bytes each.

5. Use standard input in a pipeline

Omit the optional filename when another Netpbm command supplies the image. Output stays on standard output, so each stage can be checked independently:

$ pamtopnm /path/to/input.pam | pamtoavs > output.avs
$ test -s output.avs && echo converted
converted

That first command is only an example of a separate conversion stage. If the input is already a format pamtoavs accepts, the shorter pamtoavs /path/to/input.pam skips an unnecessary process. Keep stderr visible while testing: if a stage reports an error, a non-empty destination may still be incomplete and must never be treated as a valid image.

6. Make failures and replacement safe

Test a candidate destination before writing if you are working in a directory with valuable results already in it:

$ if test -e output.avs; then
>     echo 'refusing to overwrite output.avs' >&2
>     exit 1
> fi
$ pamtoavs /path/to/input.pam > output.avs
$ test -s output.avs

For a deliberate replacement, write a temporary file in the same directory and rename it only after validation. The rename changes the directory entry, so preserve the old file first if rollback matters:

$ cp --preserve=all output.avs output.avs.bak
$ pamtoavs /path/to/input.pam > output.avs.new
$ test -s output.avs.new && mv output.avs.new output.avs

Recovery

If conversion fails, leave the old output in place and inspect the error. If the temporary file exists and is incomplete, remove only that named temporary file after confirming it is the intended one. Delete the backup only once the replacement has been checked, since that deletion is irreversible.

Common traps

  • A "bad magic number" message. This usually means the input is not a PAM, PPM, PGM or PBM image, or an earlier pipeline stage emitted an error message instead of image data. Run the first stage alone and inspect it with pamfile.
  • An "empty input" error. The pipe or file supplied no image bytes at all.
  • Output looks wrong but the command exits successfully. Verify the source dimensions and pixel format rather than adding options at random. pamtoavs has no converter-specific command-line options beyond the optional input filename and common Netpbm options such as -quiet and -version; do not invent resize, colour or output-file options for this command.

Gnuplot reads the result as binary AVS X data. A representative command is:

gnuplot> plot 'output.avs' binary filetype=avs with rgbimage

If Gnuplot rejects the file, compare the AVS dimensions and byte count with the source image, then try the two-by-two smoke test. That separates a viewer or plotting configuration problem from the converter itself.

Done means

  • pamtoavs is installed and its Netpbm version is known.
  • The input is a readable PAM, PPM, PGM or PBM image with checked dimensions.
  • The AVS X result was written to a new file or a safely staged replacement.
  • The output begins with the expected dimensions and has eight plus three-times-pixels bytes.
  • A pipeline failure cannot silently pass off an incomplete output as the accepted result.
  • The original image and any rollback copy remain available until the result is verified.