量化图像,保存剩余颜色列表

有一个我可以使用的图书馆,将允许我量化iPhone上的图像?

我想量化的图像可能是8种颜色,并logging量化后剩余的每种颜色的hex或rgb值。

不要太难自己做这个。 获取像素数据,然后迭代并量化。 以下是如何获取像素数据:

(这个代码来自Erica Sadun的食谱样本,我相信)

// Courtesy of Apple, Create Bitmap with Alpha/RGB values CGContextRef CreateARGBBitmapContext (CGImageRef inImage, CGSize size) { CGContextRef context = NULL; CGColorSpaceRef colorSpace; void * bitmapData; int bitmapByteCount; int bitmapBytesPerRow; size_t pixelsWide = size.width; size_t pixelsHigh = size.height; bitmapBytesPerRow = (pixelsWide * 4); bitmapByteCount = (bitmapBytesPerRow * pixelsHigh); colorSpace = CGColorSpaceCreateDeviceRGB(); if (colorSpace == NULL) { fprintf(stderr, "Error allocating color space\n"); return NULL; } // allocate the bitmap & create context bitmapData = malloc( bitmapByteCount ); if (bitmapData == NULL) { fprintf (stderr, "Memory not allocated!"); CGColorSpaceRelease( colorSpace ); return NULL; } context = CGBitmapContextCreate (bitmapData, pixelsWide, pixelsHigh, 8, bitmapBytesPerRow, colorSpace, kCGImageAlphaPremultipliedFirst); if (context == NULL) { free (bitmapData); fprintf (stderr, "Context not created!"); } CGColorSpaceRelease( colorSpace ); return context; } // Return a C-based bitmap of the image data inside an image unsigned char *RequestImagePixelData(UIImage *inImage) { CGImageRef img = [inImage CGImage]; CGSize size = [inImage size]; CGContextRef cgctx = CreateARGBBitmapContext(img, size); if (cgctx == NULL) return NULL; CGRect rect = {{0,0},{size.width, size.height}}; CGContextDrawImage(cgctx, rect, img); unsigned char *data = CGBitmapContextGetData (cgctx); CGContextRelease(cgctx); return data; } 

RequestImagePixelData将返回一个数组,其中每个像素被描述为8位的alpha,8位的红色,8位的绿色和8位的蓝色。

你可以使用ImageMagick来完成这个任务: https : //github.com/marforic/imagemagick_lib_iphone MagickQuantizeImage和相关的函数将会帮助你。