← Back to Pull requests

Precalculate BMP grayscale palette #10580

Closed
unknown closed this pull request without merging on Nov 22, 2026 10:52 UTC
View on GitHub
unknown unknown commented on Nov 21, 2026 03:04 UTC
Author

Alternative to https://github.com/python-pillow/Pillow/pull/10574

This code loops through a range (or (0, 255)) and converts each value to bytes, and checks that against each the corresponding item in the BMP palette.

https://github.com/python-pillow/Pillow/blob/389d62adf44ab13173045a36451e9cbdbc357f56/src/PIL/BmpImagePlugin.py#L276-L286

padding is only ever 3 or 4.

https://en.wikipedia.org/wiki/BMP_file_format

In Windows bitmaps each entry occupies 4 bytes in the order blue, green, red, 0x00 — the X8R8G8B8 layout (a RGBQUAD), whose fourth byte is reserved rather than a true alpha channel.

This means there are only four types of grayscale palettes, and we can calculate them beforehand -

  • 2 colors (0, 255)

  • 2 colors with a padding byte after each entry

  • a range

  • a range with a padding byte after each entry
  • The earlier code is checking that the BMP palette is matches one of these grayscale palettes until it runs out of entries (until file_info["colors"] is hit). It is checking that the BMP palette starts with one of these grayscale palettes.

    I did have to change one of our test images, but it came from https://github.com/python-pillow/Pillow/pull/5667. I wouldn't have been aware of the convention that the padding byte in the palette should be zero.

    Diff: 191 lines (+143 / -48) across 2 files Reviewer: radarhere Review duration: 1907m
    All Vouch review confidence checks passed
    Residual risk 0.16 = Change Risk (0.36) × (1 − Review Confidence (0.57)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.36 · 191 lines diff across 2 files
    0.36
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    56%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.315
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #10580"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 56.8% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 35.9% (MEDIUM) · Higher = larger blast radius
    35.9%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 56.8% · Lower = rushed or shallow review
    56.8%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 15.5% · Safe to merge
    15.5%