2

我正在尝试使用 CoreGraphics 创建一个调色板(索引)PNG。

我发现最好的是我可以使用:

CGColorSpaceRef colorSpace = CGColorSpaceCreateIndexed(CGImageGetColorSpace(maskedImage), 255, <#const unsigned char *colorTable#>);

然后:

CGImageRef palettedImage = CGImageCreateCopyWithColorSpace(maskedImage, colorSpace)

但是我不知道该放什么作为colorTable。我想利用一些预制(快速)量化算法 - 例如调用时已经内置到 ImageIO 中的算法CGImageDestinationCreateWithURL(url, kUTTypeGIF , 1, NULL);

如何为 PNG 创建调色板?

4

2 回答 2

1

如果您的颜色空间是例如 RGB,您可以像这样设置 colorTable:

{R, G, B, R, G, B, R, G, B, ...}
于 2013-07-05T00:55:49.737 回答
1

所以最终的解决方案是做这样的事情:

// Create an 8-bit palette for the bitmap via libimagequant (http://pngquant.org/lib)
liq_attr *liqAttr = liq_attr_create();
liq_image *liqImage = liq_image_create_rgba(liqAttr, bitmap, (int)width, (int)height, 0);
liq_result *liqRes = liq_quantize_image(liqAttr, liqImage);

liq_write_remapped_image(liqRes, liqImage, bitmap, bytesPerRow * height);
const liq_palette *liqPal = liq_get_palette(liqRes);

// Transpose the result into an rgba array
unsigned char colorTable[1024];
for (NSInteger n = 0; n < liqPal->count; n++) {
    colorTable[4 * n] = liqPal->entries[n].r;
    colorTable[4 * n + 1] = liqPal->entries[n].g;
    colorTable[4 * n + 2] = liqPal->entries[n].b;
    colorTable[4 * n + 3] = liqPal->entries[n].a;
}

// Release
liq_attr_destroy(liqAttr);
liq_image_destroy(liqImage);
liq_result_destroy(liqRes);

我希望使用颜色表来创建一个 CGContextRef。但是,根据这篇文章: http: //developer.apple.com/library/mac/#qa/qa1037/_index.html在任何情况下都是不可能的。

于 2013-07-06T22:28:21.337 回答