Beispiel #1
0
        public void CreateFromImage(Bitmap image)
        {
            var handler = image.Handler as BitmapHandler;

            SourceImage = image;
#if OSX
            var rep = handler.Control.Representations().OfType <NSBitmapImageRep>().FirstOrDefault();
            if (rep.BitsPerPixel != 32)
            {
                // CoreGraphics only supports drawing to 32bpp, create a new 32-bpp image and copy back when disposed or flushed.
                DrawingImage = new Bitmap(image.Width, image.Height, PixelFormat.Format32bppRgb);
                handler      = DrawingImage.Handler as BitmapHandler;
                rep          = handler.Control.Representations().OfType <NSBitmapImageRep>().FirstOrDefault();
            }
            graphicsContext = NSGraphicsContext.FromBitmap(rep);

            graphicsContext = graphicsContext.IsFlipped ? graphicsContext : NSGraphicsContext.FromGraphicsPort(graphicsContext.GraphicsPortHandle, true);
            disposeContext  = true;
            Control         = graphicsContext.GraphicsPort;
            PointsPerPixel  = (float)(rep.PixelsWide / handler.Control.Size.Width);
#elif IOS
            var cgimage = handler.Control.CGImage;
            Control        = new CGBitmapContext(handler.Data.MutableBytes, cgimage.Width, cgimage.Height, cgimage.BitsPerComponent, cgimage.BytesPerRow, cgimage.ColorSpace, cgimage.BitmapInfo);
            PointsPerPixel = (float)(cgimage.Width / handler.Control.Size.Width);
#endif

            height = image.Size.Height;
            SetDefaults();
            InitializeContext(true);
            if (DrawingImage != null && SourceImage != null)
            {
                // draw source image onto context, when source is incompatible for CoreGraphics drawing.
                DrawImage(SourceImage, 0, 0);
            }
        }
Beispiel #2
0
        public static NSImage Tint(this NSImage image, NSColor tint)
        {
            CIFilter colorGenerator = CIFilter.FromName("CIConstantColorGenerator");
            CIColor  color          = CIColor.FromCGColor(tint.ToCG());

            colorGenerator.SetValueForKey(color, CIFilterInputKey.Color);
            CIFilter colorFilter = CIFilter.FromName("CIColorControls");

            colorFilter.SetValueForKey(colorGenerator.ValueForKey(CIFilterOutputKey.Image), CIFilterInputKey.Image);
            colorFilter.SetValueForKey(NSNumber.FromFloat(3f), CIFilterInputKey.Saturation);
            colorFilter.SetValueForKey(NSNumber.FromFloat(0.35f), CIFilterInputKey.Brightness);
            colorFilter.SetValueForKey(NSNumber.FromFloat(1f), CIFilterInputKey.Contrast);

            CIFilter monochromeFilter = CIFilter.FromName("CIColorMonochrome");
            CIImage  baseImage        = CIImage.FromCGImage(image.CGImage);

            monochromeFilter.SetValueForKey(baseImage, CIFilterInputKey.Image);
            monochromeFilter.SetValueForKey(CIColor.FromRgb(0.75f, 0.75f, 0.75f), CIFilterInputKey.Color);
            monochromeFilter.SetValueForKey(NSNumber.FromFloat(1f), CIFilterInputKey.Intensity);

            CIFilter compositingFilter = CIFilter.FromName("CIMultiplyCompositing");

            compositingFilter.SetValueForKey(colorFilter.ValueForKey(CIFilterOutputKey.Image), CIFilterInputKey.Image);
            compositingFilter.SetValueForKey(monochromeFilter.ValueForKey(CIFilterOutputKey.Image), CIFilterInputKey.BackgroundImage);

            CIImage outputImage = (CIImage)compositingFilter.ValueForKey(CIFilterOutputKey.Image);
            var     extent      = outputImage.Extent;

            var newsize = sd.Size.Truncate(extent.Size);

            var tintedImage = new NSImage(newsize);
            var newrep      = new NSBitmapImageRep(IntPtr.Zero, newsize.Width, newsize.Height, 8, 4, true, false, NSColorSpace.DeviceRGB, 4 * newsize.Width, 32);

            tintedImage.AddRepresentation(newrep);

            var graphics = NSGraphicsContext.FromBitmap(newrep);

            NSGraphicsContext.GlobalSaveGraphicsState();
            NSGraphicsContext.CurrentContext = graphics;

            var ciContext = CIContext.FromContext(graphics.GraphicsPort, new CIContextOptions {
                UseSoftwareRenderer = true
            });

            ciContext.DrawImage(outputImage, extent, extent);

            NSGraphicsContext.GlobalRestoreGraphicsState();

            newrep.Size = image.Size;
            return(tintedImage);
        }
