Class GifExporter

All Implemented Interfaces:
AWTImageExporter

public class GifExporter extends AbstractImageExporter implements AWTImageExporter
An image exporter able to create a gif animation out of frame exported by a renderer. Can work in two modes
  • Export GIF with a regular - user defined - frame rate, in order to deal with high frame rate animations without generating too large GIF files. Frames should be either skipped or repeated if they aren't generated exactly at GIF frame rate.
  • Export GIF with a variable frame rate, in order to deal with variable frame rate animations without skipping or repeating frames.

Export with global frame rate

The interframe delay is globally configured from constructor. Once initialized, the exporter is ready to receive images. It will start counting time as soon as a first image is received via a call to AbstractImageExporter.export(BufferedImage). Following calls to AbstractImageExporter.export(BufferedImage) will arrange a regular image registration : if the second (and following) image is exported too early w.r.t. to the global delay, it will be skipped. If the it arrives too late, the previously submitted image will be used for export. If it arrives more than 2 times the expected delay, it will repeated to fill the gaps. Sizing examples :
  • 10 seconds at 40 FPS (hence 25ms per frame) on 800x600 and no HiDPI lead to approx 26MB.
  • 10 seconds at 10 FPS (hence 100ms per frame) on 800x600 and no HiDPI lead to approx 7MB

Export with frame-wise duration

Here there is no image interpolation. All images are exported and we simply write the inter-frame delay for each frame.

Terminating export

Once the export is finished, one should invoke AbstractImageExporter.terminate(long, java.util.concurrent.TimeUnit) where the parameters indicate how long time you accept to wait before timeout. Indeed, depending on the number and size of exported images, a stack of save tasks may be still be pending.
  • If save completes before timeout, then everything is ok.
  • If timeout is reached before the file is saved, then the file is in an unknown state (maybe readable but at least incomplete).
To get information about the remaining work, call progress() or progressToConsole().

Background color

The exporter supports a background color which can be defined to ensure a translucent object won't be altered by the background color of the gif viewer. Setting this background to the chart's background color is the best choice. Warning : Number of image show that timing is not accurate! 1sec of real time is replayed a bit faster than 1 sec (approx 10%). This may depend on timing to execute the screenshot, hence on the user computer.
Author:
Martin Pernollet
  • Field Details

    • DEFAULT_MAX_FPS

      protected static int DEFAULT_MAX_FPS
    • outputFile

      protected File outputFile
    • encoder

      protected AnimatedGifEncoder encoder
    • backgroundColor

      protected Color backgroundColor
    • timer

      protected TicToc timer
  • Constructor Details

    • GifExporter

      public GifExporter(File outputFile)
    • GifExporter

      public GifExporter(File outputFile, FrameRate outputFrameRate)
  • Method Details

    • doAddFrameByRunnable

      protected void doAddFrameByRunnable(BufferedImage image, boolean isLastImage) throws IOException
      This method does the effective job of adding the image to an encoder. It is invoked by the superclass which will call it inside a dedicated thread to avoid slowing down the caller thread. Although the executor used to invoke a sequence of this method is made of a single thread, visual glitches, showing two images rendered into one have been observed. Making this method synchronized surprisingly solved the issue.
      Specified by:
      doAddFrameByRunnable in class AbstractImageExporter
      Throws:
      IOException
    • doAddFrameByRunnable

      protected void doAddFrameByRunnable(BufferedImage image, int interframeDelay, boolean isLastImage) throws IOException
      Specified by:
      doAddFrameByRunnable in class AbstractImageExporter
      Throws:
      IOException
    • addFrameToEncoder

      protected void addFrameToEncoder(BufferedImage image, boolean isLastImage) throws IOException
      Throws:
      IOException
    • closeOutput

      protected void closeOutput() throws IOException
      Specified by:
      closeOutput in class AbstractImageExporter
      Throws:
      IOException
    • getOutputFile

      public File getOutputFile()
    • progress

      public double progress()
    • progressToConsole

      public void progressToConsole()
    • getBackgroundColor

      public Color getBackgroundColor()
    • setBackgroundColor

      public void setBackgroundColor(Color backgroundColor)
    • getDelay

      public int getDelay()
      Return the delay in milisecond as currently set on the encoder.