【问题标题】:Quantize Image, Save List of Remaining Colors量化图像,保存剩余颜色列表
【发布时间】:2011-11-20 10:39:01
【问题描述】:

我可以使用一个库来量化 iPhone 上的图像吗?

我想将图像量化为 8 种颜色,并记录量化后剩余的每种颜色的 hex 或 rgb 值。

【问题讨论】:

    标签: iphone ios image-processing core-graphics


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      (我相信此代码来自 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 位蓝色。

      【讨论】:

      猜你喜欢
      • 2023-01-19
      • 2016-04-06
      • 2022-08-21
      • 1970-01-01
      • 2015-05-28
      • 1970-01-01
      • 2014-12-20
      • 2015-04-03
      • 1970-01-01
      相关资源
      最近更新 更多