核心图形& GIF颜色表 [英] Core Graphics & GIF Color Table

查看:421
本文介绍了核心图形& GIF颜色表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图限制动画GIF(由 CGImageRef 的数组创建)的颜色数量。

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

但是,我实际设置自定义颜色表有困难。有没有人知道如何使用Core Graphics?

However, I am have difficulty actually setting the custom color table. Does anyone know how to do this with Core Graphics?

我知道 kCGImagePropertyGIFImageColorMap 。以下是一些测试代码(从借助这个github gist ),因为它是<$ c的唯一实例$ c> kCGImagePropertyGIFImageColorMap Google可以找到)。

I know of kCGImagePropertyGIFImageColorMap. Below is some test code (borrowing heavily from this github gist -- since it's the only instance of kCGImagePropertyGIFImageColorMap Google could 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 produce a black & white image.

此外,我尝试打开一个GIF来查找颜色表信息,但这没有什么帮助。

Furthermore, I've tried opening a GIF to find the color table information, but that's providing little help.

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

它说GIF的颜色空间是RGB。但是,如果我尝试这个代码与一个索引PNG,它说,颜色空间是索引。

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.

此外,我试过的所有GIF有一个图像字典大致如下:

Furthermore, all the GIFs I've tried have a image dictionary that looks roughly like the following:

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

(如果我使用 CGImageSourceCopyProperties ,它提到一个全局颜色图,但是没有提供颜色表。)

(If I use CGImageSourceCopyProperties(...), it mentions a global color map, but again no color table is provided.)

推荐答案

没有运行你的代码,但从阅读它我看到一个错误:gif中的颜色空间是 rgb ,所以你的颜色图应该有 numcolors * 3 项目(替代 * 4 )。 IOS不会优雅地处理大小不是3的倍数的颜色映射...

I didn't run your code, but from reading it I saw one mistake: The color space in gif is rgb, so your color map should have numcolors*3 items (in stead of *4). IOS doesn't deal gracefully with color maps whose size is not a multiple of 3...

换句话说:

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

应该做这项工作。

这篇关于核心图形&amp; GIF颜色表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