Browse Source

Added TryGetTag

pull/3023/head
Stefan Nikolei 3 months ago
parent
commit
1f79e96a7f
  1. 12
      src/ImageSharp/ColorProfiles/Icc/IccConverterBase.Checks.cs
  2. 14
      src/ImageSharp/ColorProfiles/Icc/IccConverterbase.Conversions.cs

12
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<T>(IccProfile profile, IccProfileTag tag)
where T : IccTagDataEntry

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

@ -79,9 +79,9 @@ internal abstract partial class IccConverterBase
IccXyzTagDataEntry? greenMatrixColumn = GetTag<IccXyzTagDataEntry>(profile, IccProfileTag.GreenMatrixColumn);
IccXyzTagDataEntry? blueMatrixColumn = GetTag<IccXyzTagDataEntry>(profile, IccProfileTag.BlueMatrixColumn);
IccTagDataEntry? redTrc = GetTag(profile, IccProfileTag.RedTrc);
IccTagDataEntry? greenTrc = GetTag(profile, IccProfileTag.GreenTrc);
IccTagDataEntry? blueTrc = GetTag(profile, IccProfileTag.BlueTrc);
IccTagDataEntry? redTrc = GetTag<IccTagDataEntry>(profile, IccProfileTag.RedTrc);
IccTagDataEntry? greenTrc = GetTag<IccTagDataEntry>(profile, IccProfileTag.GreenTrc);
IccTagDataEntry? blueTrc = GetTag<IccTagDataEntry>(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.");
}
}

Loading…
Cancel
Save