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; } }
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(); }
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); }
public void Create(string filename) { using (var decoder = new s.WIC.BitmapDecoder( Factory, filename, s.WIC.DecodeOptions.CacheOnDemand)) Initialize(decoder); }
public void Create(System.IO.Stream stream) { using (var decoder = new s.WIC.BitmapDecoder( Factory, stream, s.WIC.DecodeOptions.CacheOnDemand)) Initialize(decoder); }
public static void LoadFromTiff(SharpDX.WIC.ImagingFactory imagingFactory, UnmanagedImage image, string filename, int bytesPerPixel) { // TODO: this function is more generic; rewrite to handle different formats/bytesPerPixel var decoder = new SharpDX.WIC.BitmapDecoder(imagingFactory, filename, SharpDX.WIC.DecodeOptions.CacheOnLoad); var bitmapFrameDecode = decoder.GetFrame(0); bitmapFrameDecode.CopyPixels(image.Width * bytesPerPixel, image.DataIntPtr, image.Width * image.Height * bytesPerPixel); bitmapFrameDecode.Dispose(); decoder.Dispose(); }
/// <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); }
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); }
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; } }
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)); }
/// <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); } }
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)); } } } } }
/// <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; }
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)); }
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); }
/// <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; }
/// <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); }
/// <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); }
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); } }
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)); } } } } }
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)); } }
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); } } }
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); }
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; }
public void CreateDeviceDependentResourcesAsync(DeviceResources deviceResources) { byte[] bytes = Convert.FromBase64String(spriteImg); Stream stream = new MemoryStream(bytes); var factory = new SharpDX.WIC.ImagingFactory2(); 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); SharpDX.WIC.BitmapSource bitmapSource = formatConverter; imageWidth = bitmapSource.Size.Width; imageHeight = bitmapSource.Size.Height; var height = bitmapSource.Size.Height * Spritesheet.textureScaleFactor; var width = bitmapSource.Size.Width * Spritesheet.textureScaleFactor; BlendStateDescription blendSdesc = new BlendStateDescription(); blendSdesc.IndependentBlendEnable = false; blendSdesc.AlphaToCoverageEnable = false; blendSdesc.RenderTarget[0].IsBlendEnabled = true; blendSdesc.RenderTarget[0].SourceBlend = BlendOption.SourceAlpha; blendSdesc.RenderTarget[0].DestinationBlend = BlendOption.InverseSourceAlpha; blendSdesc.RenderTarget[0].BlendOperation = BlendOperation.Add; blendSdesc.RenderTarget[0].SourceAlphaBlend = BlendOption.One; blendSdesc.RenderTarget[0].DestinationAlphaBlend = BlendOption.Zero; blendSdesc.RenderTarget[0].AlphaBlendOperation = BlendOperation.Add; blendSdesc.RenderTarget[0].RenderTargetWriteMask = ColorWriteMaskFlags.All; BlendState blendS = new BlendState(deviceResources.D3DDevice, blendSdesc); deviceResources.D3DDeviceContext.OutputMerger.SetBlendState(blendS); // Load mesh vertices. Each vertex has a position and a color. // Note that the cube size has changed from the default DirectX app // template. Windows Holographic is scaled in meters, so to draw the // cube at a comfortable size we made the cube width 0.2 m (20 cm). VertexPositionTexture[] cubeVertices = { new VertexPositionTexture(new System.Numerics.Vector3(-1f * width, -1f * height, 0f), new System.Numerics.Vector2(0.0f, 1.0f)), new VertexPositionTexture(new System.Numerics.Vector3(1f * width, -1f * height, 0f), new System.Numerics.Vector2(1.0f, 1.0f)), new VertexPositionTexture(new System.Numerics.Vector3(-1f * width, 1f * height, 0f), new System.Numerics.Vector2(0.0f, 0.0f)), new VertexPositionTexture(new System.Numerics.Vector3(1f * width, 1f * height, 0f), new System.Numerics.Vector2(1.0f, 0.0f)) }; BufferDescription mdescription = new BufferDescription(sizeof(float) * 5 * cubeVertices.Length, ResourceUsage.Dynamic, BindFlags.VertexBuffer, CpuAccessFlags.Write, ResourceOptionFlags.None, 0); vertexBuffer = this.ToDispose(SharpDX.Direct3D11.Buffer.Create( deviceResources.D3DDevice, cubeVertices, mdescription)); //vertexBuffer = this.ToDispose(SharpDX.Direct3D11.Buffer.Create( // deviceResources.D3DDevice, // SharpDX.Direct3D11.BindFlags.VertexBuffer, // cubeVertices, // 0, // ResourceUsage.Dynamic, CpuAccessFlags.Write)); // Load mesh indices. Each trio of indices represents // a triangle to be rendered on the screen. // For example: 0,2,1 means that the vertices with indexes // 0, 2 and 1 from the vertex buffer compose the // first triangle of this mesh. ushort[] cubeIndices = { 2, 1, 0, // -x 2, 3, 1, //back face 2, 0, 1, // -x 2, 1, 3, }; indexCount = cubeIndices.Length; indexBuffer = this.ToDispose(SharpDX.Direct3D11.Buffer.Create( deviceResources.D3DDevice, SharpDX.Direct3D11.BindFlags.IndexBuffer, cubeIndices)); // Create a constant buffer to store the model matrix. modelConstantBuffer = this.ToDispose(SharpDX.Direct3D11.Buffer.Create( deviceResources.D3DDevice, SharpDX.Direct3D11.BindFlags.ConstantBuffer, ref modelConstantBufferData)); //Load the image Texture2D texture = TextureLoader.CreateTexture2DFromBitmap(deviceResources.D3DDevice, bitmapSource); textureView = new ShaderResourceView(deviceResources.D3DDevice, texture); deviceResources.D3DDeviceContext.PixelShader.SetShaderResource(0, textureView); //Load the sampler SamplerStateDescription samplerDesc = new SamplerStateDescription(); samplerDesc.AddressU = TextureAddressMode.Wrap; samplerDesc.AddressV = TextureAddressMode.Wrap; samplerDesc.AddressW = TextureAddressMode.Wrap; samplerDesc.ComparisonFunction = Comparison.Never; samplerDesc.Filter = Filter.MinMagMipLinear; samplerDesc.MaximumLod = float.MaxValue; SamplerState sampler = new SamplerState(deviceResources.D3DDevice, samplerDesc); deviceResources.D3DDeviceContext.PixelShader.SetSampler(0, sampler); hasLoaded = true; }
///// <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; }
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( // 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); }
public static SharpDX.WIC.BitmapSource LoadBitmap(SharpDX.WIC.ImagingFactory2 factory, Stream stream) { var bitmapDecoder = new SharpDX.WIC.BitmapDecoder( factory, stream, SharpDX.WIC.DecodeOptions.CacheOnDemand ); return LoadBitmap(bitmapDecoder, factory); }
/// <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; }