Note that there are some explanatory texts on larger screens.

plurals
  1. POCore Graphics & GIF Color Table
    primarykey
    data
    text
    <p>I am trying to limit the number of colors of an animated GIF (created from an array of <code>CGImageRef</code>).</p> <p>However, I am have difficulty actually setting the custom color table. Does anyone know how to do this with Core Graphics?</p> <p>I know of <code>kCGImagePropertyGIFImageColorMap</code>. Below is some test code (borrowing heavily from <a href="https://gist.github.com/792829" rel="nofollow">this github gist</a> -- since it's the only instance of <code>kCGImagePropertyGIFImageColorMap</code> Google could find).</p> <pre><code>NSString *path = [@"~/Desktop/test.png" stringByExpandingTildeInPath]; CGDataProviderRef imgDataProvider = CGDataProviderCreateWithCFData((CFDataRef)[NSData dataWithContentsOfFile:path]); CGImageRef image = CGImageCreateWithPNGDataProvider(imgDataProvider, NULL, true, kCGRenderingIntentDefault); const uint8_t colorTable[ 8 ] = { 0, 0, 0, 0, 255, 255, 255 , 255}; NSData* colorTableData = [ NSData dataWithBytes: colorTable length: 8 ]; NSMutableDictionary *gifProps = [NSMutableDictionary dictionary]; [gifProps setObject:colorTableData forKey:(NSString *)kCGImagePropertyGIFImageColorMap]; [gifProps setObject:[NSNumber numberWithBool:NO] forKey:(NSString *)kCGImagePropertyGIFHasGlobalColorMap]; NSDictionary* imgProps = [ NSDictionary dictionaryWithObject: gifProps forKey: (NSString*) kCGImagePropertyGIFDictionary ]; NSURL* destUrl = [NSURL fileURLWithPath:[@"~/Desktop/test.gif" stringByExpandingTildeInPath]]; CGImageDestinationRef dst = CGImageDestinationCreateWithURL( (CFURLRef) destUrl, kUTTypeGIF, 1, NULL ); CGImageDestinationAddImage( dst, image, imgProps ); CGImageDestinationSetProperties(dst, (CFDictionaryRef) imgProps); CGImageDestinationFinalize( dst ); CFRelease( dst ); </code></pre> <p>This, however, does not produce a black &amp; white image.</p> <p>Furthermore, I've tried opening a GIF to find the color table information, but that's providing little help.</p> <pre><code>CGImageSourceRef imageSourceRef = CGImageSourceCreateWithURL((CFURLRef) destUrl, NULL); NSDictionary *dict = (NSDictionary *)CGImageSourceCopyPropertiesAtIndex(imageSourceRef,0, NULL); CGImageRef img = CGImageSourceCreateImageAtIndex(imageSourceRef, 0, NULL); printf("Color space model: %d, indexed=%d, rgb = %d\n", CGColorSpaceGetModel(CGImageGetColorSpace(img)), kCGColorSpaceModelIndexed,kCGColorSpaceModelRGB); NSLog(@"%@", dict); </code></pre> <p>It says the color space is RGB for GIFs. Yet, if I try that code with an indexed PNG, it says the color space is indexed.</p> <p>Furthermore, all the GIFs I've tried have a image dictionary that looks roughly like the following:</p> <pre><code>{ ColorModel = RGB; Depth = 8; HasAlpha = 1; PixelHeight = 176; PixelWidth = 314; "{GIF}" = { DelayTime = "0.1"; UnclampedDelayTime = "0.04"; }; } </code></pre> <p>(If I use <code>CGImageSourceCopyProperties(...)</code>, it mentions a global color map, but again no color table is provided.)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload