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

发布于 2024-12-05 09:10:57 字数 84 浏览 1 评论 0原文

是否有一个库可以让我在 iPhone 上量化图像?

我想将图像量化为大约 8 种颜色,并记录量化后剩余的每种颜色的十六进制或 RGB 值。

Is there a library I can use that will allow me to quantize images on an iPhone?

I want to quantize the images down to maybe 8 colors and record either hex or rgb values for each color remaining after the quantization.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故事未完 2024-12-12 09:10:57

自己做这件事应该不会太难。获取像素数据,然后迭代并量化。以下是获取像素数据的方法:(

我相信此代码来自 Erica Sadun 的 Cookbook 示例)

// 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 位绿色。蓝色的。

Shouldn't be too hard to do this yourself. Get to the pixel data and then just iterate through and quantize. Here's how to get the pixel data:

(This code is from Erica Sadun's Cookbook samples, I believe)

// 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 will return an array where each pixel is described as 8 bits of alpha, 8 bits of red, 8 bits of green and 8 bits of blue.

巷雨优美回忆 2024-12-12 09:10:57

您可以使用 ImageMagick 来完成此任务: https://github.com/marforic/imagemagick_lib_iphone
MagickQuantizeImage 和相关函数将为您提供帮助。

You can use ImageMagick for this task: https://github.com/marforic/imagemagick_lib_iphone
MagickQuantizeImage and related function will help you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文