Home / Alt manpages / pamchannel(1)

  • pamchannel(1)
  • User command
  • linux

Extract and Reorder PAM Channels with pamchannel

pamchannel rebuilds a multi-channel PAM image from just the channels you pick, in whatever order you pick them. Handy for dropping an alpha or mask plane, pulling out one component to inspect, or reordering an RGB image without touching the source.

Allow about 10 minutes for a first pass. You need Netpbm installed, a PAM input image, and enough disk space for the output. Everything here runs as an ordinary user and writes new files; none of it needs sudo.

1. Check the installed command

Start by confirming which executable will run and which Netpbm build supplies it. This guide was checked against the Debian netpbm package version 2:11.05.02-1.1build1, reporting Netpbm 11.5.2 at runtime; your package may report something else.

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

The version output also carries build details. If command -v prints nothing, install Netpbm through your distribution's normal package mechanism before you continue. Installing changes system state and may need elevated privileges, but channel extraction itself does not.

2. Inspect the input before picking channels

Use pamfile to see the input's dimensions, depth and maximum sample value. The depth is the number of channels, and channel numbering starts at zero:

$ pamfile /path/to/input.pam
/path/to/input.pam: PAM, 1920 by 1080 by 4 maxval 255
    Tuple type: RGB_ALPHA

Here the valid channel numbers are 0, 1, 2 and 3. Do not guess from a filename: a four-channel image is not necessarily RGB plus alpha, and the tuple type is metadata, not proof of content. Check the producer's format documentation if the channel meaning actually matters.

Checkpoint

Write down the input depth and the channel numbers you intend to keep. The output depth will equal the number of channel arguments you give, not the input depth.

3. Select channels into a new file

Pass channel numbers after the options and redirect standard output to a new file. This keeps channels 0 and 2 from a four-channel input:

$ pamchannel -infile /path/to/input.pam 0 2 > /path/to/selected.pam

-infile is the input-file option pamchannel uses. It defaults to standard input, so the same operation can be written as a pipeline:

$ cat /path/to/input.pam | pamchannel 0 2 > /path/to/selected.pam

The output has the input's width and height, but a depth of 2. Its tuple type is an empty string unless you supply -tupletype. The channel order follows your arguments, so 0 2 and 2 0 give you two different two-channel layouts.

4. Verify the result

Inspect the new file rather than trusting a clean exit status. For the example above, expect two planes at the original dimensions:

$ pamfile /path/to/selected.pam
/path/to/selected.pam: PAM, 1920 by 1080 by 2 maxval 255
    Tuple type:

If the dimensions or depth are wrong, stop and remove only the newly created output, once you have checked it is not a file you meant to keep:

$ rm -- /path/to/selected.pam

That removal is irreversible unless you have another copy. It is not an undo for the source, because pamchannel reads the source and never edits it.

5. Reorder or label the output deliberately

To reorder three channels, list them in the output order you want. This leaves the original image untouched and writes a three-plane result:

$ pamchannel -infile /path/to/input.pam 2 1 0 > /path/to/reversed.pam
$ pamfile /path/to/reversed.pam
/path/to/reversed.pam: PAM, 1920 by 1080 by 3 maxval 255
    Tuple type:

Use -tupletype when downstream software needs a recognised tuple type. The option takes a string up to 255 characters. Here the selected planes get labelled RGB in the output header:

$ pamchannel -tupletype RGB -infile /path/to/input.pam 2 1 0 > /path/to/rgb.pam
$ pamfile /path/to/rgb.pam
/path/to/rgb.pam: PAM, 1920 by 1080 by 3 maxval 255
    Tuple type: RGB

Only add a label that accurately describes the selected data. A tuple type changes the header metadata; it does not convert sample values or turn arbitrary channels into colour channels.

6. Diagnose the common failures

An error saying the highest numbered channel is lower than the number you supplied means the index is invalid. Count from zero and check the input again with pamfile. For a depth of 3, the highest valid index is 2:

$ pamchannel -infile /path/to/input.pam 3 > /tmp/should-not-exist.pam
pamchannel: You specified channel number 3. The highest numbered
channel in the input image is 2.

Use a temporary destination when testing a command whose input or channel list is uncertain. A shell redirection can create an empty output before the program even reports an error, so check the exit status and inspect the file before you trust it:

$ pamchannel -infile /path/to/input.pam 3 > /tmp/pamchannel-test.pam
$ status=$?
$ test "$status" -eq 0 && pamfile /tmp/pamchannel-test.pam
$ printf 'pamchannel exit status: %s\n' "$status"
pamchannel exit status: 1

For a failed test, remove the temporary file once you no longer need it:

$ rm -- /tmp/pamchannel-test.pam

If the input is not a valid PAM stream, repair or regenerate it rather than trying random channel numbers. If a pipeline stops unexpectedly, run pamchannel with -infile and a separate output file so you can inspect the input on its own.

7. Handle streams and standard input

pamchannel supports multi-image streams and produces a matching output stream. Good for a pipeline, but it also means a single command can emit more than one image if its input contains more than one. Verify the whole stream with a tool that understands the format, and do not assume a single input file always holds one image.

The channel arguments are not an input filename, which is the one unusual part of this command's interface. Put the filename in -infile, or feed it through standard input, then list every selected channel number after the options. Slip a filename in among the channel arguments and pamchannel will treat it as an invalid channel specification.

Done means

  • Right executable. pamchannel resolves to the Netpbm installation you intended to use.
  • Source checked. pamfile confirms the source dimensions and depth.
  • Valid indexes. The selected channel indexes are zero-based and within the source depth.
  • Order verified. The output depth equals the number of selected channels, with order checked explicitly.
  • Tuple type honest. The tuple type is blank or deliberately supplied and accurate.
  • Output confirmed. The output passes pamfile inspection, and the source is unchanged.