Пример #1
0
        SKBitmap FillBitmapByteBuffer(out string description, out int milliseconds)
        {
            description = "SetPixels byte buffer";
            SKBitmap bitmap = new SKBitmap(256, 256);

            stopwatch.Restart();

            byte[,,] buffer = new byte[256, 256, 4];

            for (int rep = 0; rep < REPS; rep++)
            {
                for (int row = 0; row < 256; row++)
                {
                    for (int col = 0; col < 256; col++)
                    {
                        buffer[row, col, 0] = (byte)col;   // red
                        buffer[row, col, 1] = 0;           // green
                        buffer[row, col, 2] = (byte)row;   // blue
                        buffer[row, col, 3] = 0xFF;        // alpha
                    }
                }
            }

            unsafe
            {
                fixed(byte *ptr = buffer)
                {
                    bitmap.SetPixels((IntPtr)ptr);
                }
            }

            milliseconds = (int)stopwatch.ElapsedMilliseconds;
            return(bitmap);
        }
Пример #2
0
        private void FillBitmapByteBuffer(ref SKBitmap result)
        {
            byte[,,] buffer = new byte[_res_y, _res_x, 4];
            VectorRGBA pixel;
            Ray        r;
            Vector3    vec;
            double     u, v;

            for (int row = _res_y - 1; row >= 0; row--)
            {
                for (int col = 0; col < _res_x; col++)
                {
                    u     = (double)col / (double)_res_x;
                    v     = (double)row / (double)_res_y;
                    r     = _cam.Get_ray(ref u, ref v, 10000000.0);
                    vec   = Pixel_color(ref r, ref _hlist, 0);
                    pixel = new VectorRGBA(Math.Sqrt(vec[0]), Math.Sqrt(vec[1]), Math.Sqrt(vec[2]));
                    buffer[row, col, 0] = (byte)pixel[0];   // red
                    buffer[row, col, 1] = (byte)pixel[1];   // green
                    buffer[row, col, 2] = (byte)pixel[2];   // blue
                    buffer[row, col, 3] = (byte)pixel[3];   // alpha
                }
            }


            unsafe
            {
                fixed(byte *ptr = buffer)
                {
                    result.SetPixels((IntPtr)ptr);
                }
            }
        }
Пример #3
0
        public static SKBitmap ToSkBitmap(this Mat mat)
        {
            SKBitmap    bitmap;
            Mat         target = mat;
            SKColorType colorType;

            switch (mat.NumberOfChannels)
            {
            case 1:
                colorType = SKColorType.Gray8;
                break;

            case 2:
                colorType = SKColorType.Rg1616;
                break;

            case 3:
                CvInvoke.CvtColor(mat, target, ColorConversion.Bgr2Bgra);
                colorType = SKColorType.Bgra8888;
                break;

            case 4:
                colorType = SKColorType.Bgra8888;
                break;

            default:
                throw new Exception("Unknown color type");
            }

            bitmap = new SKBitmap(new SKImageInfo(target.Width, target.Height, colorType));
            bitmap.SetPixels(target.DataPointer);
            return(bitmap);
        }
Пример #4
0
 private static unsafe void Assign(SKBitmap bitmap, IInstruction instruction)
 {
     fixed(uint *ptr = instruction.Result)
     {
         bitmap.SetPixels((IntPtr)ptr);
     }
 }
Пример #5
0
        /// <summary>
        /// Converts the library-independent representation of pixels into a bitmap
        /// </summary>
        /// <param name="pixelData">The library-independent representation of the image</param>
        /// <returns>A <c>SKBitmap</c> in Bgra8888 representation</returns>
        private static SKBitmap ToSKBitmap(Blurhash.Core.Pixel[,] pixelData)
        {
            var width  = pixelData.GetLength(0);
            var height = pixelData.GetLength(1);

            var data = Enumerable.Range(0, height)
                       .SelectMany(y => Enumerable.Range(0, width).Select(x => (x, y)))
                       .Select(tuple => pixelData[tuple.x, tuple.y])
                       .SelectMany(pixel => new byte[]
            {
                (byte)MathUtils.LinearTosRgb(pixel.Blue), (byte)MathUtils.LinearTosRgb(pixel.Green),
                (byte)MathUtils.LinearTosRgb(pixel.Red), 255
            })
                       .ToArray();

            SKBitmap bitmap = new SKBitmap(width, height, SKColorType.Bgra8888, SKAlphaType.Opaque);

            unsafe
            {
                fixed(byte *ptr = data)
                {
                    bitmap.SetPixels((IntPtr)ptr);
                }
            }

            return(bitmap);
        }
