Added extension to load a texture from a buffer

このコミットが含まれているのは:
Sergio Pedri committed 2021-05-12 11:33:05 +02:00
1 親 ec647da78b
コミット c66ac3fe19
3ファイル変更
+84 -4

検索条件に一致するファイルはありません

+16
ファイルの表示
@@ -344,6 +344,22 @@ namespace ComputeSharp
return WICHelper.Instance.LoadTexture<T, TPixel>(device, filename);
}
/// <summary>
/// Allocates a new readonly 2D texture with the contents of the specified buffer.
/// </summary>
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
/// <typeparam name="TPixel">The type of pixels used on the GPU side.</typeparam>
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the texture.</param>
/// <param name="span">The buffer with the image data to load and decode into the texture.</param>
/// <returns>A <see cref="ReadOnlyTexture2D{T, TPixel}"/> instance with the contents of the specified file.</returns>
[Pure]
public static ReadOnlyTexture2D<T, TPixel> AllocateReadOnlyTexture2D<T, TPixel>(this GraphicsDevice device, ReadOnlySpan<byte> span)
where T : unmanaged, IUnorm<TPixel>
where TPixel : unmanaged
{
return WICHelper.Instance.LoadTexture<T, TPixel>(device, span);
}
/// <summary>
/// Allocates a new readonly 3D texture with the specified parameters.
/// </summary>
+50 -1
ファイルの表示
@@ -76,10 +76,59 @@ namespace ComputeSharp.Graphics.Helpers
wicBitmapDecoder.GetAddressOf()).Assert();
}
return LoadTexture<T, TPixel>(device, wicBitmapDecoder.Get());
}
/// <summary>
/// Loads a <see cref="ReadOnlyTexture2D{T, TPixel}"/> from a specified buffer.
/// </summary>
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
/// <typeparam name="TPixel">The type of pixels used on the GPU side.</typeparam>
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the texture.</param>
/// <param name="span">The buffer with the image data to load and decode into the texture.</param>
/// <returns>A <see cref="ReadOnlyTexture2D{T, TPixel}"/> instance with the contents of the specified file.</returns>
[Pure]
public ReadOnlyTexture2D<T, TPixel> LoadTexture<T, TPixel>(GraphicsDevice device, ReadOnlySpan<byte> span)
where T : unmanaged, IUnorm<TPixel>
where TPixel : unmanaged
{
using ComPtr<IWICBitmapDecoder> wicBitmapDecoder = default;
using ComPtr<IWICStream> wicStream = default;
// Get the bitmap decoder for the target buffer
fixed (byte* p = span)
{
this.wicImagingFactory2.Get()->CreateStream(wicStream.GetAddressOf()).Assert();
wicStream.Get()->InitializeFromMemory(p, (uint)span.Length).Assert();
this.wicImagingFactory2.Get()->CreateDecoderFromStream(
(IStream*)wicStream.Get(),
null,
WICDecodeOptions.WICDecodeMetadataCacheOnDemand,
wicBitmapDecoder.GetAddressOf()).Assert();
return LoadTexture<T, TPixel>(device, wicBitmapDecoder.Get());
}
}
/// <summary>
/// Loads a <see cref="ReadOnlyTexture2D{T, TPixel}"/> from a specified <see cref="IWICBitmapDecoder"/> object.
/// </summary>
/// <typeparam name="T">The type of items to store in the texture.</typeparam>
/// <typeparam name="TPixel">The type of pixels used on the GPU side.</typeparam>
/// <param name="device">The <see cref="GraphicsDevice"/> instance to use to allocate the texture.</param>
/// <param name="wicBitmapDecoder">The <see cref="IWICBitmapDecoder"/> object in use.</param>
/// <returns>A <see cref="ReadOnlyTexture2D{T, TPixel}"/> instance with the contents of the specified file.</returns>
[Pure]
public ReadOnlyTexture2D<T, TPixel> LoadTexture<T, TPixel>(GraphicsDevice device, IWICBitmapDecoder* wicBitmapDecoder)
where T : unmanaged, IUnorm<TPixel>
where TPixel : unmanaged
{
using ComPtr<IWICBitmapFrameDecode> wicBitmapFrameDecode = default;
// Get the first frame of the loaded image (if more are present, they will be ignored)
wicBitmapDecoder.Get()->GetFrame(0, wicBitmapFrameDecode.GetAddressOf()).Assert();
wicBitmapDecoder->GetFrame(0, wicBitmapFrameDecode.GetAddressOf()).Assert();
using ComPtr<IWICFormatConverter> wicFormatConverter = default;
Guid wicPixelFormatGuid = WICFormatHelper.GetForType<T>();
+18 -3
ファイルの表示
@@ -9,7 +9,6 @@ using ComputeSharp.Tests.Extensions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SixLabors.ImageSharp;
using ImageSharpRgba32 = SixLabors.ImageSharp.PixelFormats.Rgba32;
using ImageSharpBgra32 = SixLabors.ImageSharp.PixelFormats.Bgra32;
namespace ComputeSharp.Tests
{
@@ -19,11 +18,27 @@ namespace ComputeSharp.Tests
{
[CombinatorialTestMethod]
[AllDevices]
public void LoadImageAsRgba32(Device device)
public void LoadAsRgba32FromFile(Device device)
{
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging", "city.jpg");
using ReadOnlyTexture2D<Rgba32, Float4> texture = Gpu.Default.AllocateReadOnlyTexture2D<Rgba32, Float4>(path);
using ReadOnlyTexture2D<Rgba32, Float4> texture = device.Get().AllocateReadOnlyTexture2D<Rgba32, Float4>(path);
using Image<ImageSharpRgba32> loaded = texture.ToImage();
using Image<ImageSharpRgba32> original = Image.Load<ImageSharpRgba32>(path);
TolerantImageComparer.AssertEqual(original, loaded, 0.0000032f);
}
[CombinatorialTestMethod]
[AllDevices]
public void LoadAsRgba32FromBuffer(Device device)
{
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, "Imaging", "city.jpg");
byte[] buffer = File.ReadAllBytes(path);
using ReadOnlyTexture2D<Rgba32, Float4> texture = device.Get().AllocateReadOnlyTexture2D<Rgba32, Float4>(buffer);
using Image<ImageSharpRgba32> loaded = texture.ToImage();
using Image<ImageSharpRgba32> original = Image.Load<ImageSharpRgba32>(path);