Working with GIF images in Windows Phone

As you know, Silverlight does not have native support for decoding gif images. That is downer, but not a showstopper since several 3rd party, opensource libraries for decoding Gifs. GifCompare

The Twitter for Windows Phone team used the ImageTools project from Codeplex to decode gifs.   It worked well, but when we implemented there were two things that bothered me a little:

  1. The decoder was not perfect.  In our experience, it does ~90% of the images we tested  [and that was thousands of images, so representative test]
  2. There was one infinite loop that bothered me.  Has to do with nextflag during decoding. The Decoder checks for flags to be != 0, and for some odd reason we have seen gifs with nextFlag = –1; this can send your app into an infinite loop.

I knew there were other libraries and partners were using these (for example,  the GifDecoder library shared a long while ago by Joe Stegman).
Today, I finally compared these two libs side-by-side.  I used ~400 images.  25% came from a list of Gifs that had known issues. The other 75% was a random selection by looking at a Twitter user, and downloading his followers and filtering for the ones that used gifs.

The results:   
Decoding wise, these two libraries are pretty much the same code.  I should have started by comparing that before I wrote my test.  Instead, I found it by accident as I dived in to fix the infinite loop above.
The results are pretty inline with our original ~90% assessment.  My tests are a bit lower but again it is because I spiked the test.

The codeplex library is getting updated a lot more, and is growing and getting fixes, so that is what I am sticking with for my projects.
They have done a lot of work on their AnimatedImage control (which supports animated gifs, looks pretty neat (though I have to say animated gifs feels like going back in time).

If you need to decode Gifs, I do recommend you use this library. It is better than nothing.   Please do make sure you:

  1. Download the latest from codeplex. It is getting updated often.
  2. Test it for infinite loops.  You can download the source for my comparison app. I am attaching it to this post.  Or you can just check the source for the library.  The current issue is at ImageTools.IO.Gif.GifDecoder.Decode ()  their loop checks for  while nextFlag != 0 ….   and I simply tweaked it to  while nextFlag > 0 …    I have no idea what my changed does to GIF decoding since I know nothing about the format, but I know that I can get  ~90% of my Gifs to decode and I don’t have the infinite loop, so it works for me..

I am sure the library will improve over time,  I pinged the folks with my tests and my results in case it helps improve. 
Source for my little test, and source for the libraries I used (with small changes) are in skydrive

Happy Windows Phone coding!