How encoding affects GIF quality?
This is an article to get a deeper understanding of the different encoding techniques used in the Android NDK GIF Library by Waynejo.
The android-NDK-GIF library offers four encoding types, and it is visible how GIF quality differs from one to the other. The so-called GIF quality is perceived as granularity in the image, with little outstanding dots everywhere. And it is related to the color palette, i.e., the range of colors that the encoded pixels can have.
The color palette can be the same in every image in a GIF, in which case, it is called Global Color Table (GCT). Or, the color palette can be specific to each image in GIF, then, it will be called Local Color Table (LCT). These are important concepts to understand the encoding types that the library offers:
- ENCODING TYPE SIMPLE FAST
- ENCODING TYPE FAST
- ENCODING TYPE NORMAL LOW MEMORY
- ENCODING TYPE STABLE HIGH MEMORY
The ENCODING TYPE SIMPLE FAST is the one that presents more granularity. It differs from other encoding types because it uses a GCT that is not considering the actual pixels’ colors found in images. Its GCT is simply a common table with colors R, G, and B ranging until a max value defined in code.
The color table is used to translate the colors of the original frame to the colors in the palette. The frame with the new colors is the one to be encoded in GIF. Usually, the algorithm compares the color of the pixel in an original frame to the colors in the palette and picks the closest color to apply in the encoded image. In the ENCODING TYPE SIMPLE FAST, instead of computing the closest color in the palette, it executes a mapping to get the new color of a pixel.
In short, when the Color Table considers the original colors existing in the image, it will lead to smoother results because the new colors are closest to the original colors. Otherwise, the reduction of colors will lead to a granular appearance in regions of smooth color gradient changes.
The ENCODING TYPE STABLE HIGH MEMORY stores all images in a vector, which will consume high memory. Then it will compute a GCT using information of all images and encode the images all at once at the end of the encoding process. Therefore, it is the slowest option.
The ENCODING TYPE NORMAL LOW MEMORY computes an LCT to every image, which saves memory but it takes more time to encode each frame.
The ENCODING TYPE FAST is similar to the NORMAL LOW MEMORY because it computes an LCT to every image, but it applies concurrency to save time.
BONUS: Probably, scaling images affects the GIF quality for the same reason. Scaling will throw away pixels and their color information, so, when it comes to computing the color table, it will have fewer data to consider.
Finally, like many things in Engineering, picking the best option is a matter of trade-offs and what solution applies better to your problem.