Я пытаюсь ограничить количество цветов анимированного GIF (созданного из массива CGImageRef
).
Однако у меня возникли трудности с настройкой таблицы пользовательских цветов. Кто-нибудь знает, как это сделать с Core Graphics?
Я знаю о kCGImagePropertyGIFImageColorMap
. Ниже приведен тестовый код (заимствованный из этого github gist - поскольку это единственный экземпляр kCGImagePropertyGIFImageColorMap
, который может найти Google).
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 );
Это, однако, не создает черно-белое изображение.
Кроме того, я попытался открыть GIF, чтобы найти информацию о таблице цветов, но это мало помогает.
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);
В нем говорится, что цветовое пространство - это RGB для GIF. Тем не менее, если я попробую этот код с индексированным PNG, он скажет, что цветовое пространство проиндексировано.
Кроме того, все GIF-файлы, которые я пробовал, имеют словарь изображений, который выглядит примерно так:
{
ColorModel = RGB;
Depth = 8;
HasAlpha = 1;
PixelHeight = 176;
PixelWidth = 314;
"{GIF}" = {
DelayTime = "0.1";
UnclampedDelayTime = "0.04";
};
}
(Если я использую CGImageSourceCopyProperties(...)
, в нем упоминается глобальная карта цветов, но опять же таблица цветов не предоставляется.)