예제 #1
0
        public static PooledTexture2D BitmapToTexture2D(GraphicsDevice device, Image <Rgba32> image, out long byteSize)
        {
            var bmp = image;    //.CloneAs<Rgba32>();

            uint[] colorData;
            if (bmp.TryGetSinglePixelSpan(out var pixelSpan))
            {
                colorData = pixelSpan.ToArray().Select(x => x.Rgba).ToArray();
            }
            else
            {
                throw new Exception("Could not get image data!");
            }
            // var colorData = pixels.ToArray().Select(x => x.Rgba).ToArray();

            PooledTexture2D result = null;

            if (Thread.CurrentThread != RenderThread)
            {
                AutoResetEvent resetEvent = new AutoResetEvent(false);
                QueueOnRenderThread(
                    () =>
                {
                    result = GpuResourceManager.GetTexture2D("Image converter", device, image.Width, image.Height);
                    result.SetData(colorData);

                    resetEvent.Set();
                });
                resetEvent.WaitOne();
            }
            else
            {
                result = GpuResourceManager.GetTexture2D("Image converter", device, image.Width, image.Height);
                result.SetData(colorData);
            }

            byteSize = result.MemoryUsage;
            return(result);

            /*for (int x = 0; x < bmp.Width; x++)
             * {
             *      for (int y = 0; y < bmp.Height; y++)
             *      {
             *
             *      }
             * }
             * using (MemoryStream ms = new MemoryStream())
             * {
             *      bmp.SaveAsPng(ms);
             *
             *      ms.Position = 0;
             *      byteSize = ms.Length;
             *      return GpuResourceManager.GetTexture2D("Alex.Api.Utils.TextureUtils.ImageToTexture2D", device, ms);
             * }*/
        }
예제 #2
0
        private PooledTexture2D GetMipMappedTexture2D(GraphicsDevice device, Image <Rgba32> image)
        {
            //device.VertexSamplerStates.
            PooledTexture2D texture = GpuResourceManager.GetTexture2D(this, device, image.Width, image.Height, true, SurfaceFormat.Color);

            // Directory.CreateDirectory(name);
            // var resampler = new BicubicResampler();
            // var encoder = new PngEncoder();
            for (int level = 0; level < Alex.MipMapLevel; level++)
            {
                int mipWidth  = (int)System.Math.Max(1, image.Width >> level);
                int mipHeight = (int)System.Math.Max(1, image.Height >> level);

                var bmp = image.CloneAs <Rgba32>();            //.CloneAs<Rgba32>();
                bmp.Mutate(x => x.Resize(mipWidth, mipHeight, KnownResamplers.NearestNeighbor, true));

                uint[] colorData;

                if (bmp.TryGetSinglePixelSpan(out var pixelSpan))
                {
                    colorData = pixelSpan.ToArray().Select(x => x.Rgba).ToArray();
                }
                else
                {
                    throw new Exception("Could not get image data!");
                }

                //TODO: Resample per texture instead of whole texture map.

                texture.SetData(level, null, colorData, 0, colorData.Length);
            }

            return(texture);
        }
예제 #3
0
        public static PooledTexture2D BitmapToTexture2D(GraphicsDevice device, Image <Rgba32> image, out long byteSize)
        {
            var bmp       = image;//.CloneAs<Rgba32>();
            var pixels    = bmp.GetPixelSpan();
            var colorData = pixels.ToArray().Select(x => x.Rgba).ToArray();

            PooledTexture2D texture = GpuResourceManager.GetTexture2D("Image converter", device, image.Width, image.Height);

            texture.SetData(colorData);

            byteSize = texture.MemoryUsage;
            return(texture);

            /*for (int x = 0; x < bmp.Width; x++)
             * {
             *      for (int y = 0; y < bmp.Height; y++)
             *      {
             *
             *      }
             * }
             * using (MemoryStream ms = new MemoryStream())
             * {
             *      bmp.SaveAsPng(ms);
             *
             *      ms.Position = 0;
             *      byteSize = ms.Length;
             *      return GpuResourceManager.GetTexture2D("Alex.Api.Utils.TextureUtils.ImageToTexture2D", device, ms);
             * }*/
        }