November 30, 2004

Building a GIF, byte by byte

As part of an ASP.NET project at work, we had to resize images on the server to dynamically generate thumbnails. Things were going fine until we tried resizing transparent GIFs. The .NET framework would loose the transparency. Unwilling to accept defeat, i decided to look for alternatives to using the System.Drawing namespace. Somehow in the search i decided to just build the GIF myself, byte by byte. I found the GIF89a specification online. It's not exactly a fun read, but i learned exactly what makes up a GIF image file.

I was really fascinated by the LZW compression. It was this compression algorithm that was patented (until recently) that had made creating GIFs an expensive undertaking. I found a good walk though and coded and decoded a couple of strings by hand just to get a feel for it. The way it dynamically builds it's pattern lookup both when you encode and decode the image is actually cool.

The biggest challenge i ran into was working with bytes. Modern programming languages have made it so easy to work with numbers and strings, that it can be hard to get to the underlying bits and bytes that the operating system is actually working with. It was interesting to work with the lower-level operations required to make a well-formed binary file. I wonder if this is the stuff i would have learned in computer science classes at college.

I've now started work on reading GIF files to pick apart their pieces. It's been a fun and interesting project to work on. I hope to have time to clean up the code so i can post it online.

Posted by Matthew at November 30, 2004 11:04 PM
Comments