SixLabors/ImageSharp · error · ArgumentException

Not a valid pass index

Error message

Not a valid pass index: {passIndex}

What it means

Adam7.ComputeColumns throws this ArgumentException when the pass index is outside 0-6. The Adam7 interlacing scheme in PNG has exactly seven passes, and ComputeColumns maps each pass index to the number of columns it processes; any other index is a programming error. This is an internal argument validation, not a data-content error.

Solutions

  1. Use pass indices 0 through 6 only; fix the loop bounds driving the call.
  2. If porting from 1-based code, subtract 1 before calling ComputeColumns.
  3. Use the library's established interlace iteration helpers instead of hand-rolled pass loops.

Example fix

// before: 1-based pass numbering leaking in
for (int pass = 1; pass <= 7; pass++)
    int columns = Adam7.ComputeColumns(width, pass); // throws for pass == 7
// after
for (int pass = 0; pass < 7; pass++)
    int columns = Adam7.ComputeColumns(width, pass);
Defensive patterns

Strategy: type-guard

Validate before calling

// passIndex must be 0-6 for Adam7's seven passes
if (passIndex is < 0 or > 6)
    throw new ArgumentOutOfRangeException(nameof(passIndex), passIndex, "Adam7 has passes 0..6");

Type guard

static bool IsValidAdam7Pass(int passIndex) => (uint)passIndex < 7u;

Try / catch

try
{
    columns = Adam7.ComputeColumns(width, pass);
}
catch (ArgumentException ex)
{
    throw new InvalidOperationException($"Interlace loop produced invalid pass index: {ex.Message}", ex);
}

Prevention

When it happens

Trigger: Calling Adam7.ComputeColumns (or the Adam7 encoding/decoding paths that forward to it) with a passIndex < 0 or > 6, typically via an incorrectly written or modified interlace loop.

Common situations: Custom PNG codec modifications, porting Adam7 code from another library with 1-based pass numbering, or a loop bug such as `for (int pass = 0; pass <= 7; pass++)`.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

Thrown at src/ImageSharp/Formats/Png/Adam7.cs:86

    public static int ComputeColumns(int width, int passIndex)
    {
        uint w = (uint)width;

        uint result = passIndex switch
        {
            0 => (w + 7) / 8,
            1 => (w + 3) / 8,
            2 => (w + 3) / 4,
            3 => (w + 1) / 4,
            4 => (w + 1) / 2,
            5 => w / 2,
            6 => w,
            _ => Throw(passIndex)
        };

        return (int)result;

        static uint Throw(int passIndex) => throw new ArgumentException($"Not a valid pass index: {passIndex}");
    }
}

View on GitHub (pinned to 59ce6af6fc)