Esempio n. 1
0
        private void ApplyStretchFit(IImageProcessingContext <Rgba32> context, Rectangle area, Image <Rgba32> image)
        {
            using (var img2 = image.Clone())
            {
                img2.Mutate(ctx => ctx.Resize(new ResizeOptions
                {
                    Mode    = ResizeMode.Stretch,
                    Sampler = area.Width > image.Width ? (IResampler) new BicubicResampler() : new BoxResampler(),
                    Size    = new Size(area.Width, area.Height)
                }));

                context.DrawImage(img2, area.Location, _graphicsOptions);
            }
        }
Esempio n. 2
0
        private Matrix <float> ApplyTile(IImageProcessingContext <Rgba32> context, Image <Rgba32> image)
        {
            var numX = ((int)MaxWidth / image.Width) + 1;
            var numY = ((int)MaxHeight / image.Height) + 1;

            for (var i = 0; i < numX; i++)
            {
                for (var j = 0; j < numY; j++)
                {
                    context.DrawImage(GraphicsOptions, image, new Point(i * image.Width, j * image.Height));
                }
            }
            return(GetProjectiveTransformationMatrix(MaxWidth, MaxHeight));
        }
Esempio n. 3
0
        public override void Render(IImageProcessingContext <Rgba32> context, Rectangle cellArea, RenderCell cell)
        {
            if (!IsSupported(cell))
            {
                return;
            }
            Image <Rgba32> texture  = _textures[_random.Next(_textures.Count)];
            var            location = new Point(
                (cellArea.Left + cellArea.Right) / 2,
                cellArea.Bottom);

            location.X -= texture.Width / 2;
            location.Y -= texture.Height;
            context.DrawImage(texture, PixelBlenderMode.Normal, 1.0f, location);
        }
Esempio n. 4
0
        private void ApplyCenter(IImageProcessingContext <Rgba32> context, Rectangle area, Image <Rgba32> image)
        {
            using (var img2 = image.Clone())
            {
                if (img2.Width > area.Width || img2.Height > area.Height)
                {
                    var newWidth  = Math.Min(area.Width, img2.Width);
                    var newHeight = Math.Min(area.Height, img2.Height);
                    var x         = Math.Max(0, (image.Width - area.Width) / 2);
                    var y         = Math.Max(0, (image.Height - area.Height) / 2);
                    img2.Mutate(ctx => ctx.Crop(new Rectangle(x, y, newWidth, newHeight)));
                }
                var sx = area.X + (area.Width - img2.Width) / 2;
                var sy = area.Y + (area.Height - img2.Height) / 2;

                context.DrawImage(img2, new Point(sx, sy), GraphicsOptions);
            }
        }
Esempio n. 5
0
        private void ApplyFitWithLetterbox(IImageProcessingContext <Rgba32> context, Rectangle area, Image <Rgba32> image)
        {
            using (var img2 = image.Clone())
            {
                img2.Mutate(ctx =>
                {
                    var resizeOptions = new ResizeOptions
                    {
                        Mode    = ResizeMode.Max,
                        Compand = false,
                        Sampler = area.Width > image.Width ? (IResampler) new BicubicResampler() : new BoxResampler(),
                        Size    = area.Size
                    };
                    ctx.Resize(resizeOptions);
                });

                var sx = area.X + (area.Width - img2.Width) / 2;
                var sy = area.Y + (area.Height - img2.Height) / 2;

                context.DrawImage(img2, new Point(sx, sy), GraphicsOptions);
            }
        }
Esempio n. 6
0
        public override void Render(IImageProcessingContext <Rgba32> context, Rectangle cellArea, RenderCell cell)
        {
            if (!IsSupported(cell))
            {
                return;
            }

            string         textureKey = GetTextureKey(cell);
            Image <Rgba32> texture    = _map[textureKey];

            if (texture == null)
            {
                return;
            }
            var location = new Point(
                (cellArea.Left + cellArea.Right) / 2,
                cellArea.Bottom);

            location.X -= texture.Width / 2;
            location.Y -= texture.Height;
            context.DrawImage(texture, PixelBlenderMode.Normal, 1.0f, location);
        }
