Optimizing GIF animations

Log-in or register.

Optimizing GIF animations

Published by on September 13th 2011.

GIF is a sneaky ancient file format that should not exist. It is a mix of good and bad bits, thoroughly shaken and maybe even stirred. After more than 20 years there still is not a viable replacement. GIF is here to stay for at least a few years. And so I spent the last few days optimizing the GIF codec...

GIF and its competitors

Why the world still cares about GIF? Web animations. That is the reason, why GIF survived so long. PNG eventually overtook GIFs position as an image format for static drawings on the web, but GIF still had animations and no format took that position away from GIF.

Sure, we have APNG, MNG or WEBM, but none of them is supported by all major browsers. It looks like the web browsers are actively trying not to move things forward. APNG, my favorite (due to its simplicity), was introduced by Firefox and is supported by Opera. Google Chrome actively ignores APNG and pushes their own WEBP format instead. It is good for video but unsuitable for small animations. And Internet Explorer has no ambitions to innovate in this field. I see no light at the end of the tunnel. GIF is immortal.

There are patent issues in GIFs past and it is a proprietary format. There never was a freely available reference implementation. Every application developer, who wants to support GIFs, has to reinvent the wheel again. Another reason why there is no reference implementation of GIF encoder is the difficulty involved in creating an "optimal" one. There are too many options and some peculiar limitations.

GIF saving in RealWorld tools

Since the new RW Paint will support animations, it would be a shame if it did not have a decent GIF encoder. A decent GIF encoder should produce reasonably small files while maintaining good visual quality.

GIF supports a maximum of 256 colors per frame and no smooth transparency. The GIF encoder must decide how to pick the right colors and how many there should be. The colors may be shared by all frames or every frame may have its private 256 colors. Eventually, some frames may share colors, while others are using private colors. Sharing colors may save space, but in some cases it wastes space.

If one frame in a GIF animation shares some pixels with the next one, these duplicate pixels may be deleted, which saves space. Sometimes. In some special cases, it may cost additional space.

The biggest problem with GIF optimization is the fact that the optimization strategies influence each other. Sometimes wasting a few bytes on one frame will result in big saves later. Of course, there is not enough time to try all the possible combinations.

3 kinds of GIF encoders

  • "Lame" GIF encoders give up and do not care about file sizes or quality. This is not an option for RW.
  • "You're the boss" GIF encoders present the user with a lot of incomprehensible options. It is then user's fault if they choose wrong (and there are no reasonable defaults!). This is wrong, because computers should make life easier for the end users, not harder.
  • "Smart" GIF encoders try to make educated guesses and employ optimization techniques without bothering the users with unnecessary details.

The new encoder tries to fit in the smart category. The optimization is transparent to the users, they do not need to care about it and it gives decent results.

Example

Because the the upcoming version of the cursor maker would allow you to convert animated cursors to gifs, one of the priorities of the encoder was to produce good results for small pictures with lots of frames. Here is a comparison of a cursor converted to .gif:

rsrc/ICQ-background-RW.gif image
RealWorld (1.9kB)
rsrc/ICQ-background-anituner.gif image
AniTuner (5.9kB)
rsrc/ICQ-background-fixed.gif image
older AniTuner (4.5 kB)

The difference between optimized and not optimized could be large in some cases.

I am relatively happy with the new codec. I did some tests with various gifs collected on the web and tried to re-encode them. The results were not bad. Sometimes the resulting files were larger by a few bytes, but mostly the produced files were smaller. Maybe I'll find the time needed for a thorough test.

How to get the updated GIF codec?

It will be included in the new version of RW Paint. I took this opportunity to test the automatic updates system in the beta version. If you are using the beta version, you should get the new codec within the next 5 days (that's how often it checks for updates). If everything works, it will just automatically update without disturbing you.

Recent comments

user icon JDDellGuy contributing user on September 15th 2011

The RW one looks better than either of the other two. It's not extremely noticable in the "new" AniTuner, but if you look closely, the shadow is slightly better.

user icon Anonymous on September 20th 2011

Since you have APNG versions of the cursors, you might also compare it against apng2gif tool.

user icon Anonymous
I wish there were...
Select background
What about ICL files?
Vista & Win 7 icons