예제 #1
0
        public bool FillElement(SKSurface surface, SKImageInfo dstinf, SKColor currentColor, int x, int y)
        {
            SKBitmap bitmap = new SKBitmap(dstinf.Width, dstinf.Height);

            IntPtr dstpixels = bitmap.GetPixels();

            surface.ReadPixels(dstinf, dstpixels, dstinf.RowBytes, 0, 0);

            var pointsToFound = new Stack <SKPoint>();

            if (bitmap.GetPixel(x, y) == currentColor)
            {
                return(false);
            }

            pointsToFound.Push(new SKPoint(x, y));

            var baseColor     = bitmap.GetPixel((int)Math.Round(BasePoint.X), (int)Math.Round(BasePoint.Y));
            var currentPoints = new Stack <SKPoint>();

            while (pointsToFound.Count > 0)
            {
                currentPoints.Push(pointsToFound.Pop());
                var currentPoint = currentPoints.Peek();
                var currentX     = (int)currentPoint.X;
                var currentY     = (int)currentPoint.Y;
                bitmap.SetPixel((int)currentPoint.X, (int)currentPoint.Y, currentColor);
                Points.Add(currentPoint);
                //Path.LineTo(currentX, currentY);
                if (currentX - 2 > 0 && currentY - 2 > 0 && currentY + 2 < bitmap.Height && currentX + 2 < bitmap.Width)
                {
                    if (bitmap.GetPixel(currentX, currentY + 2) == baseColor)
                    {
                        pointsToFound.Push(new SKPoint(currentX, currentY + 2));
                    }
                    if (bitmap.GetPixel(currentX, currentY - 2) == baseColor)
                    {
                        pointsToFound.Push(new SKPoint(currentX, currentY - 2));
                    }
                    if (bitmap.GetPixel(currentX + 2, currentY) == baseColor)
                    {
                        pointsToFound.Push(new SKPoint(currentX + 2, currentY));
                    }
                    if (bitmap.GetPixel(currentX - 2, currentY) == baseColor)
                    {
                        pointsToFound.Push(new SKPoint(currentX - 2, currentY));
                    }
                }
            }

            return(true);
        }
예제 #2
0
    /// <summary>
    /// Draws all layers on top of a <see cref="SKBitmap"/>
    /// </summary>
    /// <param name="document"></param>
    /// <returns>The <see cref="SKBitmap"/> instance</returns>
    public static SKBitmap LayersToSKBitmap(this SerializableDocument document)
    {
        SKImageInfo info = new(document.Width, document.Height, SKColorType.RgbaF32, SKAlphaType.Unpremul, SKColorSpace.CreateSrgb());

        using SKSurface surface = SKSurface.Create(info);
        SKCanvas canvas = surface.Canvas;

        using SKPaint paint = new();

        foreach (SerializableLayer layer in document)
        {
            if (layer.PngBytes == null || layer.PngBytes.Length == 0)
            {
                continue;
            }

            bool visible = document.Layers.GetFinalLayerVisibilty(layer);

            if (!visible)
            {
                continue;
            }

            double opacity = document.Layers.GetFinalLayerOpacity(layer);

            if (opacity == 0)
            {
                continue;
            }

            using SKColorFilter filter = SKColorFilter.CreateBlendMode(SKColors.White.WithAlpha((byte)(opacity * 255)), SKBlendMode.DstIn);
            paint.ColorFilter          = filter;

            canvas.DrawImage(layer.ToSKImage(), layer.OffsetX, layer.OffsetY, paint);
        }

        SKBitmap bitmap = new(info);

        surface.ReadPixels(info, bitmap.GetPixels(), info.RowBytes, 0, 0);

        return(bitmap);
    }
예제 #3
0
        public MainWindow()
        {
            InitializeComponent();
            Loaded += MainWindow_Loaded;

            //this is just a code to init everything and render image on a window to make sure everything is working as expected
            new GameWindow();
            _grContext = GRContext.Create(GRBackend.OpenGL);
            _imageInfo = new SKImageInfo(5996, 4003, SKColorType.Bgra8888, SKAlphaType.Premul);
            _surface   = SKSurface.Create(_grContext, false, _imageInfo, 1, GRSurfaceOrigin.TopLeft);
            _rasterImg = SKImage.FromEncodedData(File.ReadAllBytes("hires.jpg"));
            _surface.Canvas.DrawImage(_rasterImg, 0, 0);
            _surface.Canvas.Flush();

            Img.Source = _wb = new WriteableBitmap(_imageInfo.Width, _imageInfo.Height, 1000, 1000, PixelFormats.Bgra32,
                                                   BitmapPalettes.Halftone256Transparent);

            _surface.ReadPixels(_imageInfo, _wb.BackBuffer, _imageInfo.Width * 4, 0, 0);
            _wb.Lock();
            _wb.AddDirtyRect(new Int32Rect(0, 0, _wb.PixelWidth, _wb.PixelHeight));
            _wb.Unlock();
        }
