public void ImmutableBitmapsAreNotCopied()
        {
            // create "pixel data"
            var pixelData = new SKBitmap(100, 100);

            pixelData.Erase(SKColors.Red);

            // create text bitmap
            var bitmap = new SKBitmap();

            bitmap.InstallPixels(pixelData.PeekPixels());

            // mark it as immutable
            bitmap.SetImmutable();

            // create an image
            var image = SKImage.FromBitmap(bitmap);

            Assert.Equal(SKColors.Red, image.PeekPixels().GetPixelColor(50, 50));

            // modify the "pixel data"
            pixelData.Erase(SKColors.Blue);

            // ensure that the pixels are modified
            Assert.Equal(SKColors.Blue, image.PeekPixels().GetPixelColor(50, 50));
        }
Exemple #2
0
        public void ImmutableBitmapsAreNotCopied()
        {
            // this is a really weird test as it is a mutable bitmap that is marked as immutable

            var bitmap = new SKBitmap(100, 100);

            bitmap.Erase(SKColors.Red);
            bitmap.SetImmutable();

            var image = SKImage.FromBitmap(bitmap);

            Assert.Equal(SKColors.Red, image.PeekPixels().GetPixelColor(50, 50));

            bitmap.Erase(SKColors.Blue);
            Assert.Equal(SKColors.Blue, image.PeekPixels().GetPixelColor(50, 50));
        }
        private static SKBitmap[] LoadTiles(SKBitmap texture, int frameCount)
        {
            var src      = new SKRectI(0, 0, texture.Width / frameCount, texture.Height);
            var tileInfo = texture.Info.WithSize(src.Width, src.Height);

            Debug.Assert(tileInfo.RowBytes == src.Width * texture.Info.BytesPerPixel);

            var ret = new SKBitmap[frameCount];

            for (int i = 0; i < frameCount; i++, src.Offset(src.Width, 0))
            {
                var tile = new SKBitmap(tileInfo);
                if (!texture.ExtractSubset(tile, src))
                {
                    throw new InvalidOperationException($"Failed to extract tile {i} from texture {texture}");
                }

                tile.SetImmutable();
                ret[i] = tile;
            }

            return(ret);
        }
Exemple #4
0
        private void RenderPicker(int w = 0, int h = 0)
        {
            if (_container == null)
            {
                return;
            }

            double width  = w > 0 ? w : this.Width;
            double height = h > 0 ? h : this.Height;

            if (width == -1 || height == -1)
            {
                return;
            }

            double colorVal = this.ColorValue;
            double offset   = this.HueOffset;
            bool   invert   = this.InvertSaturation;
            float  esize    = this.ElementSize;

            ColorPickerMode mode = this.Mode;

            _ = Task.Run(() =>
            {
                ColorPickerRenderer cw;

                if (w <= 0)
                {
                    if (double.IsNaN(width))
                    {
                        return;
                    }
                    w = (int)width;
                }
                if (h <= 0)
                {
                    if (double.IsNaN(height))
                    {
                        return;
                    }
                    h = (int)height;
                }

                if (w < 32 || h < 32)
                {
                    return;
                }

                if (mode == ColorPickerMode.Wheel || mode == ColorPickerMode.HexagonWheel)
                {
                    int rad;

                    if (h < w)
                    {
                        rad = h / 2;
                        w   = h;
                    }
                    else
                    {
                        rad = w / 2;
                        h   = w;
                    }

                    cw = new ColorPickerRenderer(rad, colorVal, offset, invert, true);
                }
                else
                {
                    cw = new ColorPickerRenderer(w, h, colorVal, offset, invert, mode == ColorPickerMode.LinearVertical, true);
                }

                DispatcherQueue.TryEnqueue(async() =>
                {
                    SKImage img;
                    SKBitmap bmp = new SKBitmap((int)cw.Bounds.Width, (int)cw.Bounds.Height, SKColorType.Bgra8888, SKAlphaType.Premul);

                    var ptr = bmp.GetPixels();

                    unsafe
                    {
                        var gch = GCHandle.Alloc(cw.ImageBytes, GCHandleType.Pinned);

                        Buffer.MemoryCopy((void *)gch.AddrOfPinnedObject(), (void *)ptr, cw.ImageBytes.Length, cw.ImageBytes.Length);
                        gch.Free();
                    }

                    bmp.SetImmutable();
                    img = SKImage.FromBitmap(bmp);

                    SKData encoded = img.Encode();
                    Stream stream  = encoded.AsStream();

                    //var ret = ImageSource.FromStream(() => stream);

                    cpRender = cw;


                    Compositor _compositor = _container.Compositor;
                    SpriteVisual _imageVisual;
                    CompositionSurfaceBrush _imageBrush;

                    _imageBrush = _compositor.CreateSurfaceBrush();

                    // The loadedSurface has a size of 0x0 till the image has been been downloaded, decoded and loaded to the surface. We can assign the surface to the CompositionSurfaceBrush and it will show up once the image is loaded to the surface.
                    LoadedImageSurface _loadedSurface = LoadedImageSurface.StartLoadFromStream(stream.AsRandomAccessStream());

                    _imageBrush.Surface = _loadedSurface;

                    _imageVisual       = _compositor.CreateSpriteVisual();
                    _imageVisual.Brush = _imageBrush;
                    _imageVisual.Size  = new Vector2(cw.Bounds.Width, cw.Bounds.Height);
                    //_imageVisual.Offset = new Vector3((float)Padding.Left, (float)Padding.Top, 0);

                    _container.Children.RemoveAll();
                    _container.Children.InsertAtBottom(_imageVisual);

                    currentShape  = null;
                    currentSprite = null;
                    currentGeo    = null;
                });
            });
        }
