コード例 #1
0
        private void ViewAs360ButtonOnClick(object sender, EventArgs e)
        {
            if (_image == null || _image.Width / _image.Height != 2)
            {
                return;
            }

            Bitmap bitmap;

            unsafe
            {
                var info = new SKImageInfo(_image.Width, _image.Height, SKImageInfo.PlatformColorType, SKAlphaType.Unpremul);
                // rows, columns, 4byte color
                var data = new byte[_image.Height * _image.Width * 4];
                fixed(byte *ptr = data)
                {
                    if (!_image.ReadPixels(info, new IntPtr(ptr), _image.Width * 4, 0, 0))
                    {
                        return;
                    }

                    bitmap = new Bitmap(_image.Width, _image.Height, _image.Width * 4, PixelFormat.Format32bppArgb, new IntPtr(ptr));
                }
            }

            var dialog = new ViewAs360Dialog(bitmap);

            dialog.ShowDialog(this);
            dialog.Dispose();
        }
コード例 #2
0
        public static WriteableBitmap ToWriteableBitmap(this SKImage skiaImage)
        {
            // TODO: maybe keep the same color types where we can, instead of just going to the platform default

            // TODO: remove this as it is old/default logic
            //using (var tempImage = SKImage.Create(info))
            //using (var pixmap = tempImage.PeekPixels())
            //using (var data = SKData.Create(pixmap.GetPixels(), info.BytesSize))
            //{
            //	skiaImage.ReadPixels(pixmap, 0, 0);
            //	using (var stream = bitmap.PixelBuffer.AsStream())
            //	{
            //		data.SaveTo(stream);
            //	}
            //}

            var info   = new SKImageInfo(skiaImage.Width, skiaImage.Height);
            var bitmap = new WriteableBitmap(info.Width, info.Height);

            using (var pixmap = new SKPixmap(info, bitmap.GetPixels()))
            {
                skiaImage.ReadPixels(pixmap, 0, 0);
            }
            bitmap.Invalidate();
            return(bitmap);
        }
コード例 #3
0
        public static Bitmap ToBitmap(this SKImage skiaImage)
        {
            // TODO: maybe keep the same color types where we can, instead of just going to the platform default
            var bitmap = new Bitmap(skiaImage.Width, skiaImage.Height, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
            var data   = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, bitmap.PixelFormat);

            // copy
            using (var pixmap = new SKPixmap(new SKImageInfo(data.Width, data.Height), data.Scan0, data.Stride))
                skiaImage.ReadPixels(pixmap, 0, 0);

            bitmap.UnlockBits(data);
            return(bitmap);
        }
コード例 #4
0
        public static System.Drawing.Bitmap ToSystemDrawingBitmap(this SKImage skiaImage)
        {
            var bitmap = new System.Drawing.Bitmap(skiaImage.Width, skiaImage.Height, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
            var data   = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, bitmap.PixelFormat);

            // copy
            using (var pixmap = new SKPixmap(new SKImageInfo(data.Width, data.Height), data.Scan0, data.Stride))
            {
                skiaImage.ReadPixels(pixmap, 0, 0);
            }

            bitmap.UnlockBits(data);
            return(bitmap);
        }
