Class GifDecoder

java.lang.Object
  extended by GifDecoder

public class GifDecoder
extends java.lang.Object

Class GifDecoder - Decodes a GIF file into one or more frames.

 Example:
    GifDecoder d = new GifDecoder();
    d.read("sample.gif");
    int n = d.getFrameCount();
    for (int i = 0; i < n; i++) {
         BufferedImage frame = d.getFrame(i);  // frame i
         int t = d.getDelay(i);  // display duration of frame in milliseconds
         // do something with frame
    }
 
No copyright asserted on the source code of this class. May be used for any purpose, however, refer to the Unisys LZW patent for any additional restrictions. Please forward any corrections to kweiner@fmsware.com.


Nested Class Summary
(package private) static class GifDecoder.GifFrame
          This is a private subclass to represent the frame for the GIF.
 
Field Summary
protected  int[] act
          Active color table.
protected  int bgColor
          Background color.
protected  int bgIndex
          Background color index.
protected  byte[] block
          Current data block.
protected  int blockSize
          Block size.
protected  int delay
          Delay in milliseconds.
protected  int dispose
          Last graphic control extension info.
protected  int frameCount
          Frame Count.
protected  java.util.ArrayList frames
          Frames read from current file.
protected  int[] gct
          Global color table.
protected  boolean gctFlag
          Global color table used.
protected  int gctSize
          Size of global color table.
protected  int height
          Full image height.
protected  int ih
          Current image rectangle.
protected  java.awt.image.BufferedImage image
          Current frame.
protected  java.io.BufferedInputStream in
          BufferedInputStream for reading input.
protected  boolean interlace
          Interlace flag.
protected  int iw
          Current image rectangle.
protected  int ix
          Current image rectangle.
protected  int iy
          Current image rectangle.
protected  int lastBgColor
          Previous bg color.
protected  int lastDispose
          0=no action; 1=leave in place; 2=restore to bg; 3=restore to prev.
protected  java.awt.image.BufferedImage lastImage
          Previous frame.
protected  java.awt.Rectangle lastRect
          Last image rect.
protected  int[] lct
          Local color table.
protected  boolean lctFlag
          Local color table flag.
protected  int lctSize
          Local color table size.
protected  int loopCount
          Number of Iterations; 0 = repeat forever.
protected static int MaxStackSize
          Max decoder pixel stack size.
protected  int pixelAspect
          Pixel aspect ratio.
protected  byte[] pixels
          LZW decoder working array.
protected  byte[] pixelStack
          LZW decoder working array.
protected  short[] prefix
          LZW decoder working array.
protected  int status
          An int representing the status.
static int STATUS_FORMAT_ERROR
          File read status: Error decoding file (may be partially decoded).
static int STATUS_OK
          File read status: No errors.
static int STATUS_OPEN_ERROR
          File read status: Unable to open source.
protected  byte[] suffix
          LZW decoder working array.
protected  int transIndex
          Transparent color index.
protected  boolean transparency
          Use transparent color.
protected  int width
          Full image width.
 
Constructor Summary
GifDecoder()
           
 
Method Summary
protected  void decodeImageData()
          Decodes LZW image data into pixel array.
protected  boolean err()
          Returns true if an error was encountered during reading/decoding.
 int getDelay(int n)
          Gets display duration for specified frame.
 java.awt.image.BufferedImage getFrame(int n)
          Gets the image contents of frame n.
 int getFrameCount()
          Gets the number of frames read from file.
 java.awt.Dimension getFrameSize()
          Gets image size.
 java.awt.image.BufferedImage getImage()
          Gets the first (or only) image read.
 int getLoopCount()
          Gets the "Netscape" iteration count, if any.
protected  void init()
          Initializes or re-initializes reader.
protected  int read()
          Reads a single byte from the input stream.
 int read(java.io.BufferedInputStream is)
          Reads GIF image from stream.
 int read(java.io.InputStream is)
          Reads GIF image from stream.
 int read(java.lang.String name)
          Reads GIF file from specified file/URL source (URL assumed if name contains ":/" or "file:").
protected  int readBlock()
          Reads next variable length block from input.
protected  int[] readColorTable(int ncolors)
          Reads color table as 256 RGB integer values.
protected  void readContents()
          Main file parser.
protected  void readGraphicControlExt()
          Reads Graphics Control Extension values.
protected  void readHeader()
          Reads GIF file header information.
protected  void readImage()
          Reads next frame image.
protected  void readLSD()
          Reads Logical Screen Descriptor.
protected  void readNetscapeExt()
          Reads Netscape extenstion to obtain iteration count.
protected  int readShort()
          Reads next 16-bit value, LSB first.
protected  void resetFrame()
          Resets frame state for reading next image.
protected  void setPixels()
          Creates new frame image from current data (and previous frames as specified by their disposition codes).
protected  void skip()
          Skips variable length blocks up to and including next zero length block.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

STATUS_OK

public static final int STATUS_OK
File read status: No errors.

See Also:
Constant Field Values

STATUS_FORMAT_ERROR

public static final int STATUS_FORMAT_ERROR
File read status: Error decoding file (may be partially decoded).

See Also:
Constant Field Values

STATUS_OPEN_ERROR

public static final int STATUS_OPEN_ERROR
File read status: Unable to open source.

See Also:
Constant Field Values

in

