Example #1
0
        public void NonAntiAliasedTextOnScaledCanvasIsCorrect()
        {
            using (var bitmap = new SKBitmap(new SKImageInfo(200, 200)))
                using (var canvas = new SKCanvas(bitmap))
                    using (var tf = SKTypeface.FromFamilyName(DefaultFontFamily))
                        using (var paint = new SKPaint {
                            TextSize = 50, IsAntialias = true, Typeface = tf
                        })
                        {
                            canvas.Clear(SKColors.White);
                            canvas.Scale(1, 2);
                            canvas.DrawText("Skia", 10, 60, paint);

                            Assert.Equal(SKColors.Black, bitmap.GetPixel(49, 92));
                            Assert.Equal(SKColors.White, bitmap.GetPixel(73, 63));
                            Assert.Equal(SKColors.Black, bitmap.GetPixel(100, 89));
                        }

            using (var bitmap = new SKBitmap(new SKImageInfo(200, 200)))
                using (var canvas = new SKCanvas(bitmap))
                    using (var tf = SKTypeface.FromFamilyName(DefaultFontFamily))
                        using (var paint = new SKPaint {
                            TextSize = 50, Typeface = tf
                        })
                        {
                            canvas.Clear(SKColors.White);
                            canvas.Scale(1, 2);
                            canvas.DrawText("Skia", 10, 60, paint);

                            Assert.Equal(SKColors.Black, bitmap.GetPixel(49, 92));
                            Assert.Equal(SKColors.White, bitmap.GetPixel(73, 63));
                            Assert.Equal(SKColors.Black, bitmap.GetPixel(100, 89));
                        }
        }
        public string GetLightnessPixelsAsync(IFormFile file, int lightness)
        {
            SKBitmap bitmap = SKBitmap.Decode(file.OpenReadStream());

            for (int i = 0; i < bitmap.Width; ++i)
            {
                for (int j = 0; j < bitmap.Height; ++j)
                {
                    var rlin = PixelInfo.ConvertToLinear(PixelInfo.ConvertToDecimal(bitmap.GetPixel(i, j).Red));
                    var glin = PixelInfo.ConvertToLinear(PixelInfo.ConvertToDecimal(bitmap.GetPixel(i, j).Green));
                    var blin = PixelInfo.ConvertToLinear(PixelInfo.ConvertToDecimal(bitmap.GetPixel(i, j).Blue));

                    var Y = PixelInfo.CalculateYLuminence(rlin, glin, blin);

                    var lum = PixelInfo.CalculatePerceivedLightness(Y);
                    if (lum > lightness)
                    {
                        bitmap.SetPixel(i, j, new SKColor(103, 235, 52));
                    }
                }
            }

            var image = SKImage.FromBitmap(bitmap);

            var imageStream = image.Encode().AsStream();

            using (MemoryStream ms = new MemoryStream())
            {
                imageStream.CopyTo(ms);

                var bytesArray = ms.ToArray();

                return(Convert.ToBase64String(bytesArray));
            }
        }
