From b4cfa7e95f4a3793e70777f60e12323649fa769f Mon Sep 17 00:00:00 2001 From: Stefan Nikolei Date: Sat, 22 Nov 2025 08:38:19 +0100 Subject: [PATCH] Address PR Feedback Removed not needed nullability Refactored CurveCalculator.Calculate to not need the bang operator --- .../Icc/Calculators/CurveCalculator.cs | 27 ++++++++++++++----- .../Icc/Calculators/GrayTrcCalculator.cs | 2 +- .../Icc/Calculators/TrcCalculator.cs | 2 +- .../Icc/IccConverterBase.Checks.cs | 4 +-- 4 files changed, 25 insertions(+), 10 deletions(-) diff --git a/src/ImageSharp/ColorProfiles/Icc/Calculators/CurveCalculator.cs b/src/ImageSharp/ColorProfiles/Icc/Calculators/CurveCalculator.cs index 476b8188f..1544276e6 100644 --- a/src/ImageSharp/ColorProfiles/Icc/Calculators/CurveCalculator.cs +++ b/src/ImageSharp/ColorProfiles/Icc/Calculators/CurveCalculator.cs @@ -1,6 +1,7 @@ // Copyright (c) Six Labors. // Licensed under the Six Labors Split License. +using System.Diagnostics.CodeAnalysis; using SixLabors.ImageSharp.ColorProfiles.Icc.Calculators; using SixLabors.ImageSharp.Metadata.Profiles.Icc; @@ -35,12 +36,26 @@ internal partial class CurveCalculator : ISingleCalculator } } + [MemberNotNullWhen(true, nameof(lutCalculator))] + private bool IsLut => this.type == CalculationType.Lut; + public float Calculate(float value) - => this.type switch + { + if (this.IsLut) + { + return this.lutCalculator.Calculate(value); + } + + if (this.type == CalculationType.Gamma) { - CalculationType.Identity => value, - CalculationType.Gamma => MathF.Pow(value, this.gamma), // TODO: This could be optimized using a LUT. See SrgbCompanding - CalculationType.Lut => this.lutCalculator!.Calculate(value), - _ => throw new InvalidOperationException("Invalid calculation type"), - }; + return MathF.Pow(value, this.gamma); + } + + if (this.type == CalculationType.Identity) + { + return value; + } + + throw new InvalidOperationException("Invalid calculation type"); + } } diff --git a/src/ImageSharp/ColorProfiles/Icc/Calculators/GrayTrcCalculator.cs b/src/ImageSharp/ColorProfiles/Icc/Calculators/GrayTrcCalculator.cs index dffbd8055..1016f829b 100644 --- a/src/ImageSharp/ColorProfiles/Icc/Calculators/GrayTrcCalculator.cs +++ b/src/ImageSharp/ColorProfiles/Icc/Calculators/GrayTrcCalculator.cs @@ -11,7 +11,7 @@ internal class GrayTrcCalculator : IVector4Calculator { private readonly TrcCalculator calculator; - public GrayTrcCalculator(IccTagDataEntry? grayTrc, bool toPcs) + public GrayTrcCalculator(IccTagDataEntry grayTrc, bool toPcs) => this.calculator = new TrcCalculator([grayTrc], !toPcs); [MethodImpl(MethodImplOptions.AggressiveInlining)] diff --git a/src/ImageSharp/ColorProfiles/Icc/Calculators/TrcCalculator.cs b/src/ImageSharp/ColorProfiles/Icc/Calculators/TrcCalculator.cs index 3eb8d2e00..d2fc5d9b5 100644 --- a/src/ImageSharp/ColorProfiles/Icc/Calculators/TrcCalculator.cs +++ b/src/ImageSharp/ColorProfiles/Icc/Calculators/TrcCalculator.cs @@ -12,7 +12,7 @@ internal class TrcCalculator : IVector4Calculator { private readonly ISingleCalculator[] calculators; - public TrcCalculator(IccTagDataEntry?[] entries, bool inverted) + public TrcCalculator(IccTagDataEntry[] entries, bool inverted) { Guard.NotNull(entries, nameof(entries)); diff --git a/src/ImageSharp/ColorProfiles/Icc/IccConverterBase.Checks.cs b/src/ImageSharp/ColorProfiles/Icc/IccConverterBase.Checks.cs index 7495f9ace..3254cd676 100644 --- a/src/ImageSharp/ColorProfiles/Icc/IccConverterBase.Checks.cs +++ b/src/ImageSharp/ColorProfiles/Icc/IccConverterBase.Checks.cs @@ -145,8 +145,8 @@ 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); + private static IccTagDataEntry GetTag(IccProfile profile, IccProfileTag tag) + => Array.Find(profile.Entries, t => t.TagSignature == tag) ?? throw new InvalidOperationException(); private static T? GetTag(IccProfile profile, IccProfileTag tag) where T : IccTagDataEntry