-
Notifications
You must be signed in to change notification settings - Fork 59
Things to be desired
-
Optimize color palette for animated GIF
Animated GIF may contain a global color map and each frame of it can have it's only local color map. This may increase the file size for the image since each color map could be as large as 728 bytes and it's uncompressed.
An ideal case is to have only one global color map for all the frames in the animated GIF image. But this may not be feasible or may produce undesirable results. For some kinds of animated GIF images, all the frames may actually contain the same colors such as a moving stick man. In that case, it would be perfect to have a global color map. But in other situations, the frames may have significant different colors. Another problem with the single global color map approach is that you may be forced to use the largest bit depth for all the frames. This may actually increase the file size for some frames.
One of the ways to produce a global color map is to quantize/reduce colors taking all the frames as a whole. This is both CPU intensive and memory consuming: thinking about a large animated GIF with 200 frames!
Anyway, we are going to do some testing with a few small GIFs to try to extract a global color map and see the result. We are going to take the "piggie" example which will contain 5 frames, each of dimension 138X108. We first read the frames as BufferedImage and grab the internal data as int array. "ICAFE" uses int array as input to reduce colors to take into account transparency.
The following is the results for the testing:
[I have local color map]:
[I don't have local color map]:
The output files are 13615 bytes and 12847 bytes respectively. The one with no local color maps saves 768 bytes which can be calculated as follows: the color depth is 6 bits which has a color map of 64 entries for each color components. The four local color map we get ride of will therefore save 6434 = 768 bytes exactly!
The above example is just a test, it happens to be the perfect case we talked about above. It only proves that in some cases, color palette optimization is doable.