0

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!

Chase
  • 79
  • 4
  • what is reliable and how much money are you paying?? – gpasch Apr 26 '16 at 22:08
  • By reliable, I mean that if a corrupted gif files is not given as input, the output will be reasonable. I'm not paying anything. I suspect there is a library somewhere in some language which is open source for this, or built in to the language. – Chase Apr 26 '16 at 22:10
  • Can you alter [it](http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/awt/Image.java) to suite your need? – trashgod Apr 26 '16 at 22:35
  • 1
    So, the code presented [here](http://stackoverflow.com/questions/36671767/java-swing-gif-partly-transparent-when-its-not-supposed-to-be/36683021#36683021) is used to change the dispose method of gif frames, basically it takes each frame from a gif file, saves it as a `BufferedImage` and then reconstructs the gif file. This could be adapted to also scale each image – MadProgrammer Apr 26 '16 at 22:37
  • @MadProgrammer I'd be inclined to do something similar to that, but I'm not very knowledgeable about the GIF format encoding. I suspect if I were to write my own code to resize a GIF by splitting apart the frames, that I'd likely miss some of the more subtle points of GIF, like that frames can be shown for variable lengths of time. I need to be able to reliably produce an reasonable-looking output for absolutely any GIF which is given to me, in this case my input set comes from scraping thousands of GIFs from imgur. – Chase Apr 26 '16 at 23:13
  • 1
    @Chase It would take a bit work, and the code presented does assume a static time frame, but, it wouldn't be hard to generate the meta data required for each frame, which could then be used to rebuilt the gif – MadProgrammer Apr 26 '16 at 23:15
  • 1
    GIF is not a free file-format so opensource and free is not legally possible. You can try to decode/encode GIF for yourself but if you want to made it public you still need to buy a licence. see [How to find where does Image Block start in GIF images?](http://stackoverflow.com/a/32369298/2521214) – Spektre Apr 27 '16 at 09:53
  • 1
    @Spektre IANAL, but I think the [GIF patents expired some ten years ago](http://www.freesoftwaremagazine.com/articles/gif_now_finally_free)... – Harald K Apr 28 '16 at 08:40
  • @haraldK didnt know that does it cover also the custom LZW ? if yes then it is awesome finaly. – Spektre Apr 28 '16 at 09:09
  • @Spektre Depends on what you mean by "custom LZW"? AFAIK the GIF patent issues was always about the LZW algorithm, which is now expired. – Harald K Apr 28 '16 at 09:33

0 Answers0