Beispiel #3
0
        public void CreateManager()
        {
            // This sets up the global context so our drawing doesn't produce error messages
            NSBitmapImageRep bitmap = new NSBitmapImageRep(IntPtr.Zero, 1000, 1000, 16, 4, true, false, NSColorSpace.DeviceRGB, 0, 0);

            NSGraphicsContext.CurrentContext = NSGraphicsContext.FromBitmap(bitmap);

            NSTextStorage   storage   = new NSTextStorage("Hello World");
            NSTextContainer container = new NSTextContainer();

            manager = new NSLayoutManager();

            manager.AddTextContainer(container);
            storage.AddLayoutManager(manager);
        }
        internal static NSBitmapImageRep Create()
        {
            float squareSize = 6;

            NSBitmapImageRep image = new NSBitmapImageRep(new CGRect(0, 0, squareSize * 2, squareSize * 2));

            using (CGContext gc = NSGraphicsContext.FromBitmap(image).GraphicsPort)
            {
                gc.SetFillColor(ImageDiffColors.TransparentBackgroundSquareColor);
                gc.FillRect(new CGRect(0, 0, squareSize, squareSize));
                gc.FillRect(new CGRect(squareSize, squareSize, squareSize, squareSize));
            }

            return(image);
        }
Beispiel #5
0
        public static NSImageRep Resize(this NSImageRep image, CGSize newsize, ImageInterpolation interpolation = ImageInterpolation.Default, CGSize?imageSize = null)
        {
            var newrep = new NSBitmapImageRep(IntPtr.Zero, (nint)newsize.Width, (nint)newsize.Height, 8, 4, true, false, NSColorSpace.DeviceRGB, 4 * (nint)newsize.Width, 32);

            newrep.Size = imageSize ?? newsize;

            var graphics = NSGraphicsContext.FromBitmap(newrep);

            NSGraphicsContext.GlobalSaveGraphicsState();
            NSGraphicsContext.CurrentContext           = graphics;
            graphics.GraphicsPort.InterpolationQuality = interpolation.ToCG();
            image.DrawInRect(new CGRect(CGPoint.Empty, newrep.Size), CGRect.Empty, NSCompositingOperation.SourceOver, 1f, true, DrawHints);
            NSGraphicsContext.GlobalRestoreGraphicsState();
            return(newrep);
        }
        public static NSImage Resize(this NSImage image, sd.Size newsize, ImageInterpolation interpolation = ImageInterpolation.Default)
        {
            var newimage = new NSImage(newsize);
            var newrep   = new NSBitmapImageRep(IntPtr.Zero, newsize.Width, newsize.Height, 8, 4, true, false, NSColorSpace.DeviceRGB, 4 * newsize.Width, 32);

            newimage.AddRepresentation(newrep);

            var graphics = NSGraphicsContext.FromBitmap(newrep);

            NSGraphicsContext.GlobalSaveGraphicsState();
            NSGraphicsContext.CurrentContext           = graphics;
            graphics.GraphicsPort.InterpolationQuality = interpolation.ToCG();
            image.DrawInRect(new sd.RectangleF(sd.PointF.Empty, newimage.Size), new sd.RectangleF(sd.PointF.Empty, image.Size), NSCompositingOperation.SourceOver, 1f);
            NSGraphicsContext.GlobalRestoreGraphicsState();
            return(newimage);
        }
Beispiel #7
0
        public static NSImageRep Resize(this NSImageRep image, CGSize newsize, ImageInterpolation interpolation = ImageInterpolation.Default, CGSize?imageSize = null)
        {
            var newrep = new NSBitmapImageRep(IntPtr.Zero, (nint)newsize.Width, (nint)newsize.Height, 8, 4, true, false, NSColorSpace.DeviceRGB, 4 * (nint)newsize.Width, 32);

            newrep.Size = imageSize ?? newsize;

            var graphics = NSGraphicsContext.FromBitmap(newrep);

            NSGraphicsContext.GlobalSaveGraphicsState();
            NSGraphicsContext.CurrentContext           = graphics;
            graphics.GraphicsPort.InterpolationQuality = interpolation.ToCG();
#if XAMMAC
            // Xamarin.Mac doesn't allow null for hints, remove this when it does.
            Messaging.bool_objc_msgSend_CGRect_CGRect_UIntPtr_nfloat_bool_IntPtr(image.Handle, selDrawInRect_FromRect_Operation_Fraction_RespectFlipped_Hints_Handle, new CGRect(CGPoint.Empty, newrep.Size), CGRect.Empty, (UIntPtr)(ulong)NSCompositingOperation.SourceOver, 1f, true, IntPtr.Zero);
#else
            image.DrawInRect(new CGRect(CGPoint.Empty, newrep.Size), CGRect.Empty, NSCompositingOperation.SourceOver, 1f, true, null);
#endif
            NSGraphicsContext.GlobalRestoreGraphicsState();
            return(newrep);
        }
