Package org.meteoinfo.image
Class AnimatedGifEncoder
- java.lang.Object
-
- org.meteoinfo.image.AnimatedGifEncoder
-
public class AnimatedGifEncoder extends java.lang.ObjectClass AnimatedGifEncoder - Encodes a GIF file consisting of one or more frames.Example: AnimatedGifEncoder e = new AnimatedGifEncoder(); e.start(outputFileName); e.setDelay(1000); // 1 frame per sec e.addFrame(image1); e.addFrame(image2); e.finish();No copyright asserted on the source code of this class. May be used for any purpose, however, refer to the Unisys LZW patent for restrictions on use of the associated LZWEncoder class. Please forward any corrections to kweiner@fmsware.com.
-
-
Field Summary
Fields Modifier and Type Field Description protected booleancloseStreamprotected intcolorDepthprotected byte[]colorTabprotected intdelayprotected intdisposeprotected booleanfirstFrameprotected intheightprotected java.awt.image.BufferedImageimageprotected byte[]indexedPixelsprotected java.io.OutputStreamoutprotected intpalSizeprotected byte[]pixelsprotected intrepeatprotected intsampleprotected booleansizeSetprotected booleanstartedprotected inttransIndexprotected java.awt.Colortransparentprotected boolean[]usedEntryprotected intwidth
-
Constructor Summary
Constructors Constructor Description AnimatedGifEncoder()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanaddFrame(java.awt.image.BufferedImage im)Adds next GIF frame.protected voidanalyzePixels()Analyzes image colors and creates color map.protected intfindClosest(java.awt.Color c)Returns index of palette color closest to cbooleanfinish()Flushes any pending data and closes output file.If writing to an OutputStream, the stream is not closed.protected voidgetImagePixels()Extracts image pixels into byte array "pixels"voidsetDelay(int ms)Sets the delay time between each frame, or changes it for subsequent frames (applies to last frame added).voidsetDispose(int code)Sets the GIF frame disposal code for the last added frame and any subsequent frames.voidsetFrameRate(float fps)Sets frame rate in frames per second.voidsetQuality(int quality)Sets quality of color quantization (conversion of images to the maximum 256 colors allowed by the GIF specification).voidsetRepeat(int iter)Sets the number of times the set of GIF frames should be played.voidsetSize(int w, int h)Sets the GIF frame size.voidsetTransparent(java.awt.Color c)Sets the transparent color for the last added frame and any subsequent frames.booleanstart(java.io.OutputStream os)Initiates GIF file creation on the given stream.booleanstart(java.lang.String file)Initiates writing of a GIF file with the specified name.protected voidwriteGraphicCtrlExt()Writes Graphic Control Extensionprotected voidwriteImageDesc()Writes Image Descriptorprotected voidwriteLSD()Writes Logical Screen Descriptorprotected voidwriteNetscapeExt()Writes Netscape application extension to define repeat count.protected voidwritePalette()Writes color tableprotected voidwritePixels()Encodes and writes pixel dataprotected voidwriteShort(int value)Write 16-bit value to output stream, LSB firstprotected voidwriteString(java.lang.String s)Writes string to output stream
-
-
-
Field Detail
-
width
protected int width
-
height
protected int height
-
transparent
protected java.awt.Color transparent
-
transIndex
protected int transIndex
-
repeat
protected int repeat
-
delay
protected int delay
-
started
protected boolean started
-
out
protected java.io.OutputStream out
-
image
protected java.awt.image.BufferedImage image
-
pixels
protected byte[] pixels
-
indexedPixels
protected byte[] indexedPixels
-
colorDepth
protected int colorDepth
-
colorTab
protected byte[] colorTab
-
usedEntry
protected boolean[] usedEntry
-
palSize
protected int palSize
-
dispose
protected int dispose
-
closeStream
protected boolean closeStream
-
firstFrame
protected boolean firstFrame
-
sizeSet
protected boolean sizeSet
-
sample
protected int sample
-
-
Method Detail
-
setDelay
public void setDelay(int ms)
Sets the delay time between each frame, or changes it for subsequent frames (applies to last frame added).- Parameters:
ms- int delay time in milliseconds
-
setDispose
public void setDispose(int code)
Sets the GIF frame disposal code for the last added frame and any subsequent frames. Default is 0 if no transparent color has been set, otherwise 2.- Parameters:
code- int disposal code.
-
setRepeat
public void setRepeat(int iter)
Sets the number of times the set of GIF frames should be played. Default is 1; 0 means play indefinitely. Must be invoked before the first image is added.- Parameters:
iter- int number of iterations.
-
setTransparent
public void setTransparent(java.awt.Color c)
Sets the transparent color for the last added frame and any subsequent frames. Since all colors are subject to modification in the quantization process, the color in the final palette for each frame closest to the given color becomes the transparent color for that frame. May be set to null to indicate no transparent color.- Parameters:
c- Color to be treated as transparent on display.
-
addFrame
public boolean addFrame(java.awt.image.BufferedImage im)
Adds next GIF frame. The frame is not written immediately, but is actually deferred until the next frame is received so that timing data can be inserted. Invokingfinish()flushes all frames. IfsetSizewas not invoked, the size of the first image is used for all subsequent frames.- Parameters:
im- BufferedImage containing frame to write.- Returns:
- true if successful.
-
finish
public boolean finish()
Flushes any pending data and closes output file.If writing to an OutputStream, the stream is not closed.- Returns:
-
setFrameRate
public void setFrameRate(float fps)
Sets frame rate in frames per second. Equivalent tosetDelay(1000/fps).- Parameters:
fps- float frame rate (frames per second)
-
setQuality
public void setQuality(int quality)
Sets quality of color quantization (conversion of images to the maximum 256 colors allowed by the GIF specification). Lower values (minimum = 1) produce better colors, but slow processing significantly. 10 is the default, and produces good color mapping at reasonable speeds. Values greater than 20 do not yield significant improvements in speed.- Parameters:
quality- int greater than 0.
-
setSize
public void setSize(int w, int h)Sets the GIF frame size. The default size is the size of the first frame added if this method is not invoked.- Parameters:
w- int frame width.h- int frame width.
-
start
public boolean start(java.io.OutputStream os)
Initiates GIF file creation on the given stream. The stream is not closed automatically.- Parameters:
os- OutputStream on which GIF images are written.- Returns:
- false if initial write failed.
-
start
public boolean start(java.lang.String file)
Initiates writing of a GIF file with the specified name.- Parameters:
file- String containing output file name.- Returns:
- false if open or initial write failed.
-
analyzePixels
protected void analyzePixels()
Analyzes image colors and creates color map.
-
findClosest
protected int findClosest(java.awt.Color c)
Returns index of palette color closest to c- Parameters:
c- Color- Returns:
- Index of palette color closest to c
-
getImagePixels
protected void getImagePixels()
Extracts image pixels into byte array "pixels"
-
writeGraphicCtrlExt
protected void writeGraphicCtrlExt() throws java.io.IOExceptionWrites Graphic Control Extension- Throws:
java.io.IOException
-
writeImageDesc
protected void writeImageDesc() throws java.io.IOExceptionWrites Image Descriptor- Throws:
java.io.IOException
-
writeLSD
protected void writeLSD() throws java.io.IOExceptionWrites Logical Screen Descriptor- Throws:
java.io.IOException
-
writeNetscapeExt
protected void writeNetscapeExt() throws java.io.IOExceptionWrites Netscape application extension to define repeat count.- Throws:
java.io.IOException
-
writePalette
protected void writePalette() throws java.io.IOExceptionWrites color table- Throws:
java.io.IOException
-
writePixels
protected void writePixels() throws java.io.IOExceptionEncodes and writes pixel data- Throws:
java.io.IOException
-
writeShort
protected void writeShort(int value) throws java.io.IOExceptionWrite 16-bit value to output stream, LSB first- Parameters:
value- Value- Throws:
java.io.IOException
-
writeString
protected void writeString(java.lang.String s) throws java.io.IOExceptionWrites string to output stream- Throws:
java.io.IOException
-
-