예제 #1
0
        /// <summary>
        /// Create sprite atlas from a string
        /// </summary>
        /// <param name="json">String with Json sprite file information</param>
        /// <param name="atlas">SKImage of sprite atlas</param>
        private void CreateSprites(string json, SKImage atlas)
        {
            var sprites = JsonConvert.DeserializeObject <Dictionary <string, Json.JsonSprite> >(json);

            foreach (var sprite in sprites)
            {
                // Extract sprite from atlas
                var image = atlas.Subset(new SKRectI(sprite.Value.X,
                                                     sprite.Value.Y,
                                                     sprite.Value.X + sprite.Value.Width,
                                                     sprite.Value.Y + sprite.Value.Height));

                AddSprite(sprite.Key, new MGLSprite(sprite, image));
            }
        }
예제 #2
0
        private async void HandlePaintCanvas(object sender, SKPaintSurfaceEventArgs e)
        {
            SKImageInfo info   = e.Info;
            SKCanvas    canvas = e.Surface.Canvas;

            canvas.Clear();
            if (_doSave)
            {
                using (var hole = new SKPath()) {
                    hole.AddCircle(info.Width / 2, info.Height / 2, info.Width / 3);
                    canvas.ClipPath(hole, antialias: true);
                }
            }
            canvas.SetMatrix(_m);
            //Draw ball image
            SKSize imgSize    = new SKSize(_bitmap.Width, _bitmap.Height);
            SKRect aspectRect = SKRect.Create(info.Width, info.Height).AspectFit(imgSize);

            canvas.DrawBitmap(_bitmap, aspectRect);
            if (!_doSave)
            {
                canvas.ResetMatrix();
                //Draw circle overlay
                using (var frame = new SKPath())
                    using (var hole = new SKPath()) {
                        frame.AddRect(info.Rect);
                        hole.AddCircle(info.Width / 2, info.Height / 2, info.Width / 3);
                        SKPath frameHole = frame.Op(hole, SKPathOp.Difference);
                        using (var p = new SKPaint {
                            IsAntialias = true, Style = SKPaintStyle.Fill, Color = new SKColor(128, 128, 128, 200)
                        }) {
                            canvas.DrawPath(frameHole, p);
                        }
                    }
            }
            else
            {
                SKImage snapI = e.Surface.Snapshot();
                snapI = snapI.Subset(canvas.DeviceClipBounds);
                SKData pngImage = snapI.Encode();
                File.WriteAllBytes(_ballFilename, pngImage.ToArray());
                await Navigation.PopAsync();

                OnBallImageUpdated(_ballFilename);
            }
        }
예제 #3
0
        private static List <SKImage> SplitImageBySeparators(SKImage image, IReadOnlyList <int> separators)
        {
            var subsets = new List <SKImage>();

            for (var i = 0; i < separators.Count - 1; i++)
            {
                var left   = separators[i];
                var right  = separators[i + 1];
                var width  = right - left;
                var height = image.Height;

                var crop = SKRectI.Create(width, height);
                crop.Left  = left;
                crop.Right = right;

                subsets.Add(image.Subset(crop));
            }

            return(subsets);
        }
        public static void sliceTiles(FileStream image, Tuple <double, double> topLeft, Tuple <double, double> bottomRight, string writePath)
        {
            const int tileLength         = 256;
            const int bottomLevel        = 3;
            int       tilesPerDim        = Convert.ToInt32(Math.Pow(2, Convert.ToDouble(bottomLevel)));
            int       pixelsPerDim       = tilesPerDim * tileLength;
            double    degreesLatPerPixel = 180d / pixelsPerDim;
            double    degreesLonPerPixel = degreesLatPerPixel * 2;


            Tuple <int, int> imageTopLeft     = new Tuple <int, int>(Convert.ToInt32((90 - topLeft.Item1) / degreesLatPerPixel), Convert.ToInt32((180 + topLeft.Item2) / degreesLonPerPixel));
            Tuple <int, int> imageBottomRight = new Tuple <int, int>(Convert.ToInt32((90 - bottomRight.Item1) / degreesLatPerPixel), Convert.ToInt32((180 + bottomRight.Item2) / degreesLonPerPixel));

            SKImage sKImage     = SKImage.FromEncodedData(image);
            var     fullSurface = SKSurface.Create(new SKImageInfo(pixelsPerDim, pixelsPerDim));
            var     canvas      = fullSurface.Canvas;

            SKBitmap scaledImage = new SKBitmap(imageBottomRight.Item2 - imageTopLeft.Item2, imageBottomRight.Item1 - imageTopLeft.Item1);

            SKBitmap.FromImage(sKImage).ScalePixels(scaledImage, SKFilterQuality.High);
            canvas.DrawBitmap(scaledImage, new SKPoint(imageTopLeft.Item2, imageTopLeft.Item1));

            SKImage fullImage   = fullSurface.Snapshot();
            var     tileSurface = SKSurface.Create(new SKImageInfo(tileLength, tileLength));

            canvas = tileSurface.Canvas;
            for (int y = 0; y < fullImage.Height; y += tileLength)
            {
                for (int x = 0; x < fullImage.Width; x += tileLength)
                {
                    SKBitmap tile = SKBitmap.FromImage(fullImage.Subset(SKRectI.Create(x, y, tileLength, tileLength)));
                    canvas.Clear(SKColors.Transparent);
                    canvas.DrawBitmap(tile, SKRect.Create(0, 0, tileLength, tileLength));

                    var stream = File.OpenWrite($"{writePath}/aqlatestL{bottomLevel}T{(x / tileLength).ToString("D2")}{(y / tileLength).ToString("D2")}.png");

                    var data = tileSurface.Snapshot().Encode();
                    data.SaveTo(stream);
                }
            }
        }
