com.bumptech.glide.gifdecoder
Class GifDecoder

java.lang.Object
  extended by com.bumptech.glide.gifdecoder.GifDecoder

public class GifDecoder
extends Object

Reads frame data from a GIF image source and decodes it into individual frames for animation purposes. Image data can be read from either and InputStream source or a byte[]. This class is optimized for running animations with the frames, there are no methods to get individual frame images, only to decode the next frame in the animation sequence. Instead, it lowers its memory footprint by only housing the minimum data necessary to decode the next frame in the animation sequence. The animation must be manually moved forward using advance() before requesting the next frame. This method must also be called before you request the first frame or an error will occur. Implementation adapted from sample code published in Lyons. (2004). Java for Programmers, republished under the MIT Open Source License


Nested Class Summary
static interface GifDecoder.BitmapProvider
          An interface that can be used to provide reused Bitmaps to avoid GCs from constantly allocating Bitmaps for every frame.
 
Field Summary
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.
static int STATUS_PARTIAL_DECODE
          Unable to fully decode the current frame.
 
Constructor Summary
GifDecoder(GifDecoder.BitmapProvider provider)
           
 
Method Summary
 void advance()
          Move the animation frame counter forward.
 void clear()
           
 int getCurrentFrameIndex()
          Gets the current index of the animation frame, or -1 if animation hasn't not yet started.
 byte[] getData()
           
 int getDelay(int n)
          Gets display duration for specified frame.
 int getFrameCount()
          Gets the number of frames read from file.
 int getHeight()
           
 int getLoopCount()
          Gets the "Netscape" iteration count, if any.
 int getNextDelay()
          Gets display duration for the upcoming frame in ms.
 Bitmap getNextFrame()
          Get the next frame in the animation sequence.
 int getStatus()
          Returns the current status of the decoder.
 int getWidth()
           
 int read(byte[] data)
          Reads GIF image from byte array.
 int read(InputStream is, int contentLength)
          Reads GIF image from stream.
 void resetFrameIndex()
           
 void setData(GifHeader header, byte[] data)
           
 
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

STATUS_PARTIAL_DECODE

public static final int STATUS_PARTIAL_DECODE
Unable to fully decode the current frame.

See Also:
Constant Field Values
Constructor Detail

GifDecoder

public GifDecoder(GifDecoder.BitmapProvider provider)
Method Detail

getWidth

public int getWidth()

getHeight

public int getHeight()

getData

public byte[] getData()

getStatus

public int getStatus()
Returns the current status of the decoder.

Status will update per frame to allow the caller to tell whether or not the current frame was decoded successfully and/or completely. Format and open failures persist across frames.


advance

public void advance()
Move the animation frame counter forward.


getDelay

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

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

getNextDelay

public int getNextDelay()
Gets display duration for the upcoming frame in ms.


getFrameCount

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

Returns:
frame count.

getCurrentFrameIndex

public int getCurrentFrameIndex()
Gets the current index of the animation frame, or -1 if animation hasn't not yet started.

Returns:
frame index.

resetFrameIndex

public void resetFrameIndex()

getLoopCount

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

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

getNextFrame

public Bitmap getNextFrame()
Get the next frame in the animation sequence.

Returns:
Bitmap representation of frame.

read

public int read(InputStream is,
                int contentLength)
Reads GIF image from stream.

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

clear

public void clear()

setData

public void setData(GifHeader header,
                    byte[] data)

read

public int read(byte[] data)
Reads GIF image from byte array.

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