Example #1
0
        private static SharpDX.WIC.BitmapSource LoadBitmap(Stream stream, out SharpDX.WIC.BitmapDecoder decoder)
        {
            if (imgfactory == null)
            {
                imgfactory = new SharpDX.WIC.ImagingFactory();
            }
			
			SharpDX.WIC.FormatConverter fconv = null;
			
            decoder = new SharpDX.WIC.BitmapDecoder(
                imgfactory,
                stream,
                SharpDX.WIC.DecodeOptions.CacheOnDemand
                );

			fconv = new SharpDX.WIC.FormatConverter(imgfactory);

			fconv.Initialize(
				decoder.GetFrame(0),
				SharpDX.WIC.PixelFormat.Format32bppPRGBA,
				SharpDX.WIC.BitmapDitherType.None, null,
				0.0, SharpDX.WIC.BitmapPaletteType.Custom);

			return fconv;
        }
        private void LoadBitmap(RenderTarget device, byte[] bytes)
        {
            var stream = new MemoryStream(bytes);

            SharpDX.WIC.BitmapDecoder decoder = new SharpDX.WIC.BitmapDecoder(ImagingFactory, stream, SharpDX.WIC.DecodeOptions.CacheOnDemand);
            var frame = decoder.GetFrame(0);

            SharpDX.WIC.FormatConverter converter = new SharpDX.WIC.FormatConverter(ImagingFactory);
            try
            {
                // normal ARGB images (Bitmaps / png tested)
                converter.Initialize(frame, SharpDX.WIC.PixelFormat.Format32bppRGBA1010102);
            }
            catch
            {
                // falling back to RGB if unsupported
                converter.Initialize(frame, SharpDX.WIC.PixelFormat.Format32bppRGB);
            }
            SharpDXBitmap = Bitmap.FromWicBitmap(device, converter);

            converter.Dispose();
            frame.Dispose();
            decoder.Dispose();
            stream.Dispose();
        }
Example #3
0
        private SharpDX.WIC.FormatConverter DecodeImage()
        {
            var path = Windows.ApplicationModel.Package.Current.InstalledLocation.Path;

            SharpDX.WIC.BitmapDecoder bitmapDecoder = new SharpDX.WIC.BitmapDecoder(
                _deviceManager.WICFactory,
                @"Assets\Nepal.jpg",
                SharpDX.IO.NativeFileAccess.Read,
                SharpDX.WIC.DecodeOptions.CacheOnDemand
                );

            SharpDX.WIC.BitmapFrameDecode bitmapFrameDecode = bitmapDecoder.GetFrame(0);

            SharpDX.WIC.BitmapSource bitmapSource = new SharpDX.WIC.BitmapSource(bitmapFrameDecode.NativePointer);

            SharpDX.WIC.FormatConverter formatConverter = new SharpDX.WIC.FormatConverter(_deviceManager.WICFactory);
            //formatConverter.Initialize( bitmapSource, SharpDX.WIC.PixelFormat.Format32bppBGRA);
            formatConverter.Initialize(
                bitmapSource,
                SharpDX.WIC.PixelFormat.Format32bppBGRA,
                SharpDX.WIC.BitmapDitherType.None,
                null,
                0.0f,
                SharpDX.WIC.BitmapPaletteType.Custom
                );

            imageSize = formatConverter.Size;

            return(formatConverter);
        }
Example #4
0
 public void Dispose()
 {
     Direct2DResourceManager.OnResourceDestroy(this);
     if (Texture2D != null)
     {
         Texture2D.Dispose();
         Texture2D = null;
     }
     if (fConverter != null)
     {
         fConverter.Dispose();
         fConverter = null;
     }
     if (BFDecorde != null)
     {
         BFDecorde.Dispose();
         BFDecorde = null;
     }
     if (BitDecorder != null)
     {
         BitDecorder.Dispose();
         BitDecorder = null;
     }
     if (MemStream != null)
     {
         MemStream.Dispose();
         MemStream = null;
     }
 }
Example #5
0
        public static s.WIC.Bitmap ToBitmap(this s.WIC.BitmapSource bmp, Guid?pixelFormat = null)
        {
            using (var converter = new s.WIC.FormatConverter(SDFactory.WicImagingFactory))
            {
                converter.Initialize(bmp, pixelFormat ?? s.WIC.PixelFormat.Format32bppPBGRA);

                return(new s.WIC.Bitmap(SDFactory.WicImagingFactory, converter, s.WIC.BitmapCreateCacheOption.CacheOnLoad));
            }
        }
        public virtual void Initialize(DeviceManager deviceManager)
        {
            _deviceManager = deviceManager;
            sceneColorBrush = new SolidColorBrush(deviceManager.ContextDirect2D, Color.White);
            
            //GET IMAGE DATA
            _formatConverter = DecodeImage();

            //CREATE EFFECT-GRAPH USING IMAGE DATA
            UpdateEffectGraph();
        }
Example #7
0
        public virtual void Initialize(DeviceManager deviceManager)
        {
            _deviceManager  = deviceManager;
            sceneColorBrush = new SolidColorBrush(deviceManager.ContextDirect2D, Color.White);

            //GET IMAGE DATA
            _formatConverter = DecodeImage();

            //CREATE EFFECT-GRAPH USING IMAGE DATA
            UpdateEffectGraph();
        }
Example #8
0
        /// <summary>
        /// 创建图片
        /// </summary>
        /// <param name="wicFactory"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        static SharpDX.WIC.FormatConverter CreateWicImage(SharpDX.WIC.ImagingFactory wicFactory, string fileName)
        {
            using var decoder      = new SharpDX.WIC.JpegBitmapDecoder(wicFactory);
            using var decodeStream = new SharpDX.WIC.WICStream(wicFactory, fileName, SharpDX.IO.NativeFileAccess.Read);
            decoder.Initialize(decodeStream, SharpDX.WIC.DecodeOptions.CacheOnLoad);
            using var decodeFrame = decoder.GetFrame(0);
            var converter = new SharpDX.WIC.FormatConverter(wicFactory);

            converter.Initialize(decodeFrame, SharpDX.WIC.PixelFormat.Format32bppPBGRA);
            return(converter);
        }
Example #9
0
 private void CreateFromBitmap(SharpDX.Direct2D1.RenderTarget InRenderTarget, Bitmap InBitmap)
 {
     MemStream = new System.IO.MemoryStream();
     InBitmap.Save(MemStream, System.Drawing.Imaging.ImageFormat.Png);
     BitDecorder = new SharpDX.WIC.BitmapDecoder(Direct2DDrawingSystem.instance.ImagingFactory,
                                                 MemStream,
                                                 SharpDX.WIC.DecodeOptions.CacheOnDemand);
     BFDecorde  = BitDecorder.GetFrame(0);
     fConverter = new SharpDX.WIC.FormatConverter(Direct2DDrawingSystem.instance.ImagingFactory);
     fConverter.Initialize(BFDecorde, SharpDX.WIC.PixelFormat.Format32bppPBGRA, SharpDX.WIC.BitmapDitherType.None, null, 0, SharpDX.WIC.BitmapPaletteType.Custom);
     Texture2D = SharpDX.Direct2D1.Bitmap.FromWicBitmap(InRenderTarget, fConverter);
 }
Example #10
0
        /// <summary>
        /// This doesnt work! SharpDX errors when trying to open a local system file (not relative)
        /// </summary>
        /// <param name="assetNativeUri"></param>
        /// <param name="backgroundImageFormatConverter"></param>
        /// <param name="backgroundImageSize"></param>
        public void LoadNativeAsset(string assetNativeUri, out SharpDX.WIC.FormatConverter backgroundImageFormatConverter, out Size2 backgroundImageSize)
        {
            var path = Windows.ApplicationModel.Package.Current.InstalledLocation.Path;


            var nativeFileStream = new SharpDX.IO.NativeFileStream(
                assetNativeUri,
                SharpDX.IO.NativeFileMode.Open,
                SharpDX.IO.NativeFileAccess.Read,
                SharpDX.IO.NativeFileShare.Read);


            var r = SharpDX.IO.NativeFile.ReadAllBytes(assetNativeUri);

            var data = SharpDX.IO.NativeFile.ReadAllBytes(assetNativeUri);

            using (System.IO.MemoryStream ms = new System.IO.MemoryStream(data))
            {
                if (ms != null)
                {
                    using (SharpDX.WIC.BitmapDecoder bitmapDecoder = new SharpDX.WIC.BitmapDecoder(
                               _deviceManager.WICFactory,
                               ms,
                               SharpDX.WIC.DecodeOptions.CacheOnDemand
                               ))
                    {
                        using (SharpDX.WIC.BitmapFrameDecode bitmapFrameDecode = bitmapDecoder.GetFrame(0))
                        {
                            using (SharpDX.WIC.BitmapSource bitmapSource = new SharpDX.WIC.BitmapSource(bitmapFrameDecode.NativePointer))
                            {
                                SharpDX.WIC.FormatConverter formatConverter = new SharpDX.WIC.FormatConverter(_deviceManager.WICFactory);
                                //formatConverter.Initialize( bitmapSource, SharpDX.WIC.PixelFormat.Format32bppBGRA);
                                formatConverter.Initialize(
                                    bitmapSource,
                                    SharpDX.WIC.PixelFormat.Format32bppBGRA,
                                    SharpDX.WIC.BitmapDitherType.None,
                                    null,
                                    0.0f,
                                    SharpDX.WIC.BitmapPaletteType.Custom
                                    );

                                backgroundImageSize = formatConverter.Size;

                                backgroundImageFormatConverter = formatConverter;
                            }
                        }
                    }
                }
            }

            backgroundImageFormatConverter = null;
            backgroundImageSize            = new Size2(0, 0);
        }
Example #11
0
        public override void OnRenderTargetChanged()
        {
            base.OnRenderTargetChanged();
            if (RenderTarget == null || RenderTarget.IsDisposed)
            {
                return;
            }

            // Dispose all Render dependant resources on RenderTarget change.
            if (myBitmap != null)
            {
                myBitmap.Dispose();
            }
            if (fileStream != null)
            {
                fileStream.Dispose();
            }
            if (bitmapDecoder != null)
            {
                bitmapDecoder.Dispose();
            }
            if (converter != null)
            {
                converter.Dispose();
            }
            if (frame != null)
            {
                frame.Dispose();
            }

            // Neccessary for creating WIC objects.
            fileStream = new SharpDX.IO.NativeFileStream(System.IO.Path.Combine(NinjaTrader.Core.Globals.UserDataDir, "SampleDrawBitmap.png"), SharpDX.IO.NativeFileMode.Open, SharpDX.IO.NativeFileAccess.Read);
            // Used to read the image source file.
            bitmapDecoder = new SharpDX.WIC.BitmapDecoder(Core.Globals.WicImagingFactory, fileStream, SharpDX.WIC.DecodeOptions.CacheOnDemand);
            // Get the first frame of the image.
            frame = bitmapDecoder.GetFrame(0);
            // Convert it to a compatible pixel format.
            converter = new SharpDX.WIC.FormatConverter(Core.Globals.WicImagingFactory);
            converter.Initialize(frame, SharpDX.WIC.PixelFormat.Format32bppPRGBA);
            // Create the new Bitmap1 directly from the FormatConverter.
            myBitmap = SharpDX.Direct2D1.Bitmap.FromWicBitmap(RenderTarget, converter);

            if (!HWSet && myBitmap != null)
            {
                H     = myBitmap.Size.Height;
                W     = myBitmap.Size.Width;
                Ratio = W / H;
                HWSet = true;
            }
        }
Example #12
0
        public static SharpDX.Direct2D1.Bitmap LoadFromFile(string filePath)
        {
            SharpDX.WIC.ImagingFactory  imagingFactory = new SharpDX.WIC.ImagingFactory();
            SharpDX.IO.NativeFileStream fileStream     = new SharpDX.IO.NativeFileStream(filePath,
                                                                                         SharpDX.IO.NativeFileMode.Open, SharpDX.IO.NativeFileAccess.Read);

            SharpDX.WIC.BitmapDecoder bitmapDecoder =
                new SharpDX.WIC.BitmapDecoder(imagingFactory, fileStream, SharpDX.WIC.DecodeOptions.CacheOnDemand);
            SharpDX.WIC.BitmapFrameDecode frame = bitmapDecoder.GetFrame(0);

            SharpDX.WIC.FormatConverter converter = new SharpDX.WIC.FormatConverter(imagingFactory);
            converter.Initialize(frame, SharpDX.WIC.PixelFormat.Format32bppPRGBA);

            return(SharpDX.Direct2D1.Bitmap.FromWicBitmap(RenderForm.RenderTarget, converter));
        }
Example #13
0
        private void CreateEffectGraph(SharpDX.WIC.FormatConverter formatConverter)
        {
            // Setup local variables
            var d2dDevice  = _deviceManager.DeviceDirect2D;
            var d2dContext = _deviceManager.ContextDirect2D;

            // Effect 1 : BitmapSource - take decoded image data and get a BitmapSource from it
            bitmapSourceEffect = new SharpDX.Direct2D1.Effects.BitmapSource(d2dContext);
            bitmapSourceEffect.WicBitmapSource = formatConverter;
            bitmapSourceEffect.Cached          = true; // Because the image will not be changing, we should cache the effect for performance reasons.

            // Effect 2 : PointSpecular
            _deviceManager.FactoryDirect2D.RegisterEffect <WaveEffect>();
            _waveEffect = new Effect <WaveEffect>(_deviceManager.ContextDirect2D);
            _waveEffect.SetInputEffect(0, bitmapSourceEffect);
        }
Example #14
0
        /// <summary>
        /// Loads a bitmap using WIC.
        /// </summary>
        /// <param name="deviceManager"></param>
        /// <param name="filename"></param>
        /// <returns></returns>
        public static SharpDX.WIC.BitmapSource LoadBitmap(SharpDX.WIC.ImagingFactory2 factory, string filename)
        {
            using (var bitmapDecoder = new SharpDX.WIC.BitmapDecoder(factory, filename, SharpDX.WIC.DecodeOptions.CacheOnDemand)) {
                var formatConverter = new SharpDX.WIC.FormatConverter(factory);

                formatConverter.Initialize(
                    bitmapDecoder.GetFrame(0),
                    SharpDX.WIC.PixelFormat.Format32bppPRGBA,
                    SharpDX.WIC.BitmapDitherType.None,
                    null,
                    0.0,
                    SharpDX.WIC.BitmapPaletteType.Custom);

                return(formatConverter);
            }
        }
