This is one of the few bright ideas I've ever had that I didn't see someone else come up with, first.

You see, in Microsoft Visual Basic 6.0, the stock image control will display the first frame of multi-frame, animated gifs only. So, thought I, here's an opportunity: develop a user interface component that can be dropped into a VB program to animate any old gif file you download off the internet. Off I went, looking up the gif filespec. I found some pseudo-code on the lzw compression algorithm, and some articles on the Microsoft knowledge-base on animation in Windows.

And an awful lot of noodling around later, I had succeeded. Fame and fortune would surely follow.

I had sort of a vague notion of offering the thing as shareware, until I learned of the patent encumbrance on the Gif filespec. See the GIF node for more, but, basically, the lzw compression algorithm was covered by a patent that was owned at various times by Compuserve, Unisys, and, I think, others, and any developer of either an encoder or a decoder would have to pay royalties to the patent holder. I actually wrote a letter to Unisys to ask what developing a product using this technology was going to cost me. Their reply was only that I'd have to "execute some type of agreement." Which left the impression that, for a small-time shareware developer, it wouldn't be a large amount, if any. But it still seemed like way more trouble than it was worth.

I understand that the patent expired in 2003, however, and so there's no reason (well, no PATENT related reason, anyway) not to develop with and use the lzw algorithm for the compression and uncompression of gif files.

Of course, the most recent release of the Microsoft Development Environment, Visual Studio .Net, includes Gif Animation natively, and so removes the original rationale behind this project. How utterly, utterly typical of them.

The actual Visual C++ source code for Gif decompression/decoding and animation is given in writeups that are linked below. If compiled, within Microsoft's Visual Studio development environment, these files will produce an executable. On running this executable on a Windows 2000/Windows XP platform, a dialog box opens, with an OK button and a Cancel Button. Clicking OK opens a windows standard "file picker" dialog, in which you can select a gif file for decoding and display. If you select a gif file in the file picker dialog, the selected file is opened, read, decoded, and then animation begins within the main dialog window. The Cancel button closes the application.

The strategy employed by this code is two-phase: decoding and animating.

The decode phase occurs after you select a file in the file-picker dialog. The selected file is opened, decoded, and from it an array of Win32 Device Independent Bitmap structures, along with other supporting information, is built.

When the decode phase completes, animation begins. Associated with the graphics for each frame of animation is a delay value, which is used to set a Win32 Timer a-ticking. When the timer fires, array indices are adjusted to point to the next frame of animation, and a repaint is commanded. During the repaint operation, the code retrieves the bitmap for the current frame and displays it in the main dialog window.

"Release" notes for this project are as follows:

1. It should (but won't) go without saying that this code is provided as-is, and I make no warranty, either express or implied, as to the efficacy, efficiency, or even the safety of this code. You're on your own, mate.

2. I have NOT included the code to turn this module into an ActiveX control. So you can't use this code directly from within Visual Basic. That bit is left as an exercise. And, actually, a google search reveals that, indeed, I wasn't the only one to have this bright idea...ActiveX controls for GIF animation are available.

3. The code as shown heavily uses the MFC class library, which is an object-oriented "wrapper" library for the lower-level Win32 API. There are, however, several places throughout the code where I directly use the Win32 API to do things that MFC can't do, or at least can't do easily. As a by-product of this, I've given an exhibition of how crufty the MFC library is, or has become.

4. The actual implementation is in no way optimized, being a naive, rote expression of the pseudo-code given as documentation for the Gif filespec. With a number of what will no doubt be interpreted as my own personal idiosyncrasies thrown in to boot. That being said, I've had no performance issues on CPU's as old as a 300 MHz Pentium II.

5. You must, I repeat MUST, have a copy of the Gif Filespec in hand if/as you try to analyze this code. Especially in routine CGif::ReadFile(), you can see where I read from the input filestream directly into data structures that correspond exactly with the different types of blocks and sub-blocks defined in the filespec. See:

http://www.dcs.ed.ac.uk/home/mxr/gfx/2d/GIF89a.txt ...but also many other places.

6. I've tested this with as many and varied a set of Gif files gotten from the wild as I could, but by no means can I guarantee that this code will correctly decode all of them. In fact, there are a couple of places, which I've denoted in comments, where I've kludged the code to accommodate GIF files that don't seem to be strictly compliant with the filespec as I read it. I suspect it's a similar situation to the HTML picture, where there are myriads of web pages that aren't strictly compliant with the HTML spec, but browsers are written to accommodate them and display them anyway. There are a myriad of GIF creation tools available, and information about them is often embedded within comment blocks in the Gif files they create.

7. I heavily use the Win32 TransparentBlt() function to properly handle transparent GIFs, i.e., those where you can see the background color THROUGH the image as it is drawn. The TransparentBlt() API, though, is NOT supported in Win 9X. To blit transperently in Windows 95, 98, or ME requires a whole series of Blit operations involving abstruse raster operations and a separate mask bitmap, which also had to be created from the input stream. I wrote code to do this, but it made me shudder just to look at it, so I've deleted it from the code that is given here. Let's all just say a hearty good-riddance to Windows 9X, shall we?

8. I absolutely, categorically, do NOT undertake to modify or in any way support this code in response to requests from anyone who may try to use it. If you have problems getting it to work, gut it out and fix 'em. It's what I had to do, and it'll be good for your character. All that being said, do /msg me if you build this project and get it to work.

The wus containing the source are:

Gif Animation: Class CGif: The main decoder and data structure class.

Gif Animation: Helper Classes Supporting objects for file IO and the retrieval of code values for the lzw algorithm.

Gif Animation: The Display Client This is the MFC Dialog class that controls everything and actually draws the frames within the GIF so as to achieve animation.

Y'know, if you log in, you can write something here, or contact authors directly on the site. Create a New User if you don't already have an account.