예제 #4
0
            public void Test(SKSurface surface, SKImageInfo info, SKColor[] expected, Action <SKCanvas, SKPaint> preTestCallback = null)
            {
                using var shader = effect.ToShader(false, uniforms, children);
                Assert.NotNull(shader);

                using var paint = new SKPaint
                      {
                          Shader    = shader,
                          BlendMode = SKBlendMode.Src
                      };

                preTestCallback?.Invoke(surface.Canvas, paint);

                surface.Canvas.DrawPaint(paint);

                var actual = new SKColor[4];

                fixed(void *a = actual)
                {
                    Assert.True(surface.ReadPixels(info, (IntPtr)a, info.RowBytes, 0, 0));
                }

                Assert.Equal(expected, actual);
            }
예제 #5
0
        /// <summary>
        /// イメージ描写
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        void OnPaintSurface(object sender, SKPaintSurfaceEventArgs args)
        {
            if (_onCapture)
            {
                return;
            }

            SKImageInfo info    = args.Info;
            SKSurface   surface = args.Surface;
            SKCanvas    canvas  = surface.Canvas;

            if (bitmap.Height > bitmap.Width)
            {
                // 画像とレンズのデフォルト倍率
                scaledRatioDef = (double)bitmap.Height / (double)info.Height;

                // 画像のサーズ>レンズのサーズ
                if (scaledRatioDef > 1)
                {
                    info.Width  = (int)(info.Width * scaledRatioDef);
                    info.Height = bitmap.Height;
                }
            }
            else
            {
                // 画像とレンズのデフォルト倍率
                scaledRatioDef = (double)bitmap.Width / (double)info.Width;

                // 画像のサーズ>レンズのサーズ
                if (scaledRatioDef > 1)
                {
                    info.Height = (int)(info.Height * scaledRatioDef);
                    info.Width  = bitmap.Width;
                }
            }

            info.Width  = (int)(info.Width / scaledRatio);
            info.Height = (int)(info.Height / scaledRatio);

            if (bitmap.Width - info.Width <= 0)
            {
                defX      = (bitmap.Width - info.Width) / 2;
                lastXMove = 0;
            }
            else
            {
                defX = 0;

                if (lastXMove < 0)
                {
                    lastXMove = 0;
                }
                else if (canvasView.CanvasSize.Width + Math.Abs(lastXMove) > bitmap.Width * scaledRatio / scaledRatioDef)
                {
                    lastXMove = (bitmap.Width * scaledRatio / scaledRatioDef - canvasView.CanvasSize.Width);
                }
            }

            if (bitmap.Height - info.Height <= 0)
            {
                defY      = (bitmap.Height - info.Height) / 2;
                lastYMove = 0;
            }
            else
            {
                defY = 0;

                if (lastYMove < 0)
                {
                    lastYMove = 0;
                }
                else if (canvasView.CanvasSize.Height + Math.Abs(lastYMove) > bitmap.Height * scaledRatio / scaledRatioDef)
                {
                    lastYMove = (bitmap.Height * scaledRatio / scaledRatioDef - canvasView.CanvasSize.Height);
                }
            }

            SKRect mSrcRect  = SKRectI.Create((int)(lastXMove * scaledRatioDef / scaledRatio) + defX, (int)(lastYMove * scaledRatioDef / scaledRatio) + defY, info.Width, info.Height);
            SKRect mDestRect = SKRectI.Create(0, 0, (int)canvasView.CanvasSize.Width, (int)canvasView.CanvasSize.Height);

            canvas.Clear();
            canvas.DrawBitmap(bitmap, mSrcRect, mDestRect);

            SKPaint paint = new SKPaint
            {
                Style       = SKPaintStyle.Stroke,
                Color       = Xamarin.Forms.Color.White.ToSKColor(),
                StrokeWidth = 5
            };

            if (_colorChanged)
            {
                using (var bmp = new SKBitmap(info))
                {
                    IntPtr    dstpixels = bmp.GetPixels();
                    ColorCode color     = new ColorCode();

                    var succeed = surface.ReadPixels(info, dstpixels, info.RowBytes,
                                                     (int)(canvasView.CanvasSize.Width * _point.X / canvasView.Width),
                                                     (int)(canvasView.CanvasSize.Height * _point.Y / canvasView.Height));

                    if (Math.Abs(defX) > 0)
                    {
                        double x = Math.Abs(defX) * canvasView.Width / (canvasView.CanvasSize.Width * scaledRatioDef);
                        if (_point.X < x || _point.X > canvasView.Width - x)
                        {
                            succeed = false;
                        }
                    }

                    if (Math.Abs(defY) > 0)
                    {
                        double y = Math.Abs(defY) * canvasView.Height / (canvasView.CanvasSize.Height * scaledRatioDef);
                        if (_point.Y < y || _point.Y > canvasView.Height - y)
                        {
                            succeed = false;
                        }
                    }

                    if (succeed)
                    {
                        if (qList.Count < 10)
                        {
                            SKColor _selectedColor = bmp.GetPixel(0, 0);
                            lblRgb.Text = RGBConverter(_selectedColor);
                            string hex = ToHex(_selectedColor);
                            lblHex.Text = hex;
                            Rgb rgb = color.getRgb(hex);
                            lblHsl.Text     = color.getHslString(rgb);
                            lblHsv.Text     = color.getHsvString(rgb);
                            lblCmyk.Text    = color.getCmykString(rgb);
                            lblLab.Text     = color.getLabString(rgb);
                            lblMunsell.Text = color.getMunsellString(rgb);
                            lblPccs.Text    = color.getPCCSTone(rgb);
                            lblJis.Text     = JisUtil.getJisByMunsell(lblMunsell.Text);

                            paint = new SKPaint
                            {
                                Style       = SKPaintStyle.Stroke,
                                Color       = Xamarin.Forms.Color.White.ToSKColor(),
                                StrokeWidth = 5
                            };
                            canvas.DrawCircle((float)(canvasView.CanvasSize.Width * _point.X / canvasView.Width),
                                              (float)(canvasView.CanvasSize.Height * _point.Y / canvasView.Height), (float)(20 * scaledRatio), paint);


                            ImageColorInfo colorinfo = new ImageColorInfo();
                            colorinfo.ImageID      = _imageID;
                            colorinfo.HEXValue     = this.lblHex.Text;
                            colorinfo.RGBValue     = this.lblRgb.Text;
                            colorinfo.HSLValue     = this.lblHsl.Text;
                            colorinfo.HSVValue     = this.lblHsv.Text;
                            colorinfo.CMYKValue    = this.lblCmyk.Text;
                            colorinfo.LABValue     = this.lblLab.Text;
                            colorinfo.MUNSELLValue = this.lblMunsell.Text;
                            colorinfo.PCCSValue    = this.lblPccs.Text;
                            colorinfo.JISValue     = this.lblJis.Text;
                            colorinfo.XValue       = (int)(_point.X);
                            colorinfo.YValue       = (int)(_point.Y);

                            colorinfo.ScaledRatio = scaledRatio;
                            colorinfo.XPis        = lastXMove;
                            colorinfo.YPis        = lastYMove;
                            colorinfo.DefX        = defX * canvasView.Width / (canvasView.CanvasSize.Width * scaledRatioDef);
                            colorinfo.DefY        = defY * canvasView.Height / (canvasView.CanvasSize.Height * scaledRatioDef);

                            qList.Add(colorinfo);

                            btnColor.BackgroundColor = Xamarin.Forms.Color.FromHex(colorinfo.HEXValue);
                            lblSelected.Text         = colorinfo.HEXValue;
                            updatecolorBar(qList, colorinfo.HEXValue);
                        }
                    }
                }
            }
            else
            {
                updatecolorBar(qList);
            }

            foreach (ImageColorInfo point in qList)
            {
                canvas.DrawCircle(
                    (float)((canvasView.CanvasSize.Width * (point.XValue + point.DefX - (defX * canvasView.Width / (canvasView.CanvasSize.Width * scaledRatioDef))) / canvasView.Width) * (scaledRatio / point.ScaledRatio) - lastXMove + (point.XPis * (scaledRatio / point.ScaledRatio))),
                    (float)((canvasView.CanvasSize.Height * (point.YValue + point.DefY - (defY * canvasView.Height / (canvasView.CanvasSize.Height * scaledRatioDef))) / canvasView.Height) * (scaledRatio / point.ScaledRatio) - lastYMove + (point.YPis * (scaledRatio / point.ScaledRatio))),
                    (float)(20 * scaledRatio), paint);
            }

            _colorChanged = false;
        }