Ejemplo n.º 1
0
        public static XIR.Image RemoteRepresentation(this CGLineJoin obj)
        {
            // Customize the line cap style for the new object.
            var aPath       = new CGPath();
            var lineWidth   = 10;
            var sampleWidth = 50;

            // First we draw the presentation line
            aPath.MoveToPoint(new CGPoint(lineWidth, lineWidth));
            aPath.AddLineToPoint(new CGPoint(lineWidth + sampleWidth / 2, sampleWidth));
            aPath.AddLineToPoint(new CGPoint(lineWidth + sampleWidth, lineWidth));

            // let's make sure we leave a little room for the line width drawing as well by adding the lineWidth as well
            var width  = (int)aPath.PathBoundingBox.Right + lineWidth;
            var height = (int)aPath.PathBoundingBox.Bottom + lineWidth;

            var bytesPerRow = width * 4;

            using (var context = new CGBitmapContext(
                       IntPtr.Zero, width, height,
                       8, bytesPerRow, CGColorSpace.CreateDeviceRGB(),
                       CGImageAlphaInfo.PremultipliedFirst)) {
                context.SaveState();
                context.SetStrokeColor(NSColor.Black.CGColor);
                context.SetLineWidth(lineWidth);
                context.AddPath(aPath);
                switch ((CGLineJoin)obj)
                {
                case CGLineJoin.Bevel:
                    context.SetLineJoin(CGLineJoin.Bevel);
                    break;

                case CGLineJoin.Miter:
                    context.SetLineJoin(CGLineJoin.Miter);
                    break;

                case CGLineJoin.Round:
                    context.SetLineJoin(CGLineJoin.Round);
                    break;
                }

                context.DrawPath(CGPathDrawingMode.Stroke);

                context.RestoreState();

                aPath = new CGPath();

                aPath.MoveToPoint(new CGPoint(lineWidth, lineWidth));
                aPath.AddLineToPoint(new CGPoint(lineWidth + sampleWidth / 2, sampleWidth));
                aPath.AddLineToPoint(new CGPoint(lineWidth + sampleWidth, lineWidth));

                context.SetStrokeColor(NSColor.White.CGColor);
                context.SetLineWidth(2);
                context.AddPath(aPath);

                context.DrawPath(CGPathDrawingMode.Stroke);

                return(context.RemoteRepresentation());
            }
        }
        /// <summary>
        /// Draws our animation path on the background image, just to show it
        /// </summary>
        protected void DrawPathAsBackground()
        {
            //---- create our offscreen bitmap context
            // size
            SizeF bitmapSize = new SizeF(this.View.Frame.Size);

            using (CGBitmapContext context = new CGBitmapContext(IntPtr.Zero
                                                                 , (int)bitmapSize.Width, (int)bitmapSize.Height, 8
                                                                 , (int)(4 * bitmapSize.Width), CGColorSpace.CreateDeviceRGB()
                                                                 , CGImageAlphaInfo.PremultipliedFirst))
            {
                //---- convert to View space
                CGAffineTransform affineTransform = CGAffineTransform.MakeIdentity();
                //---- invert the y axis
                affineTransform.Scale(1, -1);
                //---- move the y axis up
                affineTransform.Translate(0, this.View.Frame.Height);
                context.ConcatCTM(affineTransform);

                //---- actually draw the path
                context.AddPath(this._animationPath);
                context.SetStrokeColorWithColor(UIColor.LightGray.CGColor);
                context.SetLineWidth(3);
                context.StrokePath();

                //---- set what we've drawn as the backgound image
                this._backgroundImage.Image = UIImage.FromImage(context.ToImage());
            }
        }
Ejemplo n.º 3
0
        // Draws our animation path on the background image, just to show it
        void DrawPathAsBackground()
        {
            // create our offscreen bitmap context
            var bitmapSize = new CGSize(View.Frame.Size);

            using (var context = new CGBitmapContext(
                       IntPtr.Zero,
                       (int)bitmapSize.Width, (int)bitmapSize.Height, 8,
                       (int)(4 * bitmapSize.Width), CGColorSpace.CreateDeviceRGB(),
                       CGImageAlphaInfo.PremultipliedFirst)) {
                // convert to View space
                var affineTransform = CGAffineTransform.MakeIdentity();
                // invert the y axis
                affineTransform.Scale(1f, -1f);
                // move the y axis up
                affineTransform.Translate(0, View.Frame.Height);
                context.ConcatCTM(affineTransform);

                // actually draw the path
                context.AddPath(animationPath);
                context.SetStrokeColor(UIColor.LightGray.CGColor);
                context.SetLineWidth(3f);
                context.StrokePath();

                // set what we've drawn as the backgound image
                backgroundImage.Image = UIImage.FromImage(context.ToImage());
            }
        }
Ejemplo n.º 4
0
        private void DrawLine(int x, int y, int x2, int y2)
        {
            var path = new CGPath();

            path.AddLines(new CGPoint[] { new CGPoint(x, y), new CGPoint(x2, y2) });
            path.CloseSubpath();
            _context.AddPath(path);
            _context.DrawPath(CGPathDrawingMode.Stroke);
        }
Ejemplo n.º 5
0
        public static XIR.Image RemoteRepresentation(this CGPath cgPath)
        {
            // We add just a little padding to keep from clipping the drawings that lie on the bounds.
            var width  = (int)cgPath.PathBoundingBox.Width + 4 > 0 ? (int)cgPath.PathBoundingBox.Width + 4 : 200;
            var height = (int)cgPath.PathBoundingBox.Height + 4 > 0 ? (int)cgPath.PathBoundingBox.Height + 4 : 200;

            var bytesPerRow = width * 4;

            // We need to offset the image to keep from clipping the drawing.
            var offsetXZero = -cgPath.PathBoundingBox.X;
            var offsetYZero = -cgPath.PathBoundingBox.Y;

            // Create a transform to offset our drawing.
            var transform = CGAffineTransform.MakeIdentity();

            transform.Translate(offsetXZero + 1, offsetYZero + 1);

            using (var context = new CGBitmapContext(
                       IntPtr.Zero, width, height,
                       8, bytesPerRow, CGColorSpace.CreateDeviceRGB(),
                       CGImageAlphaInfo.PremultipliedFirst))
            {
                // Make sure we offset our drawing to keep it form clipping
                context.ConcatCTM(transform);

                context.SaveState();
                context.SetFillColor(brush.CGColor);
                context.AddPath(cgPath);
                context.FillPath();
                context.RestoreState();

                context.SetStrokeColor(pen.CGColor);
                context.SetLineWidth(1f);
                context.AddPath(cgPath);
                context.DrawPath(CGPathDrawingMode.Stroke);

                return(context.RemoteRepresentation());
            }
        }
		// Draws our animation path on the background image, just to show it
		protected void DrawPathAsBackground ()
		{
			// create our offscreen bitmap context
			var bitmapSize = new SizeF (View.Frame.Size);
			using (var context = new CGBitmapContext (
				       IntPtr.Zero,
				       (int)bitmapSize.Width, (int)bitmapSize.Height, 8,
				       (int)(4 * bitmapSize.Width), CGColorSpace.CreateDeviceRGB (),
				       CGImageAlphaInfo.PremultipliedFirst)) {
				
				// convert to View space
				var affineTransform = CGAffineTransform.MakeIdentity ();
				// invert the y axis
				affineTransform.Scale (1f, -1f);
				// move the y axis up
				affineTransform.Translate (0, View.Frame.Height);
				context.ConcatCTM (affineTransform);

				// actually draw the path
				context.AddPath (animationPath);
				context.SetStrokeColor (UIColor.LightGray.CGColor);
				context.SetLineWidth (3f);
				context.StrokePath ();
				
				// set what we've drawn as the backgound image
				backgroundImage.Image = UIImage.FromImage (context.ToImage());
			}
		}