Пример #6
0
        SKBitmap FillBitmapUintBufferColor(out string description, out int milliseconds)
        {
            description = "SetPixels SKColor";
            SKBitmap bitmap = new SKBitmap(256, 256);

            stopwatch.Restart();

            uint[,] buffer = new uint[256, 256];

            for (int rep = 0; rep < REPS; rep++)
            {
                for (int row = 0; row < 256; row++)
                {
                    for (int col = 0; col < 256; col++)
                    {
                        buffer[row, col] = (uint)new SKColor((byte)col, 0, (byte)row);
                    }
                }
            }

            unsafe
            {
                fixed(uint *ptr = buffer)
                {
                    bitmap.SetPixels((IntPtr)ptr);
                }
            }

            milliseconds = (int)stopwatch.ElapsedMilliseconds;
            return(bitmap);
        }
Пример #7
0
        public static SKImage GetImage(FTexture2DMipMap inp, string pixel_format)
        {
            byte[]      decoded;
            SKColorType color;

            switch (pixel_format)
            {
            case "PF_DXT5":
                decoded = DDSImage.DecompressDXT5(inp.data.data, DDSImage.PixelFormat.DXT5, (uint)inp.size_x, (uint)inp.size_y, (uint)inp.size_z);
                color   = SKColorType.Rgba8888;
                break;

            case "PF_DXT1":
                decoded = DDSImage.DecompressDXT1(inp.data.data, DDSImage.PixelFormat.DXT1, (uint)inp.size_x, (uint)inp.size_y, (uint)inp.size_z);
                color   = SKColorType.Rgba8888;
                break;

            case "PF_B8G8R8A8":
                decoded = inp.data.data;
                color   = SKColorType.Bgra8888;
                break;

            case "PF_BC5":
                decoded = DecodeBC5(inp.data.data, inp.size_x, inp.size_y);
                color   = SKColorType.Rgb888x;
                break;

            case "PF_BC4":
                decoded = DecodeBC4(inp.data.data, inp.size_x, inp.size_y);
                color   = SKColorType.Rgb888x;
                break;

            case "PF_G8":
                decoded = inp.data.data;
                color   = SKColorType.Gray8;
                break;

            case "PF_FloatRGBA":
                decoded = inp.data.data;
                color   = SKColorType.RgbaF16;
                break;

            default:
                throw new IOException("Unknown image type: " + pixel_format);
            }
            var info = new SKImageInfo(inp.size_x, inp.size_y, color, SKAlphaType.Unpremul);

            using (SKBitmap bitmap = new SKBitmap(info))
            {
                unsafe
                {
                    fixed(byte *p = decoded)
                    {
                        bitmap.SetPixels(new IntPtr(p));
                    }
                }
                return(SKImage.FromBitmap(bitmap));
            }
        }