Example #3
0
        public void DrawTransparentImageWithHighFilterQualityWithUnpremul()
        {
            var oceanColor = (SKColor)0xFF9EB4D6;
            var landColor  = (SKColor)0xFFACB69B;

            using (var bitmap = new SKBitmap(new SKImageInfo(300, 300)))
                using (var canvas = new SKCanvas(bitmap))
                {
                    canvas.Clear(oceanColor);

                    // decode the bitmap
                    var path = Path.Combine(PathToImages, "map.png");

                    using (var mapBitmap = SKBitmap.Decode(path))
                        using (var mapImage = SKImage.FromBitmap(mapBitmap))
                        {
                            var bounds = SKRect.Create(-259.9664f, -260.4489f, 1221.1876f, 1020.23273f);

                            // draw the bitmap
                            using (var paint = new SKPaint {
                                FilterQuality = SKFilterQuality.High
                            })
                            {
                                canvas.DrawImage(mapImage, bounds, paint);
                            }
                        }

                    // check values
                    Assert.Equal(oceanColor, bitmap.GetPixel(30, 30));
                    Assert.Equal(landColor, bitmap.GetPixel(270, 270));
                }
        }
        public void SvgRespectsClipPath()
        {
            var path       = Path.Combine(PathToImages, "clipping.svg");
            var background = (SKColor)0xffffffff;
            var fill       = (SKColor)0xff000000;

            var svg = new SKSvg();

            svg.Load(path);

            var bmp    = new SKBitmap((int)svg.CanvasSize.Width, (int)svg.CanvasSize.Height);
            var canvas = new SKCanvas(bmp);

            canvas.Clear(background);

            canvas.DrawPicture(svg.Picture);
            canvas.Flush();

            for (int x = 1; x < 20; x++)
            {
                for (int y = 1; y < 20; y++)
                {
                    Assert.Equal(fill, bmp.GetPixel(x, y));
                    Assert.Equal(background, bmp.GetPixel(x + 20, y + 20));
                }
            }
        }
Example #5
0
        public void NWayCanvasDrawsToMultipleCanvases()
        {
            using (var firstBitmap = new SKBitmap(new SKImageInfo(100, 100)))
                using (var first = new SKCanvas(firstBitmap))
                    using (var secondBitmap = new SKBitmap(new SKImageInfo(100, 100)))
                        using (var second = new SKCanvas(secondBitmap))
                        {
                            first.Clear(SKColors.Red);
                            second.Clear(SKColors.Green);

                            using (var canvas = new SKNWayCanvas(100, 100))
                            {
                                canvas.AddCanvas(first);
                                canvas.AddCanvas(second);

                                canvas.Clear(SKColors.Blue);

                                Assert.Equal(SKColors.Blue, firstBitmap.GetPixel(0, 0));
                                Assert.Equal(SKColors.Blue, secondBitmap.GetPixel(0, 0));

                                canvas.Clear(SKColors.Orange);
                            }

                            Assert.Equal(SKColors.Orange, firstBitmap.GetPixel(0, 0));
                            Assert.Equal(SKColors.Orange, secondBitmap.GetPixel(0, 0));
                        }
        }
Example #6
0
        public void CanScalePixels()
        {
            var srcInfo = new SKImageInfo(200, 200);
            var dstInfo = new SKImageInfo(100, 100);

            var srcBmp = new SKBitmap(srcInfo);
            var dstBmp = new SKBitmap(dstInfo);

            using (var canvas = new SKCanvas(srcBmp))
                using (var paint = new SKPaint {
                    Color = SKColors.Green
                })
                {
                    canvas.Clear(SKColors.Blue);
                    canvas.DrawRect(new SKRect(0, 0, 100, 200), paint);
                }

            Assert.Equal(SKColors.Green, srcBmp.GetPixel(75, 75));
            Assert.Equal(SKColors.Blue, srcBmp.GetPixel(175, 175));

            var srcPix = srcBmp.PeekPixels();
            var dstPix = dstBmp.PeekPixels();

            Assert.True(srcPix.ScalePixels(dstPix, SKFilterQuality.High));

            Assert.Equal(SKColors.Green, dstBmp.GetPixel(25, 25));
            Assert.Equal(SKColors.Blue, dstBmp.GetPixel(75, 75));
        }
        public void BitmapResizes()
        {
            var srcInfo = new SKImageInfo(200, 200);
            var dstInfo = new SKImageInfo(100, 100);

            var srcBmp = new SKBitmap(srcInfo);

            using (var canvas = new SKCanvas(srcBmp))
                using (var paint = new SKPaint {
                    Color = SKColors.Green
                }) {
                    canvas.Clear(SKColors.Blue);
                    canvas.DrawRect(new SKRect(0, 0, 100, 200), paint);
                }

            Assert.AreEqual(SKColors.Green, srcBmp.GetPixel(75, 75));
            Assert.AreEqual(SKColors.Blue, srcBmp.GetPixel(175, 175));

            var dstBmp = srcBmp.Resize(dstInfo, SKBitmapResizeMethod.Mitchell);

            Assert.NotNull(dstBmp);

            Assert.AreEqual(SKColors.Green, dstBmp.GetPixel(25, 25));
            Assert.AreEqual(SKColors.Blue, dstBmp.GetPixel(75, 75));
        }