Example #15
0
 Bitmap GetBitmap(RenderTarget target, Stream stream)
 {
     using (SharpDX.WIC.ImagingFactory wicFactory = new SharpDX.WIC.ImagingFactory())
     {
         using (SharpDX.WIC.BitmapDecoder bmpDecoder = new SharpDX.WIC.BitmapDecoder(wicFactory, stream, SharpDX.WIC.DecodeOptions.CacheOnLoad))
         {
             using (SharpDX.WIC.FormatConverter converter = new SharpDX.WIC.FormatConverter(wicFactory))
             {
                 using (SharpDX.WIC.BitmapFrameDecode frameDecode = bmpDecoder.GetFrame(0))
                 {
                     converter.Initialize(frameDecode, SharpDX.WIC.PixelFormat.Format32bppBGR);
                     return(SharpDX.Direct2D1.Bitmap.FromWicBitmap(target, converter));
                 }
             }
         }
     }
 }
Example #16
0
        private SharpDX.Direct2D1.Effect CreateEffectGraph(SharpDX.WIC.FormatConverter formatConverter, Vector2 scale, float blurDeviation)
        {
            // Setup local variables
            var d2dDevice  = _deviceManager.DeviceDirect2D;
            var d2dContext = _deviceManager.ContextDirect2D;

            // Effect 1 : BitmapSource - take decoded image data and get a BitmapSource from it
            SharpDX.Direct2D1.Effects.BitmapSource bitmapSourceEffect = new SharpDX.Direct2D1.Effects.BitmapSource(d2dContext);
            bitmapSourceEffect.ScaleSource     = scale;
            bitmapSourceEffect.WicBitmapSource = formatConverter;

            // Effect 2 : GaussianBlur - give the bitmapsource a gaussian blurred effect
            SharpDX.Direct2D1.Effects.GaussianBlur gaussianBlurEffect = new SharpDX.Direct2D1.Effects.GaussianBlur(d2dContext);
            gaussianBlurEffect.SetInput(0, bitmapSourceEffect.Output, true);
            gaussianBlurEffect.StandardDeviation = blurDeviation;

            return(gaussianBlurEffect);
        }
Example #17
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="assetUri"></param>
        /// <param name="backgroundImageFormatConverter"></param>
        /// <param name="backgroundImageSize"></param>
        public void LoadAsset(string assetUri, out SharpDX.WIC.FormatConverter backgroundImageFormatConverter, out Size2 backgroundImageSize)
        {
            SharpDX.WIC.ImagingFactory2 wicFactory = null;
            if (_deviceManager == null)
            {
                wicFactory = new SharpDX.WIC.ImagingFactory2();
            }
            else if (_deviceManager.WICFactory == null)
            {
                wicFactory = new SharpDX.WIC.ImagingFactory2();
            }
            else
            {
                wicFactory = _deviceManager.WICFactory;
            }

            var path = Windows.ApplicationModel.Package.Current.InstalledLocation.Path;

            SharpDX.WIC.BitmapDecoder bitmapDecoder = new SharpDX.WIC.BitmapDecoder(
                wicFactory,
                assetUri,
                SharpDX.IO.NativeFileAccess.Read,
                SharpDX.WIC.DecodeOptions.CacheOnDemand
                );

            SharpDX.WIC.BitmapFrameDecode bitmapFrameDecode = bitmapDecoder.GetFrame(0);

            SharpDX.WIC.BitmapSource bitmapSource = new SharpDX.WIC.BitmapSource(bitmapFrameDecode.NativePointer);

            SharpDX.WIC.FormatConverter formatConverter = new SharpDX.WIC.FormatConverter(wicFactory);
            //formatConverter.Initialize( bitmapSource, SharpDX.WIC.PixelFormat.Format32bppBGRA);
            formatConverter.Initialize(
                bitmapSource,
                SharpDX.WIC.PixelFormat.Format32bppBGRA,
                SharpDX.WIC.BitmapDitherType.None,
                null,
                0.0f,
                SharpDX.WIC.BitmapPaletteType.Custom
                );

            backgroundImageSize = formatConverter.Size;

            backgroundImageFormatConverter = formatConverter;
        }
Example #18
0
        /// <summary>
        /// Loads a bitmap using WIC.
        /// </summary>
        /// <param name="deviceManager"></param>
        /// <param name="filename"></param>
        /// <returns></returns>
        public static SharpDX.WIC.BitmapSource LoadBitmap(SharpDX.WIC.ImagingFactory2 factory, string filename)
        {
            var bitmapDecoder = new SharpDX.WIC.BitmapDecoder(
                factory,
                filename,
                SharpDX.WIC.DecodeOptions.CacheOnDemand
                );

            var formatConverter = new SharpDX.WIC.FormatConverter(factory);

            formatConverter.Initialize(
                bitmapDecoder.GetFrame(0),
                SharpDX.WIC.PixelFormat.Format32bppPRGBA,
                SharpDX.WIC.BitmapDitherType.None,
                null,
                0.0,
                SharpDX.WIC.BitmapPaletteType.Custom);

            return formatConverter;
        }
Example #19
0
        private SharpDX.WIC.FormatConverter DecodeImage()
        {
            var path = Windows.ApplicationModel.Package.Current.InstalledLocation.Path;

            SharpDX.WIC.BitmapDecoder bitmapDecoder = new SharpDX.WIC.BitmapDecoder(
                _deviceManager.WICFactory,
                @"Assets\SydneyOperaHouse002.png",
                SharpDX.IO.NativeFileAccess.Read,
                SharpDX.WIC.DecodeOptions.CacheOnDemand
                );

            SharpDX.WIC.BitmapFrameDecode bitmapFrameDecode = bitmapDecoder.GetFrame(0);

            SharpDX.WIC.BitmapSource bitmapSource = new SharpDX.WIC.BitmapSource(bitmapFrameDecode.NativePointer);

            SharpDX.WIC.FormatConverter formatConverter = new SharpDX.WIC.FormatConverter(_deviceManager.WICFactory);
            formatConverter.Initialize(bitmapSource, SharpDX.WIC.PixelFormat.Format32bppBGRA);

            return(formatConverter);
        }
Example #20
0
        public static Image Load(string path)
        {
            using (var decoder = new SharpDX.WIC.BitmapDecoder(mImageFactory,
                                                               path, SharpDX.WIC.DecodeOptions.CacheOnLoad))
            {
                using (var frame = decoder.GetFrame(0))
                {
                    using (var converter = new SharpDX.WIC.FormatConverter(mImageFactory))
                    {
                        converter.Initialize(frame, SharpDX.WIC.PixelFormat.Format32bppRGBA);

                        byte[] data = new byte[4 * converter.Size.Width * converter.Size.Height];

                        converter.CopyPixels(data, 4 * converter.Size.Width);

                        return(new Image(new Size(converter.Size.Width, converter.Size.Height), PixelFormat.RedBlueGreenAlpha8bit, data));
                    }
                }
            }
        }
        public static Resource FromMemory(Device device, Stream stream)
        {
            var bitmapDecoder = new SharpDX.WIC.BitmapDecoder(
                _Factory,
                stream,
                SharpDX.WIC.DecodeOptions.CacheOnDemand
                );

            var formatConverter = new SharpDX.WIC.FormatConverter(_Factory);

            formatConverter.Initialize(
                bitmapDecoder.GetFrame(0),
                SharpDX.WIC.PixelFormat.Format32bppPRGBA,
                SharpDX.WIC.BitmapDitherType.None,
                null,
                0.0,
                SharpDX.WIC.BitmapPaletteType.Custom);

            return(CreateTexture2DFromBitmap(device, formatConverter));
        }
Example #22
0
        /// <summary>
        /// Loads a bitmap using WIC.
        /// </summary>
        /// <param name="bitmapPath"></param>
        /// <returns></returns>
        public SharpDX.WIC.BitmapSource LoadBitmap(string path)
        {
            if (!File.Exists(path))
            {
                return(null);
            }

            var bitmapDecoder = new SharpDX.WIC.BitmapDecoder(factory, path, SharpDX.WIC.DecodeOptions.CacheOnDemand);

            var formatConverter = new SharpDX.WIC.FormatConverter(factory);

            formatConverter.Initialize(
                bitmapDecoder.GetFrame(0),
                SharpDX.WIC.PixelFormat.Format32bppPRGBA,
                SharpDX.WIC.BitmapDitherType.None,
                null,
                0.0,
                SharpDX.WIC.BitmapPaletteType.Custom);

            return(formatConverter);
        }
Example #23
0
        /// <summary>
        /// Loads a bitmap using WIC.
        /// </summary>
        /// <param name="deviceManager"></param>
        /// <param name="filename"></param>
        /// <returns></returns>
        public static SharpDX.WIC.BitmapSource LoadBitmap(SharpDX.WIC.ImagingFactory2 factory, string filename)
        {
            //filename = Windows.ApplicationModel.Package.Current.InstalledLocation.Path + filename;
            var bitmapDecoder = new SharpDX.WIC.BitmapDecoder(
                factory,
                filename,
                SharpDX.WIC.DecodeOptions.CacheOnDemand
                );

            var formatConverter = new SharpDX.WIC.FormatConverter(factory);

            formatConverter.Initialize(
                bitmapDecoder.GetFrame(0),
                SharpDX.WIC.PixelFormat.Format32bppPRGBA,
                SharpDX.WIC.BitmapDitherType.None,
                null,
                0.0,
                SharpDX.WIC.BitmapPaletteType.Custom);

            return(formatConverter);
        }
Example #24
0
        private SharpDX.Direct2D1.Effect CreateEffectGraph(SharpDX.WIC.FormatConverter formatConverter, Vector2 scale, float blurDeviation)
        {
            // Setup local variables
            var d2dDevice  = _deviceManager.DeviceDirect2D;
            var d2dContext = _deviceManager.ContextDirect2D;

            // Effect 1 : BitmapSource - take decoded image data and get a BitmapSource from it
            SharpDX.Direct2D1.Effects.BitmapSourceEffect bitmapSourceEffect = new SharpDX.Direct2D1.Effects.BitmapSourceEffect(d2dContext);
            bitmapSourceEffect.ScaleSource     = scale;
            bitmapSourceEffect.WicBitmapSource = formatConverter;
            //bitmapSourceEffect.Cached = true; // Because the image will not be changing, we should cache the effect for performance reasons.

            // Effect 2 : PointSpecular
            _pointSpecularEffect = new SharpDX.Direct2D1.Effects.PointSpecular(d2dContext);
            _pointSpecularEffect.SetInput(0, bitmapSourceEffect.Output, true);

            // Effect 3 : SpotSpecular
            _spotSpecularEffect = new SharpDX.Direct2D1.Effects.SpotSpecular(d2dContext);
            _spotSpecularEffect.SetInput(0, bitmapSourceEffect.Output, true);

            // Effect 4 : DistantSpecular
            _distantSpecularEffect = new SharpDX.Direct2D1.Effects.DistantSpecular(d2dContext);
            _distantSpecularEffect.SetInput(0, bitmapSourceEffect.Output, true);

            // Effect 5 : PointDiffuse
            _pointDiffuseEffect = new SharpDX.Direct2D1.Effects.PointDiffuse(d2dContext);
            _pointDiffuseEffect.SetInput(0, bitmapSourceEffect.Output, true);

            // Effect 6 : SpotDiffuse
            _spotDiffuseEffect = new SharpDX.Direct2D1.Effects.SpotDiffuse(d2dContext);
            _spotDiffuseEffect.SetInput(0, bitmapSourceEffect.Output, true);

            // Effect 7 : DistantDiffuse
            _distantDiffuseEffect = new SharpDX.Direct2D1.Effects.DistantDiffuse(d2dContext);
            _distantDiffuseEffect.SetInput(0, bitmapSourceEffect.Output, true);


            return(_pointSpecularEffect);
        }
Example #25
0
 private SharpDX.Direct2D1.Bitmap CreateFromBitmap(SharpDX.Direct2D1.RenderTarget InRenderTarget, Bitmap InBitmap)
 {
     using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
     {
         InBitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
         using (SharpDX.WIC.BitmapDecoder bitDecorder =
                    new SharpDX.WIC.BitmapDecoder(Direct2DDrawingSystem.instance.ImagingFactory,
                                                  ms,
                                                  SharpDX.WIC.DecodeOptions.CacheOnDemand)
                )
         {
             using (SharpDX.WIC.BitmapFrameDecode bfDecode = bitDecorder.GetFrame(0))
             {
                 using (SharpDX.WIC.FormatConverter fConverter = new SharpDX.WIC.FormatConverter(Direct2DDrawingSystem.instance.ImagingFactory))
                 {
                     fConverter.Initialize(bfDecode, SharpDX.WIC.PixelFormat.Format32bppPBGRA, SharpDX.WIC.BitmapDitherType.None, null, 0, SharpDX.WIC.BitmapPaletteType.Custom);
                     return(SharpDX.Direct2D1.Bitmap.FromWicBitmap(InRenderTarget, fConverter));
                 }
             }
         }
     }
 }
