コード例 #1
0
        private static SKImage GenerateProcedural(string texture)
        {
            if (!texture.StartsWith("#(argb"))
            {
                Debugger.Break();
            }

            var split = texture.Trim('#', '(', ')').Replace(")color(", ",").Split(',');

            var format = split[0];
            var width  = int.Parse(split[1]);
            var height = int.Parse(split[2]);
            //Mipmaps 3

            CultureInfo ci = (CultureInfo)CultureInfo.CurrentCulture.Clone();

            ci.NumberFormat.NumberDecimalSeparator = ".";
            var colorArr = split.Skip(4).Select(xy => (byte)(float.Parse(xy, NumberStyles.Any, ci) * 255)).ToList();


            byte[] data = new byte[width * height * 4];
            for (int i = 0; i < width * height * 4; i += 4)
            {
                data[i]     = colorArr[0];
                data[i + 1] = colorArr[1];
                data[i + 2] = colorArr[2];
                data[i + 3] = colorArr[3];
            }

            var bmp = SKImage.FromPixelCopy(new SKImageInfo(width, height, SKColorType.Rgba8888), data);

            return(bmp);
        }
コード例 #2
0
        public ProcessImage Convert(ProcessImage source, ProcessImageType target, bool flipHorizontal = false, bool flipVertical = false)
        {
            if (source.Type == target)
            {
                return(source);
            }

            if (source.Type == ProcessImageType.PNG)
            {
                return(Encode(SKImage.FromBitmap(SKBitmap.Decode(source.Load)), target, flipHorizontal, flipVertical));
            }
            if (source.Type == ProcessImageType.Grayscale8)
            {
                return(Encode(SKImage.FromPixelCopy(new SKImageInfo(source.Width, source.Height, SKColorType.Gray8), source.Load), target, flipHorizontal, flipVertical));
            }
            if (source.Type == ProcessImageType.RGBA8)
            {
                return(Encode(SKImage.FromPixelCopy(new SKImageInfo(source.Width, source.Height, SKColorType.Rgba8888), source.Load), target, flipHorizontal, flipVertical));
            }
            if (source.Type == ProcessImageType.RGBA16)
            {
                return(Encode(SKImage.FromPixelCopy(new SKImageInfo(source.Width, source.Height, SKColorType.RgbaF16), source.Load), target, flipHorizontal, flipVertical));
            }

            /*if (source.Type == ProcessImageType.RGBA32)
             * {
             *  Image img = Image.LoadPixelData<RgbaVector>(source.Load, source.Width, source.Height);
             *  if (flipHorizontal) { img.Mutate(x => x.Flip(FlipMode.Horizontal)); }
             *  if (flipVertical) { img.Mutate(x => x.Flip(FlipMode.Vertical)); }
             *  return Encode(img, target);
             * }*/

            throw new FormatException("Format conversion not implemented");
        }
コード例 #3
0
 public void TestFromPixelCopyIntPtr()
 {
     using (var bmp = CreateTestBitmap())
         using (var image = SKImage.FromPixelCopy(bmp.Info, bmp.GetPixels(out var length)))
         {
             ValidateTestPixmap(image.PeekPixels());
         }
 }
コード例 #4
0
        public static SKImage ToSKImage(this Bitmap bitmap)
        {
            var info  = GetInfo(bitmap);
            var ptr   = bitmap.LockPixels();
            var image = SKImage.FromPixelCopy(info, ptr);

            bitmap.UnlockPixels();
            return(image);
        }
コード例 #5
0
ファイル: SkiaRenderHandler.cs プロジェクト: vvvv/VL.CEF
        public void OnPaint(CefPaintElementType type, CefRectangle[] cefRects, IntPtr buffer, int width, int height)
        {
            var image = SKImage.FromPixelCopy(new SKImageInfo(width, height, SKColorType.Bgra8888, SKAlphaType.Premul, SKColorSpace.CreateSrgb()), buffer, width * 4);

            lock (syncRoot)
            {
                rasterImage?.Dispose();
                rasterImage = image;
            }
        }
コード例 #6
0
 public void TestFromPixelCopyByteArray()
 {
     using (var bmp = CreateTestBitmap())
     {
         var px  = bmp.GetPixels(out var length);
         var dst = new byte[(int)length];
         Marshal.Copy(px, dst, 0, (int)length);
         using (var image = SKImage.FromPixelCopy(bmp.Info, dst))
         {
             ValidateTestPixmap(image.PeekPixels());
         }
     }
 }
コード例 #7
0
        /// <summary>
        /// Create immutable bitmap from given pixel data copy.
        /// </summary>
        /// <param name="size">Size of the bitmap.</param>
        /// <param name="dpi">DPI of the bitmap.</param>
        /// <param name="stride">Stride of data pixels.</param>
        /// <param name="format">Format of data pixels.</param>
        /// <param name="data">Data pixels.</param>
        public ImmutableBitmap(PixelSize size, Vector dpi, int stride, PixelFormat format, IntPtr data)
        {
            var imageInfo = new SKImageInfo(size.Width, size.Height, format.ToSkColorType(), SKAlphaType.Premul);

            _image = SKImage.FromPixelCopy(imageInfo, data, stride);

            if (_image == null)
            {
                throw new ArgumentException("Unable to create bitmap from provided data");
            }

            PixelSize = size;
            Dpi       = dpi;
        }