Example #8
0
        public void DecodingWithPathAndDrawingOnGPUCreatesCorrectImage()
        {
            var info = new SKImageInfo(120, 120);
            var path = Path.Combine(PathToImages, "vimeo_icon_dark.png");

            using (var ctx = CreateGlContext())
            {
                ctx.MakeCurrent();

                using (var grContext = GRContext.CreateGl())
                    using (var surface = SKSurface.Create(grContext, true, info))
                    {
                        var canvas = surface.Canvas;

                        canvas.Clear(SKColors.Crimson);

                        using (var image = SKImage.FromEncodedData(path))
                        {
                            canvas.DrawImage(image, 0, 0);
                        }

                        using (var bmp = new SKBitmap(info))
                        {
                            surface.ReadPixels(info, bmp.GetPixels(), info.RowBytes, 0, 0);

                            Assert.Equal(SKColors.Crimson, bmp.GetPixel(3, 3));
                            Assert.Equal(SKColors.Crimson, bmp.GetPixel(70, 50));
                            Assert.Equal(new SKColor(23, 35, 34), bmp.GetPixel(40, 40));
                        }
                    }
            }
        }
Example #9
0
        public void CanScalePixels()
        {
            var srcInfo = new SKImageInfo(200, 200);
            var dstInfo = new SKImageInfo(100, 100);

            var srcSurface = SKSurface.Create(srcInfo);
            var dstBmp     = new SKBitmap(dstInfo);

            using (var paint = new SKPaint {
                Color = SKColors.Green
            })
            {
                srcSurface.Canvas.Clear(SKColors.Blue);
                srcSurface.Canvas.DrawRect(new SKRect(0, 0, 100, 200), paint);
            }

            var srcImage = srcSurface.Snapshot();
            var srcPix   = srcImage.PeekPixels();
            var dstPix   = dstBmp.PeekPixels();

            Assert.Equal(SKColors.Green, srcPix.GetPixelColor(75, 75));
            Assert.Equal(SKColors.Blue, srcPix.GetPixelColor(175, 175));

            Assert.True(srcImage.ScalePixels(dstPix, SKFilterQuality.High));

            Assert.Equal(SKColors.Green, dstBmp.GetPixel(25, 25));
            Assert.Equal(SKColors.Blue, dstBmp.GetPixel(75, 75));
        }
Example #10
0
        public void DecodeImageScanlines()
        {
            var path        = Path.Combine(PathToImages, "CMYK.jpg");
            var imageHeight = 516;

            var fileData      = File.ReadAllBytes(path);
            var correctBitmap = SKBitmap.Decode(path);

            var stream = new SKFileStream(path);

            using (var codec = SKCodec.Create(stream))
            {
                var info = new SKImageInfo(codec.Info.Width, codec.Info.Height);
                using (var scanlineBitmap = new SKBitmap(info))
                {
                    scanlineBitmap.Erase(SKColors.Fuchsia);

                    var result = codec.StartScanlineDecode(info);
                    Assert.Equal(SKCodecResult.Success, result);

                    Assert.Equal(SKCodecScanlineOrder.TopDown, codec.ScanlineOrder);
                    Assert.Equal(0, codec.NextScanline);

                    // only decode every second line
                    for (int y = 0; y < info.Height; y += 2)
                    {
                        Assert.Equal(1, codec.GetScanlines(scanlineBitmap.GetAddress(0, y), 1, info.RowBytes));
                        Assert.Equal(y + 1, codec.NextScanline);
                        if (codec.SkipScanlines(1))
                        {
                            Assert.Equal(y + 2, codec.NextScanline);
                        }
                        else
                        {
                            Assert.Equal(imageHeight, codec.NextScanline);                             // reached the end
                        }
                    }

                    Assert.False(codec.SkipScanlines(1));
                    Assert.Equal(imageHeight, codec.NextScanline);

                    for (var x = 0; x < info.Width; x++)
                    {
                        for (var y = 0; y < info.Height; y++)
                        {
                            if (y % 2 == 0)
                            {
                                Assert.Equal(correctBitmap.GetPixel(x, y), scanlineBitmap.GetPixel(x, y));
                            }
                            else
                            {
                                Assert.Equal(SKColors.Fuchsia, scanlineBitmap.GetPixel(x, y));
                            }
                        }
                    }
                }
            }
        }
Example #11
0
        private SKRectI BuildCenter(SKBitmap bitmap)
        {
            var left   = (bitmap.Width - 2) / 2;
            var right  = left + 1;
            var top    = (bitmap.Height - 2) / 2;
            var bottom = top + 1;

            var xs = new List <int>();

            for (int i = 1; i < bitmap.Width - 1; i++)
            {
                var color = bitmap.GetPixel(i, 0);
                if (color.Alpha != 0)
                {
                    xs.Add(i - 1);
                }
            }
            if (xs.Count > 0)
            {
                left = xs.Min();
                if (xs.Count == 1)
                {
                    right = left + 1;
                }
                else
                {
                    right = xs.Max();
                }
            }

            var ys = new List <int>();

            for (int i = 1; i < bitmap.Height - 1; i++)
            {
                var color = bitmap.GetPixel(i, 0);
                if (color.Alpha != 0)
                {
                    ys.Add(i - 1);
                }
            }
            if (ys.Count > 0)
            {
                top = ys.Min();
                if (ys.Count == 1)
                {
                    bottom = top + 1;
                }
                else
                {
                    bottom = ys.Max();
                }
            }

            var result = new SKRectI(left, top, right, bottom);

            return(result);
        }
Example #12
0
        protected static void ValidateTestBitmap(SKBitmap bmp, byte alpha = 255)
        {
            Assert.NotNull(bmp);
            Assert.Equal(40, bmp.Width);
            Assert.Equal(40, bmp.Height);

            Assert.Equal(SKColors.Red.WithAlpha(alpha), bmp.GetPixel(10, 10));
            Assert.Equal(SKColors.Green.WithAlpha(alpha), bmp.GetPixel(30, 10));
            Assert.Equal(SKColors.Blue.WithAlpha(alpha), bmp.GetPixel(10, 30));
            Assert.Equal(SKColors.Yellow.WithAlpha(alpha), bmp.GetPixel(30, 30));
        }
Example #13
0
        private void ValidateTestBitmap(SKBitmap bmp)
        {
            Assert.IsNotNull(bmp);
            Assert.AreEqual(40, bmp.Width);
            Assert.AreEqual(40, bmp.Height);

            Assert.AreEqual(SKColors.Red, bmp.GetPixel(10, 10));
            Assert.AreEqual(SKColors.Green, bmp.GetPixel(30, 10));
            Assert.AreEqual(SKColors.Blue, bmp.GetPixel(10, 30));
            Assert.AreEqual(SKColors.Yellow, bmp.GetPixel(30, 30));
        }
Example #14
0
        public void SwizzleRedBlueTest()
        {
            var info = new SKImageInfo(1, 1);

            using (var bmp = new SKBitmap(info))
            {
                bmp.Erase((uint)0xFACEB004);

                Assert.Equal((uint)0xFACEB004, (uint)bmp.GetPixel(0, 0));

                SKSwizzle.SwapRedBlue(bmp.GetPixels(), bmp.GetPixels(), 1);

                Assert.Equal((uint)0xFA04B0CE, (uint)bmp.GetPixel(0, 0));
            }
        }
