Class PNMCodec


public class PNMCodec extends ImageCodec
A codec to read and write Portable Anymap (PNM) image files. This format includes three file types well-known in the Unix world:
  • PBM (Portable Bitmap - 1 bit per pixel bilevel image),
  • PGM (Portable Graymap - grayscale image) and
  • PPM (Portable Pixmap - RGB truecolor image).

Compression

The file format only allows for uncompressed storage.

ASCII mode / binary mode

PNM streams can be stored in binary mode or ASCII mode. ASCII mode files are text files with numbers representing the pixels. They become larger than their binary counterparts, but as they are very redundant they can be compressed well with archive programs. ASCII PGM and PPM files can have all kinds of maximum sample values, thus allowing for arbitrary precision. They are not restricted by byte limits. PBM streams always have two colors, no matter if they are ASCII or binary.

Color depth for PGM / PPM

The header of a PGM and PPM file stores a maximum sample value (such a value is not stored for PBM, where the maximum value is always 1). When in binary mode, PGM and PPM typically have a maximum sample value of 255, which makes PGM 8 bits per pixel and PPM 24 bits per pixel large. One sample will be stored as a single byte. However, there also exist binary PGM files with a maximum sample value larger than 255 and smaller than 65536. These files use two bytes per sample, in network byte order (big endian). I have yet to see PPM files with that property, but they are of course imagineable. 16 bpp

DPI values

PNM files cannot store the physical resolution in DPI.

Number of images

Only one image can be stored in a PNM file.

Usage example - load an image from a PNM file

 PNMCodec codec = new PNMCodec();
 codec.setFile("test.ppm", CodecMode.LOAD);
 codec.process();
 codec.close();
 PixelImage image = codec.getImage();
 

Usage example - save an image to a PNM file

 PNMCodec codec = new PNMCodec();
 BilevelImage myFax = ...; // initialize
 codec.setImage(myFax);
 codec.setFile("out.pbm", CodecMode.SAVE);
 codec.process();
 codec.close();
 
Author:
Marco Schmidt