The Smallest Possible GIF

2005-09-27 00:15 - Programming

Okay, there's something I have to say first when I mention GIF files. There's one correct way to pronounce GIF. It's like the peanut butter. Period. (And before you whine to me, read the "Facts" column on the left, not the "mail" column on the right.) If you hear someone say "gift" and leave off the T when referring to a computer graphics file, hit them. Or, at least correct them. The "g as in graphics" argument is also just foolish. (Is it "s as in stimulated" for laser?)

Phew, that said, I've finally sat down this evening and put some serious effort into finding the smallest possible size for a GIF file to be. (I'd go for PNG, but I know the PNG header is bigger, and ultimately the GIF can be smaller.) Oh, and of course, it has to be valid. I started at a copy of the specs for the Graphics Interchange Format and hacked away for a bit. I also found a mailing list thread on this topic, which linked to some fun info that made a 20 byte file. Oh, except it's not valid and it actually crashes (or, I wager, doesn't load) in browsers. yuck.

I did manage a 29 byte GIF that loaded in Firefox and IE. But, I've read that the smallest possible gif is 31 bytes so I wondered where my two bytes went. I must have missed something. But here's the hex dump of it for posterity's sake at least, perhaps those guys were wrong:

00000000  47 49 46 38 39 61 01 00  01 00 00 00 00 2c 00 00  |GIF89a.......,..|
00000010  00 00 01 00 01 00 00 02  02 4c 01 00 3b           |.........L..;|

Anyway, even if that "works", I quickly realized that draws a black square and I want transparent. White might do, but black won't go. (Perhaps I could make white if I didn't cheat on the image data, and actually knew what I was putting in there...) So I had to add in transparency. Long story short, I had to cheat and just save a GIF from a full image editor. It was 43 bytes:

00000000  47 49 46 38 39 61 01 00  01 00 80 00 00 ff ff ff  |GIF89a..........|
00000010  00 00 00 21 f9 04 01 00  00 00 00 2c 00 00 00 00  |...!.......,....|
00000020  01 00 01 00 00 02 02 44  01 00 3b                 |.......D..;|

Seems like you need both a "graphic control extension" to turn on transparency, and since it has to point at a palette value, you need a "global color table" as well. That lumps on 8 and 6 bytes, respectively. On top of my 29 bytes (hmm, perhaps that is right!) it adds up to -- 43!

There's also this peculiarity:

00000000  47 49 46 38 39 61 01 00  01 00 00 00 00 3b        |GIF89a.......;|

That's just header, descriptor, trailer. According to the spec, those are the only required parts. It's only 14 bytes! I checked, in Firefox, oh it's transparent! But then, disaster, in IE it does not load. Damn MS!


35 bytes, Solid White
2006-09-04 12:01 - arantius

An intrepid fellow byte-pincher contacted me to let me know about his tiny-GIF concoction, weighing in at 35 bytes:

00000000  47 49 46 38 39 61 01 00  01 00 80 00 00 ff ff ff  |GIF89a..........|
00000010  00 00 00 2c 00 00 00 00  01 00 01 00 00 02 02 44  |...,...........D|
00000020  01 00 3b                                          |..;|

Which works in Mozilla and IE, at least, and probably anything else. It's a solid white block, not transparent, but that works in many situations too!

Post a comment:

  If you do not have an account to log in to yet, register your own account. You will not enter any personal info and need not supply an email address.

You may use Markdown syntax in the comment, but no HTML. Hints:

If you are attempting to contact me, ask me a question, etc, please send me a message through the contact form rather than posting a comment here. Thank you. (If you post a comment anyway when it should be a message to me, I'll probably just delete your comment. I don't like clutter.)