Example #15
0
            private SKBitmap ReadRegion(SizeL location, int level, SizeL size)
            {
                Stopwatch sw = new Stopwatch();

                sw.Start();
                OpenSlide.TraceMsg("start ReadRegion " + level + "/" + location.Height + "_" + location.Width + ": " + GetBytesReadable(size.Width * size.Height * 3));
                SKBitmap bmp = new SKBitmap((int)size.Width, (int)size.Height);

                bmp.SetPixel(0, 0, SKColors.AliceBlue);
                bmp.LockPixels();
                var bmpdata = bmp.GetPixels();

                OpenSlide.TraceMsg("bmp locked " + level + "/" + location.Height + "_" + location.Width);
                unsafe
                {
                    void *p = bmpdata.ToPointer();
                    Import.openslide_read_region(handle, p, location.Width, location.Height, level, size.Width, size.Height);
                }
                OpenSlide.TraceMsg("read finished " + level + "/" + location.Height + "_" + location.Width + ": " + GetBytesReadable(size.Width * size.Height * 3 / Math.Max(sw.ElapsedMilliseconds, 1)) + "/ms");
                bmp.UnlockPixels();
                OpenSlide.TraceMsg("unlock bits " + level + "/" + location.Height + "_" + location.Width);
                if (bmp.GetPixel(0, 0) == SKColors.Black)
                {
                    var error = CheckForLastError();
                    if (error != null)
                    {
                        throw new ArgumentException($"error reading region loc:{location}, level:{level}, size:{size}" + error);
                    }
                    //else just a black image?
                }
                OpenSlide.TraceMsg("end ReadRegion " + level + "/" + location.Height + "_" + location.Width);
                return(bmp);
            }
Example #16
0
        private new void DrawBackground(SKCanvas c)
        {
            c.DrawRect(new SKRect(Margin, Margin, Width - Margin, Height - Margin),
                       new SKPaint
            {
                IsAntialias = true, FilterQuality = SKFilterQuality.High,
                Shader      = SKShader.CreateRadialGradient(new SKPoint(Width / 2, Height / 2), Width / 5 * 2,
                                                            new[] { Background[0], Background[1] },
                                                            SKShaderTileMode.Clamp)
            });

            for (var i = 0; i < _backgroundOverlay.Width; i++)
            {
                for (var j = 0; j < _backgroundOverlay.Height; j++)
                {
                    if (_backgroundOverlay.GetPixel(i, j) == SKColors.Black)
                    {
                        _backgroundOverlay.SetPixel(i, j, SKColors.Transparent);
                    }
                }
            }

            c.DrawBitmap(_backgroundOverlay, new SKRect(Margin, Margin, Width - Margin, Height - Margin), new SKPaint
            {
                IsAntialias = true,
                ColorFilter = SKColorFilter.CreateBlendMode(SKColors.Black.WithAlpha(150), SKBlendMode.DstIn),
                ImageFilter = SKImageFilter.CreateDropShadow(2, 2, 4, 4, new SKColor(0, 0, 0))
            });
            c.DrawColor(SKColors.Black.WithAlpha(125), SKBlendMode.DstIn);
        }
Example #17
0
        /// <summary>
        /// Separate Image into Black and Red Image
        /// </summary>
        /// <param name="image"></param>
        /// <param name="imageBw"></param>
        /// <param name="imageRed"></param>
        private static void SeparateColors(SKBitmap image, out SKBitmap imageBw, out SKBitmap imageRed)
        {
            int width  = image.Width;
            int height = image.Height;

            imageBw  = new SKBitmap(width, height);
            imageRed = new SKBitmap(width, height);

            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    var pixel = image.GetPixel(x, y);

                    if (IsRed(pixel.Red, pixel.Green, pixel.Blue))
                    {
                        imageRed.SetPixel(x, y, SKColors.Red);
                        imageBw.SetPixel(x, y, SKColors.Black);
                    }
                    else
                    {
                        imageBw.SetPixel(x, y, pixel);
                        imageRed.SetPixel(x, y, SKColors.Black);
                    }
                }
            }
        }