예제 #5
0
        /// <summary>
        /// Produces the array of <see cref="Chip"/>s from the specified image.
        /// </summary>
        /// <returns>An array of <see cref="Chip"/>s, each has size less than or equal to 300 x 300.</returns>
        /// <param name="image">The image to use to create chips.</param>
        public static IEnumerable <Chip> FromImage(SKImage image)
        {
            if (image == null)
            {
                throw new ArgumentNullException();
            }

            var width  = image.Width;
            var height = image.Height;

            var chips = new List <Chip>();

            var extraWidth  = width % SharedConstants.DefaultChipWidth;
            var extraHeight = height % SharedConstants.DefaultChipHeight;

            var numWidth  = width / SharedConstants.DefaultChipWidth + (extraWidth > 0 ? 1 : 0);
            var numHeight = height / SharedConstants.DefaultChipHeight + (extraHeight > 0 ? 1 : 0);

            for (var i = 0; i < numWidth; i++)
            {
                var x        = i * SharedConstants.DefaultChipWidth;
                var subWidth = i + 1 < numWidth || extraWidth == 0 ? SharedConstants.DefaultChipWidth : extraWidth;

                for (var j = 0; j < numHeight; j++)
                {
                    var y         = j * SharedConstants.DefaultChipHeight;
                    var subHeight = j + 1 < numHeight || extraHeight == 0
                        ? SharedConstants.DefaultChipHeight
                        : extraHeight;

                    var region   = new SKRectI(x, y, x + subWidth, y + subHeight);
                    var subImage = image.Subset(region);
                    var pixels   = GetPixelsFromImage(subImage);

                    chips.Add(new Chip(region, pixels));
                }
            }

            return(chips.ToArray());
        }
예제 #6
0
        private SKImage PrepareBackground(SKBitmap background)
        {
            // calculate aspect ratio
            float bgWidth  = background.Width;
            float bgHeight = background.Height;
            float ratio    = bgWidth / bgHeight;

            if (ratio > 0)
            {
                // image is horizontal
                // resize to fit background
                int width  = (int)(profileWidth * ratio);
                int height = profileHeight;
                background = background.Resize(new SKSizeI(width, height), SKFilterQuality.High);
            }
            else
            {
                // image is vertical
                // resize to fit background
                int width  = profileWidth;
                int height = (int)(profileHeight * ratio);
                background = background.Resize(new SKSizeI(width, height), SKFilterQuality.High);
            }

            // crop image
            // calculate subset rectangle
            int subsetX      = (background.Width - profileWidth) / 2;
            int subsetY      = (background.Height - profileHeight) / 2;
            int subsetWidth  = subsetX + profileWidth;
            int subsetHeight = subsetY + profileHeight;

            // convert bitmap to image
            SKImage image = SKImage.FromBitmap(background);

            // subset image from background
            SKRectI subsetRect = new SKRectI(subsetX, subsetY, subsetWidth, subsetHeight);

            return(image.Subset(subsetRect));
        }
예제 #7
0
        static void HandleImage(string Original, string Output)
        {
            SKBitmap Picture = SKBitmap.Decode(Original);

            SKBitmap[] Digits        = new SKBitmap[4];
            SKBitmap[] CroppedDigits = new SKBitmap[4];
            SKImage    Image         = SKImage.FromBitmap(Picture);

            for (int i = 0; i < 4; i++)
            {
                Digits[i]        = SKBitmap.FromImage(Image.Subset(new SKRectI(i * Picture.Width / 4, 0, (i + 1) * Picture.Width / 4, Picture.Height)));
                CroppedDigits[i] = Crop(Digits[i]);
                SKData     data   = SKImage.FromBitmap(CroppedDigits[i]).Encode(SKEncodedImageFormat.Jpeg, 100);
                FileStream stream = new FileStream(Output + i.ToString() + ".jpg", FileMode.Create, FileAccess.Write);
                data.SaveTo(stream);
                stream.Close();
                stream.Dispose();
                data.Dispose();
                Digits[i].Dispose();
                CroppedDigits[i].Dispose();
            }
            Image.Dispose();
            Picture.Dispose();
            Console.Write("The work has been completed. Press any key to exit..");
            Console.ReadKey(true);

            SKBitmap Crop(SKBitmap bitmap)
            {
                int width = bitmap.Width, height = bitmap.Height;
                int Left = 0, Top = 0, Right = 0, Bottom = 0;

                // Left
                for (int i = 0; i < width; i++)
                {
                    for (int j = 0; j < height; j++)
                    {
                        if (bitmap.GetPixel(i, j) == SKColors.Black)
                        {
                            Left = i;
                            i    = width;
                            break;
                        }
                    }
                }
                // Top
                for (int i = 0; i < height; i++)
                {
                    for (int j = 0; j < width; j++)
                    {
                        if (bitmap.GetPixel(j, i) == SKColors.Black)
                        {
                            Top = i;
                            i   = height;
                            break;
                        }
                    }
                }
                // Right
                for (int i = width; i > 0; i--)
                {
                    for (int j = 0; j < height; j++)
                    {
                        if (bitmap.GetPixel(i, j) == SKColors.Black)
                        {
                            Right = i;
                            i     = 0;
                            break;
                        }
                    }
                }
                // Bottom
                for (int i = height; i > 0; i--)
                {
                    for (int j = 0; j < width; j++)
                    {
                        if (bitmap.GetPixel(j, i) == SKColors.Black)
                        {
                            Bottom = i;
                            i      = 0;
                            break;
                        }
                    }
                }
                return(SKBitmap.FromImage(SKImage.FromBitmap(bitmap).Subset(new SKRectI(Left, Top, Right, Bottom))));
            }
        }