示例#1
0
        public static void Colourize(NSView control, Color color, Action drawAction)
        {
            var size = control.Frame.Size;

            if (size.Width <= 0 || size.Height <= 0)
            {
                return;
            }
            var image = new NSImage(size);

            image.LockFocusFlipped(control.IsFlipped);
            drawAction();
            image.UnlockFocus();

            var ciImage = CIImage.FromCGImage(image.CGImage);

            SD.SizeF realSize;
            if (control.RespondsToSelector(selConvertSizeToBacking))
            {
                realSize = control.ConvertSizeToBacking(size);
            }
            else
            {
                realSize = control.ConvertSizeToBase(size);
            }

            if (control.IsFlipped)
            {
                var affineTransform = new NSAffineTransform();
                affineTransform.Translate(0, realSize.Height);
                affineTransform.Scale(1, -1);
                var filter1 = new CIAffineTransform();
                filter1.Image = ciImage;
                filter1.SetValueForKey(affineTransform, CIInputTransform);
                ciImage = filter1.ValueForKey(CIOutputImage) as CIImage;
            }

            var filter2 = new CIColorControls();

            filter2.SetDefaults();
            filter2.Image      = ciImage;
            filter2.Saturation = 0.0f;
            ciImage            = filter2.ValueForKey(CIOutputImage) as CIImage;

            var filter3 = new CIColorMatrix();

            filter3.SetDefaults();
            filter3.Image   = ciImage;
            filter3.RVector = new CIVector(0, color.R, 0);
            filter3.GVector = new CIVector(color.G, 0, 0);
            filter3.BVector = new CIVector(0, 0, color.B);
            ciImage         = filter3.ValueForKey(CIOutputImage) as CIImage;

            ciImage.Draw(new SD.RectangleF(SD.PointF.Empty, size), new SD.RectangleF(SD.PointF.Empty, realSize), NSCompositingOperation.SourceOver, 1);
        }
示例#2
0
        public void End()
        {
            if (_image == null)
            {
                return;
            }

            _image.UnlockFocus();

            var cgImage = _image.CGImage;
            var ciImage = CIImage.FromCGImage(cgImage);

#pragma warning disable CS0618 // Image => InputImage in Xamarin.Mac 6.6
            var filter2 = new CIColorControls();
            filter2.SetDefaults();
            filter2.Image      = ciImage;
            filter2.Saturation = 0.0f;
            ciImage            = (CIImage)filter2.ValueForKey(CIOutputImage);

            var filter3 = new CIColorMatrix();
            filter3.SetDefaults();
            filter3.Image = ciImage;
#pragma warning restore CS0618

            var cgColor    = Color.ToCG();
            var components = cgColor.Components;
            if (components.Length >= 3)
            {
                filter3.RVector = new CIVector(0, components[0], 0);
                filter3.GVector = new CIVector(components[1], 0, 0);
                filter3.BVector = new CIVector(0, 0, components[2]);
            }
            else if (components.Length >= 1)
            {
                // grayscale
                filter3.RVector = new CIVector(0, components[0], 0);
                filter3.GVector = new CIVector(components[0], 0, 0);
                filter3.BVector = new CIVector(0, 0, components[0]);
            }

            filter3.AVector = new CIVector(0, 0, 0, cgColor.Alpha);
            ciImage         = (CIImage)filter3.ValueForKey(CIOutputImage);

            // create separate context so we can force using the software renderer, which is more than fast enough for this
            var ciContext = CIContext.FromContext(NSGraphicsContext.CurrentContext.GraphicsPort, new CIContextOptions {
                UseSoftwareRenderer = true
            });
            ciContext.DrawImage(ciImage, new CGRect(CGPoint.Empty, _image.Size), new CGRect(0, 0, cgImage.Width, cgImage.Height));

            ciImage.Dispose();
            ciContext.Dispose();
            filter2.Dispose();
            filter3.Dispose();
        }
