Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Addresses limitations of detecting ImageFormat / ContainerFormat from the file extension #844

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/ComputeSharp/Graphics/Helpers/WICFormatHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,28 @@ public static bool TryGetIntermediateFormatForType<T>(Guid containerFormat, out
/// <exception cref="ArgumentException">Thrown when the input filename doesn't have a valid file extension.</exception>
public static Guid GetForFilename(ReadOnlySpan<char> filename)
{
return Path.GetExtension(filename) switch
Span<char> extension = stackalloc char[4];

int length = Path.GetExtension(filename).ToLowerInvariant(extension);

default(ArgumentException).ThrowIf(length == -1, nameof(filename));

return extension[..length] switch
{
".dib" or
".rle" or
".bmp" => GUID.GUID_ContainerFormatBmp,
".png" => GUID.GUID_ContainerFormatPng,
".jpe" or
".jfif" or
".exif" or
".jpg" or
".jpeg" => GUID.GUID_ContainerFormatJpeg,
".jxr" or
".hdp" or
".wdp" or
".wmp" => GUID.GUID_ContainerFormatWmp,
".tif" or
".tiff" => GUID.GUID_ContainerFormatTiff,
".dds" => GUID.GUID_ContainerFormatDds,
_ => default(ArgumentException).Throw<Guid>(nameof(filename))
Expand All @@ -146,4 +158,4 @@ public static Guid GetForFormat(ImageFormat format)
_ => default(ArgumentException).Throw<Guid>(nameof(format))
};
}
}
}
3 changes: 3 additions & 0 deletions src/ComputeSharp/Graphics/Helpers/WICHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ public void SaveTexture<T>(TextureView2D<T> texture, ReadOnlySpan<char> filename
where T : unmanaged
{
using ComPtr<IWICBitmapEncoder> wicBitmapEncoder = default;

Guid containerGuid = WICFormatHelper.GetForFilename(filename);

// Create the image encoder
Expand Down Expand Up @@ -397,6 +398,7 @@ public void SaveTexture<T>(TextureView2D<T> texture, Stream stream, ImageFormat
where T : unmanaged
{
using ComPtr<IWICBitmapEncoder> wicBitmapEncoder = default;

Guid containerGuid = WICFormatHelper.GetForFormat(format);

// Create the image encoder
Expand Down Expand Up @@ -431,6 +433,7 @@ public void SaveTexture<T>(TextureView2D<T> texture, IBufferWriter<byte> writer,
where T : unmanaged
{
using ComPtr<IWICBitmapEncoder> wicBitmapEncoder = default;

Guid containerGuid = WICFormatHelper.GetForFormat(format);

// Create the image encoder
Expand Down