Ejemplo n.º 7
0
        public static NSImage ToRounded(NSImage source, nfloat rad, double cropWidthRatio, double cropHeightRatio, double borderSize, string borderHexColor)
        {
            double sourceWidth  = source.CGImage.Width;
            double sourceHeight = source.CGImage.Height;

            double desiredWidth  = sourceWidth;
            double desiredHeight = sourceHeight;

            double desiredRatio = cropWidthRatio / cropHeightRatio;
            double currentRatio = sourceWidth / sourceHeight;

            if (currentRatio > desiredRatio)
            {
                desiredWidth = (cropWidthRatio * sourceHeight / cropHeightRatio);
            }
            else if (currentRatio < desiredRatio)
            {
                desiredHeight = (cropHeightRatio * sourceWidth / cropWidthRatio);
            }

            float cropX = (float)((sourceWidth - desiredWidth) / 2);
            float cropY = (float)((sourceHeight - desiredHeight) / 2);

            if (rad == 0)
            {
                rad = (nfloat)(Math.Min(desiredWidth, desiredHeight) / 2);
            }
            else
            {
                rad = (nfloat)(rad * (desiredWidth + desiredHeight) / 2 / 500);
            }

            var       colorSpace       = CGColorSpace.CreateDeviceRGB();
            const int bytesPerPixel    = 4;
            int       width            = (int)desiredWidth;
            int       height           = (int)desiredHeight;
            var       bytes            = new byte[width * height * bytesPerPixel];
            int       bytesPerRow      = bytesPerPixel * width;
            const int bitsPerComponent = 8;

            using (var context = new CGBitmapContext(bytes, width, height, bitsPerComponent, bytesPerRow, colorSpace, CGBitmapFlags.PremultipliedLast | CGBitmapFlags.ByteOrder32Big))
            {
                var clippedRect = new CGRect(0d, 0d, desiredWidth, desiredHeight);

                context.BeginPath();

                using (var path = NSBezierPath.FromRoundedRect(clippedRect, rad, rad))
                {
                    context.AddPath(path.ToCGPath());
                    context.Clip();
                }

                var drawRect = new CGRect(-cropX, -cropY, sourceWidth, sourceHeight);
                context.DrawImage(drawRect, source.CGImage);

                if (borderSize > 0d)
                {
                    borderSize = (borderSize * (desiredWidth + desiredHeight) / 2d / 1000d);
                    var borderRect = new CGRect((0d + borderSize / 2d), (0d + borderSize / 2d),
                                                (desiredWidth - borderSize), (desiredHeight - borderSize));

                    context.BeginPath();

                    using (var path = NSBezierPath.FromRoundedRect(borderRect, rad, rad))
                    {
                        context.SetStrokeColor(borderHexColor.ToUIColor().CGColor);
                        context.SetLineWidth((nfloat)borderSize);
                        context.AddPath(path.ToCGPath());
                        context.StrokePath();
                    }
                }

                using (var output = context.ToImage())
                {
                    return(new NSImage(output, CGSize.Empty));
                }
            }
        }
        public static NSImage ToTransformedCorners(NSImage source, double topLeftCornerSize, double topRightCornerSize, double bottomLeftCornerSize, double bottomRightCornerSize,
                                                   CornerTransformType cornersTransformType, double cropWidthRatio, double cropHeightRatio)
        {
            double sourceWidth  = source.CGImage.Width;
            double sourceHeight = source.CGImage.Height;

            double desiredWidth  = sourceWidth;
            double desiredHeight = sourceHeight;

            double desiredRatio = cropWidthRatio / cropHeightRatio;
            double currentRatio = sourceWidth / sourceHeight;

            if (currentRatio > desiredRatio)
            {
                desiredWidth = (cropWidthRatio * sourceHeight / cropHeightRatio);
            }
            else if (currentRatio < desiredRatio)
            {
                desiredHeight = (cropHeightRatio * sourceWidth / cropWidthRatio);
            }

            topLeftCornerSize     = topLeftCornerSize * (desiredWidth + desiredHeight) / 2 / 100;
            topRightCornerSize    = topRightCornerSize * (desiredWidth + desiredHeight) / 2 / 100;
            bottomLeftCornerSize  = bottomLeftCornerSize * (desiredWidth + desiredHeight) / 2 / 100;
            bottomRightCornerSize = bottomRightCornerSize * (desiredWidth + desiredHeight) / 2 / 100;

            float cropX = (float)((sourceWidth - desiredWidth) / 2);
            float cropY = (float)((sourceHeight - desiredHeight) / 2);

            var       colorSpace       = CGColorSpace.CreateDeviceRGB();
            const int bytesPerPixel    = 4;
            int       width            = (int)desiredWidth;
            int       height           = (int)desiredHeight;
            var       bytes            = new byte[width * height * bytesPerPixel];
            int       bytesPerRow      = bytesPerPixel * width;
            const int bitsPerComponent = 8;

            using (var context = new CGBitmapContext(bytes, width, height, bitsPerComponent, bytesPerRow, colorSpace, CGBitmapFlags.PremultipliedLast | CGBitmapFlags.ByteOrder32Big))
            {
                context.BeginPath();

                using (var path = new NSBezierPath())
                {
                    // TopLeft
                    if (cornersTransformType.HasFlag(CornerTransformType.TopLeftCut))
                    {
                        path.MoveTo(new CGPoint(0, topLeftCornerSize));
                        path.LineTo(new CGPoint(topLeftCornerSize, 0));
                    }
                    else if (cornersTransformType.HasFlag(CornerTransformType.TopLeftRounded))
                    {
                        path.MoveTo(new CGPoint(0, topLeftCornerSize));
                        path.QuadCurveToPoint(new CGPoint(topLeftCornerSize, 0), new CGPoint(0, 0));
                    }
                    else
                    {
                        path.MoveTo(new CGPoint(0, 0));
                    }

                    // TopRight
                    if (cornersTransformType.HasFlag(CornerTransformType.TopRightCut))
                    {
                        path.LineTo(new CGPoint(desiredWidth - topRightCornerSize, 0));
                        path.LineTo(new CGPoint(desiredWidth, topRightCornerSize));
                    }
                    else if (cornersTransformType.HasFlag(CornerTransformType.TopRightRounded))
                    {
                        path.LineTo(new CGPoint(desiredWidth - topRightCornerSize, 0));
                        path.QuadCurveToPoint(new CGPoint(desiredWidth, topRightCornerSize), new CGPoint(desiredWidth, 0));
                    }
                    else
                    {
                        path.LineTo(new CGPoint(desiredWidth, 0));
                    }

                    // BottomRight
                    if (cornersTransformType.HasFlag(CornerTransformType.BottomRightCut))
                    {
                        path.LineTo(new CGPoint(desiredWidth, desiredHeight - bottomRightCornerSize));
                        path.LineTo(new CGPoint(desiredWidth - bottomRightCornerSize, desiredHeight));
                    }
                    else if (cornersTransformType.HasFlag(CornerTransformType.BottomRightRounded))
                    {
                        path.LineTo(new CGPoint(desiredWidth, desiredHeight - bottomRightCornerSize));
                        path.QuadCurveToPoint(new CGPoint(desiredWidth - bottomRightCornerSize, desiredHeight), new CGPoint(desiredWidth, desiredHeight));
                    }
                    else
                    {
                        path.LineTo(new CGPoint(desiredWidth, desiredHeight));
                    }

                    // BottomLeft
                    if (cornersTransformType.HasFlag(CornerTransformType.BottomLeftCut))
                    {
                        path.LineTo(new CGPoint(bottomLeftCornerSize, desiredHeight));
                        path.LineTo(new CGPoint(0, desiredHeight - bottomLeftCornerSize));
                    }
                    else if (cornersTransformType.HasFlag(CornerTransformType.BottomLeftRounded))
                    {
                        path.LineTo(new CGPoint(bottomLeftCornerSize, desiredHeight));
                        path.QuadCurveToPoint(new CGPoint(0, desiredHeight - bottomLeftCornerSize), new CGPoint(0, desiredHeight));
                    }
                    else
                    {
                        path.LineTo(new CGPoint(0, desiredHeight));
                    }

                    path.ClosePath();
                    context.AddPath(path.ToCGPath());
                    context.Clip();
                }

                var drawRect = new CGRect(-cropX, -cropY, sourceWidth, sourceHeight);
                context.DrawImage(drawRect, source.CGImage);


                using (var output = context.ToImage())
                {
                    return(new NSImage(output, CGSize.Empty));
                }
            }
        }
