Class StandardGifDecoder
- java.lang.Object
-
- com.bumptech.glide.gifdecoder.StandardGifDecoder
-
- All Implemented Interfaces:
GifDecoder
public class StandardGifDecoder extends java.lang.Object implements GifDecoder
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
- See Also:
- GIF 89a Specification
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface com.bumptech.glide.gifdecoder.GifDecoder
GifDecoder.BitmapProvider, GifDecoder.GifDecodeStatus
-
-
Field Summary
-
Fields inherited from interface com.bumptech.glide.gifdecoder.GifDecoder
STATUS_FORMAT_ERROR, STATUS_OK, STATUS_OPEN_ERROR, STATUS_PARTIAL_DECODE, TOTAL_ITERATION_COUNT_FOREVER
-
-
Constructor Summary
Constructors Constructor Description StandardGifDecoder(GifDecoder.BitmapProvider provider)StandardGifDecoder(GifDecoder.BitmapProvider provider, GifHeader gifHeader, java.nio.ByteBuffer rawData)StandardGifDecoder(GifDecoder.BitmapProvider provider, GifHeader gifHeader, java.nio.ByteBuffer rawData, int sampleSize)
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidadvance()Move the animation frame counter forward.voidclear()intgetByteSize()Returns an estimated byte size for this decoder based on the data provided toGifDecoder.setData(GifHeader, byte[]), as well as internal buffers.intgetCurrentFrameIndex()Gets the current index of the animation frame, or -1 if animation hasn't not yet started.java.nio.ByteBuffergetData()intgetDelay(int n)Gets display duration for specified frame.intgetFrameCount()Gets the number of frames read from file.intgetHeight()intgetLoopCount()Deprecated.intgetNetscapeLoopCount()Gets the "Netscape" loop count, if any.intgetNextDelay()Gets display duration for the upcoming frame in ms.android.graphics.BitmapgetNextFrame()Get the next frame in the animation sequence.intgetStatus()Returns the current status of the decoder.intgetTotalIterationCount()Gets the total count which represents how many times the animation sequence should be displayed.intgetWidth()intread(byte[] data)Reads GIF image from byte array.intread(java.io.InputStream is, int contentLength)Reads GIF image from stream.voidresetFrameIndex()Resets the frame pointer to before the 0th frame, as if we'd never used this decoder to decode any frames.voidsetData(GifHeader header, byte[] data)voidsetData(GifHeader header, java.nio.ByteBuffer buffer)voidsetData(GifHeader header, java.nio.ByteBuffer buffer, int sampleSize)voidsetDefaultBitmapConfig(android.graphics.Bitmap.Config config)Sets the defaultBitmap.Configto use when decoding frames of a GIF.
-
-
-
Constructor Detail
-
StandardGifDecoder
public StandardGifDecoder(@NonNull GifDecoder.BitmapProvider provider, GifHeader gifHeader, java.nio.ByteBuffer rawData)
-
StandardGifDecoder
public StandardGifDecoder(@NonNull GifDecoder.BitmapProvider provider, GifHeader gifHeader, java.nio.ByteBuffer rawData, int sampleSize)
-
StandardGifDecoder
public StandardGifDecoder(@NonNull GifDecoder.BitmapProvider provider)
-
-
Method Detail
-
getWidth
public int getWidth()
- Specified by:
getWidthin interfaceGifDecoder
-
getHeight
public int getHeight()
- Specified by:
getHeightin interfaceGifDecoder
-
getData
@NonNull public java.nio.ByteBuffer getData()
- Specified by:
getDatain interfaceGifDecoder
-
getStatus
public int getStatus()
Description copied from interface:GifDecoderReturns 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.
- Specified by:
getStatusin interfaceGifDecoder
-
advance
public void advance()
Description copied from interface:GifDecoderMove the animation frame counter forward.- Specified by:
advancein interfaceGifDecoder
-
getDelay
public int getDelay(int n)
Description copied from interface:GifDecoderGets display duration for specified frame.- Specified by:
getDelayin interfaceGifDecoder- Parameters:
n- int index of frame.- Returns:
- delay in milliseconds.
-
getNextDelay
public int getNextDelay()
Description copied from interface:GifDecoderGets display duration for the upcoming frame in ms.- Specified by:
getNextDelayin interfaceGifDecoder
-
getFrameCount
public int getFrameCount()
Description copied from interface:GifDecoderGets the number of frames read from file.- Specified by:
getFrameCountin interfaceGifDecoder- Returns:
- frame count.
-
getCurrentFrameIndex
public int getCurrentFrameIndex()
Description copied from interface:GifDecoderGets the current index of the animation frame, or -1 if animation hasn't not yet started.- Specified by:
getCurrentFrameIndexin interfaceGifDecoder- Returns:
- frame index.
-
resetFrameIndex
public void resetFrameIndex()
Description copied from interface:GifDecoderResets the frame pointer to before the 0th frame, as if we'd never used this decoder to decode any frames.- Specified by:
resetFrameIndexin interfaceGifDecoder
-
getLoopCount
@Deprecated public int getLoopCount()
Deprecated.Description copied from interface:GifDecoderGets the "Netscape" loop count, if any. A count of 0 means repeat indefinitely.- Specified by:
getLoopCountin interfaceGifDecoder- Returns:
- loop count if one was specified, else 1.
-
getNetscapeLoopCount
public int getNetscapeLoopCount()
Description copied from interface:GifDecoderGets the "Netscape" loop count, if any. A count of 0 (GifHeader.NETSCAPE_LOOP_COUNT_FOREVER) means repeat indefinitely. It must not be a negative value.
UseGifDecoder.getTotalIterationCount()to know how many times the animation sequence should be displayed.- Specified by:
getNetscapeLoopCountin interfaceGifDecoder- Returns:
- loop count if one was specified,
else -1 (
GifHeader.NETSCAPE_LOOP_COUNT_DOES_NOT_EXIST).
-
getTotalIterationCount
public int getTotalIterationCount()
Description copied from interface:GifDecoderGets the total count which represents how many times the animation sequence should be displayed. A count of 0 (GifDecoder.TOTAL_ITERATION_COUNT_FOREVER) means repeat indefinitely. It must not be a negative value.The total count is calculated as follows by using
GifDecoder.getNetscapeLoopCount(). This behavior is the same as most web browsers.getNetscapeLoopCount()The total count GifHeader.NETSCAPE_LOOP_COUNT_FOREVERGifDecoder.TOTAL_ITERATION_COUNT_FOREVERGifHeader.NETSCAPE_LOOP_COUNT_DOES_NOT_EXIST1n (n > 0)n + 1- Specified by:
getTotalIterationCountin interfaceGifDecoder- Returns:
- total iteration count calculated from "Netscape" loop count.
- See Also:
- Discussion about the iteration count of animated GIFs (Chromium Issue 592735)
-
getByteSize
public int getByteSize()
Description copied from interface:GifDecoderReturns an estimated byte size for this decoder based on the data provided toGifDecoder.setData(GifHeader, byte[]), as well as internal buffers.- Specified by:
getByteSizein interfaceGifDecoder
-
getNextFrame
@Nullable public android.graphics.Bitmap getNextFrame()
Description copied from interface:GifDecoderGet the next frame in the animation sequence.- Specified by:
getNextFramein interfaceGifDecoder- Returns:
- Bitmap representation of frame.
-
read
public int read(@Nullable java.io.InputStream is, int contentLength)Description copied from interface:GifDecoderReads GIF image from stream.- Specified by:
readin interfaceGifDecoder- Parameters:
is- containing GIF file.- Returns:
- read status code (0 = no errors).
-
clear
public void clear()
- Specified by:
clearin interfaceGifDecoder
-
setData
public void setData(@NonNull GifHeader header, @NonNull byte[] data)- Specified by:
setDatain interfaceGifDecoder
-
setData
public void setData(@NonNull GifHeader header, @NonNull java.nio.ByteBuffer buffer)- Specified by:
setDatain interfaceGifDecoder
-
setData
public void setData(@NonNull GifHeader header, @NonNull java.nio.ByteBuffer buffer, int sampleSize)- Specified by:
setDatain interfaceGifDecoder
-
read
public int read(@Nullable byte[] data)Description copied from interface:GifDecoderReads GIF image from byte array.- Specified by:
readin interfaceGifDecoder- Parameters:
data- containing GIF file.- Returns:
- read status code (0 = no errors).
-
setDefaultBitmapConfig
public void setDefaultBitmapConfig(@NonNull android.graphics.Bitmap.Config config)Description copied from interface:GifDecoderSets the defaultBitmap.Configto use when decoding frames of a GIF.Valid options are
Bitmap.Config.ARGB_8888andBitmap.Config.RGB_565.Bitmap.Config.ARGB_8888will produce higher quality frames, but will also use 2x the memory ofBitmap.Config.RGB_565.Defaults to
Bitmap.Config.ARGB_8888This value is not a guarantee. For example if set to
Bitmap.Config.RGB_565and the GIF contains transparent pixels,Bitmap.Config.ARGB_8888will be used anyway to support the transparency.- Specified by:
setDefaultBitmapConfigin interfaceGifDecoder
-
-