コード例 #5
0
        public static Bitmap ToBitmap(this SKImage skiaImage)
        {
            var info = skiaImage.Info;

            // destination values
            var config  = Bitmap.Config.Argb8888;
            var dstInfo = new SKImageInfo(info.Width, info.Height);

            // try keep the pixel format if we can
            switch (info.ColorType)
            {
            case SKColorType.Alpha8:
                config            = Bitmap.Config.Alpha8;
                dstInfo.ColorType = SKColorType.Alpha8;
                break;

            case SKColorType.Rgb565:
                config            = Bitmap.Config.Rgb565;
                dstInfo.ColorType = SKColorType.Rgb565;
                dstInfo.AlphaType = SKAlphaType.Opaque;
                break;

#pragma warning disable CS0618 // Type or member is obsolete
            case SKColorType.Argb4444:
                config            = Bitmap.Config.Argb4444;
                dstInfo.ColorType = SKColorType.Argb4444;
                break;
#pragma warning restore CS0618 // Type or member is obsolete
            }

            // destination bitmap
            var bmp = Bitmap.CreateBitmap(info.Width, info.Height, config);
            var ptr = bmp.LockPixels();

            // copy
            var success = skiaImage.ReadPixels(dstInfo, ptr, dstInfo.RowBytes);

            // confirm
            bmp.UnlockPixels();
            if (!success)
            {
                bmp.Recycle();
                bmp.Dispose();
                bmp = null;
            }

            GC.KeepAlive(skiaImage);
            return(bmp);
        }
コード例 #6
0
        public static BitmapDrawable From(SKImage image)
        {
            if (image == null)
            {
                throw new ArgumentNullException(nameof(image));
            }

            var info = new SKImageInfo(image.Width, image.Height, SKImageInfo.PlatformColorType, image.AlphaType);
            var bmp  = new BitmapDrawable(info);

            if (!image.ReadPixels(info, bmp.GetPixels(), info.RowBytes, 0, 0))
            {
                bmp.Dispose();
                bmp = null;
            }
            return(bmp);
        }
コード例 #7
0
        public static Bitmap ToBitmap(this SKImage skiaImage)
        {
            var info = new SKImageInfo(skiaImage.Width, skiaImage.Height);
            var bmp  = Bitmap.CreateBitmap(info.Width, info.Height, Bitmap.Config.Argb8888);
            var ptr  = bmp.LockPixels();

            var success = skiaImage.ReadPixels(info, ptr, info.RowBytes, 0, 0);

            bmp.UnlockPixels();
            if (!success)
            {
                bmp.Recycle();
                bmp.Dispose();
                bmp = null;
            }

            return(bmp);
        }
コード例 #8
0
        public static WriteableBitmap ToWriteableBitmap(this SKImage skiaImage)
        {
            // TODO: maybe keep the same color types where we can, instead of just going to the platform default

            var info   = new SKImageInfo(skiaImage.Width, skiaImage.Height);
            var bitmap = new WriteableBitmap(info.Width, info.Height, 96, 96, PixelFormats.Pbgra32, null);

            bitmap.Lock();

            // copy
            using (var pixmap = new SKPixmap(info, bitmap.BackBuffer, bitmap.BackBufferStride))
            {
                skiaImage.ReadPixels(pixmap, 0, 0);
            }

            bitmap.AddDirtyRect(new Int32Rect(0, 0, info.Width, info.Height));
            bitmap.Unlock();
            return(bitmap);
        }
コード例 #9
0
ファイル: GTKExtensions.cs プロジェクト: zbyszekpy/SkiaSharp
        public static Pixbuf ToPixbuf(this SKImage skiaImage)
        {
            // TODO: maybe keep the same color types where we can, instead of just going to the platform default

            var info = new SKImageInfo(skiaImage.Width, skiaImage.Height);
            var pix  = new Pixbuf(Colorspace.Rgb, true, 8, info.Width, info.Height);

            // copy
            using (var pixmap = new SKPixmap(info, pix.Pixels, pix.Rowstride))
            {
                skiaImage.ReadPixels(pixmap, 0, 0);
            }

            // swap R and B
            if (info.ColorType == SKColorType.Bgra8888)
            {
                SKSwizzle.SwapRedBlue(pix.Pixels, info.Width * info.Height);
            }

            return(pix);
        }
