Transparent PNGs in Monotouch for iOS

2019-04-13 15:32发布

问题:

I have a problem, and I 've been searching for months for a solution before posting here.

The problem is about transparent PNG files in Monotouch:

I am trying to load a transparent PNG in a context in order to read and write pixel colors. I am using the code below, which works perfectly with non-transparent PNGs. (I am also including several other attempts made while trying to get the correct result)

With transparent PNGs the problem seems to be that I have to load the image in the context with premultiplied alpha info (CGImageAlphaInfo.PremultipliedLast). This causes RGB values to be altered from their original values when loaded in the context.

I tried to create a context with CGImageAlphaInfo.Last (which is theoretically correct), but I get an exception (not acceptable parameter combination).

I thought of trying with CoreImage, but this would restrict our users to iOS 5 or later and I decided that this is not acceptable.

I desperately need to find a way to get and set pixels in transparent PNGs, since I am creating a cross platform software (iPhone, iPad, Mac using MonoMac and Windows) that needs to process transparent PNGs.

Any help will be greatly appreciated. Thanks!

using System;
using System.Collections.Generic;
using System.Linq;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using System.Drawing;
using MonoTouch.CoreGraphics;
using System.Runtime.InteropServices;
using MonoTouch.CoreImage;

namespace TestPNG
{
    // The UIApplicationDelegate for the application. This class is responsible for launching the 
    // User Interface of the application, as well as listening (and optionally responding) to 
    // application events from iOS.
    [Register ("AppDelegate")]
    public partial class AppDelegate : UIApplicationDelegate
    {
        // class-level declarations
        UIWindow window;
        TestPNGViewController viewController;

        //
        // This method is invoked when the application has loaded and is ready to run. In this 
        // method you should instantiate the window, load the UI into it and then make the window
        // visible.
        //
        // You have 17 seconds to return from this method, or iOS will terminate your application.
        //
        public override bool FinishedLaunching (UIApplication app, NSDictionary options)
        {
            window = new UIWindow (UIScreen.MainScreen.Bounds);

            viewController = new TestPNGViewController ();
            window.RootViewController = viewController;
            window.MakeKeyAndVisible ();

            UIImage _CarrierImage = UIImage.FromFile ("Blue50.png");

            int width = _CarrierImage.CGImage.Width;
            int height = _CarrierImage.CGImage.Height;
            CGImage cg = _CarrierImage.CGImage;
            Console.WriteLine ("width: " + width.ToString ("000") + ", height: " + height.ToString ("000"));
            Console.WriteLine ("BitsPerComponent: " + cg.BitsPerComponent.ToString ());
            Console.WriteLine ("BytesPerRow: " + cg.BytesPerRow.ToString ());
            Console.WriteLine ("BitsPerPixel: " + cg.BitsPerPixel.ToString ());
            Console.WriteLine ("ColorSpace: " + cg.ColorSpace.ToString ());
            Console.WriteLine ("For image Blue0.png should have values: R:0, G:0, B:255, A: 0");
            Console.WriteLine ("For image Blue50.png should have values: R:0, G:0, B:255, A: 128");
            Console.WriteLine ("For image Blue100.png should have values: R:0, G:0, B:255, A: 255");

            // METHOD 1
//          CGDataProvider p = CGDataProvider.FromFile("Blue50.png");
//          CGImage cgimg = CGImage.FromPNG(p,null,false,CGColorRenderingIntent.Default);
//          UIImage _CarrierImage = new UIImage(cgimg);


            // METHOD 2
//          NSData dt = _CarrierImage.AsPNG();
//          IntPtr bitmapData = dt.Bytes;
//          
//          Console.WriteLine("Length: " + dt.Length.ToString());
//          for (int i=0; i < dt.Length; i++)
//          {
//              Console.WriteLine("i: " + i.ToString("00") + ", byte: " + dt[i].ToString());
//          }
//          


            // METHOD 3
//          UIGraphics.BeginImageContext (_CarrierImage.Size);
//          CGContext ctx = UIGraphics.GetCurrentContext ();
//          ctx.DrawImage (new RectangleF (0, 0, _CarrierImage.Size.Width, _CarrierImage.Size.Height), _CarrierImage.CGImage);
//          IntPtr dt = ctx.Handle;
//          for (int i=0; i < 100; i++)
//          {
//              Console.WriteLine("i: " + i.ToString("00") + ", byte: " + GetByte(i, dt).ToString ());
//          }
//          UIGraphics.EndImageContext();



            // METHOD 4
            int bitmapBytesPerRow = width * 4;
            IntPtr bitmapData = Marshal.AllocHGlobal (width * 4 * height);

            CGBitmapContext ctxt = new CGBitmapContext (bitmapData, width, height, cg.BitsPerComponent, bitmapBytesPerRow, cg.ColorSpace, CGImageAlphaInfo.PremultipliedLast);

            RectangleF rect = new RectangleF (0.0f, 0.0f, width, height);
            ctxt.DrawImage (rect, _CarrierImage.CGImage); 
            IntPtr dt = ctxt.Data;

            Console.WriteLine ("R: " + GetByte (0, dt).ToString ());
            Console.WriteLine ("G: " + GetByte (1, dt).ToString ());
            Console.WriteLine ("B: " + GetByte (2, dt).ToString ());
            Console.WriteLine ("A: " + GetByte (3, dt).ToString ());

            return true;
        }

        unsafe byte GetByte (int offset, IntPtr buffer)
        {
            byte* bufferAsBytes = (byte*)buffer;
            return bufferAsBytes [offset];
        }

    }
}