public class AnimatedGifEncoder
extends java.lang.Object
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.
Modifier and Type | Field and Description |
---|---|
protected boolean |
autotransparent
autotransparency flag
|
protected boolean |
closeStream
closeStream flag
|
protected int |
colorDepth
number of bit planes
|
protected byte[] |
colorTab
RGB palette
|
protected int |
delay
frame delay
|
protected int |
dispose
disposal code
|
protected boolean |
firstFrame
first frame flag
|
protected byte[] |
gct
global color table
|
protected int |
GCTbl
blue color
|
protected int |
GCTcindex
global color table index
|
protected boolean |
GCTextracted
flag for extracting global color table
|
protected int |
GCTgrn
green color
|
protected boolean |
GCTloadedExternal
flag for global color table from external image
|
protected boolean |
GCToverideColor
gct overide color flag
|
protected boolean |
GCToverideIndex
gct overide index flag
|
protected int |
GCTred
red color
|
protected boolean |
GCTsetTransparent
gct transparency flag
|
protected boolean |
gctused
use global color table flag
|
protected int |
height
image height
|
protected ij.ImagePlus |
image
current frame
|
protected byte[] |
indexedPixels
converted frame indexed to palette
|
protected int |
lctSize
local color table size
|
protected java.io.OutputStream |
out
output stream
|
protected byte[] |
pixels
BGR byte array from frame
|
protected int |
repeat
repeat factor
|
static int |
REPEAT_FOREVER
Repeat forever flag
|
protected int |
sample
default sample interval
|
protected boolean |
sizeSet
set size flag
|
protected boolean |
started
started flag
|
protected int |
transIndex
transparency index
|
protected boolean |
transparent
transparent flag
|
protected int |
width
image width
|
Constructor and Description |
---|
AnimatedGifEncoder() |
Modifier and Type | Method and Description |
---|---|
boolean |
addFrame(ij.ImagePlus image)
Adds next GIF frame.
|
boolean |
addFrame(ij.ImagePlus image,
int theDelay)
Adds next GIF frame.
|
protected void |
analyzePixels()
Analyzes image colors and creates color map.
|
static void |
createGif(java.io.OutputStream outputStream,
java.awt.Image image)
Create a GIF
|
static void |
createGif(java.lang.String filename,
java.awt.Image image)
Create a GIF
|
static void |
createGif(java.lang.String filename,
java.util.List images)
Create an animated GIF
|
static void |
createGif(java.lang.String filename,
java.util.List images,
int repeat,
int delay)
Create an animated GIF
|
static void |
createGif(java.lang.String filename,
java.util.List images,
int repeat,
int delay,
int endDelay)
Create an animated GIF
|
static void |
createGif(java.lang.String filename,
java.util.List images,
int repeat,
int delay,
int endDelay,
boolean useGlobalTable)
Create an animated GIF
|
void |
extractGCTrgb(ij.ImagePlus image)
Function to extract Global Color Table from RGB ImagePlus
This function has to be called before addFrame
|
protected int |
findClosest(byte[] colorTab,
int r,
int g,
int b)
Returns index of palette color closest to c
|
boolean |
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 images
|
void |
loadGCT8bit(ij.ImagePlus image)
Function to load Global Color Table from 8 bit ImagePlus
This function has to be called before addFrame
|
void |
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 size
|
void |
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 use
|
void |
setoptions()
Set the options
|
void |
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(java.io.OutputStream os)
Initiates GIF file creation on the given stream.
|
boolean |
start(java.lang.String file)
Initiates writing of a GIF file with the specified name.
|
protected void |
writeGraphicCtrlExt(int delay)
Writes Graphic Control Extension
|
protected void |
writeImageDesc()
Writes Image Descriptor
|
protected void |
writeLSD()
Writes Logical Screen Descriptor without global color table
|
protected void |
writeLSDgct()
Writes Logical Screen Descriptor with global color table
|
protected void |
writeNetscapeExt()
Writes Netscape application extension to define
repeat count.
|
protected void |
writePalette()
Writes color table
|
protected void |
writePixels()
Encodes and writes pixel data
|
protected void |
writeShort(int value)
Write 16-bit value to output stream, LSB first
|
protected void |
writeString(java.lang.String s)
Writes string to output stream
|
public static final int REPEAT_FOREVER
protected int width
protected int height
protected boolean transparent
protected int transIndex
protected int repeat
protected int delay
protected boolean started
protected java.io.OutputStream out
protected ij.ImagePlus image
protected byte[] pixels
protected byte[] indexedPixels
protected int colorDepth
protected byte[] colorTab
protected int lctSize
protected int dispose
protected boolean closeStream
protected boolean firstFrame
protected boolean sizeSet
protected int sample
protected byte[] gct
protected boolean gctused
protected boolean autotransparent
protected boolean GCTextracted
protected boolean GCTloadedExternal
protected int GCTred
protected int GCTgrn
protected int GCTbl
protected int GCTcindex
protected boolean GCTsetTransparent
protected boolean GCToverideIndex
protected boolean GCToverideColor
public static void createGif(java.lang.String filename, java.awt.Image image)
filename
- output file nameimage
- the image objectpublic static void createGif(java.io.OutputStream outputStream, java.awt.Image image)
outputStream
- the output streamimage
- the image objectpublic static void createGif(java.lang.String filename, java.util.List images)
filename
- output file nameimages
- list of imagespublic static void createGif(java.lang.String filename, java.util.List images, int repeat, int delay)
filename
- output file nameimages
- list of imagesrepeat
- number of times to repeatdelay
- delay between imagespublic static void createGif(java.lang.String filename, java.util.List images, int repeat, int delay, int endDelay)
filename
- output file nameimages
- list of imagesrepeat
- number of times to repeatdelay
- delay between imagesendDelay
- delay on last imagepublic static void createGif(java.lang.String filename, java.util.List images, int repeat, int delay, int endDelay, boolean useGlobalTable)
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 tablepublic boolean addFrame(ij.ImagePlus image)
finish()
flushes all
frames. If setSize
was not invoked, the size of the
first image is used for all subsequent frames.image
- the image to addpublic boolean addFrame(ij.ImagePlus image, int theDelay)
image
- the image to addtheDelay
- delay in looppublic void setoptions()
public boolean finish()
public void loadGCT8bit(ij.ImagePlus image)
image
- the imagepublic void extractGCTrgb(ij.ImagePlus image)
image
- the imagepublic void loadGCTrgb(ij.ImagePlus image)
image
- the imagepublic void setGCT(boolean flag)
flag
- the gct flagpublic void setDelay(int ms)
ms
- int delay time in millisecondspublic void setDispose(int code)
code
- int disposal code.public void setFrameRate(float fps)
setDelay(1000/fps)
.fps
- float frame rate (frames per second)public void setQuality(int quality)
quality
- int greater than 0.public void GlobalColorTableused(boolean gtu)
gtu
- the gtu flagpublic void setRepeat(int iter)
iter
- int number of iterations.public void setSize(int w, int h)
w
- int frame width.h
- int frame width.public void setTransparent(boolean c)
c
- Color to be treated as transparent on display.public boolean start(java.io.OutputStream os)
os
- OutputStream on which GIF images are written.public boolean start(java.lang.String file)
file
- String containing output file name.public void OverRideQuality(int npixs)
npixs
- the number of pixelsprotected void analyzePixels()
protected int findClosest(byte[] colorTab, int r, int g, int b)
colorTab
- the tabler
- redg
- greenb
- blueprotected void writeGraphicCtrlExt(int delay) throws java.io.IOException
delay
- the delayjava.io.IOException
- problem writingprotected void writeImageDesc() throws java.io.IOException
java.io.IOException
- problem writingprotected void writeLSDgct() throws java.io.IOException
java.io.IOException
- problem writingprotected void writeLSD() throws java.io.IOException
java.io.IOException
- problem writingprotected void writeNetscapeExt() throws java.io.IOException
java.io.IOException
- problem writingprotected void writePalette() throws java.io.IOException
java.io.IOException
- problem writingprotected void writePixels() throws java.io.IOException
java.io.IOException
- problem writingprotected void writeShort(int value) throws java.io.IOException
value
- the valuejava.io.IOException
- problem writingprotected void writeString(java.lang.String s) throws java.io.IOException
s
- the stringjava.io.IOException
- problem writing