Example #26
0
        private void LoadBitmap(RenderTarget device, byte[] bytes)
        {
            if (device == null)
            {
                throw new ArgumentNullException(nameof(device));
            }
            if (bytes == null)
            {
                throw new ArgumentNullException(nameof(bytes));
            }

            try
            {
                var stream = new MemoryStream(bytes);
                SharpDX.WIC.BitmapDecoder decoder = new SharpDX.WIC.BitmapDecoder(ImagingFactory, stream, SharpDX.WIC.DecodeOptions.CacheOnDemand);
                var frame = decoder.GetFrame(0);
                SharpDX.WIC.FormatConverter converter = new SharpDX.WIC.FormatConverter(ImagingFactory);
                try
                {
                    // normal ARGB images (Bitmaps / png tested)
                    converter.Initialize(frame, SharpDX.WIC.PixelFormat.Format32bppRGBA1010102);
                }
                catch
                {
                    // falling back to RGB if unsupported
                    converter.Initialize(frame, SharpDX.WIC.PixelFormat.Format32bppRGB);
                }
                SharpDXBitmap = Bitmap.FromWicBitmap(device, converter);

                converter.Dispose();
                frame.Dispose();
                decoder.Dispose();
                stream.Dispose();
            }
            catch (Exception ex)
            {
                throw new FormatException("Invalid or unsupported image format!", ex);
            }
        }
Example #27
0
        public static DirectXResource Load(DirectXContext dx, byte[] buffer)
        {
            using (MemoryStream stream = new MemoryStream(buffer))
            {
                var bitmapDecoder = new SharpDX.WIC.BitmapDecoder(
                    dx.ImagingFactory2,
                    stream,
                    SharpDX.WIC.DecodeOptions.CacheOnLoad);

                var formatConverter = new SharpDX.WIC.FormatConverter(dx.ImagingFactory2);

                formatConverter.Initialize(
                    bitmapDecoder.GetFrame(0),
                    SharpDX.WIC.PixelFormat.Format32bppPRGBA,
                    SharpDX.WIC.BitmapDitherType.None,
                    null,
                    0.0,
                    SharpDX.WIC.BitmapPaletteType.Custom);
                //var bmp = TextureLoader.LoadBitmap(dx, stream);
                return(TextureLoader.CreateTexture2DFromBitmap(dx, formatConverter));
            }
        }
Example #28
0
        private void Initialize(s.WIC.BitmapDecoder decoder)
        {
            using (var frame = decoder.GetFrame(0))
            {
                using (var f = new s.WIC.FormatConverter(Factory))
                {
                    f.Initialize(
                        frame,
                        s.WIC.PixelFormat.Format32bppBGRA,
                        s.WIC.BitmapDitherType.None,
                        null,
                        0f,
                        s.WIC.BitmapPaletteType.MedianCut);

                    sd.RenderTarget renderTarget = null; // BUGBUG: fix

                    Control =
                        sd.Bitmap.FromWicBitmap(
                            renderTarget: null, // TODO
                            wicBitmapSource: frame);
                }
            }
        }
Example #29
0
        public virtual void Initialize(DeviceManager deviceManager)
        {
            _deviceManager  = deviceManager;
            sceneColorBrush = new SolidColorBrush(deviceManager.ContextDirect2D, Color.White);

            //GET IMAGE DATA
            _formatConverter = DecodeImage();

            //DEFAULTS MAY NEED THIS DATA
            var size        = deviceManager.ContextDirect2D.Size;
            int pixelWidth  = (int)(size.Width * Windows.Graphics.Display.DisplayProperties.LogicalDpi / 96.0);
            int pixelHeight = (int)(size.Height * Windows.Graphics.Display.DisplayProperties.LogicalDpi / 96.0);


            //CREATE EFFECT-GRAPH USING IMAGE DATA
            Scale           = new Vector2(0.8f, 0.8f);
            BlurDeviation   = 0f;
            _lightPositionZ = 100f;
            PointsAt        = new Vector3(pixelWidth / 2, pixelHeight / 2, 0.0f);


            UpdateEffectGraph();
        }
        public Texture2D LoadTexture(System.Drawing.Bitmap b)
        {
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            b.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
            ms.Seek(0, System.IO.SeekOrigin.Begin);

            SharpDX.WIC.BitmapDecoder   decoder   = new SharpDX.WIC.BitmapDecoder(ImageFactory, ms, SharpDX.WIC.DecodeOptions.CacheOnDemand);
            SharpDX.WIC.FormatConverter converter = new SharpDX.WIC.FormatConverter(ImageFactory);
            converter.Initialize(decoder.GetFrame(0), SharpDX.WIC.PixelFormat.Format32bppPRGBA, SharpDX.WIC.BitmapDitherType.None, null, 0.0, SharpDX.WIC.BitmapPaletteType.Custom);
            SharpDX.WIC.BitmapSource bitmap = converter;
            int        stride = bitmap.Size.Width * 4;
            DataStream buffer = new DataStream(bitmap.Size.Height * stride, true, true);

            bitmap.CopyPixels(stride, buffer);
            Texture2D texture = new Texture2D(Device, new Texture2DDescription()
            {
                Width = bitmap.Size.Width, Height = bitmap.Size.Height, ArraySize = 1, BindFlags = BindFlags.ShaderResource, Usage = ResourceUsage.Immutable, CpuAccessFlags = CpuAccessFlags.None, Format = Format.R8G8B8A8_UNorm, MipLevels = 1, OptionFlags = ResourceOptionFlags.None, SampleDescription = new SampleDescription(1, 0)
            }, new DataRectangle(buffer.DataPointer, stride));

            bitmap.Dispose();
            buffer.Dispose();
            ms.Dispose();
            return(texture);
        }
        public static SharpDX.WIC.Bitmap SaveToWICBitmap(DeviceManager deviceManager, Texture2D source)
        {
            var device = deviceManager.Direct3DDevice;
            var context = deviceManager.Direct3DContext;

            var txDesc = source.Description;

            txDesc.Usage = ResourceUsage.Staging;
            txDesc.CpuAccessFlags = CpuAccessFlags.Read;
            txDesc.BindFlags = BindFlags.None;
            txDesc.OptionFlags = ResourceOptionFlags.None;
            txDesc.SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0);

            Guid pixelFormat = PixelFormatFromFormat(txDesc.Format);
            if (pixelFormat == Guid.Empty)
            {
                return null;
            }

            System.Diagnostics.Debug.Assert(BitsPerPixel(txDesc.Format) == SharpDX.WIC.PixelFormat.GetBitsPerPixel(pixelFormat), "Error with DXGI.Format -> PixelFormat");

            using (var dest = new Texture2D(device, txDesc))
            {
                if (source.Description.SampleDescription.Count > 1 || source.Description.SampleDescription.Quality > 0)
                {
                    // In order to copy a multisampled texture to a CPU readable texture, it must first be resolved into a GPU only Texture
                    // Initialize a target to resolve multi-sampled render target
                    var resolvedDesc = source.Description;
                    resolvedDesc.BindFlags = BindFlags.ShaderResource;
                    resolvedDesc.SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0);

                    // if depth stencil needs to be typeless
                    if (resolvedDesc.Format == SharpDX.DXGI.Format.D24_UNorm_S8_UInt)
                        resolvedDesc.Format = SharpDX.DXGI.Format.R24G8_Typeless;

                    using (var resolvedTarget = new Texture2D(device, resolvedDesc))
                    {
                        CopyToTexture(context, source, resolvedTarget);
                        // Now we can copy to the destination
                        CopyToTexture(context, source, dest);
                    }
                }
                else
                    CopyToTexture(context, source, dest);
                var sourceData = context.MapSubresource(dest, 0, MapMode.Read, MapFlags.None);

                var encoder = new SharpDX.WIC.PngBitmapEncoder(deviceManager.WICFactory);

                var formatConverter = new SharpDX.WIC.FormatConverter(deviceManager.WICFactory);

                SharpDX.WIC.Bitmap bm = new SharpDX.WIC.Bitmap(deviceManager.WICFactory, txDesc.Width, txDesc.Height, pixelFormat, SharpDX.WIC.BitmapCreateCacheOption.CacheOnLoad);

                var bytesPerPixel = BitsPerPixel(txDesc.Format) / 8;
                using (var l = bm.Lock(SharpDX.WIC.BitmapLockFlags.Write))
                {
                    var destPtr = l.Data.DataPointer;
                    var sourcePtr = sourceData.DataPointer;
                    for (int y = 0; y < bm.Size.Height; y++)
                    {
                        SharpDX.Utilities.CopyMemory(destPtr, sourcePtr, bm.Size.Width * bytesPerPixel);

                        sourcePtr = IntPtr.Add(sourcePtr, sourceData.RowPitch);
                        destPtr = IntPtr.Add(destPtr, l.Data.Pitch);
                    }
                }
                context.UnmapSubresource(dest, 0);

                return bm;
            }
        }
