From e7db1d33406ecd4a93326e236bd3b081730cfad6 Mon Sep 17 00:00:00 2001 From: elyoh Date: Tue, 27 Aug 2024 13:12:28 +0100 Subject: [PATCH 1/2] Addresses limitations of detecting ImageFormat / ContainerFormat from file extension Fixes #799. --- src/ComputeSharp/Graphics/Helpers/WICFormatHelper.cs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/ComputeSharp/Graphics/Helpers/WICFormatHelper.cs b/src/ComputeSharp/Graphics/Helpers/WICFormatHelper.cs index 7f258428..c8f6d5d1 100644 --- a/src/ComputeSharp/Graphics/Helpers/WICFormatHelper.cs +++ b/src/ComputeSharp/Graphics/Helpers/WICFormatHelper.cs @@ -111,16 +111,22 @@ internal static class WICFormatHelper /// Thrown when the input filename doesn't have a valid file extension. public static Guid GetForFilename(ReadOnlySpan filename) { - return Path.GetExtension(filename) switch + return Path.GetExtension(filename).ToString().ToLowerInvariant() 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(nameof(filename)) @@ -146,4 +152,4 @@ internal static class WICFormatHelper _ => default(ArgumentException).Throw(nameof(format)) }; } -} \ No newline at end of file +} From 3970ef1e3aaeff38d7ff20b491e8e3516c2c9aee Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 28 Aug 2024 00:25:47 -0700 Subject: [PATCH 2/2] Remove allocations in 'GetForFilename' --- src/ComputeSharp/Graphics/Helpers/WICFormatHelper.cs | 8 +++++++- src/ComputeSharp/Graphics/Helpers/WICHelper.cs | 3 +++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/ComputeSharp/Graphics/Helpers/WICFormatHelper.cs b/src/ComputeSharp/Graphics/Helpers/WICFormatHelper.cs index c8f6d5d1..3934af2b 100644 --- a/src/ComputeSharp/Graphics/Helpers/WICFormatHelper.cs +++ b/src/ComputeSharp/Graphics/Helpers/WICFormatHelper.cs @@ -111,7 +111,13 @@ internal static class WICFormatHelper /// Thrown when the input filename doesn't have a valid file extension. public static Guid GetForFilename(ReadOnlySpan filename) { - return Path.GetExtension(filename).ToString().ToLowerInvariant() switch + Span 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 diff --git a/src/ComputeSharp/Graphics/Helpers/WICHelper.cs b/src/ComputeSharp/Graphics/Helpers/WICHelper.cs index e7c63cd4..0b725b8d 100644 --- a/src/ComputeSharp/Graphics/Helpers/WICHelper.cs +++ b/src/ComputeSharp/Graphics/Helpers/WICHelper.cs @@ -358,6 +358,7 @@ internal sealed unsafe class WICHelper where T : unmanaged { using ComPtr wicBitmapEncoder = default; + Guid containerGuid = WICFormatHelper.GetForFilename(filename); // Create the image encoder @@ -397,6 +398,7 @@ internal sealed unsafe class WICHelper where T : unmanaged { using ComPtr wicBitmapEncoder = default; + Guid containerGuid = WICFormatHelper.GetForFormat(format); // Create the image encoder @@ -431,6 +433,7 @@ internal sealed unsafe class WICHelper where T : unmanaged { using ComPtr wicBitmapEncoder = default; + Guid containerGuid = WICFormatHelper.GetForFormat(format); // Create the image encoder