Beispiel #8
0
        public void CreateFromImage(Bitmap image)
        {
            var handler = image.Handler as BitmapHandler;

#if OSX
            var rep = handler.Control.Representations().OfType <NSBitmapImageRep>().FirstOrDefault();
            graphicsContext = NSGraphicsContext.FromBitmap(rep);
            graphicsContext = graphicsContext.IsFlipped ? graphicsContext : NSGraphicsContext.FromGraphicsPort(graphicsContext.GraphicsPortHandle, true);
            disposeContext  = true;
            Control         = graphicsContext.GraphicsPort;
            scale           = (float)(rep.PixelsWide / handler.Control.Size.Width);
#elif IOS
            var cgimage = handler.Control.CGImage;
            Control = new CGBitmapContext(handler.Data.MutableBytes, cgimage.Width, cgimage.Height, cgimage.BitsPerComponent, cgimage.BytesPerRow, cgimage.ColorSpace, cgimage.BitmapInfo);
            scale   = cgimage.Width / handler.Control.Size.Width;
#endif

            height = image.Size.Height;
            SetDefaults();
            InitializeContext(true);
        }
Beispiel #9
0
        public void CreateFromImage(Bitmap image)
        {
            var handler = image.Handler as BitmapHandler;

#if OSX
            var rep = handler.Control.Representations().OfType <NSBitmapImageRep> ().FirstOrDefault();
            graphicsContext = NSGraphicsContext.FromBitmap(rep);
            disposeContext  = true;
            Control         = graphicsContext.GraphicsPort;
#elif IOS
            var cgimage = handler.Control.CGImage;
            Control = new CGBitmapContext(handler.Data.MutableBytes, cgimage.Width, cgimage.Height, cgimage.BitsPerComponent, cgimage.BytesPerRow, cgimage.ColorSpace, cgimage.BitmapInfo);
#endif

            Flipped     = false;
            this.height = image.Size.Height;
            Control.InterpolationQuality = CGInterpolationQuality.High;
            Control.SetAllowsSubpixelPositioning(false);
            Control.SaveState();
            if (!Flipped)
            {
                FlipDrawing();
            }
        }
Beispiel #10
0
        protected void EnsureRep()
        {
            if (rep == null)
            {
                rep = GetBestRepresentation();
            }

            // on Big Sur, rep is usually going to be a proxy, so let's find the concrete NSBitmapImageRep class the slow way..

            if (bmprep != null)
            {
                return;
            }

            if (rep is IconFrameHandler.LazyImageRep lazyRep)
            {
                bmprep = lazyRep.Rep;
            }
            else
            {
                bmprep = rep as NSBitmapImageRep ?? GetBestRepresentation() as NSBitmapImageRep;
            }

            if (bmprep != null)
            {
                return;
            }

            // go through concrete representations as we might have a proxy (Big Sur)
            // this is fixed with MonoMac, but not Xamarin.Mac.
            var representations = Control.Representations();

            for (int i = 0; i < representations.Length; i++)
            {
                NSImageRep rep = representations[i];
                if (rep is NSBitmapImageRep brep)
                {
                    bmprep = brep;
                    return;
                }
            }

            // create a new bitmap rep and copy the contents
            var size             = Size;
            int numComponents    = rep.HasAlpha ? 4 : 3;
            int bitsPerComponent = 8;
            int bitsPerPixel     = numComponents * bitsPerComponent;
            int bytesPerPixel    = bitsPerPixel / 8;
            int bytesPerRow      = bytesPerPixel * size.Width;

            bmprep = new NSBitmapImageRep(IntPtr.Zero, size.Width, size.Height, bitsPerComponent, numComponents, rep.HasAlpha, false, rep.ColorSpaceName, bytesPerRow, bitsPerPixel);
            var graphicsContext = NSGraphicsContext.FromBitmap(bmprep);

            NSGraphicsContext.GlobalSaveGraphicsState();
            NSGraphicsContext.CurrentContext = graphicsContext;
            Control.Draw(CGPoint.Empty, new CGRect(CGPoint.Empty, size.ToNS()), NSCompositingOperation.Copy, 1);
            NSGraphicsContext.GlobalRestoreGraphicsState();

            // remove all existing representations
            for (int i = 0; i < representations.Length; i++)
            {
                NSImageRep rep = representations[i];
                Control.RemoveRepresentation(rep);
            }

            // add the new one back
            Control.AddRepresentation(bmprep);
        }