Home / Alt manpages / pamendian(1)

  • pamendian(1)
  • User command
  • linux

Reverse Netpbm Sample Byte Order with pamendian

You will convert a Netpbm image between the normal big-endian sample representation and the little-endian variant used by some older image programs. The command reads only from standard input and writes only to standard output, so the usual workflow is a pipe or a redirected output file. Allow about ten minutes if you know which byte order the receiving program expects.

This guide uses Netpbm 11.5.2, installed here by package netpbm version 2:11.05.02-1.1build1. The installed manual page is dated 16 March 2002. The examples therefore describe the behaviour verified on this machine, rather than promising that an unrelated Netpbm build has identical diagnostics.

1. Check the installed command

Confirm that the binary is available and record the library version. These are ordinary read-only checks and do not need sudo:

$ command -v pamendian
/usr/bin/pamendian
$ pamendian --help
pamendian: Use 'man pamendian' for help.
$ pamendian --version
pamendian: Using libnetpbm from Netpbm Version: Netpbm 11.5.2
pamendian: Built from source dated 2024-03-31 09:09:47

--help is handled by the common Netpbm command interface. The manual defines no pamendian-specific command-line options. In particular, do not look for an input filename or an endian-selection switch: the image travels through standard input, and pamendian always reverses the sample byte order.

Checkpoint

If command -v finds nothing, stop here and install or repair Netpbm through your normal package-management process. Do not copy a random binary into a system directory.

2. Confirm the format and the compatibility problem

Netpbm's binary formats with multi-byte samples are defined in big-endian order, with the most significant byte first. Some older programs use a little-endian variation of otherwise compatible PGM, PPM or PAM data. The manual specifically names the X Window System viewer xv and programs from the Independent JPEG Group as consumers of the older PGM and PPM variation.

Use pamendian only when the program on the other side of the exchange needs that variation. Images produced by current Netpbm programs and Netpbm libraries should already have the correct Netpbm byte order. If one of those programs needs pamendian, investigate the conversion or library boundary instead of silently adding a workaround to every pipeline.

Inspect an existing image before converting it:

$ pamfile /path/to/input.pgm
/path/to/input.pgm: PGM raw, 2 by 1 65535 maxval

Your dimensions and maximum value will differ. The useful facts are the format, dimensions and maximum value. A maximum value above 255 means each sample occupies multiple bytes and byte order matters. An 8-bit image has one byte per sample, so reversing its sample bytes cannot change the byte order in a useful way.

3. Convert to the opposite byte order

Redirect the input file into pamendian and write a new destination. This example does not alter the source:

$ pamendian < /path/to/input.pgm > /path/to/output-little-endian.pgm
$ pamfile /path/to/output-little-endian.pgm
/path/to/output-little-endian.pgm: PGM raw, 2 by 1 65535 maxval
$ test -s /path/to/output-little-endian.pgm && echo "output is non-empty"
output is non-empty

The header still identifies a PGM with the same dimensions and maximum value. The change is in the binary sample bytes after the header, not in the image dimensions or a visible file-name marker. Pass the output to the older consumer that requires little-endian samples.

For a PPM or true PAM input, the same standard-input and standard-output rule applies:

$ pamendian < /path/to/input.ppm > /path/to/output-little-endian.ppm
$ pamendian < /path/to/input.pam > /path/to/output-opposite-order.pam

Do not add a filename after pamendian. It is not documented as a positional input argument, and the command's contract is specifically standard input to standard output.

4. Verify a byte-level change safely

For a two-byte-sample PGM, compare the payload with a hex dump. Keep the header in view so you do not mistake its ASCII digits for sample data:

$ xxd -g1 /path/to/input.pgm | tail -n 2
00000000: 50 35 0a 32 20 31 0a 36 35 35 33 35 0a 01 02  P5.2 1.65535...
00000010: 03 04                                            ..
$ xxd -g1 /path/to/output-little-endian.pgm | tail -n 2
00000000: 50 35 0a 32 20 31 0a 36 35 35 33 35 0a 02 01  P5.2 1.65535...
00000010: 04 03                                            ..

In this small example, each pair has been reversed: 01 02 became 02 01, and 03 04 became 04 03. Your output will contain a different header and payload length. For a larger image, use pamfile to verify the structure and let the receiving program perform its own image-level check.

A useful reversibility test is to run pamendian a second time into a separate file, then compare it with the original:

$ pamendian < /path/to/output-little-endian.pgm > /tmp/input-round-trip.pgm
$ cmp -- /path/to/input.pgm /tmp/input-round-trip.pgm
$ echo $?
0

Status 0 means the bytes match exactly in this test. Remove the temporary round-trip file when you have finished checking it if it contains sensitive image data.

5. Avoid truncating a useful image

Shell redirection with > truncates an existing destination before pamendian starts. That is the main destructive trap in this workflow. Choose a new name or write a temporary file and rename it only after validation:

$ tmp_output=$(mktemp --tmpdir pamendian.XXXXXX.pgm)
$ pamendian < /path/to/input.pgm > "$tmp_output"
$ pamfile "$tmp_output"
$ mv -- "$tmp_output" /path/to/output-little-endian.pgm

mv replaces an existing destination on the same filesystem. If that destination matters, make a backup first or choose a versioned output name. If conversion fails, do not rename the incomplete temporary file; remove it after checking the error, and the original remains untouched. No example here changes system configuration, so elevated privileges are unnecessary unless your chosen directories themselves deny access.

6. Diagnose wrong-looking output

If the receiving program rejects the result, first check that the input really is a binary PGM, PPM or PAM image and that its maximum value requires multi-byte samples. pamendian is not a general image converter, resizer or format detector. It only reverses sample byte order.

If the image looks corrupted but the command exits successfully, compare dimensions, maximum value and the first few payload pairs. The most common mistake is applying the conversion to data that was already in the byte order expected by the consumer, which reverses it twice relative to that consumer. Keep the original file and regenerate the output from it rather than repeatedly applying pamendian to the latest output.

For raw sample streams, the manual recommends declaring input endianness when using rawtopgm or rawtoppm, so those tools create correctly ordered Netpbm output. That is preferable to repairing every result afterwards.

Done means

  • pamendian is the installed Netpbm 11.5.2 command, or you have recorded the version on your own host.
  • The input is a compatible binary PGM, PPM or PAM image whose consumer needs the opposite sample byte order.
  • The command reads from standard input and writes to a new standard-output destination.
  • pamfile confirms the expected format, dimensions and maximum value.
  • The original image remains available, and a failed conversion cannot destroy the previous output.