protected java.io.BufferedInputStream in
BufferedInputStream for reading input.


status

protected int status
An int representing the status.


width

protected int width
Full image width.


height

protected int height
Full image height.


gctFlag

protected boolean gctFlag
Global color table used.


gctSize

protected int gctSize
Size of global color table.


loopCount

protected int loopCount
Number of Iterations; 0 = repeat forever.


gct

protected int[] gct
Global color table.


lct

protected int[] lct
Local color table.


act

protected int[] act
Active color table.


bgIndex

protected int bgIndex
Background color index.


bgColor

protected int bgColor
Background color.


lastBgColor

protected int lastBgColor
Previous bg color.


pixelAspect

protected int pixelAspect
Pixel aspect ratio.


lctFlag

protected boolean lctFlag
Local color table flag.


interlace

protected boolean interlace
Interlace flag.


lctSize

protected int lctSize
Local color table size.


ix

protected int ix
Current image rectangle.


iy

protected int iy
Current image rectangle.


iw

protected int iw
Current image rectangle.


ih

protected int ih
Current image rectangle.


lastRect

protected java.awt.Rectangle lastRect
Last image rect.


image

protected java.awt.image.BufferedImage image
Current frame.


lastImage

protected java.awt.image.BufferedImage lastImage
Previous frame.


block

protected byte[] block
Current data block.


blockSize

protected int blockSize
Block size.


dispose

protected int dispose
Last graphic control extension info.


lastDispose

protected int lastDispose
0=no action; 1=leave in place; 2=restore to bg; 3=restore to prev.


transparency

protected boolean transparency
Use transparent color.


delay

protected int delay
Delay in milliseconds.


transIndex

protected int transIndex
Transparent color index.


MaxStackSize

protected static final int MaxStackSize
Max decoder pixel stack size.

See Also:
Constant Field Values

prefix

protected short[] prefix
LZW decoder working array.


suffix

protected byte[] suffix
LZW decoder working array.


pixelStack

protected byte[] pixelStack
LZW decoder working array.


pixels

protected byte[] pixels
LZW decoder working array.


frames

protected java.util.ArrayList frames
Frames read from current file.


frameCount

protected int frameCount
Frame Count.

Constructor Detail

GifDecoder

public GifDecoder()
Method Detail

getDelay

public int getDelay(int n)
Gets display duration for specified frame.

Parameters:
n - int index of frame
Returns:
delay in milliseconds

getFrameCount

public int getFrameCount()
Gets the number of frames read from file.

Returns:
frame count

getImage

public java.awt.image.BufferedImage getImage()
Gets the first (or only) image read.

Returns:
BufferedImage containing first frame, or null if none.

getLoopCount

public int getLoopCount()
Gets the "Netscape" iteration count, if any. A count of 0 means repeat indefinitiely.

Returns:
iteration count if one was specified, else 1.

setPixels

protected void setPixels()
Creates new frame image from current data (and previous frames as specified by their disposition codes).


getFrame

public java.awt.image.BufferedImage getFrame(int n)
Gets the image contents of frame n.

Parameters:
n - an int representing the frame number to get
Returns:
BufferedImage representation of frame, or null if n is invalid.

getFrameSize

public java.awt.Dimension getFrameSize()
Gets image size.

Returns:
GIF image dimensions

read

public int read(java.io.BufferedInputStream is)
Reads GIF image from stream.

Parameters:
is - containing GIF file.
Returns:
read BufferedInputStream status code (0 = no errors)

read

public int read(java.io.InputStream is)
Reads GIF image from stream.

Parameters:
is - InputStream containing GIF file.
Returns:
is read status code (0 = no errors)

read

public int read(java.lang.String name)
Reads GIF file from specified file/URL source (URL assumed if name contains ":/" or "file:").

Parameters:
name - String containing source
Returns:
read status code (0 = no errors)

decodeImageData

protected void decodeImageData()
Decodes LZW image data into pixel array. Adapted from John Cristy's ImageMagick.


err

protected boolean err()
Returns true if an error was encountered during reading/decoding.

Returns:
true if status != STATUS_OK, false if status == STATUS_OK

init

protected void init()
Initializes or re-initializes reader.


read

protected int read()
Reads a single byte from the input stream.

Returns:
an int representing the read in byte value

readBlock

protected int readBlock()
Reads next variable length block from input.

Returns:
number of bytes stored in "buffer"

readColorTable

protected int[] readColorTable(int ncolors)
Reads color table as 256 RGB integer values.

Parameters:
ncolors - int number of colors to read
Returns:
int array containing 256 colors (packed ARGB with full alpha)

readContents

protected void readContents()
Main file parser. Reads GIF content blocks.


readGraphicControlExt

protected void readGraphicControlExt()
Reads Graphics Control Extension values.


readHeader

protected void readHeader()
Reads GIF file header information.


readImage

protected void readImage()
Reads next frame image.


readLSD

protected void readLSD()
Reads Logical Screen Descriptor.


readNetscapeExt

protected void readNetscapeExt()
Reads Netscape extenstion to obtain iteration count.


readShort

protected int readShort()
Reads next 16-bit value, LSB first.

Returns:
an int representing a 16-bit value from the input stream

resetFrame

protected void resetFrame()
Resets frame state for reading next image.


skip

protected void skip()
Skips variable length blocks up to and including next zero length block.