Skip to content

Commit

Permalink
Add System.Drawing.Imaging.ImageFormat.Heif and Webp. (dotnet/run…
Browse files Browse the repository at this point in the history
…time#71227)

* Add `System.Drawing.Imaging.ImageFormat.Heif` and `Webp`.

* Add these formats in the ImageConverterTests.

Commit migrated from dotnet/runtime@7eb5664
  • Loading branch information
teo-tsirpanis committed Jun 29, 2022
1 parent 75041bb commit b81ce86
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/System.Drawing.Common/ref/System.Drawing.Common.netcoreapp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,13 @@ public sealed partial class Matrix
public System.Numerics.Matrix3x2 MatrixElements { get { throw null; } set { } }
}
}
namespace System.Drawing.Imaging
{
public sealed partial class ImageFormat
{
[System.Runtime.Versioning.SupportedOSPlatformAttribute("windows10.0.17763.0")]
public static ImageFormat Heif { get { throw null; } }
[System.Runtime.Versioning.SupportedOSPlatformAttribute("windows10.0.17763.0")]
public static ImageFormat Webp { get { throw null; } }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

namespace System.Drawing
{
[SuppressMessage("Interoperability", "CA1416:Validate platform compatibility", Justification = "Heif and Webp are referenced here for " +
"design-time support, the user is responsible to ensure that they are used on a supported version of Windows.")]
public class ImageFormatConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType)
Expand Down Expand Up @@ -63,6 +65,10 @@ public override bool CanConvertTo(ITypeDescriptorContext? context, [NotNullWhen(
return ImageFormat.Tiff;
else if (strFormat.Equals("Wmf", StringComparison.OrdinalIgnoreCase))
return ImageFormat.Wmf;
else if (strFormat.Equals("Heif", StringComparison.OrdinalIgnoreCase))
return ImageFormat.Heif;
else if (strFormat.Equals("Webp", StringComparison.OrdinalIgnoreCase))
return ImageFormat.Webp;

throw new FormatException(SR.Format(SR.ConvertInvalidPrimitive, strFormat, nameof(ImageFormat)));
}
Expand Down Expand Up @@ -128,7 +134,9 @@ public override StandardValuesCollection GetStandardValues(ITypeDescriptorContex
ImageFormat.Png,
ImageFormat.Tiff,
ImageFormat.Exif,
ImageFormat.Icon
ImageFormat.Icon,
ImageFormat.Heif,
ImageFormat.Webp
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.Versioning;

namespace System.Drawing.Imaging
{
Expand All @@ -24,6 +25,8 @@ public sealed class ImageFormat
private static readonly ImageFormat s_tiff = new ImageFormat(new Guid("{b96b3cb1-0728-11d3-9d7b-0000f81ef32e}"));
private static readonly ImageFormat s_exif = new ImageFormat(new Guid("{b96b3cb2-0728-11d3-9d7b-0000f81ef32e}"));
private static readonly ImageFormat s_icon = new ImageFormat(new Guid("{b96b3cb5-0728-11d3-9d7b-0000f81ef32e}"));
private static readonly ImageFormat s_heif = new ImageFormat(new Guid("{b96b3cb6-0728-11d3-9d7b-0000f81ef32e}"));
private static readonly ImageFormat s_webp = new ImageFormat(new Guid("{b96b3cb7-0728-11d3-9d7b-0000f81ef32e}"));

private Guid _guid;

Expand Down Expand Up @@ -123,6 +126,30 @@ public static ImageFormat Icon
get { return s_icon; }
}

/// <summary>
/// Specifies the High Efficiency Image Format (HEIF).
/// </summary>
/// <remarks>
/// This format is supported since Windows 10 1809.
/// </remarks>
[SupportedOSPlatform("windows10.0.17763.0")]
public static ImageFormat Heif
{
get { return s_heif; }
}

/// <summary>
/// Specifies the WebP image format.
/// </summary>
/// <remarks>
/// This format is supported since Windows 10 1809.
/// </remarks>
[SupportedOSPlatform("windows10.0.17763.0")]
public static ImageFormat Webp
{
get { return s_webp; }
}

/// <summary>
/// Returns a value indicating whether the specified object is an <see cref='ImageFormat'/> equivalent to this
/// <see cref='ImageFormat'/>.
Expand Down Expand Up @@ -170,6 +197,8 @@ public override string ToString()
if (this.Guid == s_tiff.Guid) return "Tiff";
if (this.Guid == s_exif.Guid) return "Exif";
if (this.Guid == s_icon.Guid) return "Icon";
if (this.Guid == s_heif.Guid) return "Heif";
if (this.Guid == s_webp.Guid) return "Webp";
return $"[ImageFormat: {_guid}]";
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/System.Drawing.Common/tests/Imaging/ImageFormatTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public class ImageFormatTests
private static ImageFormat IconImageFormat = new ImageFormat(new Guid("b96b3cb5-0728-11d3-9d7b-0000f81ef32e"));
private static ImageFormat JpegImageFormat = new ImageFormat(new Guid("b96b3cae-0728-11d3-9d7b-0000f81ef32e"));
private static ImageFormat WmfImageFormat = new ImageFormat(new Guid("b96b3cad-0728-11d3-9d7b-0000f81ef32e"));
private static ImageFormat HeifImageFormat = new ImageFormat(new Guid("{b96b3cb6-0728-11d3-9d7b-0000f81ef32e}"));
private static ImageFormat WebpImageFormat = new ImageFormat(new Guid("{b96b3cb7-0728-11d3-9d7b-0000f81ef32e}"));
private static ImageFormat CustomImageFormat = new ImageFormat(new Guid("48749428-316f-496a-ab30-c819a92b3137"));

public static IEnumerable<object[]> ImageFormatGuidTestData
Expand All @@ -35,6 +37,10 @@ public static IEnumerable<object[]> ImageFormatGuidTestData
yield return new object[] { IconImageFormat.Guid, ImageFormat.Icon };
yield return new object[] { JpegImageFormat.Guid, ImageFormat.Jpeg };
yield return new object[] { WmfImageFormat.Guid, ImageFormat.Wmf };
#if NET
yield return new object[] { HeifImageFormat.Guid, ImageFormat.Heif };
yield return new object[] { WebpImageFormat.Guid, ImageFormat.Webp };
#endif
yield return new object[] { new Guid("48749428-316f-496a-ab30-c819a92b3137"), CustomImageFormat };
}
}
Expand All @@ -53,6 +59,10 @@ public static IEnumerable<object[]> ImageFormatToStringTestData
yield return new object[] { "Icon", ImageFormat.Icon };
yield return new object[] { "Jpeg", ImageFormat.Jpeg };
yield return new object[] { "Wmf", ImageFormat.Wmf };
#if NET
yield return new object[] { "Heif", ImageFormat.Heif };
yield return new object[] { "Webp", ImageFormat.Webp };
#endif
yield return new object[] { "[ImageFormat: 48749428-316f-496a-ab30-c819a92b3137]", CustomImageFormat };
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ public void ConvertFrom_ShortName()
Assert.Equal(ImageFormat.Icon, ConvertFromName("Icon"));
Assert.Equal(ImageFormat.Jpeg, ConvertFromName("Jpeg"));
Assert.Equal(ImageFormat.Wmf, ConvertFromName("Wmf"));
#if NET
Assert.Equal(ImageFormat.Heif, ConvertFromName("Heif"));
Assert.Equal(ImageFormat.Webp, ConvertFromName("Webp"));
#endif
}

[ConditionalFact(Helpers.IsDrawingSupported)]
Expand Down Expand Up @@ -174,6 +178,10 @@ private void CheckStandardValues(ICollection values)
bool tiff = false;
bool exif = false;
bool icon = false;
#if NET
bool heif = false;
bool webp = false;
#endif

foreach (ImageFormat iformat in values)
{
Expand Down Expand Up @@ -209,6 +217,14 @@ private void CheckStandardValues(ICollection values)
case "b96b3cb5-0728-11d3-9d7b-0000f81ef32e":
icon = true;
break;
#if NET
case "b96b3cb6-0728-11d3-9d7b-0000f81ef32e":
heif = true;
break;
case "b96b3cb7-0728-11d3-9d7b-0000f81ef32e":
webp = true;
break;
#endif
default:
throw new InvalidOperationException($"Unknown GUID {iformat.Guid}.");
}
Expand All @@ -223,6 +239,10 @@ private void CheckStandardValues(ICollection values)
Assert.True(tiff, "Tiff");
Assert.True(exif, "Exif");
Assert.True(icon, "Icon");
#if NET
Assert.True(heif, "Heif");
Assert.True(webp, "Webp");
#endif
}

[ConditionalFact(Helpers.IsDrawingSupported)]
Expand Down

0 comments on commit b81ce86

Please sign in to comment.