com.pearsoneduc.ip.io
Class PNGDecoder

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

public class PNGDecoder
extends java.lang.Object
implements ImageDecoder

Reads image data in the PNG format from a stream or a file. This class is merely a wrapper for PNGDataDecoder from the com.visualtek.PNG package.

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

Constructor Summary
PNGDecoder()
          Constructs a PNGDecoder associated with standard input.
PNGDecoder(java.io.InputStream in)
          Constructs a PNGDecoder that reads from an existing InputStream.
PNGDecoder(java.lang.String imgfile)
          Constructs a PNGDecoder that reads from a named file.
 
Method Summary
 java.awt.image.BufferedImage decodeAsBufferedImage()
          Decodes the input data and creates an image.
 int getBitDepth()
           
 int getHeight()
           
 int getNumPixels()
           
 int getRowSize()
           
 int getType()
          Indicates what type of image is available for decoding.
 int getWidth()
           
 boolean hasPalette()
          Indicates whether indexed colour image data are available for decoding.
 boolean isBinary()
          Indicates whether binary image data are available for decoding.
 boolean isGrey()
          Indicates whether greyscale image data are available for decoding.
 boolean isRGB()
          Indicates whether RGB image data are available for decoding.
 byte[] readRow(byte[] row)
          Reads a row of image data.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PNGDecoder

public PNGDecoder()
           throws java.io.IOException,
                  PNGDecoderException
Constructs a PNGDecoder associated with standard input.
Throws:
java.io.IOException - if there was a problem reading data from standard input.
PNGDecoderException - if the data do not conform to the PNG format.

PNGDecoder

public PNGDecoder(java.io.InputStream in)
           throws java.io.IOException,
                  PNGDecoderException
Constructs a PNGDecoder that reads from an existing InputStream.
Parameters:
in - InputStream representing the source of image data
Throws:
java.io.IOException - if there was a problem reading data from the stream.
PNGDecoderException - if the data do not conform to the PNG format.

PNGDecoder

public PNGDecoder(java.lang.String imgfile)
           throws java.io.IOException,
                  PNGDecoderException
Constructs a PNGDecoder that reads from a named file.
Parameters:
imgfile - name of the file containing the image data
Throws:
java.io.IOException - if the file does not exist or there was a problem reading data from it.
PNGDecoderException - if the data do not conform to the PNG format.
Method Detail

getType

public int getType()
Indicates what type of image is available for decoding.
Returns:
one of the constants TYPE_UNKNOWN, TYPE_PBM, TYPE_PGM and TYPE_PPM, defined in the PPMConstants interface.
See Also:
PPMConstants

isBinary

public boolean isBinary()
Indicates whether binary image data are available for decoding.
Returns:
true if a binary image is available, false otherwise.

isGrey

public boolean isGrey()
Indicates whether greyscale image data are available for decoding.
Returns:
true if a greyscale image is available, false otherwise.

hasPalette

public boolean hasPalette()
Indicates whether indexed colour image data are available for decoding.
Returns:
true if indexed colour data are available, false otherwise.

isRGB

public boolean isRGB()
Indicates whether RGB image data are available for decoding.
Returns:
true if a colour image is available, false otherwise.

getWidth

public int getWidth()
Returns:
width of image to be decoded.

getHeight

public int getHeight()
Returns:
height of image to be decoded.

getNumPixels

public int getNumPixels()
Returns:
number of pixels in image to be decoded.

getBitDepth

public int getBitDepth()
Returns:
bit depth (number of bits per sample) for image to be decoded.

getRowSize

public int getRowSize()
Returns:
number of bytes required to store one row of image data.

readRow

public byte[] readRow(byte[] row)
               throws java.io.IOException,
                      PNGDecoderException
Reads a row of image data. The data will be read into the supplied array or, if this is null, returned as a new array of bytes. The array size required to store a single row is obtained using the getRowSize method.
Parameters:
row - byte array in which image data will be placed, or null
Returns:
a new array containing row data, if null was passed as a parameter; or null if an array was passed.
Throws:
java.io.IOException - if there was a problem reading data.
PNGDecoderException - if the data do not conform to the PNG format.

decodeAsBufferedImage

public java.awt.image.BufferedImage decodeAsBufferedImage()
                                                   throws java.io.IOException,
                                                          PNGDecoderException
Decodes the input data and creates an image. Currently, this method supports only 8 bits per sample and it ignores alpha channels. It also converts paletted images into 24-bit colour images. For different behaviour, you can read an image one row at a time using readRow and manipulate the returned bytes in an appropriate manner.
Specified by:
decodeAsBufferedImage in interface ImageDecoder
Returns:
a BufferedImage containing the data.
Throws:
java.io.IOException - if there was a problem reading the data.
PNGDecoderException - if the data are not formatted correctly.