Browse Source

Address PR Feedback

Removed not needed nullability
Refactored CurveCalculator.Calculate to not need the bang operator
pull/3023/head
Stefan Nikolei 3 months ago
parent
commit
b4cfa7e95f
  1. 27
      src/ImageSharp/ColorProfiles/Icc/Calculators/CurveCalculator.cs
  2. 2
      src/ImageSharp/ColorProfiles/Icc/Calculators/GrayTrcCalculator.cs
  3. 2
      src/ImageSharp/ColorProfiles/Icc/Calculators/TrcCalculator.cs
  4. 4
      src/ImageSharp/ColorProfiles/Icc/IccConverterBase.Checks.cs

27
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");
}
}

2
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)]

2
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));

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

Loading…
Cancel
Save