Switch test helpers from WIC to ImageSharp
このコミットが含まれているのは:
1 親
fcb783195d
コミット
cd3c696c4e
5ファイル変更
+102
-217
検索条件に一致するファイルはありません
@@ -108,7 +108,7 @@ public partial class D2D1ResourceTextureManagerTests
|
||||
|
||||
using ComPtr<ID2D1Bitmap1> d2D1Bitmap1Buffer = D2D1Helper.CreateD2D1Bitmap1Buffer(d2D1DeviceContext.Get(), d2D1BitmapTarget.Get(), out D2D1_MAPPED_RECT d2D1MappedRect);
|
||||
|
||||
WICHelper.SaveBitmapToFile(destinationPath, (uint)expected.Width, (uint)expected.Height, d2D1MappedRect.pitch, d2D1MappedRect.bits);
|
||||
ImageHelper.SaveBitmapToFile(destinationPath, (uint)expected.Width, (uint)expected.Height, d2D1MappedRect.pitch, d2D1MappedRect.bits);
|
||||
|
||||
TolerantImageComparer.AssertEqual(destinationPath, expectedPath, 0.00001f);
|
||||
}
|
||||
@@ -162,7 +162,7 @@ public partial class D2D1ResourceTextureManagerTests
|
||||
|
||||
using ComPtr<ID2D1Bitmap1> d2D1Bitmap1Buffer = D2D1Helper.CreateD2D1Bitmap1Buffer(d2D1DeviceContext.Get(), d2D1BitmapTarget.Get(), out D2D1_MAPPED_RECT d2D1MappedRect);
|
||||
|
||||
WICHelper.SaveBitmapToFile(destinationPath, (uint)expected.Width, (uint)expected.Height, d2D1MappedRect.pitch, d2D1MappedRect.bits);
|
||||
ImageHelper.SaveBitmapToFile(destinationPath, (uint)expected.Width, (uint)expected.Height, d2D1MappedRect.pitch, d2D1MappedRect.bits);
|
||||
|
||||
TolerantImageComparer.AssertEqual(destinationPath, expectedPath, 0.00001f);
|
||||
}
|
||||
@@ -233,7 +233,7 @@ public partial class D2D1ResourceTextureManagerTests
|
||||
|
||||
using ComPtr<ID2D1Bitmap1> d2D1Bitmap1Buffer = D2D1Helper.CreateD2D1Bitmap1Buffer(d2D1DeviceContext.Get(), d2D1BitmapTarget.Get(), out D2D1_MAPPED_RECT d2D1MappedRect);
|
||||
|
||||
WICHelper.SaveBitmapToFile(destinationPath, (uint)expected.Width, (uint)expected.Height, d2D1MappedRect.pitch, d2D1MappedRect.bits);
|
||||
ImageHelper.SaveBitmapToFile(destinationPath, (uint)expected.Width, (uint)expected.Height, d2D1MappedRect.pitch, d2D1MappedRect.bits);
|
||||
|
||||
TolerantImageComparer.AssertEqual(destinationPath, expectedPath, 0.00001f);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using ComputeSharp.D2D1.Tests.Extensions;
|
||||
using System;
|
||||
using ComputeSharp.D2D1.Tests.Extensions;
|
||||
using TerraFX.Interop.DirectX;
|
||||
using TerraFX.Interop.Windows;
|
||||
|
||||
@@ -100,17 +101,40 @@ internal static class D2D1Helper
|
||||
/// Creates an <see cref="ID2D1Bitmap"/> instance.
|
||||
/// </summary>
|
||||
/// <param name="d2D1DeviceContext">The input <see cref="ID2D1DeviceContext"/> instance to use to create the bitmap source.</param>
|
||||
/// <param name="wicBitmap">The input <see cref="IWICBitmap"/> to use to create the bitmap source.</param>
|
||||
/// <param name="pixels">The loaded pixel data.</param>
|
||||
/// <param name="width">The width of the bitmap.</param>
|
||||
/// <param name="height">The height of the bitmap.</param>
|
||||
/// <param name="d2D1Effect">The input <see cref="ID2D1Effect"/> to set the source for.</param>
|
||||
/// <returns>A new <see cref="ID2D1Bitmap"/> instance.</returns>
|
||||
public static unsafe ComPtr<ID2D1Bitmap> CreateD2D1BitmapAndSetAsSource(ID2D1DeviceContext* d2D1DeviceContext, IWICBitmap* wicBitmap, ID2D1Effect* d2D1Effect)
|
||||
public static unsafe ComPtr<ID2D1Bitmap> CreateD2D1BitmapAndSetAsSource(
|
||||
ID2D1DeviceContext* d2D1DeviceContext,
|
||||
ReadOnlyMemory<byte> pixels,
|
||||
uint width,
|
||||
uint height,
|
||||
ID2D1Effect* d2D1Effect)
|
||||
{
|
||||
using ComPtr<ID2D1Bitmap> d2D1BitmapSource = default;
|
||||
|
||||
// Create a source D2D1 bitmap from the WIC bitmap
|
||||
d2D1DeviceContext->CreateBitmapFromWicBitmap(
|
||||
wicBitmapSource: (IWICBitmapSource*)wicBitmap,
|
||||
bitmap: d2D1BitmapSource.GetAddressOf()).Assert();
|
||||
D2D_SIZE_U d2DSize = default;
|
||||
d2DSize.width = width;
|
||||
d2DSize.height = height;
|
||||
|
||||
D2D1_BITMAP_PROPERTIES d2DBitmapProperties = default;
|
||||
d2DBitmapProperties.pixelFormat.format = DXGI_FORMAT.DXGI_FORMAT_B8G8R8A8_UNORM;
|
||||
d2DBitmapProperties.pixelFormat.alphaMode = D2D1_ALPHA_MODE.D2D1_ALPHA_MODE_PREMULTIPLIED;
|
||||
d2DBitmapProperties.dpiX = 96;
|
||||
d2DBitmapProperties.dpiY = 96;
|
||||
|
||||
fixed (byte* p = pixels.Span)
|
||||
{
|
||||
// Create a source D2D1 bitmap from the image data
|
||||
d2D1DeviceContext->CreateBitmap(
|
||||
size: d2DSize,
|
||||
srcData: p,
|
||||
pitch: width * 4,
|
||||
bitmapProperties: &d2DBitmapProperties,
|
||||
bitmap: d2D1BitmapSource.GetAddressOf()).Assert();
|
||||
}
|
||||
|
||||
d2D1Effect->SetInput(0, (ID2D1Image*)d2D1BitmapSource.Get());
|
||||
|
||||
|
||||
@@ -117,8 +117,9 @@ internal static class D2D1TestRunner
|
||||
|
||||
D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(in shader, d2D1Effect.Get());
|
||||
|
||||
using ComPtr<IWICBitmap> wicBitmap = WICHelper.LoadBitmapFromFile(sourcePath, out uint width, out uint height);
|
||||
using ComPtr<ID2D1Bitmap> d2D1BitmapSource = D2D1Helper.CreateD2D1BitmapAndSetAsSource(d2D1DeviceContext.Get(), wicBitmap.Get(), d2D1Effect.Get());
|
||||
ReadOnlyMemory<byte> pixels = ImageHelper.LoadBitmapFromFile(sourcePath, out uint width, out uint height);
|
||||
|
||||
using ComPtr<ID2D1Bitmap> d2D1BitmapSource = D2D1Helper.CreateD2D1BitmapAndSetAsSource(d2D1DeviceContext.Get(), pixels, width, height, d2D1Effect.Get());
|
||||
using ComPtr<ID2D1Bitmap> d2D1BitmapTarget = D2D1Helper.CreateD2D1BitmapAndSetAsTarget(d2D1DeviceContext.Get(), width, height);
|
||||
|
||||
D2D1Helper.DrawEffect(d2D1DeviceContext.Get(), d2D1Effect.Get());
|
||||
@@ -126,7 +127,7 @@ internal static class D2D1TestRunner
|
||||
using ComPtr<ID2D1Bitmap1> d2D1Bitmap1Buffer = D2D1Helper.CreateD2D1Bitmap1Buffer(d2D1DeviceContext.Get(), d2D1BitmapTarget.Get(), out D2D1_MAPPED_RECT d2D1MappedRect);
|
||||
|
||||
// Save the image
|
||||
WICHelper.SaveBitmapToFile(destinationPath, width, height, d2D1MappedRect.pitch, d2D1MappedRect.bits);
|
||||
ImageHelper.SaveBitmapToFile(destinationPath, width, height, d2D1MappedRect.pitch, d2D1MappedRect.bits);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -165,6 +166,6 @@ internal static class D2D1TestRunner
|
||||
using ComPtr<ID2D1Bitmap1> d2D1Bitmap1Buffer = D2D1Helper.CreateD2D1Bitmap1Buffer(d2D1DeviceContext.Get(), d2D1BitmapTarget.Get(), out D2D1_MAPPED_RECT d2D1MappedRect);
|
||||
|
||||
// Save the image
|
||||
WICHelper.SaveBitmapToFile(destinationPath, (uint)width, (uint)height, d2D1MappedRect.pitch, d2D1MappedRect.bits);
|
||||
ImageHelper.SaveBitmapToFile(destinationPath, (uint)width, (uint)height, d2D1MappedRect.pitch, d2D1MappedRect.bits);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
using System;
|
||||
using System.Buffers;
|
||||
using System.Runtime.InteropServices;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using SixLabors.ImageSharp;
|
||||
using SixLabors.ImageSharp.PixelFormats;
|
||||
|
||||
namespace ComputeSharp.D2D1.Tests.Helpers;
|
||||
|
||||
/// <summary>
|
||||
/// A <see langword="class"/> with helpers to load and save images.
|
||||
/// </summary>
|
||||
internal static class ImageHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// Loads a <see cref="ReadOnlyMemory{T}"/> instance from a given path.
|
||||
/// </summary>
|
||||
/// <param name="filename">The path to load the image from.</param>
|
||||
/// <param name="width">The resulting image width.</param>
|
||||
/// <param name="height">The resulting image height.</param>
|
||||
/// <returns>The resulting <see cref="ReadOnlyMemory{T}"/> instance.</returns>
|
||||
public static unsafe ReadOnlyMemory<byte> LoadBitmapFromFile(string filename, out uint width, out uint height)
|
||||
{
|
||||
using Image<Bgra32> image = Image.Load<Bgra32>(filename);
|
||||
|
||||
width = (uint)image.Width;
|
||||
height = (uint)image.Height;
|
||||
|
||||
if (!image.DangerousTryGetSinglePixelMemory(out Memory<Bgra32> memory))
|
||||
{
|
||||
Assert.Inconclusive();
|
||||
}
|
||||
|
||||
return MemoryMarshal.AsBytes(memory.Span).ToArray();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Saves a given image buffer to a target path.
|
||||
/// </summary>
|
||||
/// <param name="filename">The path to save the image to.</param>
|
||||
/// <param name="width">The image width.</param>
|
||||
/// <param name="height">The image height.</param>
|
||||
/// <param name="strideInBytes">The image stride in bytes.</param>
|
||||
/// <param name="buffer">The image pixel buffer.</param>
|
||||
public static unsafe void SaveBitmapToFile(string filename, uint width, uint height, uint strideInBytes, byte* buffer)
|
||||
{
|
||||
byte[] pixelData = ArrayPool<byte>.Shared.Rent((int)width * (int)height * sizeof(Bgra32));
|
||||
|
||||
for (int i = 0; i < (int)height; i++)
|
||||
{
|
||||
ReadOnlySpan<byte> source = new(buffer + i * (int)strideInBytes, (int)width * sizeof(Bgra32));
|
||||
|
||||
source.CopyTo(pixelData.AsSpan(i * (int)width * sizeof(Bgra32)));
|
||||
}
|
||||
|
||||
using (Image<Bgra32> image = Image.WrapMemory<Bgra32>(pixelData, (int)width, (int)height))
|
||||
{
|
||||
image.Save(filename);
|
||||
}
|
||||
|
||||
ArrayPool<byte>.Shared.Return(pixelData);
|
||||
}
|
||||
}
|
||||
@@ -1,203 +0,0 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using ComputeSharp.D2D1.Tests.Extensions;
|
||||
using TerraFX.Interop.Windows;
|
||||
|
||||
namespace ComputeSharp.D2D1.Tests.Helpers;
|
||||
|
||||
/// <summary>
|
||||
/// A <see langword="class"/> that uses the WIC APIs to load and decode images.
|
||||
/// </summary>
|
||||
internal static class WICHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// Loads an <see cref="IWICBitmap"/> instance from a given path.
|
||||
/// </summary>
|
||||
/// <param name="filename">The path to load the image from.</param>
|
||||
/// <param name="width">The resulting image width.</param>
|
||||
/// <param name="height">The resulting image height.</param>
|
||||
/// <returns>The resulting <see cref="IWICBitmap"/> instance.</returns>
|
||||
public static unsafe ComPtr<IWICBitmap> LoadBitmapFromFile(string filename, out uint width, out uint height)
|
||||
{
|
||||
using ComPtr<IWICImagingFactory2> wicImagingFactory2 = default;
|
||||
|
||||
// Create a Windows Imaging Component (WIC) factory
|
||||
Windows.CoCreateInstance(
|
||||
rclsid: (Guid*)Unsafe.AsPointer(ref Unsafe.AsRef(in CLSID.CLSID_WICImagingFactory2)),
|
||||
pUnkOuter: null,
|
||||
dwClsContext: (uint)CLSCTX.CLSCTX_INPROC_SERVER,
|
||||
riid: Windows.__uuidof<IWICImagingFactory2>(),
|
||||
ppv: (void**)wicImagingFactory2.GetAddressOf()).Assert();
|
||||
|
||||
using ComPtr<IWICBitmapDecoder> wicBitmapDecoder = default;
|
||||
|
||||
// Get the bitmap decoder for the target file
|
||||
fixed (char* p = filename)
|
||||
{
|
||||
wicImagingFactory2.Get()->CreateDecoderFromFilename(
|
||||
wzFilename: (ushort*)p,
|
||||
pguidVendor: null,
|
||||
dwDesiredAccess: Windows.GENERIC_READ,
|
||||
metadataOptions: WICDecodeOptions.WICDecodeMetadataCacheOnDemand,
|
||||
ppIDecoder: wicBitmapDecoder.GetAddressOf()).Assert();
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
fixed (uint* widthPtr = &width)
|
||||
fixed (uint* heightPtr = &height)
|
||||
{
|
||||
// Extract the image size info
|
||||
wicBitmapFrameDecode.Get()->GetSize(widthPtr, heightPtr).Assert();
|
||||
}
|
||||
|
||||
Guid wicTargetPixelFormatGuid = GUID.GUID_WICPixelFormat32bppPBGRA;
|
||||
Guid wicActualPixelFormatGuid;
|
||||
|
||||
// Get the current and target pixel format info
|
||||
wicBitmapFrameDecode.Get()->GetPixelFormat(&wicActualPixelFormatGuid).Assert();
|
||||
|
||||
using ComPtr<IWICBitmap> wicBitmap = default;
|
||||
|
||||
// Create the target bitmap
|
||||
wicImagingFactory2.Get()->CreateBitmap(
|
||||
uiWidth: width,
|
||||
uiHeight: height,
|
||||
pixelFormat: &wicTargetPixelFormatGuid,
|
||||
option: WICBitmapCreateCacheOption.WICBitmapCacheOnLoad,
|
||||
ppIBitmap: wicBitmap.GetAddressOf()).Assert();
|
||||
|
||||
WICRect wicRect;
|
||||
wicRect.X = 0;
|
||||
wicRect.Y = 0;
|
||||
wicRect.Width = (int)width;
|
||||
wicRect.Height = (int)height;
|
||||
|
||||
using (ComPtr<IWICBitmapLock> wicBitmapLock = default)
|
||||
{
|
||||
// Lock the bitmap
|
||||
wicBitmap.Get()->Lock(
|
||||
prcLock: &wicRect,
|
||||
flags: (uint)WICBitmapLockFlags.WICBitmapLockWrite,
|
||||
ppILock: wicBitmapLock.GetAddressOf()).Assert();
|
||||
|
||||
uint wicBitmapStride;
|
||||
|
||||
// Get the stride
|
||||
wicBitmapLock.Get()->GetStride(&wicBitmapStride).Assert();
|
||||
|
||||
uint wicBitmapSize;
|
||||
byte* wicBitmapPointer;
|
||||
|
||||
// Get the bitmap size and pointer
|
||||
wicBitmapLock.Get()->GetDataPointer(&wicBitmapSize, &wicBitmapPointer).Assert();
|
||||
|
||||
// Check if a pixel format conversion is needed
|
||||
if (wicTargetPixelFormatGuid == wicActualPixelFormatGuid)
|
||||
{
|
||||
// No conversion is needed, just copy the pixels directly
|
||||
wicBitmapFrameDecode.Get()->CopyPixels(
|
||||
prc: &wicRect,
|
||||
cbStride: wicBitmapStride,
|
||||
cbBufferSize: wicBitmapSize,
|
||||
pbBuffer: wicBitmapPointer).Assert();
|
||||
}
|
||||
else
|
||||
{
|
||||
using ComPtr<IWICFormatConverter> wicFormatConverter = default;
|
||||
|
||||
// Create a format converter
|
||||
wicImagingFactory2.Get()->CreateFormatConverter(wicFormatConverter.GetAddressOf()).Assert();
|
||||
|
||||
// Get a format converter to decode the pixel data
|
||||
wicFormatConverter.Get()->Initialize(
|
||||
pISource: (IWICBitmapSource*)wicBitmapFrameDecode.Get(),
|
||||
dstFormat: &wicTargetPixelFormatGuid,
|
||||
dither: WICBitmapDitherType.WICBitmapDitherTypeNone,
|
||||
pIPalette: null,
|
||||
alphaThresholdPercent: 0,
|
||||
paletteTranslate: WICBitmapPaletteType.WICBitmapPaletteTypeCustom).Assert();
|
||||
|
||||
// Decode the pixel data into the upload buffer
|
||||
wicFormatConverter.Get()->CopyPixels(
|
||||
prc: &wicRect,
|
||||
cbStride: wicBitmapStride,
|
||||
cbBufferSize: wicBitmapSize,
|
||||
pbBuffer: wicBitmapPointer).Assert();
|
||||
}
|
||||
}
|
||||
|
||||
return wicBitmap.Move();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Saves a given image buffer to a target path.
|
||||
/// </summary>
|
||||
/// <param name="filename">The path to save the image to.</param>
|
||||
/// <param name="width">The image width.</param>
|
||||
/// <param name="height">The image height.</param>
|
||||
/// <param name="strideInBytes">The image stride in bytes.</param>
|
||||
/// <param name="buffer">The image pixel buffer.</param>
|
||||
public static unsafe void SaveBitmapToFile(string filename, uint width, uint height, uint strideInBytes, byte* buffer)
|
||||
{
|
||||
using ComPtr<IWICImagingFactory2> wicImagingFactory2 = default;
|
||||
|
||||
// Create a Windows Imaging Component (WIC) factory
|
||||
Windows.CoCreateInstance(
|
||||
rclsid: (Guid*)Unsafe.AsPointer(ref Unsafe.AsRef(in CLSID.CLSID_WICImagingFactory2)),
|
||||
pUnkOuter: null,
|
||||
dwClsContext: (uint)CLSCTX.CLSCTX_INPROC_SERVER,
|
||||
riid: Windows.__uuidof<IWICImagingFactory2>(),
|
||||
ppv: (void**)wicImagingFactory2.GetAddressOf()).Assert();
|
||||
|
||||
using ComPtr<IWICBitmapEncoder> wicBitmapEncoder = default;
|
||||
|
||||
// Create the image encoder
|
||||
wicImagingFactory2.Get()->CreateEncoder(
|
||||
guidContainerFormat: (Guid*)Unsafe.AsPointer(ref Unsafe.AsRef(in GUID.GUID_ContainerFormatPng)),
|
||||
pguidVendor: null,
|
||||
ppIEncoder: wicBitmapEncoder.GetAddressOf()).Assert();
|
||||
|
||||
using ComPtr<IWICStream> wicStream = default;
|
||||
|
||||
// Create and initialize a stream to the target file
|
||||
wicImagingFactory2.Get()->CreateStream(wicStream.GetAddressOf()).Assert();
|
||||
|
||||
fixed (char* p = filename)
|
||||
{
|
||||
// Initialize the stream to the target file
|
||||
wicStream.Get()->InitializeFromFilename((ushort*)p, Windows.GENERIC_WRITE);
|
||||
}
|
||||
|
||||
// Initialize the encoder
|
||||
wicBitmapEncoder.Get()->Initialize(
|
||||
pIStream: (IStream*)wicStream.Get(),
|
||||
cacheOption: WICBitmapEncoderCacheOption.WICBitmapEncoderNoCache).Assert();
|
||||
|
||||
using ComPtr<IWICBitmapFrameEncode> wicBitmapFrameEncode = default;
|
||||
|
||||
// Create the image frame and initialize it
|
||||
wicBitmapEncoder.Get()->CreateNewFrame(wicBitmapFrameEncode.GetAddressOf(), null).Assert();
|
||||
|
||||
Guid wicTargetPixelFormatGuid = GUID.GUID_WICPixelFormat32bppPBGRA;
|
||||
|
||||
// Set the encoding properties
|
||||
wicBitmapFrameEncode.Get()->Initialize(null).Assert();
|
||||
wicBitmapFrameEncode.Get()->SetSize(width, height).Assert();
|
||||
wicBitmapFrameEncode.Get()->SetPixelFormat(&wicTargetPixelFormatGuid).Assert();
|
||||
|
||||
// Encode the target image
|
||||
wicBitmapFrameEncode.Get()->WritePixels(
|
||||
lineCount: height,
|
||||
cbStride: strideInBytes,
|
||||
cbBufferSize: strideInBytes * height,
|
||||
pbPixels: buffer).Assert();
|
||||
|
||||
// Flush the changes
|
||||
wicBitmapFrameEncode.Get()->Commit();
|
||||
wicBitmapEncoder.Get()->Commit();
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする