I'm making an application which needs to be able to resize animated GIF files at run time. I currently have a GUI working in swing, and have code to resize and display the GIFs. Here is the snippet of code which resizes the gifs.
image = Toolkit.getDefaultToolkit().createImage(directory);
MediaTracker mTracker = new MediaTracker(this);
mTracker.addImage(image,1);
try {
mTracker.waitForID(1);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
int width = image.getWidth(null);
int height = image.getHeight(null);
List<Integer> widthHeight = calcResizedWidthAndHeight(width, height);
image = image.getScaledInstance(widthHeight.get(0), widthHeight.get(1), Image.SCALE_FAST);
getScaledInstance will resize the GIF to the desired resoution, and does so beautifully for the most part. However, it corrupts the GIF about 10 - 20% of the time. My application will need to resize tens of thousands of GIFs, so this is not acceptable. I've tried using every type of scaling algorithm, but all of them corrupt the image some notable percentage of the time. By corrupt, I mean that it either damages the coloring of the gif to the point that it is garbage, or destroys frames entirely and displays some animated mismash of all the frames together. I have not managed to find any correlation between what gifs get corrupted and which don't, though I suspect it has something to do with ones with alpha values and/or also variable frame rates.
Furthermore, I scoured the depths of google to attempt to find a package which will resize animated gifs correctly in java, but to no avail.
I would prefer to do this with a java library, but ultimately my project is not tied to it, and could be ported to another language. I'm could also resize in a different language and then display the resized gif in Java. I'm open to any libraries in any language which are very good for resizing GIFs so long as it can handle any GIF I give it, runs on Linux, and is free.
Performance is also not a huge issue, as each gif will have about 6 seconds of processor time to complete the resizing process.
Thank you!