Ejemplo n.º 9
0
        public static XIR.Image RemoteRepresentation(this CGLineCap obj)
        {
            var aPath       = new CGPath();
            var lineWidth   = 10;
            var sampleWidth = 50;

            aPath.MoveToPoint(new CGPoint(lineWidth, lineWidth));
            aPath.AddLineToPoint(new CGPoint(lineWidth + sampleWidth, lineWidth));

            // let's make sure we leave a little room for the line width drawing as well by adding the lineWidth as well
            var width  = (int)aPath.PathBoundingBox.Right + lineWidth;
            var height = (int)aPath.PathBoundingBox.Bottom + lineWidth;

            var bytesPerRow = width * 4;

            using (var context = new CGBitmapContext(
                       IntPtr.Zero, width, height,
                       8, bytesPerRow, CGColorSpace.CreateDeviceRGB(),
                       CGImageAlphaInfo.PremultipliedFirst)) {
                context.SaveState();
                context.SetStrokeColor(new CGColor(0, 0, 0));
                context.SetLineWidth(lineWidth);
                context.AddPath(aPath);
                switch ((CGLineCap)obj)
                {
                case CGLineCap.Square:
                    context.SetLineCap(CGLineCap.Square);
                    break;

                case CGLineCap.Butt:
                    context.SetLineCap(CGLineCap.Butt);
                    break;

                case CGLineCap.Round:
                    context.SetLineCap(CGLineCap.Round);
                    break;
                }

                context.DrawPath(CGPathDrawingMode.Stroke);

                context.RestoreState();

                // Second, we draw the inset line to demonstrate the bounds
                aPath = new CGPath();
                aPath.MoveToPoint(new CGPoint(lineWidth, lineWidth));
                aPath.AddLineToPoint(new CGPoint(lineWidth + sampleWidth, lineWidth));
                context.SetLineCap(CGLineCap.Butt);
                context.SetStrokeColor(NSColor.White.CGColor);
                context.SetLineWidth(1);

                context.SaveState();

                context.AddPath(aPath);
                context.DrawPath(CGPathDrawingMode.Stroke);

                context.RestoreState();


                // Third, we draw the inset line endings which are two circles
                var circleWidth = 2;
                aPath = new CGPath();
                aPath.AddEllipseInRect(new CGRect(lineWidth - (int)(circleWidth / 2), lineWidth - (int)(circleWidth / 2), circleWidth, circleWidth));
                aPath.AddEllipseInRect(new CGRect(lineWidth + sampleWidth - (int)(circleWidth / 2), lineWidth - (int)(circleWidth / 2), circleWidth, circleWidth));
                context.SetLineWidth(circleWidth);
                context.SetStrokeColor(NSColor.White.CGColor);
                context.AddPath(aPath);
                context.DrawPath(CGPathDrawingMode.Stroke);

                return(RemoteRepresentation(context));
            }
        }
