dyslexic
edit
qrcode
+=-

The NetPBM Image File Format


In this course, we will write/read images using the NetPBM format.

NetPBM is a very old image format. According to the Wikipedia page, NetPBM (or Netpbm) was initially released in 1988. Details on the format can be seen on the Sourceforge page and in Donald H. House's Clemson University CPSC 4050 class notes, but the format consists of an ASCII header and a basic dump of the pixel channel values. The header contains at least the version used and the size (width and height) of the image. Depending on the version, other properties might be stored, too, such as number of channels or maximum value.

I chose this image file format, because it is simple to implement and debug, it can write out files in ASCII or Binary, and it does not depend on any external packages (ex: zlib).

There are several versions of the NetPBM format, each with a corresponding extension and set of features.

ext ver type description values
.pbm P1/P4 ASCII/Binary Portable BitMap image black/white
.pgm P2/P5 ASCII/Binary Portable GrapMap image grayscale
.ppm P3/P6 ASCII/Binary Portable PixMap image RGB
.pam P7 Binary only Portable Arbitrary Map any, can have alpha

Note: although not "standard" files for modern systems, most image editors should be able to load a NetPBM image (except for P7). Sadly, Internet browsers do not support NetPBM images out of the box, so you will need to convert your images to PNG to include them in your readme.html Markdeep write up.

Versions P1P6 (.pbm, .pgm, .ppm extensions) do not support an alpha channel. You can still write out images with corresponding alpha values, but the alpha values are written to a separate image file as a mask. Version P7 (.pam) supports any number of channels, including an alpha channel, but very few image viewers/editors recognize this version.

NetPBM Tools


Below are a collection of tools for the NetPBM format.

Viewing and Conversion


If your system does not support viewing NetPBM images directly, there are two ways to indirectly view the generated output.

The first is to use ImageMagick's convert tool to convert a NetPBM image (P7 or any other version) into a more modern format, such as PNG.

$ convert image.ppm image.png
$ convert image.ppm mask.pgm -compose CopyOpacity -composite image.png
$ convert image.pam image.png

IMPORTANT: do NOT use a lossy image format, such as JPG! Only use loss-less formats, even though they generally produce larger files.

The second way is to use the basic Javascript NetPBM conversion tool below (based on the Online NetPBM Viewer). Simply drag the PNM file into the green area below, and the image will show up in the following gray area as a PNG file. Either left-click the image to download it to you system's download folder, or right-click the image and choose "Save image as..." to save the image in the desired folder.

Tip: you can drag multiple files into the green area at the same time!

Drag NetPBM file(s) here to view/convert
(does not support P7 or mask files, yet)

Diffing


You can compare your output image against the corresponding reference image to see if there are any differences. It is very easy to miss a subtle difference using only a simple visual inspection. For example, an off-by-one (or sometimes off-by-half) error can be imperceptible to the untrained eye, but these errors can compound into other visual artifacts that are extremely tricky to debug. There are two easy ways to compute an image diff.

The first way is to use ImageMagick's compare tool. It takes two images and produces a new image that highlights the differences in corresponding pixel values, even the slightest values.

$ magick compare reference.ppm myversion.ppm difference.png

Another way is to use the basic Javacript NetPBM difference viewing tool below. Simply drag the reference PNM file into one of the green areas and your program's output PNM file into the other. The area at the bottom will highlight in red any differences in corresponding RGB values, and highlight in blue any differences in corresponding alpha values.

Drag Version A (NetPBM file) here
(does not support P7 or mask files, yet)
Drag Version B (NetPBM file) here
(does not support P7 or mask files, yet)