WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 1f79e96

Browse files
committed
Added TryGetTag
1 parent b4cfa7e commit 1f79e96

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

src/ImageSharp/ColorProfiles/Icc/IccConverterBase.Checks.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
33

4+
using System.Diagnostics.CodeAnalysis;
45
using SixLabors.ImageSharp.Metadata.Profiles.Icc;
56

67
namespace SixLabors.ImageSharp.ColorProfiles.Conversion.Icc;
@@ -145,8 +146,15 @@ private static ConversionMethod CheckMethod2(IccProfile profile)
145146
private static bool HasTag(IccProfile profile, IccProfileTag tag)
146147
=> profile.Entries.Any(t => t.TagSignature == tag);
147148

148-
private static IccTagDataEntry GetTag(IccProfile profile, IccProfileTag tag)
149-
=> Array.Find(profile.Entries, t => t.TagSignature == tag) ?? throw new InvalidOperationException();
149+
private static bool TryGetTag(IccProfile profile, IccProfileTag tag, [NotNullWhen(true)] out IccTagDataEntry? entry)
150+
{
151+
entry = GetTag(profile, tag);
152+
153+
return entry is not null;
154+
}
155+
156+
private static IccTagDataEntry? GetTag(IccProfile profile, IccProfileTag tag)
157+
=> Array.Find(profile.Entries, t => t.TagSignature == tag);
150158

151159
private static T? GetTag<T>(IccProfile profile, IccProfileTag tag)
152160
where T : IccTagDataEntry

src/ImageSharp/ColorProfiles/Icc/IccConverterbase.Conversions.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ private static ColorTrcCalculator InitColorTrc(IccProfile profile, bool toPcs)
7979
IccXyzTagDataEntry? greenMatrixColumn = GetTag<IccXyzTagDataEntry>(profile, IccProfileTag.GreenMatrixColumn);
8080
IccXyzTagDataEntry? blueMatrixColumn = GetTag<IccXyzTagDataEntry>(profile, IccProfileTag.BlueMatrixColumn);
8181

82-
IccTagDataEntry? redTrc = GetTag(profile, IccProfileTag.RedTrc);
83-
IccTagDataEntry? greenTrc = GetTag(profile, IccProfileTag.GreenTrc);
84-
IccTagDataEntry? blueTrc = GetTag(profile, IccProfileTag.BlueTrc);
82+
IccTagDataEntry? redTrc = GetTag<IccTagDataEntry>(profile, IccProfileTag.RedTrc);
83+
IccTagDataEntry? greenTrc = GetTag<IccTagDataEntry>(profile, IccProfileTag.GreenTrc);
84+
IccTagDataEntry? blueTrc = GetTag<IccTagDataEntry>(profile, IccProfileTag.BlueTrc);
8585

8686
if (redMatrixColumn == null ||
8787
greenMatrixColumn == null ||
@@ -105,7 +105,11 @@ private static ColorTrcCalculator InitColorTrc(IccProfile profile, bool toPcs)
105105

106106
private static GrayTrcCalculator InitGrayTrc(IccProfile profile, bool toPcs)
107107
{
108-
IccTagDataEntry? entry = GetTag(profile, IccProfileTag.GrayTrc);
109-
return new GrayTrcCalculator(entry, toPcs);
108+
if (TryGetTag(profile, IccProfileTag.GrayTrc, out IccTagDataEntry? entry))
109+
{
110+
return new GrayTrcCalculator(entry, toPcs);
111+
}
112+
113+
throw new InvalidIccProfileException("Missing GrayTRC entry.");
110114
}
111115
}

0 commit comments

Comments
 (0)