Ejemplo n.º 10
0
        public static UIImage OverlayRectangles(UIImage uiImage, CGSize imageSize, VNRectangleObservation[] observations)
        {
            nfloat fWidth  = uiImage.Size.Width;
            nfloat fHeight = uiImage.Size.Height;

            CGColorSpace colorSpace = CGColorSpace.CreateDeviceRGB();

            using (CGBitmapContext ctx = new CGBitmapContext(IntPtr.Zero, (nint)fWidth, (nint)fHeight, 8, 4 * (nint)fWidth, CGColorSpace.CreateDeviceRGB(), CGImageAlphaInfo.PremultipliedFirst))
            {
                Console.WriteLine("Orientation:" + uiImage.Orientation);
                if (uiImage.Orientation == UIImageOrientation.Up)
                {   // correct orientation
                    ctx.DrawImage(new CGRect(0, 0, (double)fWidth, (double)fHeight), uiImage.CGImage);
                }
                else
                {   // need to rotate image so that rectangle overlays match
                    UIGraphics.BeginImageContextWithOptions(uiImage.Size, false, 0);
                    uiImage.Draw(new CGRect(0, 0, (double)fWidth, (double)fHeight));
                    var img = UIGraphics.GetImageFromCurrentImageContext();
                    UIGraphics.EndImageContext();
                    ctx.DrawImage(new CGRect(0, 0, (double)fWidth, (double)fHeight), img.CGImage);
                }

                var count = 0;
                foreach (var o in observations)
                {
                    // Draw rectangle
                    var text = "Rectangle: " + count++.ToString();
                    Console.WriteLine(o.BoundingBox + " " + o.Confidence);

                    var topLeft     = o.TopLeft.Scaled(imageSize);
                    var topRight    = o.TopRight.Scaled(imageSize);
                    var bottomLeft  = o.BottomLeft.Scaled(imageSize);
                    var bottomRight = o.BottomRight.Scaled(imageSize);

                    //set up drawing attributes
                    ctx.SetStrokeColor(UIColor.Red.CGColor);
                    ctx.SetLineWidth(10);

                    //create geometry
                    var path = new CGPath();

                    path.AddLines(new CGPoint[] {
                        topLeft, topRight, bottomRight, bottomLeft
                    });

                    path.CloseSubpath();

                    //add geometry to graphics context and draw it
                    ctx.AddPath(path);
                    ctx.DrawPath(CGPathDrawingMode.Stroke);


                    // Draw text
                    ctx.SelectFont("Helvetica", 60, CGTextEncoding.MacRoman);
                    //Measure the text's width - This involves drawing an invisible string to calculate the X position difference
                    float start, end, textWidth;
                    //Get the texts current position
                    start = (float)ctx.TextPosition.X;
                    //Set the drawing mode to invisible
                    ctx.SetTextDrawingMode(CGTextDrawingMode.Invisible);
                    //Draw the text at the current position
                    ctx.ShowText(text);
                    //Get the end position
                    end = (float)ctx.TextPosition.X;
                    //Subtract start from end to get the text's width
                    textWidth = end - start;
                    ctx.SetFillColor(UIColor.Red.CGColor);

                    //Set the drawing mode back to something that will actually draw Fill for example
                    ctx.SetTextDrawingMode(CGTextDrawingMode.Fill);

                    //Draw the text at given coords.
                    ctx.ShowTextAtPoint(topLeft.X + 60, topLeft.Y - 60, text);
                }
                return(UIImage.FromImage(ctx.ToImage()));
            }
        }
