ucar.unidata.ui
Class AnimatedGifEncoder

java.lang.Object
  extended by ucar.unidata.ui.AnimatedGifEncoder

public class AnimatedGifEncoder
extends Object

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
protected  boolean autotransparent
          _more_
protected  boolean closeStream
          _more_
protected  int colorDepth
          _more_
protected  byte[] colorTab
          _more_
protected  int delay
          _more_
protected  int dispose
          _more_
protected  boolean firstFrame
          _more_
protected  byte[] gct
          _more_
protected  int GCTbl
          _more_
protected  int GCTcindex
          _more_
protected  boolean GCTextracted
          _more_
protected  int GCTgrn
          _more_
protected  boolean GCTloadedExternal
          _more_
protected  boolean GCToverideColor
          _more_
protected  boolean GCToverideIndex
          _more_
protected  int GCTred
          _more_
protected  boolean GCTsetTransparent
          _more_
protected  boolean gctused
          _more_
protected  int height
          _more_
protected  ij.ImagePlus image
          _more_
protected  byte[] indexedPixels
          _more_
protected  int lctSize
          _more_
protected  OutputStream out
          _more_
protected  byte[] pixels
          _more_
protected  int repeat
          _more_
static int REPEAT_FOREVER
          _more_
protected  int sample
          _more_
protected  boolean sizeSet
          _more_
protected  boolean started
          _more_
protected  int transIndex
          _more_
protected  boolean transparent
          _more_
protected  int width
          _more_
 
Constructor Summary
AnimatedGifEncoder()
           
 
Method Summary
 boolean addFrame(ij.ImagePlus image)
          Adds next GIF frame.
 boolean addFrame(ij.ImagePlus image, int theDelay)
          _more_
protected  void analyzePixels()
          Analyzes image colors and creates color map.
static void createGif(OutputStream outputStream, Image image)
          _more_
static void createGif(String filename, Image image)
          _more_
static void createGif(String filename, List images)
          _more_
static void createGif(String filename, List images, int repeat, int delay)
          _more_
 void extractGCTrgb(ij.ImagePlus image)
          _more_
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)
          _more_
 void loadGCTrgb(ij.ImagePlus image)
          _more_
 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()
          _more_
 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(OutputStream os)
          Initiates GIF file creation on the given stream.
 boolean start(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(String s)
          Writes string to output stream
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

REPEAT_FOREVER

public static final int REPEAT_FOREVER
_more_

See Also:
Constant Field Values

width

protected int width
_more_


height

protected int height
_more_


transparent

protected boolean transparent
_more_


transIndex

protected int transIndex
_more_


repeat

protected int repeat
_more_


delay

protected int delay
_more_


started

protected boolean started
_more_


out

protected OutputStream out
_more_


image

protected ij.ImagePlus image
_more_


pixels

protected byte[] pixels
_more_


indexedPixels

protected byte[] indexedPixels
_more_


colorDepth

protected int colorDepth
_more_


colorTab

protected byte[] colorTab
_more_


lctSize

protected int lctSize
_more_


dispose

protected int dispose
_more_


closeStream

protected boolean closeStream
_more_


firstFrame

protected boolean firstFrame
_more_


sizeSet

protected boolean sizeSet
_more_


sample

protected int sample
_more_


gct

protected byte[] gct
_more_


gctused

protected boolean gctused
_more_


autotransparent

protected boolean autotransparent
_more_


GCTextracted

protected boolean GCTextracted
_more_


GCTloadedExternal

protected boolean GCTloadedExternal
_more_


GCTred

protected int GCTred
_more_


GCTgrn

protected int GCTgrn
_more_


GCTbl

protected int GCTbl
_more_


GCTcindex

protected int GCTcindex
_more_


GCTsetTransparent

protected boolean GCTsetTransparent
_more_


GCToverideIndex

protected boolean GCToverideIndex
_more_


GCToverideColor

protected boolean GCToverideColor
_more_

Constructor Detail

AnimatedGifEncoder

public AnimatedGifEncoder()
Method Detail

createGif

public static void createGif(String filename,
                             Image image)
_more_

Parameters:
filename - _more_
image - _more_

createGif

public static void createGif(OutputStream outputStream,
                             Image image)
_more_

Parameters:
outputStream -
image - _more_

createGif

public static void createGif(String filename,
                             List images)
_more_

Parameters:
filename - _more_
images - _more_

createGif

public static void createGif(String filename,
                             List images,
                             int repeat,
                             int delay)
_more_

Parameters:
filename - _more_
images - _more_
repeat - _more_
delay - _more_

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. Invoking finish() flushes all frames. If setSize was not invoked, the size of the first image is used for all subsequent frames.

Parameters:
image - _more_
Returns:
true if successful.

addFrame

public boolean addFrame(ij.ImagePlus image,
                        int theDelay)
_more_

Parameters:
image - _more_
theDelay - _more_
Returns:
_more_

setoptions

public void setoptions()
_more_


finish

public boolean finish()
Flushes any pending data and closes output file. If writing to an OutputStream, the stream is not closed.

Returns:
_more_

loadGCT8bit

public void loadGCT8bit(ij.ImagePlus image)
_more_

Parameters:
image - _more_

extractGCTrgb

public void extractGCTrgb(ij.ImagePlus image)
_more_

Parameters:
image - _more_

loadGCTrgb

public void loadGCTrgb(ij.ImagePlus image)
_more_

Parameters:
image - _more_

setGCT

public void setGCT(boolean flag)
If gct = true then a global color table is use

Parameters:
flag - _more_

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 to setDelay(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 - _more_

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

public boolean start(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(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.

OverRideQuality

public void OverRideQuality(int npixs)
Sets Net sample size depending on image size

Parameters:
npixs - _more_

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 - _more_
r - _more_
g - _more_
b - _more_
Returns:
_more_

writeGraphicCtrlExt

protected void writeGraphicCtrlExt(int delay)
                            throws IOException
Writes Graphic Control Extension

Parameters:
delay - _more_
Throws:
IOException - _more_

writeImageDesc

protected void writeImageDesc()
                       throws IOException
Writes Image Descriptor

Throws:
IOException - _more_

writeLSDgct

protected void writeLSDgct()
                    throws IOException
Writes Logical Screen Descriptor with global color table

Throws:
IOException - _more_

writeLSD

protected void writeLSD()
                 throws IOException
Writes Logical Screen Descriptor without global color table

Throws:
IOException - _more_

writeNetscapeExt

protected void writeNetscapeExt()
                         throws IOException
Writes Netscape application extension to define repeat count.

Throws:
IOException - _more_

writePalette

protected void writePalette()
                     throws IOException
Writes color table

Throws:
IOException - _more_

writePixels

protected void writePixels()
                    throws IOException
Encodes and writes pixel data

Throws:
IOException - _more_

writeShort

protected void writeShort(int value)
                   throws IOException
Write 16-bit value to output stream, LSB first

Parameters:
value - _more_
Throws:
IOException - _more_

writeString

protected void writeString(String s)
                    throws IOException
Writes string to output stream

Parameters:
s - _more_
Throws:
IOException - _more_