C# 在monotouch中将24位图像转换为8位?

C# 在monotouch中将24位图像转换为8位?,c#,ios,xamarin.ios,imaging,bit-depth,C#,Ios,Xamarin.ios,Imaging,Bit Depth,我们有一个图像处理windows应用程序,其中我们使用lead工具将图像从24/48位图像转换为8位图像 作为一个实验,我正在使用MonoTouch和C#将应用程序移植到iPad上,现在LeadTools组件与MonoTouch不兼容。我可以用其他的吗?如果没有,我如何将24/48位图像转换为8位?我认为您最好的选择是对LeadTools库进行本机调用-我能想到的任何C#中的图像处理都将依赖于GDI+和System.Drawing命名空间等组件,而monotouch不支持这些组件 您可以通过创建

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


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

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

您可以通过创建绑定项目从monotouch项目调用本机objective-C代码-


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

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

您可以通过创建绑定项目从monotouch项目调用本机objective-C代码-


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

要使用苹果的图像工具,我将从这里开始:

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

  • 将原始字节转换为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);
    
  • 使用颜色转换为单色

        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);
    
  • 最后,通过将图像绘制到根据所需参数构造的CGBitmapContext中,可以检索该图像的原始字节


  • 我怀疑这条管道可以优化,但这是一个起点。我很想知道你最终会得到什么。

    要使用苹果的成像工具,我将从这里开始:

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

  • 将原始字节转换为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);
    
  • 使用颜色转换为单色

        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);
    
  • 最后,通过将图像绘制到根据所需参数构造的CGBitmapContext中,可以检索该图像的原始字节


  • 我怀疑这条管道可以优化,但这是一个起点。我很想知道你最终会得到什么。

    你对单色转换感兴趣吗?你对单色转换感兴趣吗?