Ejemplo n.º 11
0
        public static NSImage ToCropped(NSImage source, double zoomFactor, double xOffset, double yOffset, double cropWidthRatio, double cropHeightRatio)
        {
            double sourceWidth  = source.CGImage.Width;
            double sourceHeight = source.CGImage.Height;

            double desiredWidth  = sourceWidth;
            double desiredHeight = sourceHeight;

            double desiredRatio = cropWidthRatio / cropHeightRatio;
            double currentRatio = sourceWidth / sourceHeight;

            if (currentRatio > desiredRatio)
            {
                desiredWidth = (cropWidthRatio * sourceHeight / cropHeightRatio);
            }
            else if (currentRatio < desiredRatio)
            {
                desiredHeight = (cropHeightRatio * sourceWidth / cropWidthRatio);
            }

            xOffset = xOffset * desiredWidth;
            yOffset = yOffset * desiredHeight;

            desiredWidth  = desiredWidth / zoomFactor;
            desiredHeight = desiredHeight / zoomFactor;

            float cropX = (float)(((sourceWidth - desiredWidth) / 2) + xOffset);
            float cropY = (float)(((sourceHeight - desiredHeight) / 2) + yOffset);

            if (cropX < 0)
            {
                cropX = 0;
            }

            if (cropY < 0)
            {
                cropY = 0;
            }

            if (cropX + desiredWidth > sourceWidth)
            {
                cropX = (float)(sourceWidth - desiredWidth);
            }

            if (cropY + desiredHeight > sourceHeight)
            {
                cropY = (float)(sourceHeight - desiredHeight);
            }


            var       colorSpace       = CGColorSpace.CreateDeviceRGB();
            const int bytesPerPixel    = 4;
            int       width            = (int)desiredWidth;
            int       height           = (int)desiredHeight;
            var       bytes            = new byte[width * height * bytesPerPixel];
            int       bytesPerRow      = bytesPerPixel * width;
            const int bitsPerComponent = 8;

            using (var context = new CGBitmapContext(bytes, width, height, bitsPerComponent, bytesPerRow, colorSpace, CGBitmapFlags.PremultipliedLast | CGBitmapFlags.ByteOrder32Big))
            {
                var clippedRect = new CGRect(0, 0, desiredWidth, desiredHeight);
                context.BeginPath();

                using (var path = NSBezierPath.FromRect(clippedRect))
                {
                    context.AddPath(path.ToCGPath());
                    context.Clip();
                }

                var drawRect = new CGRect(-cropX, -cropY, sourceWidth, sourceHeight);
                context.DrawImage(drawRect, source.CGImage);

                using (var output = context.ToImage())
                {
                    return(new NSImage(output, CGSize.Empty));
                }
            }
        }