Пример #8
0
        public static SKImage DecodeImage(byte[] sequence, int width, int height, int depth, EPixelFormat format)
        {
            byte[]      data;
            SKColorType colorType;

            switch (format)
            {
            case EPixelFormat.PF_DXT5:
                data      = DXTDecoder.DecodeDXT5(sequence, width, height, depth);
                colorType = SKColorType.Rgba8888;
                break;

            case EPixelFormat.PF_DXT1:
                data      = DXTDecoder.DecodeDXT1(sequence, width, height, depth);
                colorType = SKColorType.Rgba8888;
                break;

            case EPixelFormat.PF_B8G8R8A8:
                data      = sequence;
                colorType = SKColorType.Bgra8888;
                break;

            case EPixelFormat.PF_BC5:
                data      = BCDecoder.DecodeBC5(sequence, width, height);
                colorType = SKColorType.Bgra8888;
                break;

            case EPixelFormat.PF_BC4:
                data      = BCDecoder.DecodeBC4(sequence, width, height);
                colorType = SKColorType.Bgra8888;
                break;

            case EPixelFormat.PF_G8:
                data      = sequence;
                colorType = SKColorType.Gray8;
                break;

            case EPixelFormat.PF_FloatRGBA:
                data      = sequence;
                colorType = SKColorType.RgbaF16;
                break;

            default:
                throw new NotImplementedException($"Cannot decode {format} format");
            }

            using (var bitmap = new SKBitmap(new SKImageInfo(width, height, colorType, SKAlphaType.Unpremul)))
            {
                unsafe
                {
                    fixed(byte *p = data)
                    {
                        bitmap.SetPixels(new IntPtr(p));
                    }
                }
                return(SKImage.FromBitmap(bitmap));
            }
        }
        public static SKBitmap CreateBitmapFromPixels(int[] pixelData, int width, int height)
        {
            var      bmp         = new SKBitmap(width, height, SKColorType.Rgba8888, SKAlphaType.Premul);
            GCHandle pinnedArray = GCHandle.Alloc(pixelData, GCHandleType.Pinned);
            IntPtr   pointer     = pinnedArray.AddrOfPinnedObject();

            bmp.SetPixels(pointer);
            pinnedArray.Free();
            return(bmp);
        }
Пример #10
0
 public static SKImage?Decode(this UTexture2D texture)
 {
     if (!texture.IsVirtual && texture.GetFirstMip() is { } mip)
     {
         DecodeTexture(mip, texture.Format, out byte[] data, out var colorType);
         using var bitmap = new SKBitmap(new SKImageInfo(mip.SizeX, mip.SizeY, colorType, SKAlphaType.Unpremul));
         unsafe
         {
             fixed(byte *p = data)
             {
                 bitmap.SetPixels(new IntPtr(p));
             }
         }
         return(SKImage.FromBitmap(bitmap));
     }
     return(null);
 }
Пример #11
0
        public static SkiaGraphics FromImage(Bitmap bmp)
        {
            var ans = new SkiaGraphics();

            var data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite,
                                    PixelFormat.Format32bppArgb);
            SKBitmap bm =
                new SKBitmap(new SKImageInfo(bmp.Width, bmp.Height, SKColorType.Bgra8888, SKAlphaType.Premul),
                             bmp.Width * 4);

            bm.SetPixels(data.Scan0);

            ans._surface = SKSurface.Create(bm.Info, data.Scan0, bmp.Width * 4);

            bmp.UnlockBits(data);

            return(ans);
        }
Пример #12
0
        public void DrawImage(Image img, long i, long i1, long width, long height)
        {
            var data = ((Bitmap)img).LockBits(new Rectangle(0, 0, img.Width, img.Height), ImageLockMode.ReadOnly,
                                              PixelFormat.Format32bppArgb);

            using (var skbmp = new SKBitmap(new SKImageInfo(img.Width, img.Height, SKColorType.Bgra8888)))
            {
                skbmp.SetPixels(data.Scan0);

                //skbmp.InstallPixels(new SKPixmap(skbmp.Info, data.Scan0));

                _image.DrawBitmap(skbmp, new SKRect(i, i1, i + width, i1 + height), _paint);
            }

            ((Bitmap)img).UnlockBits(data);

            data = null;
        }
Пример #13
0
        private void OnPaintSurface(object sender, SKPaintSurfaceEventArgs e)
        {
            var canvas = e.Surface.Canvas;

            var bitmap = new SKBitmap(256, 240);

            unsafe
            {
                fixed(int *framePointer = _frameBuffer)
                {
                    bitmap.SetPixels((IntPtr)framePointer);
                }
            }
            var b = bitmap.Resize(new SKImageInfo(e.Info.Width, e.Info.Height), SKFilterQuality.None);

            canvas.DrawBitmap(b, SKPoint.Empty);

            canvas.Flush();
        }
