animated-gif-lib-for-java
animated-gif-lib-for-java copied to clipboard
Fix "restore to previous"
The disposal method "restore to previous" should restore to what was there before the graphic is drawn, so to the image data after the disposal of the previous frame, not to the previous frame. This disposal method isn't used very often, but I've encountered a few GIFs where it is an issue. A few examples are on this page.
The GIF Spec says:
3 - Restore to previous. The decoder is required to restore the area overwritten by the graphic with what was there prior to rendering the graphic.
Which I didn't quite know how to interpret exactly (and there do seem to be different interpretations on some websites that explain how it's supposed to work), but based on the GIFs I encountered and how modern browsers handle them, this fix should be correct.
Example
Following are screenshots of the individual frames of this GIF (top row the finished frame, bottom row what each frame adds). In this case, the GIF uses "restore to previous" to fully clear each frame (which works because each frame uses it, so it always restores to the empty canvas present before drawing the first frame's graphic).
Original:
Fixed:
This change is my own work and while I'm not convinced I can legally disclaim or transfer copyright, you can see this change as being under CC0, so effectively Public Domain.
Hi @tduva thanks for this contribution - it looks great, thanks for taking the time to do this! Could you add a test case to TestGifDecoder
so we can have a unit test for this feature?
Note that depending on the version of Java you're using, you may need to rebase on top of the latest master commit (2a3b078a80) to get the current tests to pass.
I've been busy, but I'll get around to adding a test eventually..