Home / Alt manpages / pamtoavs(1)

  • pamtoavs(1)
  • User command
  • linux

Convert a Netpbm Image to AVS X with pamtoavs

You will convert a PAM, PPM, PGM or PBM image into an AVS X file that Gnuplot 4.2 and later can read as an RGB image. 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 does not need sudo.

1. Check the installed command

Confirm that 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 identify the build environment and can differ. The useful checkpoint is that the command reports a Netpbm version and exits without attempting a conversion. The long form --version is also accepted by 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 then name that format. A readable file is not necessarily a sensible picture, so check its dimensions and contents with an image tool before relying on the AVS output. If pamfile is absent, use the Netpbm inspection command available on your system 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 contain useful data:

$ 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 is the command's standard output. The output is binary, not a text report. A successful exit status means the conversion completed; it does not prove 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 starts, so do not 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 beginning without 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 example values describe a 640 by 480 image: 0x00000280 is 640 and 0x000001e0 is 480. The expected total is eight bytes plus three bytes for every pixel, so this image should occupy 8 + (640 * 480 * 3) = 921608 bytes. Substitute the dimensions reported for your input. If the size does not match, stop before importing the file into another program and investigate the source or the conversion failure.

A small reproducible smoke test is useful when diagnosing a pipeline. This creates a temporary PBM on standard output, converts it through a pipe, then checks the exact size. It does not need 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 per pixel.

5. Use standard input in a pipeline

Omit the optional filename when another Netpbm command supplies the image. Output remains 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

The first command is only an example of a separate Netpbm conversion stage. If the input is already a format that pamtoavs accepts, the shorter form pamtoavs /path/to/input.pam avoids an unnecessary process. Keep stderr visible while testing. If a stage reports an error, a non-empty destination may still be incomplete and must not 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 containing valuable results:

$ 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

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 checking it is the intended one. The backup is your recovery copy; delete it only after the replacement has been checked, because that deletion is irreversible.

7. Diagnose the common traps

A message about a bad magic number usually means the input is not a PAM, PPM, PGM or PBM image, or that an earlier pipeline stage emitted an error message instead of image data. Run the first stage by itself and inspect it with pamfile. An empty-input error means the pipe or file supplied no image bytes.

If 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. Its documented interface is the optional input filename plus 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. This 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 safely staged replacement.
  • The output begins with the expected dimensions and has eight plus three-times-pixels bytes.
  • A pipeline failure cannot silently turn an incomplete output into the accepted result.
  • The original image and any rollback copy remain available until the result is verified.