コード例 #10
0
 public static void ToSKPixmap(this Bitmap bitmap, SKPixmap pixmap)
 {
     if (pixmap.ColorType == SKImageInfo.PlatformColorType)
     {
         SKImageInfo info = pixmap.Info;
         using (Bitmap image = new Bitmap(info.Width, info.Height, info.RowBytes, PixelFormat.Format32bppPArgb, pixmap.GetPixels()))
         {
             using (Graphics graphics = Graphics.FromImage(image))
             {
                 graphics.Clear(Color.Transparent);
                 graphics.DrawImageUnscaled(bitmap, 0, 0);
             }
         }
     }
     else
     {
         using (SKImage sKImage = bitmap.ToSKImage())
         {
             sKImage.ReadPixels(pixmap, 0, 0);
         }
     }
 }
コード例 #11
0
        private static float[] ImageToFloats(SKImage image)
        {
            int width       = Constants.YoloImage.Width;
            int height      = Constants.YoloImage.Height;
            var imageInfo   = new SKImageInfo(width, height);
            var bytes       = new byte[imageInfo.BytesSize];
            var pixelBuffer = IntPtr.Zero;

            try
            {
                pixelBuffer = Marshal.AllocHGlobal(imageInfo.BytesSize);
                image.ReadPixels(imageInfo, pixelBuffer, imageInfo.RowBytes, 0, 0);
                Marshal.Copy(pixelBuffer, bytes, 0, imageInfo.BytesSize);
            }
            finally
            {
                Marshal.FreeHGlobal(pixelBuffer);
            }

            // Loop over every pixel, RGB -> BGR
            var floats = new float[3 * width * height];

            for (var x = 0; x < width; x++)
            {
                for (var y = 0; y < height; y++)
                {
                    for (var channel = 2; channel >= 0; channel--)
                    {
                        var destIndex   = channel * height * width + y * width + x;
                        var sourceIndex = y * imageInfo.RowBytes + x * imageInfo.BytesPerPixel + channel;
                        floats[destIndex] = bytes[sourceIndex] / 255.0f;
                    }
                }
            }
            return(floats);
        }
コード例 #12
0
ファイル: Blend.cs プロジェクト: orf53975/IoTGateway
        /// <summary>
        /// Blends two images of the same size using a blending factor.
        /// </summary>
        /// <param name="Image1">Image 1</param>
        /// <param name="Image2">Image 2</param>
        /// <param name="p">Blending factor (0=<paramref name="Image1"/>, 1=<paramref name="Image2"/>).</param>
        /// <returns>Blended image.</returns>
        public static SKImage BlendColors(SKImage Image1, SKImage Image2, double p)
        {
            if (Image1.Width != Image2.Width || Image1.Height != Image2.Height)
            {
                throw new ArgumentException("Images not of the same size.", nameof(Image2));
            }

            SKImageInfo ImageInfo = new SKImageInfo(Image1.Width, Image1.Height, SKColorType.Bgra8888);
            int         i, j, c = ImageInfo.BytesSize;
            IntPtr      Pixels1 = Marshal.AllocCoTaskMem(c);
            IntPtr      Pixels2 = IntPtr.Zero;

            try
            {
                Pixels2 = Marshal.AllocCoTaskMem(c);

                Image1.ReadPixels(ImageInfo, Pixels1, ImageInfo.RowBytes, 0, 0);
                Image2.ReadPixels(ImageInfo, Pixels2, ImageInfo.RowBytes, 0, 0);

                byte[] Bin1 = new byte[c];
                byte[] Bin2 = new byte[c];
                Marshal.Copy(Pixels1, Bin1, 0, c);
                Marshal.Copy(Pixels2, Bin2, 0, c);

                for (i = 0; i < c; i++)
                {
                    j = (int)(Bin1[i] * (1 - p) + Bin2[i] * p + 0.5);
                    if (j < 0)
                    {
                        Bin1[i] = 0;
                    }
                    else if (j > 255)
                    {
                        Bin1[i] = 255;
                    }
                    else
                    {
                        Bin1[i] = (byte)j;
                    }
                }

                Marshal.Copy(Bin1, 0, Pixels1, c);

                using (SKData Data = SKData.Create(Pixels1, c))
                {
                    SKImage Result = SKImage.FromPixelData(new SKImageInfo(ImageInfo.Width, ImageInfo.Height, SKColorType.Bgra8888), Data, ImageInfo.RowBytes);
                    Pixels1 = IntPtr.Zero;

                    return(Result);
                }
            }
            finally
            {
                if (Pixels1 != IntPtr.Zero)
                {
                    Marshal.FreeCoTaskMem(Pixels1);
                }

                if (Pixels2 != IntPtr.Zero)
                {
                    Marshal.FreeCoTaskMem(Pixels2);
                }
            }
        }
