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();
IMAGE_TYPE_BILEVEL
public static final int IMAGE_TYPE_BILEVEL
Image type constant for bilevel images, stored in PBM files.
IMAGE_TYPE_COLOR
public static final int IMAGE_TYPE_COLOR
Image type constant for RGB truecolor images, stored in PPM files.
IMAGE_TYPE_FILE_EXTENSIONS
private static final String[] IMAGE_TYPE_FILE_EXTENSIONS
IMAGE_TYPE_GRAY
public static final int IMAGE_TYPE_GRAY
Image type constant for grayscale images, stored in PGM files.
IMAGE_TYPE_UNKNOWN
public static final int IMAGE_TYPE_UNKNOWN
Image type constant for images of unknown type.
ascii
private Boolean ascii
columns
private int columns
imageType
private int imageType
in
private PushbackInputStream in
maxSample
private int maxSample
out
private DataOutput out
determineImageTypeFromFileName
public static int determineImageTypeFromFileName(String fileName)
fileName
- the file name to be examined
- one of the
IMAGE_TYPE_xxx
constants of this class
getAscii
public Boolean getAscii()
Returns if ASCII mode was used for loading an image or will
be used to store an image.
- true for ASCII mode, false for binary mode, null if that information is not available
getTypicalFileExtension
public static String getTypicalFileExtension(int imageType)
imageType
- the image type for which the extension is required
- the file extension or null
loadType
private void loadType()
throws InvalidFileStructureException,
IOException,
WrongFileFormatException
Loads the first two characters (which are expected to be a capital P
followed by a decimal digit between 1 and 6, inclusively) and skips
following LF and CR characters.
This method not only checks the two bytes, it also initializes internal fields
for storage mode (ASCII or binary) and image type.
save
private void save(BilevelImage image)
throws IOException
save
private void save(Gray16Image image)
throws IOException
save
private void save(Gray8Image image)
throws IOException
save
private void save(RGB24Image image)
throws IOException
save
private void save(RGB48Image image)
throws IOException
saveAsciiNumber
private void saveAsciiNumber(int number)
throws IOException
saveHeader
private void saveHeader()
throws IOException
setAscii
public void setAscii(boolean asciiMode)
Specify whether ASCII mode is to be used when saving an image.
Default is binary mode.
asciiMode
- if true, ASCII mode is used, binary mode otherwise