Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use kCGImagePropertyGIFImageColorMap or create a color table?

I'm trying to tinker with a couple GIF properties such as kCGImagePropertyGIFImageColorMap and kCGImagePropertyGIFHasGlobalColorMap (reference) in Core Graphics.

I'm creating animated GIFs and I'd like to see if I can improve the quality by playing with the amount of colors. I've only found one other code sample and I'm not sure it works.

I've tried using a couple sample codes linked above, but it doesn't seem to work. Nor does setting the kCGImagePropertyGIFHasGlobalColorMap seem to do anything. Thanks

like image 955
user339946 Avatar asked Jun 09 '13 20:06

user339946


1 Answers

Core Graphics does not allow for the setting of a global color table, just a local color table for a single-image GIF file. Multi-image gif files require individual properties of each image to be set, which means the kCGImagePropertyGIFImageColorMap will have no effect when the source images are not themselves GIF files, and the code in the linked gist is wrong. Instead of trying to set a global color map, set the properties each one of the images you're trying to string together, which can be manipulated with Core Graphics by using an image context or by setting the properties of the image when you add them to the image destination ref.

If you're still wondering about GIF color tables, they're explained better than I ever could by the giflib library, which would probably be a much better avenue than Core Graphics for generating a gif and manipulating its color table. If you're going the Core Graphics route and still want to know how to instantiate a color table, the general format is as follows:

// Color tables are arrays of 8-bit bytes from 0 (deepest black) to 255 (brightest white) // with each color's intensity grouped in 3's for a total of 9 values. // The format is interpreted as hex values. const uint8_t colorTable[9] = { 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF }; //                            {   White Bytes  }, {   Red Bytes  }, {   Blue Bytes  } 
like image 53
CodaFi Avatar answered Nov 07 '22 17:11

CodaFi