Esempio n. 7
0
        private void DrawImage(IImageProcessingContext <Rgba32> processor, string name, int x, int y)
        {
            var pieceSquare = SixLabors.ImageSharp.Image.Load(_assetService.GetImagePath($"{name}.png"));

            processor.DrawImage(pieceSquare, new Size(50, 50), new Point(x * 50 + 117, y * 50 + 19), new GraphicsOptions());
        }
Esempio n. 8
0
        public void Render(GraphBuilder context, IImageProcessingContext <Rgba32> renderContext, GraphicsOptions options)
        {
            // top and right
            PointF labelEndpoint;

            if (IsVertical)
            {
                PointF endpoint1 = new PointF(context.GridRegion.Left + context.Origin.X, context.GridRegion.Top + PositiveEndAxisMargin + ArrowLength);
                PointF endpoint2 = new PointF(context.GridRegion.Left + context.Origin.X, context.GridRegion.Bottom - NegativeEndAxisMargin - ArrowLength);
                labelEndpoint = endpoint1;

                renderContext.DrawLines(Brush, LineThickness, new PointF[] { endpoint1, endpoint2 });
                if (ArrowWidth > 0 && ArrowLength > 0)
                {
                    if (EnablePositiveEndArrow)
                    {
                        // top, add Y
                        renderContext.FillPolygon(ArrowColor, new PointF[] {
                            endpoint1 - new PointF(0, ArrowLength),
                            endpoint1 + new PointF(-ArrowWidth / 2, 0),
                            endpoint1 + new PointF(ArrowWidth / 2, 0)
                        }, options);
                    }
                    if (EnableNegativeEndArrow)
                    {
                        // bottom, subtract Y
                        renderContext.FillPolygon(ArrowColor, new PointF[] {
                            endpoint2 + new PointF(0, ArrowLength),
                            endpoint2 - new PointF(-ArrowWidth / 2, 0),
                            endpoint2 - new PointF(ArrowWidth / 2, 0)
                        }, options);
                    }
                }

                if (TitleLabel != null)
                {
                    RectangleF textBounds      = TextMeasurer.MeasureBounds(TitleLabel.Text, new RendererOptions(TitleLabel.Font));
                    int        higherDimension = (int)Math.Ceiling(Math.Max(textBounds.Width, textBounds.Height));
                    using (Image <Rgba32> textRenderImage = new Image <Rgba32>(higherDimension, higherDimension))
                    {
                        textRenderImage.Mutate(tempContext =>
                                               tempContext
                                               .DrawText(TitleLabel.Text, TitleLabel.Font, TitleLabel.Color, PointF.Empty, options)
                                               .Rotate(-90));
                        // TODO can I do this in the same IImageProcessingContext or do I have to stop, let it write, then read, then write
                        textRenderImage.MutateCropToColored();
                        PointF renderPosition = context.GridRegion.Position() + context.Origin + TitleLabel.Displacement - textRenderImage.Size() + new PointF(0, (textRenderImage.Height / 2) - ((context.Origin.Y + context.GridRegion.Top - endpoint1.Y) / 2));
                        renderContext.DrawImage(textRenderImage, textRenderImage.Size(), (Point)renderPosition, options);
                    }
                }
            }
            else
            {
                PointF endpoint1 = new PointF(context.GridRegion.Left + NegativeEndAxisMargin + ArrowLength, context.GridRegion.Top + context.Origin.Y);
                PointF endpoint2 = new PointF(context.GridRegion.Right - PositiveEndAxisMargin - ArrowLength, context.GridRegion.Top + context.Origin.Y);
                labelEndpoint = endpoint2;

                renderContext.DrawLines(Brush, LineThickness, new PointF[] { endpoint1, endpoint2 });
                if (ArrowWidth > 0 && ArrowLength > 0)
                {
                    if (EnableNegativeEndArrow)
                    {
                        // left, add X
                        renderContext.FillPolygon(ArrowColor, new PointF[] {
                            endpoint1 - new PointF(ArrowLength, 0),
                            endpoint1 + new PointF(0, -ArrowWidth / 2),
                            endpoint1 + new PointF(0, ArrowWidth / 2)
                        }, options);
                    }
                    if (EnablePositiveEndArrow)
                    {
                        // right, subtract X
                        renderContext.FillPolygon(ArrowColor, new PointF[] {
                            endpoint2 + new PointF(ArrowLength, 0),
                            endpoint2 - new PointF(0, -ArrowWidth / 2),
                            endpoint2 - new PointF(0, ArrowWidth / 2)
                        }, options);
                    }
                }

                if (TitleLabel != null)
                {
                    //var textBounds = TextMeasurer.Measure(TitleLabel.Text, new RendererOptions(TitleLabel.Font));
                    // TODO: why does this work without a textBounds displacement for re-origining?
                    var pos = context.GridRegion.Position() + context.Origin + TitleLabel.Displacement + new PointF(((endpoint2.X - (context.GridRegion.Left + context.Origin.X)) / 2), 0);
                    renderContext.DrawText(TitleLabel.Text, TitleLabel.Font, TitleLabel.Color, pos, options);
                }
            }

            if (EndpointLabel != null)
            {
                renderContext.DrawText(EndpointLabel.Text, EndpointLabel.Font, EndpointLabel.Color, labelEndpoint + EndpointLabel.Displacement, options);
            }
        }