Example #18
0
 /// <summary>
 /// Открыть файл с изображением
 /// </summary>
 /// <param name="path"></param>
 /// <returns></returns>
 public static Image Open(string path)
 {
     if (!File.Exists(path))
     {
         throw new ArgumentException("File does not exists");
     }
     using (SKBitmap image = SKBitmap.Decode(path))
     {
         if (image == null)
         {
             throw new ArgumentException("Failed to open image");
         }
         var result = new Image(image.Height, image.Width);
         for (int i = 0; i < image.Height; i++)
         {
             for (int j = 0; j < image.Width; j++)
             {
                 var pixel = image.GetPixel(j, i);
                 result[i, j, 0] = pixel.Red;
                 result[i, j, 1] = pixel.Green;
                 result[i, j, 2] = pixel.Blue;
             }
         }
         return(result);
     }
 }
Example #19
0
        /// <summary>
        /// Získá obrázek v podobě pole bajtů
        /// </summary>
        /// <returns></returns>
        internal byte[] GetBitmapByByteArray()
        {
            List <byte> bitmap = new List <byte>();

            if (_SKBitmap.Width % 8 == 0)
            {
                StringBuilder sbLine = new StringBuilder();
                for (int ii = 0; ii < _SKBitmap.Height; ii++)
                {
                    // loop each row of image
                    for (int jj = 0; jj < _SKBitmap.Width; jj++)
                    {
                        // loop each pixel in row and add to sbLine string to build
                        // string of bits for black and white image
                        SKColor pixelColor = _SKBitmap.GetPixel(jj, ii);
                        sbLine.Append(HexConverter(pixelColor));
                    }
                    // convert sbline string to byte array
                    byte[] buffer = GetBytes(sbLine.ToString());
                    bitmap.AddRange(buffer);
                    sbLine.Clear();
                }
            }
            else
            {
                throw new Exception("CMLChartCanvas.GetBitmap() - Image is not a multiple of 8 pixels wide");
            }

            return(bitmap.ToArray());
        }
Example #20
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            string   uri   = Console.ReadLine();
            SKBitmap Image = null;

            Load(ref Image, uri);

            Console.WriteLine(Image.GetPixel(0, 0));

            if (Exist2(Image))
            {
                Console.WriteLine("Ano");
            }
            else
            {
                Console.WriteLine("Ne");
            }

            Console.WriteLine(s + " : " + d);
            Console.WriteLine(Image.Width);
            Console.WriteLine(Image.Height);

            Strips(Image);

            foreach (SKColor color in colors)
            {
                Console.WriteLine(color);
            }
        }
Example #21
0
        private async Task CreateWithPickedBitmap()
        {
            if (_applyDitherBitmap == null)
            {
                return;
            }

            for (int x = 0; x < _applyDitherBitmap.Width; x++)
            {
                for (int y = 0; y < _applyDitherBitmap.Height; y++)
                {
                    var pixel = _applyDitherBitmap.GetPixel(x, y);

                    var color1     = _selectedColorPalette.Colors.OrderBy(colorFromPalette => ColorDistance(pixel, colorFromPalette)).FirstOrDefault();
                    var colorDist1 = ColorDistance(pixel, color1);
                    var color2     = _selectedColorPalette.Colors.OrderBy(colorFromPalette => ColorDistance(pixel, colorFromPalette)).ElementAt(1);
                    var colorDist2 = ColorDistance(pixel, color2);

                    var ditherFactor = DitherSlider.Value * 50;

                    if (Math.Abs(colorDist1 - colorDist2) > ditherFactor || (x % 2 == y % 2))
                    {
                        _applyDitherBitmap.SetPixel(x, y, color1);
                    }
                    else
                    {
                        _applyDitherBitmap.SetPixel(x, y, color2);
                    }
                }
            }
        }
        public SKBitmap GetDecorateSelectedBitmap()
        {
            SKBitmap initialImageStretched = new SKBitmap(DecorateSelectionImageBitmap.Width, DecorateSelectionImageBitmap.Height);

            using (SKCanvas canvas = new SKCanvas(initialImageStretched))
            {
                canvas.DrawBitmap(
                    DecorateInitialImageBitmap,
                    new SKRect(0, 0, DecorateSelectionImageBitmap.Width, DecorateSelectionImageBitmap.Height),
                    BitmapStretch.Uniform);
            }

            DecorateInitialImageStretchedBitmap = initialImageStretched;

            SKBitmap selectedBitmap = new SKBitmap(DecorateSelectionImageBitmap.Width, DecorateSelectionImageBitmap.Height);

            for (int x = 0; x < DecorateSelectionImageBitmap.Width; ++x)
            {
                for (int y = 0; y < DecorateSelectionImageBitmap.Height; ++y)
                {
                    selectedBitmap.SetPixel(x, y,
                                            DecorateSelectionImageBitmap.GetPixel(x, y).Red == 0
                            ? new SKColor(0, 0, 0, 0)
                            : initialImageStretched.GetPixel(x, y));
                }
            }

            return(selectedBitmap);
        }
Example #23
0
        public void OverdrawCanvasDrawsProperly()
        {
            using (var bitmap = new SKBitmap(new SKImageInfo(100, 100)))
                using (var canvas = new SKCanvas(bitmap))
                    using (var overdraw = new SKOverdrawCanvas(canvas))
                    {
                        bitmap.Erase(SKColors.Transparent);

                        overdraw.DrawRect(SKRect.Create(10, 10, 30, 30), new SKPaint());
                        overdraw.DrawRect(SKRect.Create(20, 20, 30, 30), new SKPaint());

                        Assert.Equal(0, bitmap.GetPixel(5, 5).Alpha);
                        Assert.Equal(1, bitmap.GetPixel(15, 15).Alpha);
                        Assert.Equal(2, bitmap.GetPixel(25, 25).Alpha);
                        Assert.Equal(1, bitmap.GetPixel(45, 45).Alpha);
                    }
        }
        public static int GetLetterboxHeight(Stream file)
        {
            SKBitmap bitmap      = SKBitmap.Decode(file);
            var      centerWidth = bitmap.Width / 2;
            var      zeroPoint   = bitmap.GetPixel(centerWidth, 0);
            SKColor  somePoint;

            for (int i = 1; i < bitmap.Height; i++)
            {
                somePoint = bitmap.GetPixel(centerWidth, i);
                if (somePoint != zeroPoint)
                {
                    return(i);
                }
            }
            throw new Exception();
        }
Example #25
0
        public void DrawShapedTextExtensionMethodDraws()
        {
            using (var bitmap = new SKBitmap(new SKImageInfo(512, 512)))
                using (var canvas = new SKCanvas(bitmap))
                    using (var tf = SKTypeface.FromFile(Path.Combine(PathToFonts, "content-font.ttf")))
                        using (var shaper = new SKShaper(tf))
                            using (var paint = new SKPaint {
                                IsAntialias = true, TextSize = 64, Typeface = tf
                            })
                            {
                                canvas.Clear(SKColors.White);

                                canvas.DrawShapedText(shaper, "متن", 100, 200, paint);

                                canvas.Flush();

                                Assert.Equal(SKColors.Black, bitmap.GetPixel(110, 210));
                                Assert.Equal(SKColors.Black, bitmap.GetPixel(127, 196));
                                Assert.Equal(SKColors.Black, bitmap.GetPixel(142, 197));
                                Assert.Equal(SKColors.Black, bitmap.GetPixel(155, 195));
                                Assert.Equal(SKColors.Black, bitmap.GetPixel(131, 181));
                                Assert.Equal(SKColors.White, bitmap.GetPixel(155, 190));
                                Assert.Equal(SKColors.White, bitmap.GetPixel(110, 200));
                            }
        }
