Advertisement
Razer

WIC Texture2D Import

Dec 14th, 2017
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.63 KB | None | 0 0
  1. // Opening the stream
  2.     public Stream GetBaseStream(Stream baseStream, PipelineContext context)
  3.     {
  4.         if (!(Settings is FileStreamWrapperSettings settings))
  5.             Settings = settings = (FileStreamWrapperSettings) GetDefaultSettings();
  6.  
  7.         FileStream fs = null;
  8.         try
  9.         {
  10.             // SKIPPED CODE (NOT RELEVANT)
  11.             fs = new FileStream(TargetResource.LocalPath, settings.Mode, settings.Access, settings.Share);
  12.             _base = fs;
  13.             if (context.IsCancellationRequested)
  14.                 context.ApproveCancellation();
  15.         }
  16.         catch (Exception ex)
  17.         {
  18.             context.Log.AddTranslated(...);
  19.             SafeDisposer.Dispose(ref fs);
  20.         }
  21.         return fs;
  22.     }
  23.  
  24. // The import task
  25.     public BitmapSource Import(Stream baseStream, PipelineContext context)
  26.     {
  27.         BitmapSource source = TextureLoader.LoadBitmap(baseStream);
  28.         // Disposes the source once the import pipeline has completed
  29.         context.DisposeOf(source);
  30.         return source;
  31.     }
  32.  
  33. // BitmapSource loader (from Stream)
  34.     public static BitmapSource LoadBitmap([NotNull] Stream stream)
  35.     {
  36.         if (stream == null) throw new ArgumentNullException(nameof(stream));
  37.  
  38.         ImagingFactory factory = new ImagingFactory();
  39.  
  40.         BitmapDecoder decoder = new BitmapDecoder(
  41.             factory,
  42.             stream,
  43.             DecodeOptions.CacheOnDemand);
  44.         FormatConverter converter = new FormatConverter(factory);
  45.            
  46.         converter.Initialize(decoder.GetFrame(0), PixelFormat.Format32bppPRGBA,
  47.             BitmapDitherType.None, null, 0.0, BitmapPaletteType.Custom);
  48.         SafeDisposer.Dispose(ref decoder);
  49.         return converter;
  50.     }
  51.  
  52. // Followed by the transformation task (BitmapSource -> Texture2D)
  53.     public Texture2D Transform(BitmapSource unprocessedObject, PipelineContext context)
  54.     {
  55.         IGraphicsDevice device = Core.IDE.GetGraphicsCore().CreateDevice(RenderingSystem.DirectX11,
  56.             unprocessedObject.Size.Width, unprocessedObject.Size.Height, IntPtr.Zero);
  57.         var conversion = BitmapToTexture.CreateTexture2DFromBitmap(device.DxDevice, unprocessedObject);
  58.         Core.IDE.GetGraphicsCore().DestroyDevice(device);
  59.  
  60.         return conversion;
  61.     }
  62.  
  63. // Conversion from WIC BitmapSource to Texture2D
  64.     public static SharpDX.Direct3D11.Texture2D CreateTexture2DFromBitmap(SharpDX.Direct3D11.Device device, BitmapSource bitmapSource)
  65.     {
  66.         // Allocate DataStream to receive the WIC image pixels
  67.  
  68.         int stride = PixelFormat.GetStride(bitmapSource.PixelFormat, bitmapSource.Size.Width);
  69.         using (var buffer = new DataStream(bitmapSource.Size.Height * stride, true, true))
  70.         {
  71.             // Copy the content of the WIC to the buffer
  72.             bitmapSource.CopyPixels(stride, buffer);
  73.             return new SharpDX.Direct3D11.Texture2D(device, new SharpDX.Direct3D11.Texture2DDescription()
  74.             {
  75.                 Width = bitmapSource.Size.Width,
  76.                 Height = bitmapSource.Size.Height,
  77.                 ArraySize = 1,
  78.                 BindFlags = SharpDX.Direct3D11.BindFlags.ShaderResource,
  79.                 Usage = SharpDX.Direct3D11.ResourceUsage.Default,
  80.                 CpuAccessFlags = SharpDX.Direct3D11.CpuAccessFlags.None,
  81.                 Format = SharpDX.DXGI.Format.R8G8B8A8_UNorm,
  82.                 MipLevels = 1,
  83.                 OptionFlags = SharpDX.Direct3D11.ResourceOptionFlags.None,
  84.                 SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
  85.             }, new DataRectangle(buffer.DataPointer , stride));
  86.         }
  87.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement