49

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

3
  • have a look at stackoverflow.com/q/15584931/2515572 Jul 17, 2013 at 6:50
  • i found another example gist.github.com/hellopatrick/1711925
    – TonyMkenu
    Jul 17, 2013 at 19:00
  • You can use other plain c or c++ libs, unrelated to objective-c, to produce your GIFs. I know this is somehow discomforting, because it seems that it should work with kCGImagePropertyGIFImageColorMap. But sometimes an API just doesn't work. The solution with external libs though should be a bit of reading and trial and error and then it will be easy.
    – Daniel S.
    Oct 1, 2013 at 8:57

1 Answer 1

4

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  }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.