【问题标题】:Convert 24bit image to 8bit in monotouch?在单点触控中将 24 位图像转换为 8 位?
【发布时间】:2012-07-24 15:06:15
【问题描述】:

我们有一个图像处理 Windows 应用程序,我们在其中使用领先工具将图像从 24/48 位图像转换为 8 位图像。

作为一个实验,我正在使用 MonoTouch 和 C# 将应用程序移植到 iPad,现在 LeadTools 组件与 Monotouch 不兼容。有什么我可以使用的替代品吗?如果不是,我如何将 24/48 位图像转换为 8 位?

【问题讨论】:

  • 您对单色转换感兴趣吗?

标签: c# ios xamarin.ios imaging bit-depth


【解决方案1】:

我将从这里开始使用 Apple 的成像工具:

  1. 将原始字节转换为平台支持的像素格式。请参阅supported pixel formats 上的 Quartz 2D 文档。
    请注意,iOS 目前没有 24 或 48 位格式。但是,如果您的 24 位格式是每通道 8 位 (RGB),您可以添加 8 位忽略的 alpha。 (Alpha 选项在 MonoTouch.CoreGraphics.CGImageAlphaInfo 中)

  2. 将原始字节转换为 CGImage。这是一个如何做到这一点的例子

        var provider = new CGDataProvider(bytes, 0, bytes.Length);
        int bitsPerComponent = 8;
        int components = 4;
        int height = bytes.Length / components / width;
        int bitsPerPixel = components * bitsPerComponent;
        int bytesPerRow = components * width;   // Tip:  When you create a bitmap graphics context, you’ll get the best performance if you make sure the data and bytesPerRow are 16-byte aligned.
        bool shouldInterpolate = false;
        var colorSpace = CGColorSpace.CreateDeviceRGB();
        var cgImage = new CGImage(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, 
                                  colorSpace, CGImageAlphaInfo.Last, provider,
                                  null, shouldInterpolate, CGColorRenderingIntent.Default);
    
  3. 使用Core Image Filter 转换为单色

        var mono = new CIColorMonochrome
        {
            Color = CIColor.FromRgb(1, 1, 1),
            Intensity = 1.0f,
            Image = CIImage.FromCGImage(image)
        };
        CIImage output = mono.OutputImage;
        var context = CIContext.FromOptions(null);
        var renderedImage = context.CreateCGImage(output, output.Extent);
    
  4. 最后,您可以通过绘制到根据所需参数构造的 CGBitmapContext 中来检索该图像的原始字节。

我怀疑这个管道可以优化,但它是一个开始的地方。我很想听听你的结果。

【讨论】:

    【解决方案2】:

    我认为您最好的选择是对 LeadTools 库进行本机调用 - 我能想到的 C# 中的任何图像操作都将依赖于 GDI+ 和 System.Drawing 命名空间等组件,而 monotouch 不支持.

    您可以通过创建一个 Binding 项目 - http://docs.xamarin.com/ios/advanced_topics/binding_objective-c_types 从您的 monotouch 项目中调用本机 Objective-C 代码

    这应该允许您以能够产生完全相同的图像/质量/格式的方式移植您的代码,而无需重新编写当前的转换代码。

    【讨论】:

      猜你喜欢
      • 2012-07-17
      • 2011-01-01
      • 2015-10-17
      • 1970-01-01
      • 2021-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-01
      相关资源
      最近更新 更多