Example #26
0
        private async void btnSolve_Clicked(object sender, EventArgs e)
        {
            //BinaryReader reader = new BinaryReader(GetImageStream());

            try
            {
                if (_MediaFile == null)
                {
                    return;
                }

                IsLoading = true;

                var listbyte = await Task.Run(() =>
                {
                    SKBitmap myBitmap           = new SKBitmap();
                    myBitmap                    = SKBitmap.Decode(_MediaFile.GetStream());
                    List <double> listbyte_wait = new List <double>();
                    for (int j = 0; j < myBitmap.Height; j++)
                    {
                        for (int i = 0; i < myBitmap.Width; i++)
                        {
                            //for (int j = 0; j < myBitmap.Height; j++)
                            //{
                            var color = myBitmap.GetPixel(i, j);
                            listbyte_wait.Add(((int)color.Red) / 255.00);
                            listbyte_wait.Add(((int)color.Green) / 255.00);
                            listbyte_wait.Add(((int)color.Blue) / 255.00);
                        }
                    }

                    return(listbyte_wait);
                });

                if (listbyte.Count == 7581600)
                {
                    Predictor predictor  = new Predictor(App.assembly);
                    var       UnUsedMove = await predictor.PredictUnUsedMoveAsync(listbyte.ToArray());

                    var MoveLimit = await predictor.PredictMoveLimitAsync(listbyte.ToArray());
                    await DisplayAlert("Prediction", $"UnUsedMove : {UnUsedMove}{Environment.NewLine}MoveLimit : {MoveLimit}", "OK");
                }
                else
                {
                    await DisplayAlert("Not Support This Image", listbyte.Count.ToString(), "OK");
                }
            }
            catch (Exception ex)
            {
                await DisplayAlert("Error", ex.Message, "OK");
            }
            finally
            {
                IsLoading = false;
            }
        }
Example #27
0
        public void LinearShaderDrawColorCorrectly()
        {
            var size      = 160;
            var colors    = new[] { SKColors.Blue, SKColors.Gray, SKColors.Gray, SKColors.Red };
            var positions = new[] { 0.1f, 0.4f, 0.6f, 0.9f };

            using var bitmap = new SKBitmap(new SKImageInfo(size, size));
            using var canvas = new SKCanvas(bitmap);
            using var p      = new SKPaint
                  {
                      Shader = SKShader.CreateLinearGradient(new SKPoint(10, 10), new SKPoint(150, 10), colors, positions, SKShaderTileMode.Clamp)
                  };

            canvas.DrawRect(SKRect.Create(size, size), p);

            Assert.Equal(SKColors.Blue, bitmap.GetPixel(1, 0));
            Assert.Equal(SKColors.Gray, bitmap.GetPixel(80, 0));
            Assert.Equal(SKColors.Red, bitmap.GetPixel(159, 0));
        }
Example #28
0
        public void CanCreateUsingRowBytes()
        {
            using var src = CreateTestBitmap();

            var xOffset = 19 * src.BytesPerPixel;
            var yOffset = 19 * src.RowBytes;
            var offset  = yOffset + xOffset;

            using var bmp = new SKBitmap();
            bmp.InstallPixels(new SKImageInfo(2, 2), src.GetPixels() + offset, src.RowBytes);

            Assert.Equal(2, bmp.Width);
            Assert.Equal(2, bmp.Height);

            Assert.Equal(SKColors.Red, bmp.GetPixel(0, 0));
            Assert.Equal(SKColors.Green, bmp.GetPixel(1, 0));
            Assert.Equal(SKColors.Blue, bmp.GetPixel(0, 1));
            Assert.Equal(SKColors.Yellow, bmp.GetPixel(1, 1));
        }
Example #29
0
 private static bool IsTransparentColumn(SKBitmap bmp, int col)
 {
     for (var i = 0; i < bmp.Height; ++i)
     {
         if (!IsTransparent(bmp.GetPixel(col, i)))
         {
             return(false);
         }
     }
     return(true);
 }
Example #30
0
 private static bool IsTransparentRow(SKBitmap bmp, int row)
 {
     for (var i = 0; i < bmp.Width; ++i)
     {
         if (!IsTransparent(bmp.GetPixel(i, row)))
         {
             return(false);
         }
     }
     return(true);
 }