Package ucar.unidata.ui
Class AnimatedGifEncoder
java.lang.Object
ucar.unidata.ui.AnimatedGifEncoder
Class AnimatedGifEncoder - Encodes a GIF file consisting of one or
more frames.
Extensively Modified for ImagePlus Extended to handle 8 bit Images with more complex Color lookup tables with transparency index Ryan Raz March 2002 raz@rraz.ca Version 1.01 Extensively Modified for ImagePlus Extended to handle 8 bit Images with more complex Color lookup tables with transparency index Ryan Raz March 2002 ryan@rraz.ca Version 1.01 Please report any bugs Operation Manual 1) Load stack with 8 bit or RGB images it is possible to use the animated gif reader but because the color table is lost it is best to also load a separate copy of the first image in the series this will allow extraction of the original image color look up table (see 1below) 2)Check the option list to bring up the option list. 3)Experiment with the option list. I usually use a global color table to save space, set to do not dispose if each consecutive image is overlayed on the previous image. 4)Color table can be imported from another image or extracted from 8bit stack images or loaded as the first 256 RGB triplets from a RGB images, the last mode takes either a imported image or current stack and creates the color table from scratch. To do list 1) Modify existing Animated Gif reader plug in to import in 8 bit mode (currently only works in RGB mode. Right now the best way to alter an animated gif is to save the first image separately and then read the single gif and use the plugin animated reader to read the animated gif to the stack. Let this plugin encode the stack using the single gif's color table. 2) Add support for background colors easy but I have no use for them 3) RGB to 8 bit converter is a linear search. Needs to be replaced with sorted list and fast search. But this update could cause problems with some types of gifs. Easy fix get a faster computer. 4) Try updating NN color converter seems to be heavily weighted towards quantity of pixels. example: if there is 90% of the image covered in shades of one color or grey the 10% of other colors tend to be poorly represented it over fits the shades and under fits the others. Works well if the distribution is balanced. 5) Add support for all sizes of Color Look Up tables. 6) Re-code to be cleaner. This is my second Java program and I started with some code with too many global variables and I added more switches so its a bit hard to follow. Credits for the base conversion codes 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.
- Version:
- 1.0 December 2000 Example: AnimatedGifEncoder e = new AnimatedGifEncoder(); e.start(outputFileName); e.addFrame(image1); e.addFrame(image2); " " " e.finish();
- Author:
- Kevin Weiner, FM Software
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected boolean
autotransparency flagprotected boolean
closeStream flagprotected int
number of bit planesprotected byte[]
RGB paletteprotected int
frame delayprotected int
disposal codeprotected boolean
first frame flagprotected byte[]
global color tableprotected int
blue colorprotected int
global color table indexprotected boolean
flag for extracting global color tableprotected int
green colorprotected boolean
flag for global color table from external imageprotected boolean
gct overide color flagprotected boolean
gct overide index flagprotected int
red colorprotected boolean
gct transparency flagprotected boolean
use global color table flagprotected int
image heightprotected ij.ImagePlus
current frameprotected byte[]
converted frame indexed to paletteprotected int
local color table sizeprotected OutputStream
output streamprotected byte[]
BGR byte array from frameprotected int
repeat factorstatic final int
Repeat forever flagprotected int
default sample intervalprotected boolean
set size flagprotected boolean
started flagprotected int
transparency indexprotected boolean
transparent flagprotected int
image width -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionboolean
addFrame
(ij.ImagePlus image) Adds next GIF frame.boolean
addFrame
(ij.ImagePlus image, int theDelay) Adds next GIF frame.protected void
Analyzes image colors and creates color map.static void
createGif
(OutputStream outputStream, Image image) Create a GIFstatic void
Create a GIFstatic void
Create an animated GIFstatic void
Create an animated GIFstatic void
Create an animated GIFstatic void
createGif
(String filename, List images, int repeat, int delay, int endDelay, boolean useGlobalTable) Create an animated GIFvoid
extractGCTrgb
(ij.ImagePlus image) Function to extract Global Color Table from RGB ImagePlus This function has to be called before addFrameprotected int
findClosest
(byte[] colorTab, int r, int g, int b) Returns index of palette color closest to cboolean
finish()
Flushes any pending data and closes output file.void
GlobalColorTableused
(boolean gtu) Set True for Global Color Table use This saves space in the output file but colors may not be so goodif the stack uses True color imagesvoid
loadGCT8bit
(ij.ImagePlus image) Function to load Global Color Table from 8 bit ImagePlus This function has to be called before addFramevoid
loadGCTrgb
(ij.ImagePlus image) Function to use the first up to 255 elements of a RGB ImagePlus to construct a global color table.void
OverRideQuality
(int npixs) Sets Net sample size depending on image sizevoid
setDelay
(int ms) Sets the delay time between each frame, or changes it for subsequent frames (applies to last frame added).void
setDispose
(int code) Sets the GIF frame disposal code for the last added frame and any subsequent frames.void
setFrameRate
(float fps) Sets frame rate in frames per second.void
setGCT
(boolean flag) If gct = true then a global color table is usevoid
Set the optionsvoid
setQuality
(int quality) Sets quality of color quantization (conversion of images to the maximum 256 colors allowed by the GIF specification).void
setRepeat
(int iter) Sets the number of times the set of GIF frames should be played.void
setSize
(int w, int h) Sets the GIF frame size.void
setTransparent
(boolean c) Sets the transparent color for the last added frame and any subsequent frames.boolean
start
(OutputStream os) Initiates GIF file creation on the given stream.boolean
Initiates writing of a GIF file with the specified name.protected void
writeGraphicCtrlExt
(int delay) Writes Graphic Control Extensionprotected void
Writes Image Descriptorprotected void
writeLSD()
Writes Logical Screen Descriptor without global color tableprotected void
Writes Logical Screen Descriptor with global color tableprotected void
Writes Netscape application extension to define repeat count.protected void
Writes color tableprotected void
Encodes and writes pixel dataprotected void
writeShort
(int value) Write 16-bit value to output stream, LSB firstprotected void
Writes string to output stream
-
Field Details
-
REPEAT_FOREVER
public static final int REPEAT_FOREVERRepeat forever flag- See Also:
-
width
protected int widthimage width -
height
protected int heightimage height -
transparent
protected boolean transparenttransparent flag -
transIndex
protected int transIndextransparency index -
repeat
protected int repeatrepeat factor -
delay
protected int delayframe delay -
started
protected boolean startedstarted flag -
out
output stream -
image
protected ij.ImagePlus imagecurrent frame -
pixels
protected byte[] pixelsBGR byte array from frame -
indexedPixels
protected byte[] indexedPixelsconverted frame indexed to palette -
colorDepth
protected int colorDepthnumber of bit planes -
colorTab
protected byte[] colorTabRGB palette -
lctSize
protected int lctSizelocal color table size -
dispose
protected int disposedisposal code -
closeStream
protected boolean closeStreamcloseStream flag -
firstFrame
protected boolean firstFramefirst frame flag -
sizeSet
protected boolean sizeSetset size flag -
sample
protected int sampledefault sample interval -
gct
protected byte[] gctglobal color table -
gctused
protected boolean gctuseduse global color table flag -
autotransparent
protected boolean autotransparentautotransparency flag -
GCTextracted
protected boolean GCTextractedflag for extracting global color table -
GCTloadedExternal
protected boolean GCTloadedExternalflag for global color table from external image -
GCTred
protected int GCTredred color -
GCTgrn
protected int GCTgrngreen color -
GCTbl
protected int GCTblblue color -
GCTcindex
protected int GCTcindexglobal color table index -
GCTsetTransparent
protected boolean GCTsetTransparentgct transparency flag -
GCToverideIndex
protected boolean GCToverideIndexgct overide index flag -
GCToverideColor
protected boolean GCToverideColorgct overide color flag
-
-
Constructor Details
-
AnimatedGifEncoder
public AnimatedGifEncoder()
-
-
Method Details
-
createGif
Create a GIF- Parameters:
filename
- output file nameimage
- the image object
-
createGif
Create a GIF- Parameters:
outputStream
- the output streamimage
- the image object
-
createGif
Create an animated GIF- Parameters:
filename
- output file nameimages
- list of images
-
createGif
Create an animated GIF- Parameters:
filename
- output file nameimages
- list of imagesrepeat
- number of times to repeatdelay
- delay between images
-
createGif
Create an animated GIF- Parameters:
filename
- output file nameimages
- list of imagesrepeat
- number of times to repeatdelay
- delay between imagesendDelay
- delay on last image
-
createGif
public static void createGif(String filename, List images, int repeat, int delay, int endDelay, boolean useGlobalTable) Create an animated GIF- Parameters:
filename
- output file nameimages
- list of imagesrepeat
- number of times to repeatdelay
- delay between imagesendDelay
- delay on last imageuseGlobalTable
- true to use a global color table
-
addFrame
public boolean addFrame(ij.ImagePlus image) 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. IfsetSize
was not invoked, the size of the first image is used for all subsequent frames.- Parameters:
image
- the image to add- Returns:
- true if successful.
-
addFrame
public boolean addFrame(ij.ImagePlus image, int theDelay) Adds next GIF frame.- Parameters:
image
- the image to addtheDelay
- delay in loop- Returns:
- true if successful.
-
setoptions
public void setoptions()Set the options -
finish
public boolean finish()Flushes any pending data and closes output file. If writing to an OutputStream, the stream is not closed.- Returns:
- true if successful
-
loadGCT8bit
public void loadGCT8bit(ij.ImagePlus image) Function to load Global Color Table from 8 bit ImagePlus This function has to be called before addFrame- Parameters:
image
- the image
-
extractGCTrgb
public void extractGCTrgb(ij.ImagePlus image) Function to extract Global Color Table from RGB ImagePlus This function has to be called before addFrame- Parameters:
image
- the image
-
loadGCTrgb
public void loadGCTrgb(ij.ImagePlus image) Function to use the first up to 255 elements of a RGB ImagePlus to construct a global color table. This function has to be called before addFrame- Parameters:
image
- the image
-
setGCT
public void setGCT(boolean flag) If gct = true then a global color table is use- Parameters:
flag
- the gct flag
-
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.
-
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.
-
GlobalColorTableused
public void GlobalColorTableused(boolean gtu) Set True for Global Color Table use This saves space in the output file but colors may not be so goodif the stack uses True color images- Parameters:
gtu
- the gtu flag
-
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.
-
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.
-
setTransparent
public void setTransparent(boolean 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.
-
start
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
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.
-
OverRideQuality
public void OverRideQuality(int npixs) Sets Net sample size depending on image size- Parameters:
npixs
- the number of pixels
-
analyzePixels
protected void analyzePixels()Analyzes image colors and creates color map. -
findClosest
protected int findClosest(byte[] colorTab, int r, int g, int b) Returns index of palette color closest to c- Parameters:
colorTab
- the tabler
- redg
- greenb
- blue- Returns:
- the index
-
writeGraphicCtrlExt
Writes Graphic Control Extension- Parameters:
delay
- the delay- Throws:
IOException
- problem writing
-
writeImageDesc
Writes Image Descriptor- Throws:
IOException
- problem writing
-
writeLSDgct
Writes Logical Screen Descriptor with global color table- Throws:
IOException
- problem writing
-
writeLSD
Writes Logical Screen Descriptor without global color table- Throws:
IOException
- problem writing
-
writeNetscapeExt
Writes Netscape application extension to define repeat count.- Throws:
IOException
- problem writing
-
writePalette
Writes color table- Throws:
IOException
- problem writing
-
writePixels
Encodes and writes pixel data- Throws:
IOException
- problem writing
-
writeShort
Write 16-bit value to output stream, LSB first- Parameters:
value
- the value- Throws:
IOException
- problem writing
-
writeString
Writes string to output stream- Parameters:
s
- the string- Throws:
IOException
- problem writing
-