Esempio n. 9
0
        private void DrawTextBox(IImageProcessingContext context, SoftwareKeyboardUiState state)
        {
            var inputTextRectangle = MeasureString(state.InputText, _inputTextFont);

            float boxWidth  = (int)(Math.Max(300, inputTextRectangle.Width + inputTextRectangle.X + 8));
            float boxHeight = 32;
            float boxY      = _panelRectangle.Y + 110;
            float boxX      = (int)((_panelRectangle.Width - boxWidth) / 2);

            RectangleF boxRectangle = new RectangleF(boxX, boxY, boxWidth, boxHeight);

            RectangleF boundRectangle = new RectangleF(_panelRectangle.X, boxY - _textBoxOutlineWidth,
                                                       _panelRectangle.Width, boxHeight + 2 * _textBoxOutlineWidth);

            context.Fill(_panelBrush, boundRectangle);

            context.Draw(_textBoxOutlinePen, boxRectangle);

            float inputTextX = (_panelRectangle.Width - inputTextRectangle.Width) / 2 - inputTextRectangle.X;
            float inputTextY = boxY + 5;

            var inputTextPosition = new PointF(inputTextX, inputTextY);

            context.DrawText(state.InputText, _inputTextFont, _textNormalColor, inputTextPosition);

            // Draw the cursor on top of the text and redraw the text with a different color if necessary.

            Color  cursorTextColor;
            IBrush cursorBrush;
            Pen    cursorPen;

            float cursorPositionYTop    = inputTextY + 1;
            float cursorPositionYBottom = cursorPositionYTop + _inputTextFontSize + 1;
            float cursorPositionXLeft;
            float cursorPositionXRight;

            bool cursorVisible = false;

            if (state.CursorBegin != state.CursorEnd)
            {
                Debug.Assert(state.InputText.Length > 0);

                cursorTextColor = _textSelectedColor;
                cursorBrush     = _selectionBoxBrush;
                cursorPen       = _selectionBoxPen;

                string textUntilBegin = state.InputText.Substring(0, state.CursorBegin);
                string textUntilEnd   = state.InputText.Substring(0, state.CursorEnd);

                var selectionBeginRectangle = MeasureString(textUntilBegin, _inputTextFont);
                var selectionEndRectangle   = MeasureString(textUntilEnd, _inputTextFont);

                cursorVisible        = true;
                cursorPositionXLeft  = inputTextX + selectionBeginRectangle.Width + selectionBeginRectangle.X;
                cursorPositionXRight = inputTextX + selectionEndRectangle.Width + selectionEndRectangle.X;
            }
            else
            {
                cursorTextColor = _textOverCursorColor;
                cursorBrush     = _cursorBrush;
                cursorPen       = _cursorPen;

                if (state.TextBoxBlinkCounter < TextBoxBlinkThreshold)
                {
                    // Show the blinking cursor.

                    int    cursorBegin         = Math.Min(state.InputText.Length, state.CursorBegin);
                    string textUntilCursor     = state.InputText.Substring(0, cursorBegin);
                    var    cursorTextRectangle = MeasureString(textUntilCursor, _inputTextFont);

                    cursorVisible       = true;
                    cursorPositionXLeft = inputTextX + cursorTextRectangle.Width + cursorTextRectangle.X;

                    if (state.OverwriteMode)
                    {
                        // The blinking cursor is in overwrite mode so it takes the size of a character.

                        if (state.CursorBegin < state.InputText.Length)
                        {
                            textUntilCursor      = state.InputText.Substring(0, cursorBegin + 1);
                            cursorTextRectangle  = MeasureString(textUntilCursor, _inputTextFont);
                            cursorPositionXRight = inputTextX + cursorTextRectangle.Width + cursorTextRectangle.X;
                        }
                        else
                        {
                            cursorPositionXRight = cursorPositionXLeft + _inputTextFontSize / 2;
                        }
                    }
                    else
                    {
                        // The blinking cursor is in insert mode so it is only a line.
                        cursorPositionXRight = cursorPositionXLeft;
                    }
                }
                else
                {
                    cursorPositionXLeft  = inputTextX;
                    cursorPositionXRight = inputTextX;
                }
            }

            if (state.TypingEnabled && cursorVisible)
            {
                float cursorWidth  = cursorPositionXRight - cursorPositionXLeft;
                float cursorHeight = cursorPositionYBottom - cursorPositionYTop;

                if (cursorWidth == 0)
                {
                    PointF[] points = new PointF[]
                    {
                        new PointF(cursorPositionXLeft, cursorPositionYTop),
                        new PointF(cursorPositionXLeft, cursorPositionYBottom),
                    };

                    context.DrawLines(cursorPen, points);
                }
                else
                {
                    var cursorRectangle = new RectangleF(cursorPositionXLeft, cursorPositionYTop, cursorWidth, cursorHeight);

                    context.Draw(cursorPen, cursorRectangle);
                    context.Fill(cursorBrush, cursorRectangle);

                    Image <Argb32> textOverCursor = new Image <Argb32>((int)cursorRectangle.Width, (int)cursorRectangle.Height);
                    textOverCursor.Mutate(context =>
                    {
                        var textRelativePosition = new PointF(inputTextPosition.X - cursorRectangle.X, inputTextPosition.Y - cursorRectangle.Y);
                        context.DrawText(state.InputText, _inputTextFont, cursorTextColor, textRelativePosition);
                    });

                    var cursorPosition = new Point((int)cursorRectangle.X, (int)cursorRectangle.Y);
                    context.DrawImage(textOverCursor, cursorPosition, 1);
                }
            }
            else if (!state.TypingEnabled)
            {
                // Just draw a semi-transparent rectangle on top to fade the component with the background.
                // TODO (caian): This will not work if one decides to add make background semi-transparent as well.

                context.Fill(_disabledBrush, boundRectangle);
            }
        }