コード例 #13
0
ファイル: Blend.cs プロジェクト: orf53975/IoTGateway
        /// <summary>
        /// Blends an image with a fixed color using a blending factor.
        /// </summary>
        /// <param name="Image">Image</param>
        /// <param name="Color">Color</param>
        /// <param name="p">Blending factor (0=<paramref name="Image"/>, 1=<paramref name="Color"/>).</param>
        /// <returns>Blended image.</returns>
        public static SKImage BlendColors(SKImage Image, SKColor Color, double p)
        {
            SKImageInfo ImageInfo = new SKImageInfo(Image.Width, Image.Height, SKColorType.Bgra8888);
            int         i, j, c = ImageInfo.BytesSize;
            byte        R      = Color.Red;
            byte        G      = Color.Green;
            byte        B      = Color.Blue;
            byte        A      = Color.Alpha;
            IntPtr      Pixels = Marshal.AllocCoTaskMem(c);

            try
            {
                Image.ReadPixels(ImageInfo, Pixels, ImageInfo.RowBytes, 0, 0);

                byte[] Bin = new byte[c];
                Marshal.Copy(Pixels, Bin, 0, c);

                for (i = 0; i < c; i++)
                {
                    j = (int)(Bin[i] * (1 - p) + B * p + 0.5);
                    if (j < 0)
                    {
                        Bin[i] = 0;
                    }
                    else if (j > 255)
                    {
                        Bin[i] = 255;
                    }
                    else
                    {
                        Bin[i] = (byte)j;
                    }

                    j = (int)(Bin[++i] * (1 - p) + G * p + 0.5);
                    if (j < 0)
                    {
                        Bin[i] = 0;
                    }
                    else if (j > 255)
                    {
                        Bin[i] = 255;
                    }
                    else
                    {
                        Bin[i] = (byte)j;
                    }

                    j = (int)(Bin[++i] * (1 - p) + R * p + 0.5);
                    if (j < 0)
                    {
                        Bin[i] = 0;
                    }
                    else if (j > 255)
                    {
                        Bin[i] = 255;
                    }
                    else
                    {
                        Bin[i] = (byte)j;
                    }

                    j = (int)(Bin[++i] * (1 - p) + A * p + 0.5);
                    if (j < 0)
                    {
                        Bin[i] = 0;
                    }
                    else if (j > 255)
                    {
                        Bin[i] = 255;
                    }
                    else
                    {
                        Bin[i] = (byte)j;
                    }
                }

                Marshal.Copy(Bin, 0, Pixels, c);

                using (SKData Data = SKData.Create(Pixels, c))
                {
                    SKImage Result = SKImage.FromPixelData(new SKImageInfo(ImageInfo.Width, ImageInfo.Height, SKColorType.Bgra8888), Data, ImageInfo.RowBytes);
                    Pixels = IntPtr.Zero;

                    return(Result);
                }
            }
            finally
            {
                if (Pixels != IntPtr.Zero)
                {
                    Marshal.FreeCoTaskMem(Pixels);
                }
            }
        }
コード例 #14
0
ファイル: SKTexture.cs プロジェクト: piechade/Artemis
 internal void CopyPixelData()
 {
     using SKImage skImage = Surface.Snapshot();
     skImage.ReadPixels(_pixelData);
 }