Пример #14
0
        public static SKImage?Decode(this UTexture2D texture, bool bNearest = false)
        {
            if (!texture.IsVirtual && texture.GetFirstMip() is { } mip)
            {
                DecodeTexture(mip, texture.Format, out byte[] data, out var colorType);

                var width  = mip.SizeX;
                var height = mip.SizeY;
                using var bitmap = new SKBitmap(new SKImageInfo(width, height, colorType, SKAlphaType.Unpremul));
                unsafe
                {
                    fixed(byte *p = data)
                    {
                        bitmap.SetPixels(new IntPtr(p));
                    }
                }

                return(SKImage.FromBitmap(!bNearest ? bitmap : bitmap.Resize(new SKImageInfo(width, height), SKFilterQuality.None)));
            }
            return(null);
        }
        public void DrawImage(Image img, long i, long i1, long width, long height)
        {
            ((Bitmap)img).MakeTransparent(Color.Transparent);
            var data = ((Bitmap)img).LockBits(new Rectangle(0, 0, img.Width, img.Height), ImageLockMode.ReadOnly,
                                              PixelFormat.Format32bppArgb);

            using (var skbmp = new SKBitmap(new SKImageInfo(img.Width, img.Height, SKColorType.Bgra8888, SKAlphaType.Unpremul)))
            {
                skbmp.SetPixels(data.Scan0);

                //skbmp.InstallPixels(new SKPixmap(skbmp.Info, data.Scan0));

                _paint.BlendMode = SKBlendMode.SrcOver;
                //_paint.ColorFilter = SKColorFilter.CreateBlendMode(SKColors.Transparent, SKBlendMode.SrcOver);

                _image.DrawBitmap(skbmp, new SKRect(i, i1, i + width, i1 + height), _paint);
            }

            ((Bitmap)img).UnlockBits(data);

            data = null;
        }
Пример #16
0
 public Bitmap(int width, int height, int stride, PixelFormat bgra8888 = Imaging.PixelFormat.Format32bppArgb,
               IntPtr data = default(IntPtr))
 {
     nativeSkBitmap = new SKBitmap(new SKImageInfo(width, height, SKColorType.Bgra8888));
     nativeSkBitmap.SetPixels(data);
 }
Пример #17
0
 public Bitmap(int width, int height, int stride, SKColorType bgra8888 = (SKColorType.Bgra8888),
               IntPtr data = default(IntPtr))
 {
     nativeSkBitmap = new SKBitmap(new SKImageInfo(width, height, bgra8888));
     nativeSkBitmap.SetPixels(data);
 }
