Home / Alt manpages / pnmtojbig(1)

  • pnmtojbig(1)
  • User command
  • linux

Convert PBM and PGM Images to JBIG with pnmtojbig

You will finish with a lossless JBIG file made from a PBM or PGM image, a way to inspect the encoder's choices, and a safe round-trip check with jbigtopnm. The examples use Netpbm 11.5.2, supplied here by package version 2:11.05.02-1.1build1.

Allow about fifteen minutes. You need pnmtojbig and, for the verification step, jbigtopnm. The input must be a PBM bitmap or PGM greyscale image. Ordinary conversion does not need elevated privileges. Keep the source image until the JBIG output has been checked.

1. Check the installed tools

Start with read-only checks. This confirms which executable will run and records the local Netpbm build:

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

The -v option means verbose output when it is used with a conversion. With no input, this invocation prints build information and then reports that the input is missing. That is harmless. If your version reports different details, use its installed manpage as the authority for the behaviour you are about to script.

Checkpoint

Confirm that the input is really PBM or PGM, rather than a PNG, JPEG or a colour PPM file:

$ file /path/to/input.pbm
/path/to/input.pbm: Netpbm image data, size = 16 x 16, rawbits, bitmap
$ file /path/to/input.pgm
/path/to/input.pgm: Netpbm image data, size = 16 x 16, rawbits, greymap

2. Create a JBIG file without overwriting anything

The command accepts an optional input file followed by an optional output file. Use explicit paths and a new destination while testing:

$ pnmtojbig /path/to/input.pbm /path/to/output.bie
$ test -s /path/to/output.bie && echo 'JBIG output is non-empty'
JBIG output is non-empty

For a PGM image, the command creates several bitplanes when the image contains more than one bit per pixel:

$ pnmtojbig /path/to/input.pgm /path/to/output-pgm.bie
$ file /path/to/output-pgm.bie
/path/to/output-pgm.bie: data

Do not read too much into the output of file for a BIE. JBIG is not one of the formats that every file database labels well. A successful exit status and a non-empty file are useful first checks, but they do not prove that a decoder can read the image.

Shell redirection is another supported shape when you omit the output filename:

$ pnmtojbig /path/to/input.pbm > /path/to/output.bie

Safety warning

Both a named output and > can replace an existing file. If the destination matters, choose a fresh name such as output.bie.new, verify it, then rename it deliberately. Do not use sudo just to convert an image. If a destination is protected, fix the directory choice or permissions instead of making the converter run as root.

3. Inspect the encoded image

Add -v to see the input dimensions, bit-plane count, resolution layers, stripe count and output length:

$ pnmtojbig -v /path/to/input.pbm /path/to/output.bie
Information about the created JBIG bi-level image entity (BIE):

              input image size: 16 x 16 pixel
                    bit planes: 1
                       stripes: 1
           lowest layer in BIE: 0
          highest layer in BIE: 0
          length of output file: 50 byte

The exact lines and values depend on the input. A small image can have one layer and one stripe. A larger image may have several progressive resolution layers. The verbose report is especially useful in a build log because it records what the encoder actually wrote, rather than what the command line intended.

By default, pnmtojbig chooses enough layers that the lowest layer is no larger than 640 by 480 pixels. It writes all selected layers into one BIE. This is progressive JBIG encoding: higher layers describe differences from lower layers. The default is usually a sensible starting point, so do not add layer options until you have a reason to control the file layout.

4. Choose sequential encoding when one complete image matters

Use -q for one resolution layer:

$ pnmtojbig -q /path/to/input.pbm /path/to/output-sequential.bie
$ test -s /path/to/output-sequential.bie && echo 'sequential BIE written'
sequential BIE written

The manpage describes sequential mode as usually the most efficient compression method. It is a practical choice when you want one complete encoded image rather than progressive layers. Compare the sizes on representative inputs before changing a batch default, because compression depends on the image.

