diff --git a/src/ImageSharp/ColorProfiles/Icc/IccConverterBase.Checks.cs b/src/ImageSharp/ColorProfiles/Icc/IccConverterBase.Checks.cs index 3254cd676..c108ac775 100644 --- a/src/ImageSharp/ColorProfiles/Icc/IccConverterBase.Checks.cs +++ b/src/ImageSharp/ColorProfiles/Icc/IccConverterBase.Checks.cs @@ -1,6 +1,7 @@ // Copyright (c) Six Labors. // Licensed under the Six Labors Split License. +using System.Diagnostics.CodeAnalysis; using SixLabors.ImageSharp.Metadata.Profiles.Icc; namespace SixLabors.ImageSharp.ColorProfiles.Conversion.Icc; @@ -145,8 +146,15 @@ internal abstract partial class IccConverterBase private static bool HasTag(IccProfile profile, IccProfileTag tag) => profile.Entries.Any(t => t.TagSignature == tag); - private static IccTagDataEntry GetTag(IccProfile profile, IccProfileTag tag) - => Array.Find(profile.Entries, t => t.TagSignature == tag) ?? throw new InvalidOperationException(); + private static bool TryGetTag(IccProfile profile, IccProfileTag tag, [NotNullWhen(true)] out IccTagDataEntry? entry) + { + entry = GetTag(profile, tag); + + return entry is not null; + } + + private static IccTagDataEntry? GetTag(IccProfile profile, IccProfileTag tag) + => Array.Find(profile.Entries, t => t.TagSignature == tag); private static T? GetTag(IccProfile profile, IccProfileTag tag) where T : IccTagDataEntry diff --git a/src/ImageSharp/ColorProfiles/Icc/IccConverterbase.Conversions.cs b/src/ImageSharp/ColorProfiles/Icc/IccConverterbase.Conversions.cs index a25b15152..4bbd961ab 100644 --- a/src/ImageSharp/ColorProfiles/Icc/IccConverterbase.Conversions.cs +++ b/src/ImageSharp/ColorProfiles/Icc/IccConverterbase.Conversions.cs @@ -79,9 +79,9 @@ internal abstract partial class IccConverterBase IccXyzTagDataEntry? greenMatrixColumn = GetTag(profile, IccProfileTag.GreenMatrixColumn); IccXyzTagDataEntry? blueMatrixColumn = GetTag(profile, IccProfileTag.BlueMatrixColumn); - IccTagDataEntry? redTrc = GetTag(profile, IccProfileTag.RedTrc); - IccTagDataEntry? greenTrc = GetTag(profile, IccProfileTag.GreenTrc); - IccTagDataEntry? blueTrc = GetTag(profile, IccProfileTag.BlueTrc); + IccTagDataEntry? redTrc = GetTag(profile, IccProfileTag.RedTrc); + IccTagDataEntry? greenTrc = GetTag(profile, IccProfileTag.GreenTrc); + IccTagDataEntry? blueTrc = GetTag(profile, IccProfileTag.BlueTrc); if (redMatrixColumn == null || greenMatrixColumn == null || @@ -105,7 +105,11 @@ internal abstract partial class IccConverterBase private static GrayTrcCalculator InitGrayTrc(IccProfile profile, bool toPcs) { - IccTagDataEntry? entry = GetTag(profile, IccProfileTag.GrayTrc); - return new GrayTrcCalculator(entry, toPcs); + if (TryGetTag(profile, IccProfileTag.GrayTrc, out IccTagDataEntry? entry)) + { + return new GrayTrcCalculator(entry, toPcs); + } + + throw new InvalidIccProfileException("Missing GrayTRC entry."); } }