Module bus.extra

Class GifDecoder

java.lang.Object
org.miaixz.bus.extra.image.gif.GifDecoder

public class GifDecoder extends 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 questions at fmsware.com.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected int[]
    The active color table.
    protected int
    The background color.
    protected int
    The background color index.
    protected byte[]
    The current data block.
    protected int
    The size of the current block.
    protected int
    The delay in milliseconds.
    protected int
    The disposal method for the last graphic control extension.
    protected int
    The total number of frames.
    protected ArrayList<org.miaixz.bus.extra.image.gif.GifDecoder.GifFrame>
    List of frames read from the current file.
    protected int[]
    The global color table.
    protected boolean
    Flag indicating if a global color table is used.
    protected int
    Size of the global color table.
    protected int
    The full image height.
    protected int
    The current image rectangle coordinates and dimensions.
    protected BufferedImage
    The current frame image.
    The buffered input stream.
    protected boolean
    Flag indicating if the image is interlaced.
    protected int
    The current image rectangle coordinates and dimensions.
    protected int
    The current image rectangle coordinates and dimensions.
    protected int
    The current image rectangle coordinates and dimensions.
    protected int
    The previous background color.
    protected int
    The last disposal method used.
    protected BufferedImage
    The previous frame image.
    protected Rectangle
    The last image rectangle.
    protected int[]
    The local color table.
    protected boolean
    Flag indicating if a local color table is used.
    protected int
    The size of the local color table.
    protected int
    Number of iterations; 0 means repeat forever.
    protected static final int
    The maximum decoder pixel stack size.
    protected int
    The pixel aspect ratio.
    protected byte[]
    Byte array of pixel data.
    protected byte[]
    LZW decoder working array.
    protected short[]
    LZW decoder working array.
    protected int
    The current status of the decoder.
    static final int
    File read status: Error decoding file (may be partially decoded)
    static final int
    File read status: No errors.
    static final int
    File read status: Unable to open source.
    protected byte[]
    LZW decoder working array.
    protected int
    The transparent color index.
    protected boolean
    Flag indicating if a transparent color is used.
    protected int
    The full image width.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    protected void
    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.
    getFrame(int n)
    Gets the image contents of frame n.
    int
    Gets the number of frames read from file.
    Gets image size.
    Gets the first (or only) image read.
    int
    Gets the "Netscape" iteration count, if any.
    protected void
    Initializes or re-initializes reader
    protected int
    Reads a single byte from the input stream.
    int
    Reads GIF image from stream
    int
    Reads GIF image from stream
    int
    read(String name)
    Reads GIF file from specified file/URL source (URL assumed if name contains ":/" or "file:")
    protected int
    Reads next variable length block from input.
    protected int[]
    readColorTable(int ncolors)
    Reads color table as 256 RGB integer values
    protected void
    Main file parser.
    protected void
    Reads Graphics Control Extension values
    protected void
    Reads GIF file header information.
    protected void
    Reads next frame image
    protected void
    Reads Logical Screen Descriptor
    protected void
    Reads Netscape extenstion to obtain iteration count
    protected int
    Reads next 16-bit value, LSB first
    protected void
    Resets frame state for reading next image.
    protected void
    Creates new frame image from current data (and previous frames as specified by their disposition codes).
    protected void
    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 Details

    • STATUS_OK

      public static final int STATUS_OK
      File read status: No errors.
      See Also:
    • STATUS_FORMAT_ERROR

      public static final int STATUS_FORMAT_ERROR
      File read status: Error decoding file (may be partially decoded)
      See Also:
    • STATUS_OPEN_ERROR

      public static final int STATUS_OPEN_ERROR
      File read status: Unable to open source.
      See Also:
    • MaxStackSize

      protected static final int MaxStackSize
      The maximum decoder pixel stack size.
      See Also:
    • in

      protected BufferedInputStream in
      The buffered input stream.
    • status

      protected int status
      The current status of the decoder.
    • width

      protected int width
      The full image width.
    • height

      protected int height
      The full image height.
    • gctFlag

      protected boolean gctFlag
      Flag indicating if a global color table is used.
    • gctSize

      protected int gctSize
      Size of the global color table.
    • loopCount

      protected int loopCount
      Number of iterations; 0 means repeat forever.
    • gct

      protected int[] gct
      The global color table.
    • lct

      protected int[] lct
      The local color table.
    • act

      protected int[] act
      The active color table.
    • bgIndex

      protected int bgIndex
      The background color index.
    • bgColor

      protected int bgColor
      The background color.
    • lastBgColor

      protected int lastBgColor
      The previous background color.
    • pixelAspect

      protected int pixelAspect
      The pixel aspect ratio.
    • lctFlag

      protected boolean lctFlag
      Flag indicating if a local color table is used.
    • interlace

      protected boolean interlace
      Flag indicating if the image is interlaced.
    • lctSize

      protected int lctSize
      The size of the local color table.
    • ix

      protected int ix
      The current image rectangle coordinates and dimensions.
    • iy

      protected int iy
      The current image rectangle coordinates and dimensions.
    • iw

      protected int iw
      The current image rectangle coordinates and dimensions.
    • ih

      protected int ih
      The current image rectangle coordinates and dimensions.
    • lastRect

      protected Rectangle lastRect
      The last image rectangle.
    • image

      protected BufferedImage image
      The current frame image.
    • lastImage

      protected BufferedImage lastImage
      The previous frame image.
    • block

      protected byte[] block
      The current data block.
    • blockSize

      protected int blockSize
      The size of the current block.
    • dispose

      protected int dispose
      The disposal method for the last graphic control extension.
    • lastDispose

      protected int lastDispose
      The last disposal method used.
    • transparency

      protected boolean transparency
      Flag indicating if a transparent color is used.
    • delay

      protected int delay
      The delay in milliseconds.
    • transIndex

      protected int transIndex
      The transparent color index.
    • 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
      Byte array of pixel data.
    • frames

      protected ArrayList<org.miaixz.bus.extra.image.gif.GifDecoder.GifFrame> frames
      List of frames read from the current file.
    • frameCount

      protected int frameCount
      The total number of frames.
  • Constructor Details

    • GifDecoder

      public GifDecoder()
  • Method Details

    • 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 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 BufferedImage getFrame(int n)
      Gets the image contents of frame n.
      Parameters:
      n - the frame index.
      Returns:
      BufferedImage representation of frame, or null if n is invalid.
    • getFrameSize

      public Dimension getFrameSize()
      Gets image size.
      Returns:
      GIF image dimensions
    • read

      public int read(BufferedInputStream is)
      Reads GIF image from stream
      Parameters:
      is - BufferedInputStream containing GIF file.
      Returns:
      read status code (0 = no errors)
    • read

      public int read(InputStream is)
      Reads GIF image from stream
      Parameters:
      is - InputStream containing GIF file.
      Returns:
      read status code (0 = no errors)
    • read

      public int read(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 an error occurred.
    • init

      protected void init()
      Initializes or re-initializes reader
    • read

      protected int read()
      Reads a single byte from the input stream.
      Returns:
      the byte read.
    • 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:
      the 16-bit value.
    • 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.