Пример #18
0
        public static SKImage DecodeImage(byte[] sequence, int width, int height, int depth, EPixelFormat format)
        {
            byte[]      data;
            SKColorType colorType;

            switch (format)
            {
            case EPixelFormat.PF_DXT5:
                data      = DXTDecoder.DecodeDXT5(sequence, width, height, depth);
                colorType = SKColorType.Rgba8888;
                break;

            case EPixelFormat.PF_DXT1:
                data      = DXTDecoder.DecodeDXT1(sequence, width, height, depth);
                colorType = SKColorType.Rgba8888;
                break;

            case EPixelFormat.PF_ASTC_8x8:
                var x = (int)TextureFormatHelper.GetBlockWidth(format);
                var y = (int)TextureFormatHelper.GetBlockHeight(format);
                var z = (int)TextureFormatHelper.GetBlockDepth(format);
                data      = ASTCDecoder.DecodeToRGBA8888(sequence, x, y, z, width, height, 1);
                colorType = SKColorType.Rgba8888;
                break;

            case EPixelFormat.PF_B8G8R8A8:
                data      = sequence;
                colorType = SKColorType.Bgra8888;
                break;

            case EPixelFormat.PF_BC5:
                data      = BCDecoder.DecodeBC5(sequence, width, height);
                colorType = SKColorType.Rgb888x;
                break;

            case EPixelFormat.PF_BC4:
                data      = BCDecoder.DecodeBC4(sequence, width, height);
                colorType = SKColorType.Rgb888x;
                break;

            case EPixelFormat.PF_G8:
                data      = sequence;
                colorType = SKColorType.Gray8;
                break;

            case EPixelFormat.PF_FloatRGBA:
                data      = sequence;
                colorType = SKColorType.RgbaF16;
                break;

            case EPixelFormat.PF_BC7:
                data = Detex.DecodeDetexLinear(sequence, width, height, isFloat: false,
                                               inputFormat: DetexTextureFormat.DETEX_TEXTURE_FORMAT_BPTC,
                                               outputPixelFormat: DetexPixelFormat.DETEX_PIXEL_FORMAT_RGBA8);
                colorType = SKColorType.Rgb888x;
                break;

            case EPixelFormat.PF_BC6H:
                data = Detex.DecodeDetexLinear(sequence, width, height, isFloat: true,
                                               inputFormat: DetexTextureFormat.DETEX_TEXTURE_FORMAT_BPTC_FLOAT,
                                               outputPixelFormat: DetexPixelFormat.DETEX_PIXEL_FORMAT_RGBX8); // Not sure whether that works, would actually be DETEX_PIXEL_FORMAT_FLOAT_RGBX32
                data      = Detex.DecodeBC6H(sequence, width, height);
                colorType = SKColorType.Rgb888x;
                break;

            case EPixelFormat.PF_ETC1:
                data = Detex.DecodeDetexLinear(sequence, width, height, isFloat: false,
                                               inputFormat: DetexTextureFormat.DETEX_TEXTURE_FORMAT_ETC1,
                                               outputPixelFormat: DetexPixelFormat.DETEX_PIXEL_FORMAT_RGBA8);
                colorType = SKColorType.Rgba8888;
                break;

            case EPixelFormat.PF_ETC2_RGB:
                data = Detex.DecodeDetexLinear(sequence, width, height, isFloat: false,
                                               inputFormat: DetexTextureFormat.DETEX_TEXTURE_FORMAT_ETC2,
                                               outputPixelFormat: DetexPixelFormat.DETEX_PIXEL_FORMAT_RGBA8);
                colorType = SKColorType.Rgba8888;
                break;

            case EPixelFormat.PF_ETC2_RGBA:
                data = Detex.DecodeDetexLinear(sequence, width, height, isFloat: false,
                                               inputFormat: DetexTextureFormat.DETEX_TEXTURE_FORMAT_ETC2_EAC,
                                               outputPixelFormat: DetexPixelFormat.DETEX_PIXEL_FORMAT_RGBA8);
                colorType = SKColorType.Rgba8888;
                break;

            default:
                throw new NotImplementedException($"Cannot decode {format} format");
            }

            using var bitmap = new SKBitmap(new SKImageInfo(width, height, colorType, SKAlphaType.Unpremul));
            unsafe
            {
                fixed(byte *p = data)
                {
                    bitmap.SetPixels(new IntPtr(p));
                }
            }
            return(SKImage.FromBitmap(bitmap));
        }
        private unsafe SKImage ProjectFisheye(SKPoint centerPoint, float radius, float radians)
        {
            var srcInfo = new SKImageInfo(_image.Width, _image.Height, SKImageInfo.PlatformColorType, SKAlphaType.Unpremul);
            // rows, columns, 4byte color
            var srcData = new byte[_image.Height, _image.Width, 4];

            fixed(byte *ptr = srcData)
            {
                if (!_image.ReadPixels(srcInfo, new IntPtr(ptr), _image.Width * 4, 0, 0))
                {
                    return(null);
                }
            }

            var dstWidth  = (int)Math.Floor(2 * radius);
            var dstHeight = dstWidth / 2;

            var rows    = dstHeight;
            var columns = dstWidth;
            // rows, columns, 4byte color
            var dstData = new byte[rows, columns, 4];

            for (var row = 0; row < rows; row++)
            {
                for (var column = 0; column < columns; column++)
                {
                    // normalize x, y to equirectangular space
                    var dstWidthHalf  = dstWidth / 2.0;
                    var dstHeightHalf = dstHeight / 2.0;
                    var equirectX     = -1 * (column - dstWidthHalf) / dstWidthHalf;
                    var equirectY     = -1 * (row - dstHeightHalf) / dstHeightHalf;

                    // long/lat
                    var longitude = equirectX * Math.PI;
                    var latitude  = equirectY * Math.PI / 2;

                    // 3D projection
                    var pX = Math.Cos(latitude) * Math.Cos(longitude);
                    var pY = Math.Cos(latitude) * Math.Sin(longitude);
                    var pZ = Math.Sin(latitude);

                    // fish eye normalized coords
                    var r     = 2 * Math.Atan2(Math.Sqrt(Math.Pow(pX, 2) + Math.Pow(pZ, 2)), pY) / (202 * Math.PI / 180); // 202 degrees aperture for ricoh theta s
                    var theta = Math.Atan2(pZ, pX);

                    var unitCirclePoint = new SKPoint((float)(r * Math.Cos(theta)), (float)(r * Math.Sin(theta)));

                    if (Math.Abs(unitCirclePoint.X) > 1 || Math.Abs(unitCirclePoint.Y) > 1)
                    {
                        continue;
                    }

                    // compensate for rotation of fish eye
                    var matrix = SKMatrix.MakeRotation(radians);
                    unitCirclePoint = matrix.MapPoint(unitCirclePoint);

                    // fisheye normalized coords to src image space.
                    var srcX = (int)Math.Floor(centerPoint.X + unitCirclePoint.X * radius);
                    var srcY = (int)Math.Floor(centerPoint.Y - unitCirclePoint.Y * radius);

                    // clamp to image bounds
                    if (srcX < 0 || srcX >= _image.Width || srcY < 0 || srcY >= _image.Height)
                    {
                        continue;
                    }

                    // y, x flipped cause doing row column
                    var b   = srcData[srcY, srcX, 0];
                    var g   = srcData[srcY, srcX, 1];
                    var red = srcData[srcY, srcX, 2];
                    var a   = srcData[srcY, srcX, 3];

                    dstData[row, column, 0] = b;
                    dstData[row, column, 1] = g;
                    dstData[row, column, 2] = red;
                    dstData[row, column, 3] = a;
                }
            }

            var surface = SKSurface.Create(new SKImageInfo(dstWidth, dstHeight));

            surface.Canvas.Clear(SKColors.DarkSlateGray);
            var dstBitmap = new SKBitmap(dstWidth, dstHeight);

            fixed(byte *ptr = dstData)
            {
                dstBitmap.SetPixels((IntPtr)ptr);
            }

            surface.Canvas.DrawBitmap(dstBitmap, 0, 0);

            return(surface.Snapshot());
        }
        public void DrawImage(Image img, Rectangle rectangle, float srcX, float srcY, float srcWidth, float srcHeight,
                              GraphicsUnit graphicsUnit, ImageAttributes tileFlipXYAttributes)
        {
            var coltype = SKColorType.Bgra8888;

            ((Bitmap)img).MakeTransparent(Color.Transparent);
            var data = ((Bitmap)img).LockBits(new Rectangle(0, 0, img.Width, img.Height),
                                              ImageLockMode.ReadOnly,
                                              SKColorType.Bgra8888);

            if (CompositingMode == CompositingMode.SourceOver)
            {
                _paint.BlendMode = SKBlendMode.SrcOver;
            }
            if (CompositingMode == CompositingMode.SourceCopy)
            {
                _paint.BlendMode = SKBlendMode.Src;
            }
            //if(img.PixelFormat == PixelFormat.Format32bppArgb)
            _paint.Color = SKColors.Black;



            var imginfo = new SKImageInfo(img.Width, img.Height, coltype, SKAlphaType.Premul);

            var pxmap = new SKPixmap(imginfo, data.Scan0, data.Stride);

            var image = SKImage.FromPixels(pxmap);

            if (image == null)
            {
                return;
            }


            _image.DrawImage(image, new SKRect(srcX, srcY, srcX + srcWidth, srcY + srcHeight),
                             new SKRect(rectangle.X, rectangle.Y, rectangle.Right, rectangle.Bottom), _paint);
            ((Bitmap)img).UnlockBits(data);
            data = null;

            return;

            try
            {
                using (var skbmp =
                           new SKBitmap(new SKImageInfo(img.Width, img.Height, coltype, SKAlphaType.Unpremul)))
                {
                    skbmp.SetPixels(data.Scan0);



                    _image.DrawBitmap(skbmp, new SKRect(srcX, srcY, srcX + srcWidth, srcY + srcHeight),
                                      new SKRect(rectangle.X, rectangle.Y, rectangle.Right, rectangle.Bottom), _paint);
                }
            }
            catch
            {
            }

            ((Bitmap)img).UnlockBits(data);
            data = null;
        }
Пример #21
0
 public SkiaBitmap(int width, int height, int stride, PixelFormat format, IntPtr scan0)
 {
     _bitmap = new SKBitmap(width, height, colorType: format.ToSKColorType(), alphaType: SKAlphaType.Premul);
     _bitmap.SetPixels(scan0);
 }