com.pearsoneduc.ip.io
Class PPMEncoder

java.lang.Object
  |
  +--com.pearsoneduc.ip.io.PPMEncoder

public class PPMEncoder
extends java.lang.Object
implements ImageEncoder, PPMConstants

Writes image data to a stream or a file encoded in PBM, PGM or PPM format, as appropriate.

The PBM, PGM and PPM formats are simple, popular formats for binary, greyscale and colour images on Unix systems. This class supports only the ASCII variants of these formats; the more compact raw binary variants are not currently supported.

A PBM, PGM or PPM header contains a two-character signature (see table below), the image width, the image height and (unless it is a PBM file) the maximum pixel value. Each of these elements are separated by whitespace. The header may also contain comment lines, each of which begin with the '#' character. By default, an encoder adds a comment giving the date and time of image creation; to prevent this, invoke the encoder's disableComments method.

Signature Image type
P1 binary (1 bit per pixel)
P2 greyscale (8 or 16 bits)
P3 RGB colour (24 bits)

Image data are written using ASCII numeric characters. Line breaks are inserted after each row of data, and also wherever necessary to ensure that no line in the file is more than 70 characters in length.

PBM data are written as a stream of the characters '1' or '0'. By default, the inverted meaning of these values is used, as required by the official definition of the PBM format - with '1' signifying black and '0' signifying white. This can be reversed by invoking the disableBitmapInversion method.

PGM data are written as a series of ASCII decimal values, right-justified in a field four characters wide (or six characters wide in the case of 16-bit images). PPM data are written in a band-interleaved fashion - i.e., the R, G and B values are written for each pixel in turn - with each colour component's value written as for greyscale images. An extra space is inserted between each RGB triplet.

Example of use:

     BufferedImage image =
      new BufferedImage(128, 128, BufferedImage.TYPE_BYTE_GRAY);
     ...
     PPMEncoder pgm = new PPMEncoder("test.pgm");
     pgm.disableComments();
     pgm.encode(image);
 

Version:
1.1 [1999/06/27]
Author:
Nick Efford
See Also:
PPMEncoderException, PPMDecoder, BufferedImage

Constructor Summary
PPMEncoder()
          Constructs a PPMEncoder associated with standard output.
PPMEncoder(java.io.OutputStream out)
          Constructs a PPMEncoder that writes to an existing OutputStream object.
PPMEncoder(java.lang.String filename)
          Constructs a PPMEncoder that writes to a named file.
 
Method Summary
 boolean bitmapInversionEnabled()
          Indicates whether a binary image will be inverted when written to a PBM file, such that black and white are written as 1 and 0, respectively.
 boolean commentsEnabled()
          Indicates whether comments may be added to a PBM, PGM or PPM header.
 void disableBitmapInversion()
          Disables bitmap inversion when writing data in the PBM format.
 void disableComments()
          Disables commenting of the PBM, PGM or PPM header with creation date.
 void enableBitmapInversion()
          Enables bitmap inversion when writing data in the PBM format.
 void enableComments()
          Enables commenting of the PBM, PGM or PPM header.
 void encode(java.awt.image.BufferedImage image)
          Encodes the specified image using the most appropriate image format.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PPMEncoder

public PPMEncoder()
Constructs a PPMEncoder associated with standard output.

PPMEncoder

public PPMEncoder(java.io.OutputStream out)
Constructs a PPMEncoder that writes to an existing OutputStream object.
Parameters:
out - the destination for the image data

PPMEncoder

public PPMEncoder(java.lang.String filename)
           throws java.io.IOException
Constructs a PPMEncoder that writes to a named file.
Parameters:
filename - Name of the file to which image data will be written
Throws:
java.io.IOException - if the file could not be accessed.
Method Detail

commentsEnabled

public boolean commentsEnabled()
Indicates whether comments may be added to a PBM, PGM or PPM header.
Returns:
true if comments may be added, false otherwise.

disableComments

public void disableComments()
Disables commenting of the PBM, PGM or PPM header with creation date.

enableComments

public void enableComments()
Enables commenting of the PBM, PGM or PPM header.

bitmapInversionEnabled

public boolean bitmapInversionEnabled()
Indicates whether a binary image will be inverted when written to a PBM file, such that black and white are written as 1 and 0, respectively.
Returns:
true if a bitmap would be inverted, false otherwise.

disableBitmapInversion

public void disableBitmapInversion()
Disables bitmap inversion when writing data in the PBM format.

enableBitmapInversion

public void enableBitmapInversion()
Enables bitmap inversion when writing data in the PBM format.

encode

public void encode(java.awt.image.BufferedImage image)
            throws PPMEncoderException
Encodes the specified image using the most appropriate image format. Images of type BufferedImage.TYPE_BYTE_BINARY are encoded using the PBM format. Images of type BufferedImage.TYPE_BYTE_GRAY or BufferedImage.TYPE_USHORT_GRAY are encoded using the PGM format. Other image types are encoded using the PPM format.
Specified by:
encode in interface ImageEncoder
Parameters:
image - the image to be encoded
Throws:
PPMEncoderException - if the image could not be written successfully.
See Also:
BufferedImage