Example #32
0
            static SharpDX.WIC.BitmapSource LoadBitmap(SharpDX.WIC.BitmapDecoder decoder, SharpDX.WIC.ImagingFactory2 factory)
            {
                var formatConverter = new SharpDX.WIC.FormatConverter(factory);

                formatConverter.Initialize(
                    decoder.GetFrame(0),
                    SharpDX.WIC.PixelFormat.Format32bppPRGBA,
                    SharpDX.WIC.BitmapDitherType.None,
                    null,
                    0.0,
                    SharpDX.WIC.BitmapPaletteType.Custom);

                return formatConverter;
            }
Example #33
0
        public virtual void Initialize(DeviceManager deviceManager)
        {
            _deviceManager = deviceManager;
            sceneColorBrush = new SolidColorBrush(deviceManager.ContextDirect2D, Color.White);
            
            //GET IMAGE DATA
            _formatConverter = DecodeImage();

            //DEFAULTS MAY NEED THIS DATA
            var size = deviceManager.ContextDirect2D.Size;
            int pixelWidth = (int)(size.Width * Windows.Graphics.Display.DisplayProperties.LogicalDpi / 96.0);
            int pixelHeight = (int)(size.Height * Windows.Graphics.Display.DisplayProperties.LogicalDpi / 96.0);


            //CREATE EFFECT-GRAPH USING IMAGE DATA
            Scale = new Vector2(0.8f, 0.8f);
            BlurDeviation = 0f;
            _lightPositionZ = 100f;
            PointsAt = new Vector3(pixelWidth / 2, pixelHeight / 2, 0.0f);


            UpdateEffectGraph();

        }
Example #34
0
        private SharpDX.WIC.FormatConverter DecodeImage()
        {
            var path = Windows.ApplicationModel.Package.Current.InstalledLocation.Path;

            SharpDX.WIC.BitmapDecoder bitmapDecoder = new SharpDX.WIC.BitmapDecoder(
                                                                                        _deviceManager.WICFactory,
                                                                                        @"Assets\heightmap.png",
                                                                                        SharpDX.IO.NativeFileAccess.Read,
                                                                                        SharpDX.WIC.DecodeOptions.CacheOnDemand
                                                                                    );

            SharpDX.WIC.BitmapFrameDecode bitmapFrameDecode = bitmapDecoder.GetFrame(0);

            SharpDX.WIC.BitmapSource bitmapSource = new SharpDX.WIC.BitmapSource(bitmapFrameDecode.NativePointer);

            SharpDX.WIC.FormatConverter formatConverter = new SharpDX.WIC.FormatConverter(_deviceManager.WICFactory);
            //formatConverter.Initialize( bitmapSource, SharpDX.WIC.PixelFormat.Format32bppBGRA);
            formatConverter.Initialize(
                bitmapSource,
                SharpDX.WIC.PixelFormat.Format32bppBGRA, 
                SharpDX.WIC.BitmapDitherType.None, 
                null, 
                0.0f, 
                SharpDX.WIC.BitmapPaletteType.Custom
                ); 
            
            return formatConverter;
        }
        /// <summary>
        /// This doesnt work! SharpDX errors when trying to open a local system file (not relative)
        /// </summary>
        /// <param name="assetNativeUri"></param>
        /// <param name="backgroundImageFormatConverter"></param>
        /// <param name="backgroundImageSize"></param>
        public void LoadNativeAsset(string assetNativeUri, out SharpDX.WIC.FormatConverter backgroundImageFormatConverter, out Size2 backgroundImageSize)
        {
            var path = Windows.ApplicationModel.Package.Current.InstalledLocation.Path;


            var nativeFileStream = new SharpDX.IO.NativeFileStream(
                assetNativeUri,
                SharpDX.IO.NativeFileMode.Open,
                SharpDX.IO.NativeFileAccess.Read,
                SharpDX.IO.NativeFileShare.Read);


            var r = SharpDX.IO.NativeFile.ReadAllBytes(assetNativeUri);

            var data = SharpDX.IO.NativeFile.ReadAllBytes(assetNativeUri);

            using (System.IO.MemoryStream ms = new System.IO.MemoryStream(data))
            {
                if (ms != null)
                {

                    using (SharpDX.WIC.BitmapDecoder bitmapDecoder = new SharpDX.WIC.BitmapDecoder(
                                                                                                _deviceManager.WICFactory,
                                                                                                ms,
                                                                                                SharpDX.WIC.DecodeOptions.CacheOnDemand
                                                                                            ))
                    {


                        using (SharpDX.WIC.BitmapFrameDecode bitmapFrameDecode = bitmapDecoder.GetFrame(0))
                        {

                            using (SharpDX.WIC.BitmapSource bitmapSource = new SharpDX.WIC.BitmapSource(bitmapFrameDecode.NativePointer))
                            {

                                SharpDX.WIC.FormatConverter formatConverter = new SharpDX.WIC.FormatConverter(_deviceManager.WICFactory);
                                //formatConverter.Initialize( bitmapSource, SharpDX.WIC.PixelFormat.Format32bppBGRA);
                                formatConverter.Initialize(
                                    bitmapSource,
                                    SharpDX.WIC.PixelFormat.Format32bppBGRA,
                                    SharpDX.WIC.BitmapDitherType.None,
                                    null,
                                    0.0f,
                                    SharpDX.WIC.BitmapPaletteType.Custom
                                    );

                                backgroundImageSize = formatConverter.Size;

                                backgroundImageFormatConverter = formatConverter;

                            }




                        }



                    }



                }
            }

            backgroundImageFormatConverter = null;
            backgroundImageSize = new Size2(0, 0);

        }
