7 minutes read

As you might already know, GIF is a raster file format that provides compression without loss of quality, supports animation, and transparency. The word GIF is an acronym that stands for Graphics Interchange Format. The format became popular because of its wide support and portability between different applications and operating systems. Let's take a closer look at what makes a GIF so universal and its structure. We will also look at its strengths and shortcomings.

Structure of GIF

Currently, there are two versions of GIF: 87a and 89a. The former is the original GIF format while the latter is the newer one. GIF89a has some added features like animation delays, transparent background colors, storage of specific metadata, and others.

The structure of the GIF file in its simplified form consists of a fixed area at the beginning of the file, followed by a variable number of blocks. The fixed area consists of three blocks: Header with the version of GIF, Global screen descriptor which specifies dimensions of images, and Global color table which defines colors used by images. The file ends with an image trailer.

structure of the GIF file

An image block along with other things contains the compressed image data. The GIF encodes pixels with 8 bits maximum and uses the RGB color model and palette. The pixel data within a GIF image is compressed using a process known as LZW. In many respects, GIFs became popular because of the LZW compression algorithm. With this compression, large images could be downloaded more quickly in the past even with slower modems. Let's now look at how this compression algorithm works.

LZW compression algorithm

The Lempel-Ziv-Welch algorithm was created by two people, Abraham Lempel and Jacob Ziv, and later updated by Terry Welch. This is lossless compression which means no data is lost during compression. It is also a dictionary-based algorithm. By that, we mean a compression method that uses a list or dictionary of sequences within the uncompressed data. The longer the sequences are in the dictionary and the more frequently they are repeated in the data, the more effective the compression is.

The central point of the LZW algorithm is to use recurring patterns in the data. The algorithm builds for every message its own dictionary, which maps all the unique sequences in that message. This dictionary is constructed on the fly when encoding and also when decoding. Usually, the indexes in the dictionary from 0 to 255 represent 1-character sequences, and the indexes after 255 represent sequences encountered in the data while encoding and decoding.

The general algorithm for LZW compression resembles the following list:

  1. Prepare the dictionary containing all 1-character sequences.

  2. Find the longest sequence in the dictionary that matches the current input and replace the current input with the dictionary index.

  3. Take the current input together with the next symbol in the input stream and add them to the next row in the dictionary.

  4. Go to Step 2.

The animated GIF below shows the encoding process of the LZW algorithm.

animated GIF showing encoding process of the LZW algorithmDecoding is carried out in a similar way. It takes the input from the compressed data stream and builds the dictionary, the same one again. This beautiful method allows us to decode information without the need of sending a dictionary to the decoder. This helps us save valuable space.

Now let's move on to the benefits of using this image format.

Advantages

You may know, that a GIF can store not only images but also animation. Probably, you also know that small animations and reactions in online messengers almost always use the GIF format.

Next, GIFs are good for pictures with a limited number of colors, and logos. The LZW compression helps in this scenario, as it saves areas having a single color with clearly defined edges.

At last, comes transparency. Image frames can give a so-called transparent background color to one of the indexes. It means that in animated GIFs, pixels with a particular index will take the same color as pixels in the same position from the last image frame.

Next in our schedule, is the shortcomings concerning this image format.

Shortcomings

One drawback is the limit in its toolbox for colors for one image — only 256 colors. This is a drop in the bucket compared to, say, a JPEG image, which can display up to 16 million colors. This limitation was reasonable when this format emerged because most hardware could not display many colors simultaneously. Now it is rarely enough for a good representation of photographs and images with a color gradient.

Another drawback is that GIF does not have an alpha channel for transparency. Some other formats, more advanced in this respect, save pixel transparency in a separate channel — the alpha channel. For example, RGB with an additional alpha channel becomes RGBA, where "A" is the alpha channel. Remember how a GIF is dealing with transparency? It displays the same color for pixels in the same position using a special index. However, this is a less efficient solution when compared to the alpha channel.

Conclusion

Let's recall and summarize the most important points:

  • GIF file consists of Header, Global screen descriptor, and Global color table blocks at the beginning of the file. This is followed by a variable number of image blocks containing compressed image data and ends with the trailer block.

  • Image data compressed with the Lempel-Ziv-Welch algorithm builds a new dictionary for every message when encoding and also when decoding the message.

  • It is suitable for logos and other pictures with a small set of colors. It can store animation and also apply transparency.

  • However, it has limitations to the set of colors for an image and lacks an alpha channel.

6 learners liked this piece of theory. 0 didn't like it. What about you?
Report a typo