Package com.luna.common.img.gif
Class GifDecoder
java.lang.Object
com.luna.common.img.gif.GifDecoder
GIF文件解析
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
}
来自:https://github.com/rtyley/animated-gif-lib-for-java
- Author:
- Kevin Weiner, FM Software; LZW decoder adapted from John Cristy's ImageMagick.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected int[]protected intprotected intprotected byte[]protected intprotected intprotected intprotected intprotected ArrayList<com.luna.common.img.gif.GifDecoder.GifFrame>protected int[]protected booleanprotected intprotected intprotected intprotected BufferedImageprotected BufferedInputStreamprotected booleanprotected intprotected intprotected intprotected intprotected intprotected BufferedImageprotected Rectangleprotected int[]protected booleanprotected intprotected intprotected static final intprotected intprotected byte[]protected byte[]protected short[]protected intstatic final intFile read status: Error decoding file (may be partially decoded)static final intFile read status: No errors.static final intFile read status: Unable to open source.protected byte[]protected intprotected booleanprotected int -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidDecodes LZW image data into pixel array.protected booleanerr()Returns true if an error was encountered during reading/decodingintgetDelay(int n) Gets display duration for specified frame.getFrame(int n) Gets the image contents of frame n.intGets the number of frames read from file.Gets image size.getImage()Gets the first (or only) image read.intGets the "Netscape" iteration count, if any.protected voidinit()Initializes or re-initializes readerprotected intread()Reads a single byte from the input stream.intReads GIF image from streamintread(InputStream is) Reads GIF image from streamintReads GIF file from specified file/URL source (URL assumed if name contains ":/" or "file:")protected intReads next variable length block from input.protected int[]readColorTable(int ncolors) Reads color table as 256 RGB integer valuesprotected voidMain file parser.protected voidReads Graphics Control Extension valuesprotected voidReads GIF file header information.protected voidReads next frame imageprotected voidreadLSD()Reads Logical Screen Descriptorprotected voidReads Netscape extenstion to obtain iteration countprotected intReads next 16-bit value, LSB firstprotected voidResets frame state for reading next image.protected voidCreates new frame image from current data (and previous frames as specified by their disposition codes).protected voidskip()Skips variable length blocks up to and including next zero length block.
-
Field Details
-
STATUS_OK
public static final int STATUS_OKFile read status: No errors.- See Also:
-
STATUS_FORMAT_ERROR
public static final int STATUS_FORMAT_ERRORFile read status: Error decoding file (may be partially decoded)- See Also:
-
STATUS_OPEN_ERROR
public static final int STATUS_OPEN_ERRORFile read status: Unable to open source.- See Also:
-
MAX_STACK_SIZE
protected static final int MAX_STACK_SIZE- See Also:
-
in
-
status
protected int status -
width
protected int width -
height
protected int height -
gctFlag
protected boolean gctFlag -
gctSize
protected int gctSize -
loopCount
protected int loopCount -
gct
protected int[] gct -
lct
protected int[] lct -
act
protected int[] act -
bgIndex
protected int bgIndex -
bgColor
protected int bgColor -
lastBgColor
protected int lastBgColor -
pixelAspect
protected int pixelAspect -
lctFlag
protected boolean lctFlag -
interlace
protected boolean interlace -
lctSize
protected int lctSize -
ix
protected int ix -
iy
protected int iy -
iw
protected int iw -
ih
protected int ih -
lastRect
-
image
-
lastImage
-
block
protected byte[] block -
blockSize
protected int blockSize -
dispose
protected int dispose -
lastDispose
protected int lastDispose -
transparency
protected boolean transparency -
delay
protected int delay -
transIndex
protected int transIndex -
prefix
protected short[] prefix -
suffix
protected byte[] suffix -
pixelStack
protected byte[] pixelStack -
pixels
protected byte[] pixels -
frames
-
frameCount
protected int frameCount
-
-
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
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
Gets the image contents of frame n.- Parameters:
n- frame- Returns:
- BufferedImage
-
getFrameSize
Gets image size.- Returns:
- GIF image dimensions
-
read
Reads GIF image from stream- Parameters:
is- BufferedInputStream containing GIF file.- Returns:
- read status code (0 = no errors)
-
read
Reads GIF image from stream- Parameters:
is- InputStream containing GIF file.- Returns:
- read status code (0 = no errors)
-
read
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 was encountered during reading/decoding
-
init
protected void init()Initializes or re-initializes reader -
read
protected int read()Reads a single byte from the input stream.- Returns:
- single byte
-
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:
- next 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.
-