Esempio n. 10
0
        private void DrawImage(IImageProcessingContext <Rgba32> processor, string name, int x, int y)
        {
            var image = Image.Load(_assetService.GetImagePath("Chess", $"{name}.png"));

            processor.DrawImage(image, new Size(50, 50), new Point(x * 50 + 117, y * 50 + 19), new GraphicsOptions());
        }
Esempio n. 11
0
 public static void ApplyFrame(this IImageProcessingContext ctx)
 {
     using var overlayImage = Image.Load("assets/frame.png");
     ctx.DrawImage(overlayImage, PixelColorBlendingMode.Normal, 1);
 }
Esempio n. 12
0
 private Matrix <float> ApplyStretchFit(IImageProcessingContext <Rgba32> context, Image <Rgba32> image)
 {
     context.Resize(image.Width, image.Height);
     context.DrawImage(GraphicsOptions, image, new Point(0, 0));
     return(GetProjectiveTransformationMatrix(image.Width, image.Height));
 }
Esempio n. 13
0
 private Matrix <float> ApplyNone(IImageProcessingContext <Rgba32> context, Image <Rgba32> image)
 {
     context.DrawImage(GraphicsOptions, image, new Point(0, 0));
     return(GetProjectiveTransformationMatrix());
 }
Esempio n. 14
0
 public static IImageProcessingContext <TPixel> BlitImage <TPixel>(this IImageProcessingContext <TPixel> source, Image <TPixel> image) where TPixel : struct, IPixel <TPixel>
 {
     return(source.DrawImage(image, PixelColorBlendingMode.Normal, PixelAlphaCompositionMode.Src, 1));
     // return source.DrawImage(image, 1, image.Size(), Point.Empty);
 }