Exemple #5
0
        private void RenderPicker(int w = 0, int h = 0)
        {
            var disp = Dispatcher;

            double width  = w > 0 ? w : this.Width;
            double height = h > 0 ? h : this.Height;

            if (width == -1 || height == -1)
            {
                return;
            }

            double colorVal = this.ColorValue;
            double offset   = this.HueOffset;
            bool   invert   = this.InvertSaturation;
            float  esize    = this.ElementSize;

            ColorPickerMode mode = this.Mode;

            _ = Task.Run(() =>
            {
                ColorPickerRenderer cw;

                if (w <= 0)
                {
                    if (double.IsNaN(width))
                    {
                        return;
                    }
                    w = (int)width;
                }
                if (h <= 0)
                {
                    if (double.IsNaN(height))
                    {
                        return;
                    }
                    h = (int)height;
                }

                if (w < 32 || h < 32)
                {
                    return;
                }

                if (mode == ColorPickerMode.Wheel || mode == ColorPickerMode.HexagonWheel)
                {
                    int rad;

                    if (h < w)
                    {
                        rad = h / 2;
                        w   = h;
                    }
                    else
                    {
                        rad = w / 2;
                        h   = w;
                    }

                    cw = new ColorPickerRenderer(rad, colorVal, offset, invert, true);
                }
                else
                {
                    cw = new ColorPickerRenderer(w, h, colorVal, offset, invert, mode == ColorPickerMode.LinearVertical, true);
                }

                Device.BeginInvokeOnMainThread(() =>
                {
                    SKImage img;
                    SKBitmap bmp = new SKBitmap((int)cw.Bounds.Width, (int)cw.Bounds.Height, SKColorType.Bgra8888, SKAlphaType.Premul);

                    var ptr = bmp.GetPixels();

                    unsafe
                    {
                        var gch = GCHandle.Alloc(cw.ImageBytes, GCHandleType.Pinned);

                        Buffer.MemoryCopy((void *)gch.AddrOfPinnedObject(), (void *)ptr, cw.ImageBytes.Length, cw.ImageBytes.Length);
                        gch.Free();
                    }

                    bmp.SetImmutable();
                    img = SKImage.FromBitmap(bmp);

                    SKData encoded = img.Encode();
                    Stream stream  = encoded.AsStream();

                    var ret = ImageSource.FromStream(() => stream);

                    cpRender          = cw;
                    PickerSite.Source = ret;
                });
            });
        }