how to save a GIF without losing transparency?

hi everyone,

does anyone know how to load a gif file, scale it and the save the result as a new gif without losing transparency?? im using an encoder called Gif89Encoder. so far i have only tried to load a gif and the save the image as a new file, the result looks just like the original but without transparency :’(

here is a small example


//test method
public static void main(String args[]) {
      Image image = null;
      try {
            image = loadImage("input.gif");
      } catch(Exception ex) {
            //...
      }
      
      if(image != null) {
            ColorModel cm = getColorModel(image);
            IndexColorModel icm = null;
            int transindex = -1;
            if(cm instanceof IndexColorModel) {
                  icm = (IndexColorModel)cm;
                  transindex = icm.getTransparentPixel(); // image in this test had transindex = 97 
            }
            try {
                  BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("out.gif"));
                  Gif89Encoder gifenc = new Gif89Encoder(image);
                  gifenc.setTransparentIndex(transindex);
                  gifenc.getFrameAt(0).setInterlaced(false);
                  gifenc.encode(out);
            } catch(Exception e) {
                  //...
            }
      }
}

//loading the original gif image
private Image loadImage(String path) throws FileNotFoundException, InterruptedException {
      File f = new File(path);
      if(!f.exists()) {
            throw new FileNotFoundException();
      }
      f = null;
            
      Image image = null;
      try {
            image = Toolkit.getDefaultToolkit().getImage(path);
            MediaTracker tracker = new MediaTracker(new Frame());
            tracker.addImage(image, 0);
            tracker.waitForID(0);
                  
            if (tracker.isErrorAny()) {
                  //...
            }
                  
            return image;
      } catch(InterruptedException ex) {
            throw new InterruptedException();
      }
}

//get colormodel from the original image
private ColorModel getColorModel(Image image) {
      PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);
      try {
            pg.grabPixels();
      } catch (InterruptedException ex) {
            ex.printStackTrace(System.out);
      }
      return pg.getColorModel();
}

if anyone can help, maybe an example or advice, i would really appreciate it!!!

best regards,
javamunky

We had similar problems with gif encoding (loosing transparency, bad quality output images, “more than 256 colors” exception) but just some days ago we found the new gif animation & encoding library Gif4J (http://www.gif4j.com). It’s commercial but very powerful, really easy to use and solves all our problems ;D

The JAI Image I/O Tools release 1.0_01 includes a GIFImageWriter, so you may want to give that a try:
http://java.sun.com/developer/earlyAccess/jai_imageio/index.html

Unless you’re bound to JDK 1.1, you’d be much better off using more modern approaches (e.g. BufferedImage instead of PixelGrabber/MediaTracker/etc). For example (once you’ve installed the above libraries):
BufferedImage img = ImageIO.read(“input.gif”);
// do stuff with img
ImageIO.write(img, “gif”, new File(“out.gif”));

It’s just that simple, and should have no difficulties preserving transparency.

Hope this helps,
Chris

[quote]The JAI Image I/O Tools release 1.0_01 includes a GIFImageWriter, so you may want to give that a try:
http://java.sun.com/developer/earlyAccess/jai_imageio/index.html

Unless you’re bound to JDK 1.1, you’d be much better off using more modern approaches (e.g. BufferedImage instead of PixelGrabber/MediaTracker/etc). For example (once you’ve installed the above libraries):
BufferedImage img = ImageIO.read(“input.gif”);
// do stuff with img
ImageIO.write(img, “gif”, new File(“out.gif”));

It’s just that simple, and should have no difficulties preserving transparency.

Hope this helps,
Chris
[/quote]
Yes, but to use JAI you should download and install it anywhere you plan to deploy your solutions…
And Gif4J also supports ImageIO…

hi,

thanks for all replies, appreciate it! i tried a few different encoders and with some changes i got it working with the acme gifencoder.

cheers,
javamunky