コード例 #8
0
ファイル: ImmutableBitmap.cs プロジェクト: yaoworld/Avalonia
        /// <summary>
        /// Create immutable bitmap from given pixel data copy.
        /// </summary>
        /// <param name="width">Width of data pixels.</param>
        /// <param name="height">Height of data pixels.</param>
        /// <param name="stride">Stride of data pixels.</param>
        /// <param name="format">Format of data pixels.</param>
        /// <param name="data">Data pixels.</param>
        public ImmutableBitmap(int width, int height, int stride, PixelFormat format, IntPtr data)
        {
            var imageInfo = new SKImageInfo(width, height, format.ToSkColorType(), SKAlphaType.Premul);

            _image = SKImage.FromPixelCopy(imageInfo, data, stride);

            if (_image == null)
            {
                throw new ArgumentException("Unable to create bitmap from provided data");
            }

            PixelWidth  = width;
            PixelHeight = height;
        }
コード例 #9
0
 public ProcessImage Rgba8ToPng(ProcessImage source, bool flipHorizontal, bool flipVertical)
 {
     if (source.Type != ProcessImageType.RGBA8)
     {
         throw new FormatException("Process image should be RGBA8");
     }
     using SKImage img      = SKImage.FromPixelCopy(new SKImageInfo(source.Width, source.Height, SKColorType.Rgba8888), source.Load);
     using SKBitmap bmp     = new SKBitmap(img.Width, img.Height);
     using SKCanvas surface = new SKCanvas(bmp);
     surface.Scale(flipHorizontal ? -1 : 1, flipVertical ? -1 : 1, flipHorizontal ? source.Width / 2f : 0, flipVertical ? source.Height / 2f : 0);
     surface.DrawImage(img, 0, 0);
     return(new ProcessImage {
         Load = SKImage.FromBitmap(bmp).Encode(SKEncodedImageFormat.Png, 99).ToArray(), Height = source.Height, Type = ProcessImageType.PNG, Width = source.Width
     });
 }
コード例 #10
0
        private void OpenButtonOnClick(object sender, EventArgs e)
        {
            var openFileDialog = new OpenFileDialog();
            var result         = openFileDialog.ShowDialog(this);

            if (result == DialogResult.OK)
            {
                var bitmap = Image.FromFile(openFileDialog.FileName) as Bitmap;
                if (bitmap == null)
                {
                    return;
                }

                var pixels = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
                _image = SKImage.FromPixelCopy(new SKPixmap(new SKImageInfo(bitmap.Width, bitmap.Height), pixels.Scan0));
                bitmap.UnlockBits(pixels);
                bitmap.Dispose();
                _skGlControl.Invalidate();
            }
            openFileDialog.Dispose();
        }
コード例 #11
0
        public void OnNetworkMessage(IEnumerable <string> cmd, JObject args)
        {
            if (cmd.First() == "TextureFile")
            {
                var path   = args["path"].Value <string>();
                var data   = args["data"].Value <string>();
                int width  = args["width"].Value <int>();
                int height = args["height"].Value <int>();

                ImageRequest request;

                lock (imageCache)
                {
                    if (!pendingRequests.ContainsKey(path))
                    {
                        return;                                     //#TODO log/handle
                    }
                    request = pendingRequests[path];
                }

                var dataBytes = Convert.FromBase64String(data);

                //ABGR -> BGRA
                for (int i = 0; i < dataBytes.Length; i += 4)
                {
                    var A = dataBytes[i];
                    var B = dataBytes[i + 1];
                    var G = dataBytes[i + 2];
                    var R = dataBytes[i + 3];

                    dataBytes[i]     = B;
                    dataBytes[i + 1] = G;
                    dataBytes[i + 2] = R;
                    dataBytes[i + 3] = A;
                }

                var bmp = SKImage.FromPixelCopy(new SKImageInfo(width, height, SKColorType.Bgra8888), dataBytes);
                //if (bmp == null) Debugger.Break();
                request.completionSource.SetResult(bmp);


                //if (bmp != null)
                //{
                //    using (var data2 = bmp.Encode(SKEncodedImageFormat.Png, 80))
                //    using (var stream = File.OpenWrite(Path.Combine("P:/", Path.ChangeExtension(Path.GetFileName(path), ".png") )))
                //    {
                //        // save the data to a stream
                //        data2.SaveTo(stream);
                //    }
                //}



                lock (imageCache)
                {
                    imageCache[path] = bmp;
                    pendingRequests.Remove(path);
                }
            }
            else if (cmd.First() == "MapFile")
            {
                var path = args["name"].Value <string>();
                var data = args["data"].Value <string>();

                MapfileRequest request = pendingMapRequests.FirstOrDefault(x => path.StartsWith(x.name));

                lock (imageCache)
                {
                    request = pendingMapRequests.FirstOrDefault(x => path.StartsWith(x.name)); //#TODO log/handle
                    if (request == null)
                    {
                        return;
                    }
                }

                var dataBytes = Convert.FromBase64String(data);

                //If source was not compressed, still store it compressed, save some disk space especially on mobile
                if (path.EndsWith("z") || Path.GetExtension(path) == ".zip")
                {
                    using (var writer = File.Create(Path.Combine(request.targetDirectory, path)))
                    {
                        writer.Write(dataBytes, 0, dataBytes.Length);
                    }
                }
                else
                {
                    path = path + "z"; //svgz
                    using (var writer = File.Create(Path.Combine(request.targetDirectory, path)))
                    {
                        using (GZipStream compressionStream = new GZipStream(writer,
                                                                             CompressionMode.Compress))
                        {
                            compressionStream.Write(dataBytes, 0, dataBytes.Length);
                        }
                    }
                }

                request.completionSource.SetResult(Path.Combine(request.targetDirectory, path));

                lock (imageCache)
                {
                    pendingMapRequests.Remove(request);
                }
            }
        }