package com.spun.util.images;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;

import javax.imageio.ImageIO;

import com.spun.util.logger.SimpleLogger;

import Acme.JPM.Encoders.GifEncoder;

public class ImageWriter
{
  public enum Encoding {
                        JPEG, GIF
  }
  private OutputStream  out   = null;
  private BufferedImage image = null;
  private Encoding      type  = null;
  private static Random rand  = new Random();
  private static int    index = rand.nextInt();
  /***********************************************************************/
  public ImageWriter(BufferedImage image, OutputStream out, Encoding type)
  {
    this.image = image;
    this.out = out;
    this.type = type;
  }
  public static void writeImage(BufferedImage image, OutputStream out, Encoding type)
  {
    new ImageWriter(image, out, type).start();
  }
  public static BufferedImage toBufferedImage(Image image)
  {
    BufferedImage bimage = new BufferedImage(image.getWidth(null), image.getHeight(null),
        BufferedImage.TYPE_INT_RGB);
    Graphics g = bimage.createGraphics();
    g.drawImage(image, 0, 0, null);
    g.dispose();
    return bimage;
  }
  /***********************************************************************/
  public void start()
  {
    try
    {
      switch (type)
      {
        case JPEG :
          encodeJPEG();
          break;
        case GIF :
          encodeGIF();
          break;
      }
    }
    catch (IOException e)
    {
      SimpleLogger.warning(e);
    }
  }
  /***********************************************************************/
  private void encodeJPEG() throws IOException
  {
    ImageIO.write(image, "jpg", out);
  }
  /***********************************************************************/
  private void encodeGIF() throws IOException
  {
    SimpleLogger.variable("making GIF");
    GifEncoder encoder = new GifEncoder(image, out, true);
    encoder.encode();
  }
  /***********************************************************************/
  public static synchronized int getImageId()
  {
    return (index++);
  }
  /***********************************************************************/
}
@@ -35,25 +35,72 @@
 ort 
-java.awt.Graphics
+com.spun.util.logger.SimpleLogger;%0A%0Aimport javax.imageio.ImageIO
 ;%0Aim
@@ -113,21 +113,17 @@
 ava.awt.
-Image
+*
 ;%0Aimport
@@ -210,16 +210,16 @@
 Stream;%0A
+
 import j
@@ -240,120 +240,8 @@
 m;%0A%0A
-import javax.imageio.ImageIO;%0A%0Aimport com.spun.util.logger.SimpleLogger;%0A%0Aimport Acme.JPM.Encoders.GifEncoder;%0A%0A
 publ
@@ -1903,82 +1903,39 @@
     
-GifEncoder encoder = new GifEncoder(image, out, true);%0A    encoder.encode(
+ImageIO.write(image, %22GIF%22, out
 );%0A 

change code to remove awkward dependency on ACME package which is not in Maven central