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