For more direct layer control, -x and -y set the maximum horizontal and vertical size of the lowest layer. Their defaults are 640 and 480. For example:

$ pnmtojbig -x 320 -y 240 /path/to/input.pbm /path/to/output-320x240-layer.bie

-d instead specifies the total number of differential layers and overrides -x and -y. Use it only when the number of layers is part of your interchange requirement. The lowest layer is number 0. Options -l and -h select the lowest and highest layer written:

$ pnmtojbig -l 0 -h 0 /path/to/input.pbm /path/to/output-lowest-layer.bie

That output contains only layer 0. It is not a complete replacement for a BIE containing all layers unless the decoder and your workflow are specifically designed for separate progressive files. Check the verbose layer report after using these options.

5. Control PGM bitplanes deliberately

PGM input can produce a multi-bitplane BIE. Use -t when you need only a specified number of the most significant planes:

$ pnmtojbig -t 4 /path/to/input.pgm /path/to/output-four-planes.bie
$ jbigtopnm /path/to/output-four-planes.bie /path/to/roundtrip.pgm
jbigtopnm: WRITING PGM FILE
$ file /path/to/roundtrip.pgm
/path/to/roundtrip.pgm: Netpbm image data, size = 16 x 16, rawbits, greymap

Reducing the planes reduces greyscale precision. It is not a lossless representation of the original PGM values, even though JBIG itself compresses the planes without loss. If retaining all input levels matters, leave -t out and compare the decoded image with the source.

-b selects binary values rather than Gray code words for multi-bitplane input. The decoder must make the same selection; the BIE does not say which choice the encoder made. Do not use -b in a hand-off format unless the receiving application is configured to match it. The option has no effect for a single-plane PBM image.

6. Verify the result by decoding it

A round trip catches malformed output more reliably than a file-size check:

$ jbigtopnm /path/to/output.bie /path/to/decoded.pbm
jbigtopnm: WRITING PBM FILE
$ file /path/to/decoded.pbm
/path/to/decoded.pbm: Netpbm image data, size = 16 x 16, rawbits, bitmap

Use a .pgm destination for a multi-bitplane BIE. With -t 1, the decoder writes a PBM because only one plane remains. The exact diagnostic text and dimensions will follow your input, so treat the sample output as a shape to check, not a fixed transcript.

For a stronger check, compare the source and decoded files in a format-aware image tool. Do not use a byte-for-byte comparison: JBIG compression changes the representation, and PGM files can differ in whitespace or header spelling while carrying the same pixels. If the conversion fails, retain the source, inspect the command's exit status and rerun with -v. An incomplete destination should be treated as untrusted output and replaced with a new filename on the next attempt.

7. Avoid advanced tuning unless a test requires it

-s controls the number of lines in each horizontal stripe of layer 0. The default aims for approximately 35 stripes. -m controls the maximum horizontal movement of the adaptive template pixel. Values above 16 are not recommended by the manpage if interoperability with other JBIG decoders matters, because the standard only requires decoders to handle at least 16 pixels.

-o changes the order of stripe, layer and plane data inside the BIE. This matters for applications decoding an image while it is still arriving over a slow connection, not for ordinary local conversion. -p changes optional JBIG prediction algorithms; its default is 28 and the manpage advises leaving it alone except for specialised compatibility or debugging work. -c selects a delayed adaptive-template change for particular conformance tests.

These settings can change interoperability or compression without making a visually obvious difference. Record the full command line in the build system, test with the actual decoder used downstream, and keep the default when no requirement calls for tuning.

Done means

  • The installed Netpbm version and input format were checked.
  • A PBM or PGM input produced a non-empty BIE at a new destination.
  • -v was used when layer, stripe or bit-plane details mattered.
  • Any -q, layer, -t or -b choice was made for a stated interoperability or size requirement.
  • jbigtopnm decoded the BIE, and the decoded dimensions and format were checked.
  • The original image remains available, and no command required elevated privileges.