Skip to content

Commit 2ff7dba

Browse files
committed
Address PR Feedback
Removed not needed nullability Refactored CurveCalculator.Calculate to not need the bang operator
1 parent 197dc44 commit 2ff7dba

File tree

4 files changed

+26
-10
lines changed

4 files changed

+26
-10
lines changed

src/ImageSharp/ColorProfiles/Icc/Calculators/CurveCalculator.cs

Lines changed: 22 additions & 6 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.ColorProfiles.Icc.Calculators;
56
using SixLabors.ImageSharp.Metadata.Profiles.Icc;
67

@@ -12,6 +13,10 @@ internal partial class CurveCalculator : ISingleCalculator
1213
private readonly float gamma;
1314
private readonly CalculationType type;
1415

16+
[MemberNotNullWhen(true, nameof(lutCalculator))]
17+
private bool IsLut => this.type == CalculationType.Lut;
18+
19+
1520
public CurveCalculator(IccCurveTagDataEntry entry, bool inverted)
1621
{
1722
if (entry.IsIdentityResponse)
@@ -36,11 +41,22 @@ public CurveCalculator(IccCurveTagDataEntry entry, bool inverted)
3641
}
3742

3843
public float Calculate(float value)
39-
=> this.type switch
44+
{
45+
if (this.IsLut)
46+
{
47+
return this.lutCalculator.Calculate(value);
48+
}
49+
50+
if (this.type == CalculationType.Gamma)
4051
{
41-
CalculationType.Identity => value,
42-
CalculationType.Gamma => MathF.Pow(value, this.gamma), // TODO: This could be optimized using a LUT. See SrgbCompanding
43-
CalculationType.Lut => this.lutCalculator!.Calculate(value),
44-
_ => throw new InvalidOperationException("Invalid calculation type"),
45-
};
52+
return MathF.Pow(value, this.gamma);
53+
}
54+
55+
if (this.type == CalculationType.Identity)
56+
{
57+
return value;
58+
}
59+
60+
throw new InvalidOperationException("Invalid calculation type");
61+
}
4662
}

src/ImageSharp/ColorProfiles/Icc/Calculators/GrayTrcCalculator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ internal class GrayTrcCalculator : IVector4Calculator
1111
{
1212
private readonly TrcCalculator calculator;
1313

14-
public GrayTrcCalculator(IccTagDataEntry? grayTrc, bool toPcs)
14+
public GrayTrcCalculator(IccTagDataEntry grayTrc, bool toPcs)
1515
=> this.calculator = new TrcCalculator([grayTrc], !toPcs);
1616

1717
[MethodImpl(MethodImplOptions.AggressiveInlining)]

src/ImageSharp/ColorProfiles/Icc/Calculators/TrcCalculator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ internal class TrcCalculator : IVector4Calculator
1212
{
1313
private readonly ISingleCalculator[] calculators;
1414

15-
public TrcCalculator(IccTagDataEntry?[] entries, bool inverted)
15+
public TrcCalculator(IccTagDataEntry[] entries, bool inverted)
1616
{
1717
Guard.NotNull(entries, nameof(entries));
1818

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,8 @@ private static ConversionMethod CheckMethod2(IccProfile profile)
145145
private static bool HasTag(IccProfile profile, IccProfileTag tag)
146146
=> profile.Entries.Any(t => t.TagSignature == tag);
147147

148-
private static IccTagDataEntry? GetTag(IccProfile profile, IccProfileTag tag)
149-
=> Array.Find(profile.Entries, t => t.TagSignature == tag);
148+
private static IccTagDataEntry GetTag(IccProfile profile, IccProfileTag tag)
149+
=> Array.Find(profile.Entries, t => t.TagSignature == tag) ?? throw new InvalidOperationException();
150150

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

0 commit comments

Comments
 (0)