示例#3
0
        public static void Colourize(NSView control, Color color, Action drawAction)
        {
            var size = control.Frame.Size;

            if (size.Width <= 0 || size.Height <= 0)
            {
                return;
            }
            var image = new NSImage(size);

            image.LockFocusFlipped(!control.IsFlipped);
            drawAction();
            image.UnlockFocus();

            var ciImage = CIImage.FromCGImage(image.CGImage);

            CGSize realSize;

            if (control.RespondsToSelector(selConvertSizeToBacking))
            {
                realSize = control.ConvertSizeToBacking(size);
            }
            else
            {
                realSize = control.ConvertSizeToBase(size);
            }

            var filter2 = new CIColorControls();

            filter2.SetDefaults();
            filter2.Image      = ciImage;
            filter2.Saturation = 0.0f;
            ciImage            = (CIImage)filter2.ValueForKey(CIOutputImage);

            var filter3 = new CIColorMatrix();

            filter3.SetDefaults();
            filter3.Image   = ciImage;
            filter3.RVector = new CIVector(0, color.R, 0);
            filter3.GVector = new CIVector(color.G, 0, 0);
            filter3.BVector = new CIVector(0, 0, color.B);
            filter3.AVector = new CIVector(0, 0, 0, color.A);
            ciImage         = (CIImage)filter3.ValueForKey(CIOutputImage);

            // create separate context so we can force using the software renderer, which is more than fast enough for this
            var ciContext = CIContext.FromContext(NSGraphicsContext.CurrentContext.GraphicsPort, new CIContextOptions {
                UseSoftwareRenderer = true
            });

            ciContext.DrawImage(ciImage, new CGRect(CGPoint.Empty, size), new CGRect(CGPoint.Empty, realSize));
        }
示例#4
0
文件: Util.cs 项目: mingyaaaa/xwt
        public static void DrawWithColorTransform(this NSView view, Color?color, Action drawDelegate)
        {
            if (color.HasValue)
            {
                var size = view.Frame.Size;
                if (size.Width <= 0 || size.Height <= 0)
                {
                    return;
                }

                // render view to image
                var image = new NSImage(size);
                image.LockFocusFlipped(!view.IsFlipped);
                drawDelegate();
                image.UnlockFocus();

                // create Core image for transformation
                var ciImage = CIImage.FromCGImage(image.CGImage);

                CGSize displaySize;
                                #pragma warning disable iOSAndMacApiUsageIssue
                if (view.RespondsToSelector(selConvertSizeToBacking))
                {
                    displaySize = view.ConvertSizeToBacking(size);
                }
                else
                {
                    displaySize = view.ConvertSizeToBase(size);
                }
                                #pragma warning restore iOSAndMacApiUsageIssue

                // apply color matrix
                var transformColor = new CIColorMatrix();
                transformColor.SetDefaults();
                transformColor.Image   = ciImage;
                transformColor.RVector = new CIVector(0, (float)color.Value.Red, 0);
                transformColor.GVector = new CIVector((float)color.Value.Green, 0, 0);
                transformColor.BVector = new CIVector(0, 0, (float)color.Value.Blue);
                using (var key = new NSString("outputImage"))
                    ciImage = (CIImage)transformColor.ValueForKey(key);

                var ciCtx = CIContext.FromContext(NSGraphicsContext.CurrentContext.GraphicsPort, null);
                ciCtx.DrawImage(ciImage, new CGRect(CGPoint.Empty, size), new CGRect(CGPoint.Empty, displaySize));
            }
            else
            {
                drawDelegate();
            }
        }
示例#5
0
        public static void DrawWithColorTransform(this NSView view, Color?color, Action drawDelegate)
        {
            if (color.HasValue)
            {
                if (view.Frame.Size.Width <= 0 || view.Frame.Size.Height <= 0)
                {
                    return;
                }

                // render view to image
                var image = new NSImage(view.Frame.Size);
                image.LockFocusFlipped(!view.IsFlipped);
                drawDelegate();
                image.UnlockFocus();

                // create Core image for transformation
                var rr      = new RectangleF(0, 0, view.Frame.Size.Width, view.Frame.Size.Height);
                var ciImage = CIImage.FromCGImage(image.AsCGImage(ref rr, NSGraphicsContext.CurrentContext, null));

                // apply color matrix
                var transformColor = new CIColorMatrix();
                transformColor.SetDefaults();
                transformColor.Image   = ciImage;
                transformColor.RVector = new CIVector(0, (float)color.Value.Red, 0);
                transformColor.GVector = new CIVector((float)color.Value.Green, 0, 0);
                transformColor.BVector = new CIVector(0, 0, (float)color.Value.Blue);
                ciImage = (CIImage)transformColor.ValueForKey(new NSString("outputImage"));

                var ciCtx = CIContext.FromContext(NSGraphicsContext.CurrentContext.GraphicsPort, null);
                ciCtx.DrawImage(ciImage, rr, rr);
            }
            else
            {
                drawDelegate();
            }
        }