SixLabors/ImageSharp · error · InvalidOperationException

Invalid calculation type

Error message

Invalid calculation type

What it means

CurveCalculator.Calculate uses a switch over its CalculationType (Identity, Gamma, Lut) and throws InvalidOperationException for any other value. Since the type is set only by the calculator's own constructor from known tag entries, reaching the default arm signals corrupted internal state — the calculator was built without a recognized curve type.

Solutions

  1. Reconstruct the CurveCalculator from a valid IccCurveTagDataEntry via the public constructor.
  2. Check whether a custom/direct construction path assigned an out-of-range CalculationType and fix it.
  3. If triggered by normal library use, report it as a bug with the offending ICC profile.

Example fix

// before
var calc = new CurveCalculator(); // default state, invalid type
// after
var calc = new CurveCalculator(curveTagDataEntry, inverted: false);
Defensive patterns

Strategy: try-catch

Try / catch

try { float v = curveCalculator.Calculate(x); }
catch (InvalidOperationException ex) when (ex.Message == "Invalid calculation type") { /* rebuild calculator from a valid curve tag entry */ }

Prevention

When it happens

Trigger: Calling Calculate on a CurveCalculator that was constructed in a way that left type as an undefined CalculationType value (default-initialized struct/enum or internal construction bug).

Common situations: Rare; usually indicates a library-internal invariant violation, memory corruption of the calculator, or constructing the calculator manually with a default(CalculationType) that isn't a valid member.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


AI-assisted analysis of SixLabors/ImageSharp@59ce6af6fc (2026-09-13). Data as JSON: /api/errors/def5614b9a6a2f6a. Report an issue: GitHub.

Appendix: source

Thrown at src/ImageSharp/ColorProfiles/Icc/Calculators/CurveCalculator.cs:45

                this.gamma = 1f / this.gamma;
            }

            this.type = CalculationType.Gamma;
        }
        else
        {
            this.lutCalculator = new LutCalculator(entry.CurveData, inverted);
            this.type = CalculationType.Lut;
        }
    }

    public float Calculate(float value)
        => this.type switch
        {
            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"),
        };
}

View on GitHub (pinned to 59ce6af6fc)