GIF color chart and color chart

I am trying to limit the number of colors of an animated GIF (created from a CGImageRef array).

However, itโ€™s hard for me to set up a custom color table. Does anyone know how to do this using Core Graphics?

I know kCGImagePropertyGIFImageColorMap . Below is some test code (heavily borrowed from this github gist - as this is the only instance of kCGImagePropertyGIFImageColorMap that Google can find).

 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 ); 

This, however, does not create a black and white image.

In addition, I tried to open GIFs to find information about the color table, but that helps a little.

 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); 

It states that the RGB color space is for GIF. However, if I try this code with indexed PNG, it says the color space is indexed.

Also, all the GIFs I tried have an image dictionary that looks something like this:

 { ColorModel = RGB; Depth = 8; HasAlpha = 1; PixelHeight = 176; PixelWidth = 314; "{GIF}" = { DelayTime = "0.1"; UnclampedDelayTime = "0.04"; }; } 

(If I use CGImageSourceCopyProperties(...) , it mentions the global color map, but again the color table is not displayed.)

+7
cocoa core-graphics gif macos
May 27 '11 at 3:51 am
source share
1 answer

I did not run your code, but after reading it, I saw one error: The color space in gif rgb , so your color map should have numcolors*3 elements (instead of *4 ). IOS does not correctly handle color cards whose size is not a multiple of 3 ...

In other words:

 const uint8_t colorTable[ 6 ] = { 0, 0, 0, 255, 255 , 255}; NSData* colorTableData = [ NSData dataWithBytes: colorTable length:6]; 

must do the job.

+1
Jul 28 '12 at 19:46
source share



All Articles