Example #36
0
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="assetUri"></param>
        ///// <param name="backgroundImageFormatConverter"></param>
        ///// <param name="backgroundImageSize"></param>
        //public void LoadAsset(
        //    SharpDX.WIC.ImagingFactory2 wicFactory, 
        //    string assetUri, 
        //    out SharpDX.WIC.FormatConverter backgroundImageFormatConverter, 
        //    out DrawingSize backgroundImageSize)
        //{
            
        //    //var path = Windows.ApplicationModel.Package.Current.InstalledLocation.Path;

        //    SharpDX.WIC.BitmapDecoder bitmapDecoder = new SharpDX.WIC.BitmapDecoder(
        //                                                                                wicFactory,
        //                                                                                assetUri,
        //                                                                                SharpDX.IO.NativeFileAccess.Read,
        //                                                                                SharpDX.WIC.DecodeOptions.CacheOnDemand
        //                                                                            );

        //    SharpDX.WIC.BitmapFrameDecode bitmapFrameDecode = bitmapDecoder.GetFrame(0);

        //    SharpDX.WIC.BitmapSource bitmapSource = new SharpDX.WIC.BitmapSource(bitmapFrameDecode.NativePointer);

        //    SharpDX.WIC.FormatConverter formatConverter = new SharpDX.WIC.FormatConverter(wicFactory);
        //    //formatConverter.Initialize( bitmapSource, SharpDX.WIC.PixelFormat.Format32bppBGRA);
        //    formatConverter.Initialize(
        //        bitmapSource,
        //        SharpDX.WIC.PixelFormat.Format32bppBGRA,
        //        SharpDX.WIC.BitmapDitherType.None,
        //        null,
        //        0.0f,
        //        SharpDX.WIC.BitmapPaletteType.Custom
        //        );

        //    backgroundImageSize = formatConverter.Size;

        //    backgroundImageFormatConverter = formatConverter;
        //}


        /// <summary>
        /// Loads bitmap asynchronously and injects into global variables. I need to work out how to NOT make them global
        /// </summary>
        /// <param name="assetNativeUri"></param>
        /// <returns></returns>
        public async Task<Tuple<SharpDX.WIC.FormatConverter, Size2, Stream>> LoadAssetAsync(
            SharpDX.WIC.ImagingFactory2 wicFactory, 
            string assetNativeUri,
            string cacheId,
            string path = ""
            )
        {

            if (_listOfAssets.ContainsKey(cacheId)) return _listOfAssets[cacheId];


            SharpDX.WIC.FormatConverter _backgroundImageFormatConverter = null;
            Size2 _backgroundImageSize = new Size2(0, 0);

            Windows.Storage.StorageFile storageFile = null;

            if(path == string.Empty) {
                path = Windows.ApplicationModel.Package.Current.InstalledLocation.Path;
                storageFile = await Windows.Storage.StorageFile.GetFileFromPathAsync(path + assetNativeUri);
            }
            else if (path == "PicturesLibrary")
            {

                var assetNativeUriParts = assetNativeUri.Split("\\".ToCharArray());

                var foundFolder = await Windows.Storage.KnownFolders.PicturesLibrary.GetFolderAsync(assetNativeUriParts[0]);
                storageFile = await foundFolder.GetFileAsync(assetNativeUriParts[1]);
            }
            else if (path == "PublicPicturesLibrary")
            {

                var assetNativeUriParts = assetNativeUri.Split("\\".ToCharArray());

                var foundFolder = await Windows.Storage.KnownFolders.PicturesLibrary.GetFolderAsync(assetNativeUriParts[0]);
                storageFile = await foundFolder.GetFileAsync(assetNativeUriParts[1]);
            }

            if (storageFile == null) return null;
            
            Stream ms = await storageFile.OpenStreamForReadAsync();  //ras.GetResults().AsStreamForRead())
            //var data = SharpDX.IO.NativeFile.ReadAllBytes(assetNativeUri);
            //using (System.IO.MemoryStream ms = new System.IO.MemoryStream(data))
            {
                if (ms != null)
                {

                    SharpDX.WIC.BitmapDecoder bitmapDecoder = new SharpDX.WIC.BitmapDecoder(
                                                                                                wicFactory,
                                                                                                ms,
                                                                                                SharpDX.WIC.DecodeOptions.CacheOnDemand
                                                                                            );
                    {


                        using (SharpDX.WIC.BitmapFrameDecode bitmapFrameDecode = bitmapDecoder.GetFrame(0))
                        {

                            using(SharpDX.WIC.BitmapSource bitmapSource = new SharpDX.WIC.BitmapSource(bitmapFrameDecode.NativePointer))
                            {

                                SharpDX.WIC.FormatConverter formatConverter = new SharpDX.WIC.FormatConverter(wicFactory);
                                //formatConverter.Initialize( bitmapSource, SharpDX.WIC.PixelFormat.Format32bppBGRA);
                                formatConverter.Initialize(
                                    bitmapSource,
                                    SharpDX.WIC.PixelFormat.Format32bppBGRA,
                                    SharpDX.WIC.BitmapDitherType.None,
                                    null,
                                    0.0f,
                                    SharpDX.WIC.BitmapPaletteType.Custom
                                    );
                                
                                _backgroundImageSize = formatConverter.Size;
                                _backgroundImageFormatConverter = formatConverter;


                                //return Tuple.Create<SharpDX.WIC.FormatConverter, Size2>(_backgroundImageFormatConverter, _backgroundImageSize);
                            }

                        }

                    }

                }
            }

            //ras.Close();
            

            var ret = Tuple.Create<SharpDX.WIC.FormatConverter, Size2, Stream>(_backgroundImageFormatConverter, _backgroundImageSize, ms);

            _listOfAssets.Add(cacheId, ret);

            return ret;

        }
