jwo.utils.gifutils
Class GIFOutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by java.io.FilterOutputStream
          extended by jwo.utils.gifutils.BitOutputStream
              extended by jwo.utils.gifutils.GIFOutputStream
All Implemented Interfaces:
Closeable, Flushable

public class GIFOutputStream
extends BitOutputStream

GIFOutputStream provides writeXXX() methods useful for writing out a bitmap image to a GIF file. Included are methods which handle LZW compression 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 compression
protected  jwo.utils.gifutils.LZWStringTable stringTable
          String table for LZW compression
 
Fields inherited from class jwo.utils.gifutils.BitOutputStream
cachedBits, numCachedBits
 
Fields inherited from class java.io.FilterOutputStream
out
 
Constructor Summary
GIFOutputStream(OutputStream os)
          Creates an output stream suitable for writing .gif files.
 
Method Summary
protected  void writeCode(short code, BitOutputStream bos)
          Writes a code to the given BitOutputStream using the current bit length from the string table.
 void writeColor(Color colour)
          Writes a color as 3 bytes in RRGGBB hex format.
 void writeDataBlock(byte[] block)
          Writes the given bytes to the underlying output stream as a series of data sub-blocks.
 void writeDataSubBlock(byte[] subBlock)
          Writes a data sub-block from the underlying stream.
 void writeImageData(byte[] pixels, int bpp)
          LZW compresses the given pixel data and writes it to the underlying output stream as a data block.
 void writeWord(int word)
          Writes an unsigned LSB-first 16 byte word to the underlying stream.
 
Methods inherited from class jwo.utils.gifutils.BitOutputStream
flush, write, writeBit, writeBits
 
Methods inherited from class java.io.FilterOutputStream
close, write, write
 
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 compression

See Also:
Constant Field Values

stringTable

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

Constructor Detail

GIFOutputStream

public GIFOutputStream(OutputStream os)
Creates an output stream suitable for writing .gif files.

Parameters:
os - Underlying output stream.
Method Detail

writeWord

public void writeWord(int word)
               throws IOException
Writes an unsigned LSB-first 16 byte word to the underlying stream.

Parameters:
word - Word to write.
Throws:
IOException - if problem writing word.

writeColor

public void writeColor(Color colour)
                throws IOException
Writes a color as 3 bytes in RRGGBB hex format.

Parameters:
colour - Colour to write.
Throws:
IOException - if problem writing to output stream.

writeDataSubBlock

public void writeDataSubBlock(byte[] subBlock)
                       throws IOException
Writes a data sub-block from the underlying stream. Before the given bytes, the length of the block (0-255) is written.

Parameters:
subBlock - Sub-block to write.
Throws:
IOException - if problem writing sub-block.

writeDataBlock

public void writeDataBlock(byte[] block)
                    throws IOException
Writes the given bytes to the underlying output stream as a series of data sub-blocks. As many sub-blocks of size 255 as possible will be written, then one smaller block as necessary. A final sub-block of a single zero byte is written to terminate the block.

Parameters:
block - Block to write.
Throws:
IOException - if problem writing block.

writeCode

protected void writeCode(short code,
                         BitOutputStream bos)
                  throws IOException
Writes a code to the given BitOutputStream using the current bit length from the string table. (The string table is instantiated by the writeImageData() method, so this should not be called directly.)

Parameters:
code - Code to write. Sub-block to write.
bos - Output stream to write to.
Throws:
IOException - if problem writing code.

writeImageData

public void writeImageData(byte[] pixels,
                           int bpp)
                    throws IOException
LZW compresses the given pixel data and writes it to the underlying output stream as a data block. Before the data is written, the root code size is output. This code size is just the given color depth of the image, or 2 if the depth is one.

Parameters:
pixels - Pixel data to write.
bpp - Image depth.
Throws:
IOException - If problem writing the compressed data.


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