Home / Alt manpages / hdifftopam(1)

  • hdifftopam(1)
  • User command
  • linux

Recover an Original PAM Image from Netpbm hdiff Data

Netpbm's horizontal-difference format shrinks image data well, but hdifftopam is the only tool that turns it back into a normal image. It rebuilds the picture as PAM (the default) or as PGM/PPM when the image depth permits it. Examples use Netpbm 11.5.2, installed from the Debian package version 2:11.05.02-1.1build1. Allow about 10 minutes if the input already exists, longer if you also need to locate the matching hdiff file.

Before you start

You need Linux, the hdifftopam command, and a horizontal difference image produced by Netpbm's pamtohdiff. The input is normally a PAM file whose tuple type is hdiff. This is not a general-purpose image converter: a PNG, JPEG or ordinary PPM is not a valid substitute for hdiff data.

Check the installed version and command location first:

command -v hdifftopam
hdifftopam --version

On this installation, the version command reports Netpbm Version 11.5.2, and the executable is /usr/bin/hdifftopam. The command needs no elevated privileges. Use a directory where you can write the output, and keep the original input untouched until you have checked the result.

1. Check the hdiff input

Inspect the header and image properties before converting. Substitute your real path for /path/to/input.pam:

pamfile /path/to/input.pam
head -12 /path/to/input.pam

A suitable file normally identifies itself as PAM and has a tuple type of hdiff. The header may look like this:

P7
WIDTH 16
HEIGHT 1
DEPTH 3
MAXVAL 255
TUPLTYPE hdiff
ENDHDR

Do not edit the header to force an unrelated image through the decoder. If the file is not hdiff data, find the source produced by pamtohdiff instead.

Checkpoint

Continue once pamfile recognises the file and its PAM header says TUPLTYPE hdiff. If this fails, stop: the useful fix is finding the correct file, not changing the command line.

2. Recover a PAM image

With no output filename option, hdifftopam writes the recovered image to standard output. Redirect that output to a new file:

hdifftopam /path/to/input.pam > /path/to/recovered.pam

The default output is PAM with tuple type unhdiff, which records that the image has been reconstructed from hdiff data; it does not mean the pixels are still differences. Confirm the result:

pamfile /path/to/recovered.pam

For a valid conversion, the width, height, depth and maxval should describe the recovered image. For example, this installation prints the following diagnostic for a 16 by 1, three-channel input:

hdifftopam: Output is 16 x 1 x 3, maxval 255

That line only appears when you ask for verbose diagnostics. It goes to standard error, so it does not corrupt the image redirected from standard output.

3. Request PGM or PPM when appropriate

Use -pnm when the receiving tool expects the older PNM family rather than PAM:

hdifftopam -pnm /path/to/input.pam > /path/to/recovered.ppm
pnmfile /path/to/recovered.ppm

The option selects PGM or PPM according to the recovered depth, and it only works for depth 1 or 3. A different depth cannot be represented by PGM or PPM, so hdifftopam -pnm fails rather than silently discarding channels or samples. Leave out -pnm and keep PAM when the input has another depth.

The output format is determined by the bytes written, not the filename extension. A command such as hdifftopam input.pam > output.jpg still creates a PAM file named output.jpg; it does not create a JPEG.

4. Use standard input in a pipeline

The input filename is optional, which lets you place hdifftopam after another command that writes hdiff PAM data:

cat /path/to/input.pam | hdifftopam -pnm > /path/to/recovered.ppm

For a simple file, naming the input is easier to audit. Either way, keep image output on standard output and diagnostic messages on standard error: redirect both streams together and a verbose line can make the image unusable.

5. Verify against the original when you can

If the hdiff file was made from an original image, reconstruct it and compare image properties with the source. The serialised files will usually differ, since PAM and PNM headers, whitespace and encoding can differ, so do not use a raw checksum as the comparison:

pamfile /path/to/original.pam /path/to/recovered.pam
pnmfile /path/to/original.ppm /path/to/recovered.ppm

For a stronger pixel comparison, convert both files to the same Netpbm format and compare those outputs with a suitable image comparison tool. At minimum, matching width, height, depth and maxval is a useful checkpoint; a mismatch often means you selected the wrong hdiff file or requested PNM for data it cannot represent.

Common mistakes and recovery

  • Overwriting the source: do not redirect to the input path. If you did, recover the source from a backup or recreate it with pamtohdiff; the shell truncates the destination before the command runs.
  • Using ordinary image data: hdifftopam undoes pamtohdiff. It is not the inverse of a normal PPM or PAM export.
  • Expecting a file to appear automatically: without > output.pam, the recovered bytes go to your terminal. Stop with Ctrl-C if you notice this before redirecting, then rerun with a new destination.
  • Mixing diagnostics with image bytes: use -verbose only for a check, and keep standard error separate if you capture logs.
  • Choosing PNM for a deep image: remove -pnm and keep the default PAM output when the depth is not 1 or 3.

Done means

  • Input confirmed. The input is a PAM hdiff image, confirmed with pamfile or its header.
  • Recovered file written. It is written to a new path and is recognised as PAM, PGM or PPM.
  • Dimensions match. Its dimensions, depth and maxval match the expected original image.
  • Format chosen correctly. You used -pnm only for depth 1 or 3, and kept PAM for other depths.
  • Source preserved. The source file remains available for another conversion or comparison.