Esempio n. 15
0
        public void Render(GraphBuilder context, IImageProcessingContext <Rgba32> renderContext, GraphicsOptions options)
        {
            PointF originPixelPos = (context.GridRegion.Position() + context.Origin);

            if (Axis.IsHorizontal)
            {
                float leftBound  = context.GridRegion.Left;
                float rightBound = context.GridRegion.Right;

                float xData = EnableForZero ? 0 : TickDistance;
                float x     = originPixelPos.X + context.ToPixelsHorizontal(xData);

                // doesn't lend itself to log scales...
                float pixelTick = context.ToPixelsHorizontal(TickDistance);

                while (x < rightBound)
                {
                    Image <Rgba32> generatedLabel = GenerateLabel(xData, options);
                    renderContext.DrawImage(generatedLabel, generatedLabel.Size(), new Point((int)x, (int)originPixelPos.Y), options);

                    x     += pixelTick;
                    xData += TickDistance;
                }

                xData = -TickDistance;
                x     = originPixelPos.X + context.ToPixelsHorizontal(xData);


                while (x > leftBound)
                {
                    x     -= pixelTick;
                    xData -= TickDistance;
                }
            }
            else
            {
                float topBound    = context.GridRegion.Left;
                float bottomBound = context.GridRegion.Right;

                float yData = EnableForZero ? 0 : TickDistance;
                float y     = originPixelPos.Y + context.ToPixelsVertical(yData);

                // doesn't lend itself to log scales...
                float pixelTick = context.ToPixelsVertical(TickDistance);

                while (y < bottomBound)
                {
                    Image <Rgba32> generatedLabel = GenerateLabel(yData, options);
                    renderContext.DrawImage(generatedLabel, generatedLabel.Size(), new Point((int)originPixelPos.X, (int)y), options);

                    y     += pixelTick;
                    yData += TickDistance;
                }

                yData = -TickDistance;
                y     = originPixelPos.Y + context.ToPixelsVertical(yData);


                while (y > topBound)
                {
                    Image <Rgba32> generatedLabel = GenerateLabel(yData, options);
                    renderContext.DrawImage(generatedLabel, generatedLabel.Size(), new Point((int)originPixelPos.X, (int)y), options);

                    y     -= pixelTick;
                    yData -= TickDistance;
                }
            }
        }
Esempio n. 16
0
 public static void DrawImage(this IImageProcessingContext <Rgba32> context, Image image, Rectangle rectangle)
 {
     using (Image resized = image.Clone(x => x.Resize(rectangle.Width, rectangle.Height)))
         context.DrawImage(resized, new Point(rectangle.X, rectangle.Y), 1f);
 }