Convert a MGR Bitmap to PBM with mgrtopbm
An old MGR bitmap turns up and nothing modern opens it, so mgrtopbm converts it to a PBM image any Netpbm tool can read. Allow about ten minutes if the MGR file is already to hand. The examples use Netpbm 11.5.2, installed here as package version 2:11.05.02-1.1build1.
The route
Jump straight to the step you need, or tick off Done means at the end.
You need a Linux shell, the netpbm package and a readable MGR bitmap. The command writes PBM data to standard output, never edits the input file, and normally needs no elevated privileges.
1. Check the installed command
Confirm the executable and package version before you put it in a script:
$ command -v mgrtopbm
/usr/bin/mgrtopbm
$ dpkg-query -W -f='${Package} ${Version}\n' netpbm
netpbm 2:11.05.02-1.1build1
$ mgrtopbm --help
mgrtopbm: Use 'man mgrtopbm' for help.
That last line is the installed program's actual help response. The manual defines one positional argument, mgrfile, and no options specific to mgrtopbm itself, though it does recognise options common to libnetpbm programs. Don't assume a flag from another image converter belongs here.
Checkpoint
If command -v finds nothing, stop and install Netpbm through your normal package-management process. Don't copy a binary from an untrusted location.
2. Convert a named MGR file
Choose a new destination and redirect the PBM output into it:
$ mgrtopbm /path/to/input.mgr > /path/to/output.pbm
A successful conversion normally prints no progress message, because the image itself is what's going to standard output. A zero exit status means the command completed, but it's still worth checking that the output exists and matches the dimensions you expect:
$ file /path/to/output.pbm
/path/to/output.pbm: Netpbm image data, size = 640 x 480, rawbits, bitmap
$ head -c 2 /path/to/output.pbm
P4
The wording from file can vary. The checks that matter are a non-empty output, the expected width and height, and the PBM binary magic number P4. PBM is a black-and-white bitmap format, not greyscale or colour.
3. Test the standard-input form
Skip the filename when another command is supplying the MGR bitmap. This is handy in a pipeline, and it's the form shown by the manual's optional argument:
$ cat /path/to/input.mgr | mgrtopbm > /path/to/output.pbm
A shell pipeline can hide the exit status of the first command in it. In Bash, use pipefail when the input command's success actually matters:
$ set -o pipefail
$ cat /path/to/input.mgr | mgrtopbm > /path/to/output.pbm
$ printf 'pipeline status: %s\n' "$?"
pipeline status: 0
For a regular file, passing the filename directly is easier to diagnose, because an input-open error is then attached straight to mgrtopbm. Use standard input only when the bitmap genuinely comes from an earlier command or a redirected file descriptor.
4. Keep replacement output recoverable
Warning
Shell redirection with > truncates an existing destination before mgrtopbm even starts. If the conversion then fails, the old PBM may already be gone. Use a temporary output beside the final file, check it, then rename it:
$ mgrtopbm /path/to/input.mgr > /path/to/output.pbm.new
$ test -s /path/to/output.pbm.new
$ file /path/to/output.pbm.new
/path/to/output.pbm.new: Netpbm image data, size = 640 x 480, rawbits, bitmap
$ mv /path/to/output.pbm.new /path/to/output.pbm
The mv step only replaces the destination once the converter has finished and the temporary file is non-empty. If a check fails, leave the existing output alone and remove only the incomplete temporary file:
$ rm -- /path/to/output.pbm.new
That removal is irreversible, so check the path before you run it. There's no need to delete the original MGR bitmap after conversion: keeping it gives you a clean way back if the PBM later turns out unusable.
5. Diagnose the common failures
If the input can't be opened, inspect the path and permissions without changing anything:
$ ls -l /path/to/input.mgr
$ test -r /path/to/input.mgr && echo readable
If that second command prints nothing, fix the path or access permissions through the file's owner or administrator. Don't make a whole directory world-readable just to get one conversion working.
If the output is empty, or file doesn't recognise it as PBM, keep the input and capture the command's diagnostic on standard error instead:
$ mgrtopbm /path/to/input.mgr > /tmp/mgrtopbm-output.pbm 2> /tmp/mgrtopbm-error.txt
$ status=$?
$ printf 'exit status: %s\n' "$status"
$ sed -n '1,20p' /tmp/mgrtopbm-error.txt
Don't treat a file with the right name as a valid image. Check its size, its PBM header and its dimensions. If a later viewer rejects it, preserve the original MGR file and investigate the source format rather than repeatedly overwriting the same destination.
6. Hand the PBM to the next tool
Once the PBM has passed the checks, pass it on to another Netpbm program or image utility that accepts PBM. For example, if pnmtopng is installed, this creates a PNG without touching the MGR source at all:
$ pnmtopng /path/to/output.pbm > /path/to/output.png
$ file /path/to/output.png
pnmtopng is a separate program, not an option to mgrtopbm. Keep each conversion as its own checked step, so a failure is easy to locate.
Done means
- Version confirmed.
mgrtopbmis the expected Netpbm 11.5.2 executable. - Source untouched. The source MGR bitmap remains readable and unchanged.
- Output verified. The output is a non-empty PBM file with the expected dimensions and a
P4header. - Pipeline used deliberately. Standard input was used only when it genuinely made the pipeline clearer.
- Replacement checked first. A temporary output was verified before it replaced any existing PBM.