jwo.utils.gifutils
Class GIFInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by java.io.FilterInputStream
          extended by jwo.utils.gifutils.BitInputStream
              extended by jwo.utils.gifutils.GIFInputStream
All Implemented Interfaces:
Closeable

public class GIFInputStream
extends BitInputStream

GIFInputStream provides readXXX() methods useful for reading in the contents of a GIF file. Included are methods which handle LZW decompression of the image data.

Author:
Benjamin E. Norman. Very minor modifications, Jo Wood.

Field Summary
static int MAXIMUM_CODE_LENGTH
          Maximum code size in bits for GIF LZW decompression
protected  jwo.utils.gifutils.LZWStringTable stringTable
          String table for LZW decompression
 
Fields inherited from class jwo.utils.gifutils.BitInputStream
cachedBits, numCachedBits
 
Fields inherited from class java.io.FilterInputStream
in
 
Constructor Summary
GIFInputStream(InputStream is)
          Creates an input stream suitable for reading .gif files.
 
Method Summary
 byte readByte()
          Reads an 8 bit byte at the current bit boundary.
protected  short readCode(BitInputStream bis)
          Reads a code from the given BitInputStream using the code size from the instance LZW string table.
 Color readColor()
          Reads 3 bytes and interprets them as an RRGGBB hex color.
 byte[] readDataBlock()
          Reads data sub-blocks from the underlying input stream until a block terminator (a single zero byte) is encountered.
 byte[] readDataSubBlock()
          Reads a data sub-block from the underlying stream.
 byte[] readImageData(int length)
          Reads the compressed data for a table-based image and return the decompressed raster data.
 int readWord()
          Reads an unsigned LSB-first 16 byte word from the underlying stream.
 
Methods inherited from class jwo.utils.gifutils.BitInputStream
read, readBit, readBits
 
Methods inherited from class java.io.FilterInputStream
available, close, mark, markSupported, read, read, reset, skip
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MAXIMUM_CODE_LENGTH

public static final int MAXIMUM_CODE_LENGTH
Maximum code size in bits for GIF LZW decompression

See Also:
Constant Field Values

stringTable

protected jwo.utils.gifutils.LZWStringTable stringTable
String table for LZW decompression

Constructor Detail

GIFInputStream

public GIFInputStream(InputStream is)
Creates an input stream suitable for reading .gif files.

Parameters:
is - Underlying input stream.
Method Detail

readByte

public byte readByte()
              throws IOException
Reads an 8 bit byte at the current bit boundary.

Returns:
Byte read from input stream.
Throws:
IOException - if 8 bits are not available.

readWord

public int readWord()
             throws IOException
Reads an unsigned LSB-first 16 byte word from the underlying stream.

Returns:
Word read from underlying input stream.
Throws:
IOException - if EOF is reached before 2 bytes are read.

readColor

public Color readColor()
                throws IOException
Reads 3 bytes and interprets them as an RRGGBB hex color.

Returns:
Colour read from input stream.
Throws:
IOException - if EOF is reached before 3 bytes are read.

readDataSubBlock

public byte[] readDataSubBlock()
                        throws IOException
Reads a data sub-block from the underlying stream. The first byte read is taken to be the sub-block's length and is not returned in the array.

Returns:
Data block.
Throws:
IOException - if a complete block is not read before EOF

readDataBlock

public byte[] readDataBlock()
                     throws IOException
Reads data sub-blocks from the underlying input stream until a block terminator (a single zero byte) is encountered. All bytes read except for the first byte of each sub-block are returned.

Returns:
Data block.
Throws:
IOException - if a complete block is not read before EOF

readCode

protected short readCode(BitInputStream bis)
                  throws IOException
Reads a code from the given BitInputStream using the code size from the instance LZW string table. Therefore, this should only be called after (usually by) readImageData(), which instantiates the string table.

Returns:
Code.
Throws:
IOException - if a code is not read before EOF

readImageData

public byte[] readImageData(int length)
                     throws IOException
Reads the compressed data for a table-based image and return the decompressed raster data. Each byte in the return array will correspond to one pixel, regardless of image bit depth. The root code size is read as a single byte before the image data sub-blocks. The length of the uncompressed data is required so that the array may be created; this should be simply the image width times the image height.

Parameters:
length - Amount of data to read in bytes.
Returns:
Image data.
Throws:
IOException - if EOF is reached prematurely or the LZW decoding fails


Copyright Jo Wood, 1996-2009, last modified, 17th April, 2009