Home / Alt manpages / rgb3toppm(1)

  • rgb3toppm(1)
  • User command
  • linux

Combine Three PGM Channels into a PPM with rgb3toppm

You will combine separate greyscale PGM files for the red, green and blue channels into one colour PPM image. The installed Netpbm 11.5.2 command reads the three files in a fixed order and writes the result to standard output, so the normal workflow is to redirect into a new file and verify it afterwards.

Allow about ten minutes if the three inputs already exist. You need the rgb3toppm program and three readable PGM files. The examples are ordinary user commands. They do not need sudo, and the conversion does not alter its input files.

1. Check the installed command

Confirm the executable and package version before relying on a script or a saved example:

$ command -v rgb3toppm
/usr/bin/rgb3toppm
$ dpkg-query -W -f='${Package} ${Version}\n' netpbm
netpbm 2:11.05.02-1.1build1
$ rgb3toppm --version
rgb3toppm: Using libnetpbm from Netpbm Version: Netpbm 11.5.2
rgb3toppm: Built from source dated 2024-03-31 09:09:47
rgb3toppm: Built by Debian

The last command may print different build details on another installation. This program has no rgb3toppm-specific command-line options in the installed manual. It does recognise options common to libnetpbm programs, but the three positional input names are the part that matters here.

Checkpoint: you have a command at /usr/bin/rgb3toppm, or you know which installed path your script will use, and the package query identifies the version.

2. Confirm the inputs and their channel order

The syntax is:

$ rgb3toppm RED_PGM GREEN_PGM BLUE_PGM > OUTPUT_PPM

The first file supplies red samples, the second supplies green samples, and the third supplies blue samples. The names are not interpreted as labels, so a file called blue.pgm in the first position will still be used as the red channel. Keep the order explicit in scripts.

Check the files before converting. This is read-only and catches a surprising number of path errors:

$ ls -l /path/to/red.pgm /path/to/green.pgm /path/to/blue.pgm
$ file /path/to/red.pgm /path/to/green.pgm /path/to/blue.pgm
/path/to/red.pgm: PGM image data, 1920 x 1080, 8-bit grayscale
/path/to/green.pgm: PGM image data, 1920 x 1080, 8-bit grayscale
/path/to/blue.pgm: PGM image data, 1920 x 1080, 8-bit grayscale

Your file wording can vary. The useful checks are that all three files are readable PGM images and describe the same width and height. If they came from separate tools, inspect their metadata rather than trusting the filenames.

3. Convert into a new PPM file

Use an output name that does not already contain useful data:

$ rgb3toppm /path/to/red.pgm \
    /path/to/green.pgm \
    /path/to/blue.pgm > /path/to/combined.ppm
$ printf 'exit status: %s\n' "$?"
exit status: 0

A successful command normally prints no progress message because the image is its standard output. The shell redirection creates or truncates combined.ppm before the program starts. That is an irreversible overwrite of an existing destination. If the destination matters, stop and choose a new name, or make a deliberate backup first:

$ cp --preserve=all /path/to/combined.ppm /path/to/combined.ppm.bak
$ rgb3toppm /path/to/red.pgm /path/to/green.pgm /path/to/blue.pgm \
    > /path/to/combined.ppm.new
$ mv /path/to/combined.ppm.new /path/to/combined.ppm

The temporary output is replaced only after conversion completes. If conversion fails, leave the original in place and inspect the error. Remove the backup only after you have checked the new image; deleting it cannot be undone from this workflow.

4. Verify the PPM header and dimensions

PPM can be binary, so do not use a text editor as your main check. Use an image-aware command:

$ file /path/to/combined.ppm
/path/to/combined.ppm: Netpbm image data, size = 1920 x 1080, rawbits, pixmap
$ head -c 2 /path/to/combined.ppm
P6

The exact file description varies by version. A normal result has the expected dimensions and a PPM magic number of P6 for the raw output produced by this installed command. The remaining bytes are pixel data, not readable text. If another Netpbm utility is available, pnmfile /path/to/combined.ppm gives a more image-specific check.

For a small, reproducible smoke test, create three one-row PGM inputs in a temporary directory:

$ workdir=$(mktemp -d /tmp/rgb3toppm.XXXXXX)
$ printf 'P2\n2 1\n255\n255 0\n' > "$workdir/red.pgm"
$ printf 'P2\n2 1\n255\n0 128\n' > "$workdir/green.pgm"
$ printf 'P2\n2 1\n255\n0 64\n' > "$workdir/blue.pgm"
$ rgb3toppm "$workdir/red.pgm" "$workdir/green.pgm" "$workdir/blue.pgm" > "$workdir/colour.ppm"
$ file "$workdir/colour.ppm"
/tmp/rgb3toppm.XXXXXX/colour.ppm: Netpbm image data, size = 2 x 1, rawbits, pixmap
$ rm -rf "$workdir"

The final command removes only the temporary directory created by mktemp. Check the output first if you want to keep the fixture for further tests. Do not substitute a broad path for $workdir.

5. Diagnose a failed conversion

If one input path is wrong or unreadable, the command exits non-zero and reports the file it could not open. Check the path and permissions without changing them:

$ test -r /path/to/red.pgm && echo 'red input is readable'
red input is readable
$ ls -l /path/to/red.pgm

If the inputs have different dimensions, rgb3toppm rejects them rather than silently cropping or stretching. A typical diagnostic is:

rgb3toppm: all three graymaps must be the same size

Fix the upstream images or regenerate them at matching dimensions. Do not assume that a successful-looking output is safe when the source images describe different canvases. If the inputs are the same size but the result has unexpected colours, revisit the positional order first, then inspect the channel files independently.

Do not run the converter as root to solve an input permission problem. Use an administrator only to correct ownership or permissions under your normal system policy, and keep that separate from the image conversion. If a failed command created a partial new file, remove that specific file or rerun using the backup-safe .new pattern above.

Done means

  • The installed Netpbm version and executable path are known.
  • Red, green and blue PGM files are readable and have matching dimensions.
  • The files were passed in red, green, blue order.
  • The output is a verified PPM with the expected dimensions and P6 header.
  • No useful destination was overwritten without a deliberate backup or temporary replacement.