¶Fun with animated GIFs
For no particular reason, I sat down this weekend and wrote animated GIF import/export routines. Animated GIFs aren't that common anymore -- at least, for non-obnoxious uses -- but I don't have any tools to manipulate them and hadn't ever worked with the format. Besides, the last time I'd experimented with LZW compression was in high school, where I got around 10 bytes/sec on a 7MHz 68000. I was pretty sure I could improve on that since I actually know about hash tables this time.
It's now apparent to me why the GIF image format became so popular: it's much simpler and easier to implement than either JPEG or PNG. JPEG is difficult because of the inverse discrete cosine transform (IDCT) and marker stuffing in the bitstream; PNG's Deflate compression uses two nested layers of Huffman trees in addition to sliding window compression. The LZW compression used by GIF, on the other hand, can be implemented in a reasonably performant fashion with about a page of code for the encoder and decoder each. All told, I'd say it only took about a day each to get the GIF decoder and encoders working, which is astonishing when you consider that GIF is still competitive with PNG for some types of images. It helps that the original Compuserve GIF specification is well-written and precise, although the PNG specification is even better in these respects.
It also helps that I cut a bunch of corners in the initial implementations, but it works on the test images so far....
Now, any export code must of course be appropriately tested:
01/15/2007 04:54 PM 170,706,854 SHUFFLE! OP.gif
640x360, 2183 frames, 24 fps. And no, I'm not uploading it.
Internet Explorer did manage to open the 163MB animated GIF, but froze for about a minute at 100% CPU during the load. It then only managed to play the video at about 8 fps and only up to about a quarter of it. That's disappointing, but reasonable.
Mozilla Firefox also had difficulty opening the GIF file. A lot of difficulty, in fact:
(FYI, my laptop has 1.2GB of memory.)
Amazingly enough, although it swapped a lot, it did manage to play the entire video, at full frame rate, and without crashing. So, if you want to play your favorite anime openings in a horrendously inefficiently encoded fashion and in 256 color glory, Firefox is clearly better than Internet Explorer. I might try writing an animated GIF larger than 4GB next, just to see what happens.