Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Opening the stream
- public Stream GetBaseStream(Stream baseStream, PipelineContext context)
- {
- if (!(Settings is FileStreamWrapperSettings settings))
- Settings = settings = (FileStreamWrapperSettings) GetDefaultSettings();
- FileStream fs = null;
- try
- {
- // SKIPPED CODE (NOT RELEVANT)
- fs = new FileStream(TargetResource.LocalPath, settings.Mode, settings.Access, settings.Share);
- _base = fs;
- if (context.IsCancellationRequested)
- context.ApproveCancellation();
- }
- catch (Exception ex)
- {
- context.Log.AddTranslated(...);
- SafeDisposer.Dispose(ref fs);
- }
- return fs;
- }
- // The import task
- public BitmapSource Import(Stream baseStream, PipelineContext context)
- {
- BitmapSource source = TextureLoader.LoadBitmap(baseStream);
- // Disposes the source once the import pipeline has completed
- context.DisposeOf(source);
- return source;
- }
- // BitmapSource loader (from Stream)
- public static BitmapSource LoadBitmap([NotNull] Stream stream)
- {
- if (stream == null) throw new ArgumentNullException(nameof(stream));
- ImagingFactory factory = new ImagingFactory();
- BitmapDecoder decoder = new BitmapDecoder(
- factory,
- stream,
- DecodeOptions.CacheOnDemand);
- FormatConverter converter = new FormatConverter(factory);
- converter.Initialize(decoder.GetFrame(0), PixelFormat.Format32bppPRGBA,
- BitmapDitherType.None, null, 0.0, BitmapPaletteType.Custom);
- SafeDisposer.Dispose(ref decoder);
- return converter;
- }
- // Followed by the transformation task (BitmapSource -> Texture2D)
- public Texture2D Transform(BitmapSource unprocessedObject, PipelineContext context)
- {
- IGraphicsDevice device = Core.IDE.GetGraphicsCore().CreateDevice(RenderingSystem.DirectX11,
- unprocessedObject.Size.Width, unprocessedObject.Size.Height, IntPtr.Zero);
- var conversion = BitmapToTexture.CreateTexture2DFromBitmap(device.DxDevice, unprocessedObject);
- Core.IDE.GetGraphicsCore().DestroyDevice(device);
- return conversion;
- }
- // Conversion from WIC BitmapSource to Texture2D
- public static SharpDX.Direct3D11.Texture2D CreateTexture2DFromBitmap(SharpDX.Direct3D11.Device device, BitmapSource bitmapSource)
- {
- // Allocate DataStream to receive the WIC image pixels
- int stride = PixelFormat.GetStride(bitmapSource.PixelFormat, bitmapSource.Size.Width);
- using (var buffer = new DataStream(bitmapSource.Size.Height * stride, true, true))
- {
- // Copy the content of the WIC to the buffer
- bitmapSource.CopyPixels(stride, buffer);
- return new SharpDX.Direct3D11.Texture2D(device, new SharpDX.Direct3D11.Texture2DDescription()
- {
- Width = bitmapSource.Size.Width,
- Height = bitmapSource.Size.Height,
- ArraySize = 1,
- BindFlags = SharpDX.Direct3D11.BindFlags.ShaderResource,
- Usage = SharpDX.Direct3D11.ResourceUsage.Default,
- CpuAccessFlags = SharpDX.Direct3D11.CpuAccessFlags.None,
- Format = SharpDX.DXGI.Format.R8G8B8A8_UNorm,
- MipLevels = 1,
- OptionFlags = SharpDX.Direct3D11.ResourceOptionFlags.None,
- SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
- }, new DataRectangle(buffer.DataPointer , stride));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement