Estoy tratando de limitar el número de colores de un GIF animado (creado a partir de una matriz de CGImageRef
).Core Graphics & GIF Color Table
Sin embargo, tengo dificultades para establecer la tabla de colores personalizada. ¿Alguien sabe cómo hacer esto con Core Graphics?
Sé de kCGImagePropertyGIFImageColorMap
. A continuación se muestra un código de prueba (tomando en gran medida de this github gist - ya que es la única instancia de kCGImagePropertyGIFImageColorMap
que Google pudo encontrar).
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);
Esto, sin embargo, no produce una imagen en blanco y negro &.
Además, he intentado abrir un GIF para encontrar la información de la tabla de colores, pero eso proporciona poca ayuda.
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);
Dice que el espacio de color es RGB para GIF. Sin embargo, si pruebo ese código con un PNG indexado, dice que el espacio de color está indexado.
Además, todos los archivos GIF que he probado tienen un diccionario de la imagen que se ve más o menos como la siguiente:
{
ColorModel = RGB;
Depth = 8;
HasAlpha = 1;
PixelHeight = 176;
PixelWidth = 314;
"{GIF}" = {
DelayTime = "0.1";
UnclampedDelayTime = "0.04";
};
}
(Si uso CGImageSourceCopyProperties(...)
, se menciona un mapa global de color, pero de nuevo no hay ninguna tabla de colores es se proporciona)
Dice OS X en las etiquetas. – uchuugaka