Example #37
0
        /// <summary>
        /// Copies to a stream using WIC. The format is converted if necessary.
        /// </summary>
        /// <param name="context"></param>
        /// <param name="texture"></param>
        /// <param name="outputFormat"></param>
        /// <param name="stream"></param>
        public void ToStream(SharpDX.Direct3D11.DeviceContext context, Texture2D texture, ImageFormat outputFormat, Stream stream)
        {
            if (wicFactory == null)
            {
                wicFactory = ToDispose(new SharpDX.WIC.ImagingFactory2());
            }

            DataStream dataStream;
            var        dataBox = context.MapSubresource(
                texture,
                0,
                0,
                MapMode.Read,
                SharpDX.Direct3D11.MapFlags.None,
                out dataStream);

            try
            {
                var dataRectangle = new DataRectangle
                {
                    DataPointer = dataStream.DataPointer,
                    Pitch       = dataBox.RowPitch
                };

                var format = PixelFormatFromFormat(texture.Description.Format);

                if (format == Guid.Empty)
                {
                    return;
                }

                using (var bitmap = new SharpDX.WIC.Bitmap(
                           wicFactory,
                           texture.Description.Width,
                           texture.Description.Height,
                           format,
                           dataRectangle))
                {
                    stream.Position = 0;

                    SharpDX.WIC.BitmapEncoder bitmapEncoder = null;
                    switch (outputFormat)
                    {
                    case ImageFormat.Bitmap:
                        bitmapEncoder = new SharpDX.WIC.BmpBitmapEncoder(wicFactory, stream);
                        break;

                    case ImageFormat.Jpeg:
                        bitmapEncoder = new SharpDX.WIC.JpegBitmapEncoder(wicFactory, stream);
                        break;

                    case ImageFormat.Png:
                        bitmapEncoder = new SharpDX.WIC.PngBitmapEncoder(wicFactory, stream);
                        break;

                    default:
                        return;
                    }

                    try
                    {
                        using (var bitmapFrameEncode = new SharpDX.WIC.BitmapFrameEncode(bitmapEncoder))
                        {
                            bitmapFrameEncode.Initialize();
                            bitmapFrameEncode.SetSize(bitmap.Size.Width, bitmap.Size.Height);
                            var pixelFormat = format;
                            bitmapFrameEncode.SetPixelFormat(ref pixelFormat);

                            if (pixelFormat != format)
                            {
                                // IWICFormatConverter
                                var converter = new SharpDX.WIC.FormatConverter(wicFactory);
                                if (converter.CanConvert(format, pixelFormat))
                                {
                                    converter.Initialize(bitmap, SharpDX.WIC.PixelFormat.Format24bppBGR, SharpDX.WIC.BitmapDitherType.None, null, 0, SharpDX.WIC.BitmapPaletteType.MedianCut);
                                    bitmapFrameEncode.SetPixelFormat(ref pixelFormat);
                                    bitmapFrameEncode.WriteSource(converter);
                                }
                                else
                                {
                                    this.DebugMessage(string.Format("Unable to convert Direct3D texture format {0} to a suitable WIC format", texture.Description.Format.ToString()));
                                    return;
                                }
                            }
                            else
                            {
                                bitmapFrameEncode.WriteSource(bitmap);
                            }
                            bitmapFrameEncode.Commit();
                            bitmapEncoder.Commit();
                        }
                    }
                    finally
                    {
                        bitmapEncoder.Dispose();
                    }
                }
            }
            finally
            {
                context.UnmapSubresource(texture, 0);
            }
        }
Example #38
0
        private static unsafe Texture3D CreateTexture3D(DirectXContext dx, byte[] file)
        {
            using MemoryStream stream = new MemoryStream(file);
            using var bitmapDecoder   = new SharpDX.WIC.BitmapDecoder(
                      dx.ImagingFactory2,
                      stream,
                      SharpDX.WIC.DecodeOptions.CacheOnLoad);

            using var bitmapSource = new SharpDX.WIC.FormatConverter(dx.ImagingFactory2);

            bitmapSource.Initialize(
                bitmapDecoder.GetFrame(0),
                SharpDX.WIC.PixelFormat.Format32bppPRGBA,
                SharpDX.WIC.BitmapDitherType.None,
                null,
                0.0,
                SharpDX.WIC.BitmapPaletteType.Custom);

            int width  = bitmapSource.Size.Width;
            int stride = width * 4;
            int height = bitmapSource.Size.Height;

            var source = new byte[stride * height];

            bitmapSource.CopyPixels(source, stride);

            var target = new byte[stride * height];

            int cubeWidth = 64;

            var macro_width  = width / cubeWidth;
            var macro_height = height / cubeWidth;

            IntPtr ptr;


            fixed(byte *bbuffer = source)
            fixed(byte *bcursor = target)
            {
                int *buffer = (int *)bbuffer;
                int *cursor = (int *)bcursor;

                for (int z = 0; z < cubeWidth; ++z)
                {
                    int z_x = (z % macro_width) * cubeWidth;
                    int z_y = (z / macro_height) * cubeWidth;
                    for (int y = 0; y < cubeWidth; ++y)
                    {
                        int row_index = width * (z_y + y);
                        for (int x = 0; x < cubeWidth; ++x)
                        {
                            int index = row_index + z_x + x;

                            *cursor = *(buffer + index);
                            cursor += 1;
                        }
                    }
                }

                ptr = (IntPtr)bcursor;
            }

            var rowSizeBits    = cubeWidth * 32;
            var sliceSizeBytes = cubeWidth * rowSizeBits / 8;
            var newRowSize     = rowSizeBits / 8;
            var newSlizeSize   = sliceSizeBytes;

            var data = new DataBox(ptr, newRowSize, newSlizeSize);

            return(new Texture3D(dx.Device, new Texture3DDescription()
            {
                Width = cubeWidth,
                Height = cubeWidth,
                Depth = cubeWidth,
                BindFlags = BindFlags.ShaderResource,
                Usage = ResourceUsage.Default,
                CpuAccessFlags = CpuAccessFlags.None,
                Format = SharpDX.DXGI.Format.R8G8B8A8_UNorm,
                MipLevels = 1,
                OptionFlags = ResourceOptionFlags.None,
            }, new[] { data }));
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="assetUri"></param>
        /// <param name="backgroundImageFormatConverter"></param>
        /// <param name="backgroundImageSize"></param>
        public void LoadAsset(string assetUri, out SharpDX.WIC.FormatConverter backgroundImageFormatConverter, out Size2 backgroundImageSize)
        {
            SharpDX.WIC.ImagingFactory2 wicFactory = null;
            if (_deviceManager == null) wicFactory = new SharpDX.WIC.ImagingFactory2();
            else if (_deviceManager.WICFactory == null) wicFactory = new SharpDX.WIC.ImagingFactory2();
            else wicFactory = _deviceManager.WICFactory;

            var path = Windows.ApplicationModel.Package.Current.InstalledLocation.Path;

            SharpDX.WIC.BitmapDecoder bitmapDecoder = new SharpDX.WIC.BitmapDecoder(
                                                                                        wicFactory,
                                                                                        assetUri,
                                                                                        SharpDX.IO.NativeFileAccess.Read,
                                                                                        SharpDX.WIC.DecodeOptions.CacheOnDemand
                                                                                    );

            SharpDX.WIC.BitmapFrameDecode bitmapFrameDecode = bitmapDecoder.GetFrame(0);

            SharpDX.WIC.BitmapSource bitmapSource = new SharpDX.WIC.BitmapSource(bitmapFrameDecode.NativePointer);

            SharpDX.WIC.FormatConverter formatConverter = new SharpDX.WIC.FormatConverter(wicFactory);
            //formatConverter.Initialize( bitmapSource, SharpDX.WIC.PixelFormat.Format32bppBGRA);
            formatConverter.Initialize(
                bitmapSource,
                SharpDX.WIC.PixelFormat.Format32bppBGRA,
                SharpDX.WIC.BitmapDitherType.None,
                null,
                0.0f,
                SharpDX.WIC.BitmapPaletteType.Custom
                );

            backgroundImageSize = formatConverter.Size;

            backgroundImageFormatConverter = formatConverter;
        }
Example #40
0
		public static s.WIC.Bitmap ToBitmap(this s.WIC.BitmapSource bmp, Guid? pixelFormat = null)
		{
			using (var converter = new s.WIC.FormatConverter(SDFactory.WicImagingFactory))
			{
				converter.Initialize(
					bmp,
					pixelFormat ?? s.WIC.PixelFormat.Format32bppPBGRA,
					s.WIC.BitmapDitherType.None,
					null,
					0f,
					s.WIC.BitmapPaletteType.Custom);

				return new s.WIC.Bitmap(SDFactory.WicImagingFactory, converter, s.WIC.BitmapCreateCacheOption.CacheOnLoad);
			}
		}