8000 Fix get_histogram reading PDB return values from the status slot (#47) by serrade · Pull Request #48 · gimp-mcp/gimp-mcp · GitHub
[go: up one dir, main page]

Skip to content

Fix get_histogram reading PDB return values from the status slot (#47) - #48

Open
serrade wants to merge 1 commit into
gimp-mcp:mainfrom
serrade:fix/get-histogram-field-offset
Open

serrade wants to merge 1 commit into
gimp-mcp:mainfrom
serrade:fix/get-histogram-field-offset

Conversation

@serrade
@serrade serrade commented Aug 31, 2026 •
Copy link
Copy Markdown
Contributor

Fixes #47.

Summary

get_histogram returns plausible-looking numbers, which is why this hid for a while, but every field is offset by one. proc.run() returns a Gimp.ValueArray whose index 0 is the PDB status, so reading from index 0 attaches each label to its predecessor's value.

Evidence

A uniform fill pins down every expected number in advance. On a 300x200 canvas (60,000 pixels) filled with #808080, every pixel has value 128 and the standard deviation must be exactly 0:

get_histogram(channel="value", image_index=0)

before:  { "mean": 3, "std_dev": 128.0, "median": 0.0, "pixels": 128.0, "count": 60000.0 }
after:   { "mean": 128.0, "std_dev": 0.0, "median": 128.0, "pixels": 60000.0, "count": 60000.0 }

Three values in the "before" row are impossible on their face: pixels: 128 for a 60,000 pixel image, std_dev: 128 for a uniform fill, and mean: 3 when every pixel is 128. That 3 is GIMP_PDB_SUCCESS.

Printing the array directly on GIMP 3.2.4 shows the layout:

ValueArray length = 7
  index 0 -> <enum GIMP_PDB_SUCCESS of type Gimp.PDBStatusType>
  index 1 -> 128.0      <- mean
  index 2 -> 0.0        <- std-dev
  index 3 -> 128.0      <- median
  index 4 -> 60000.0    <- pixels   (300 * 200)
  index 5 -> 60000.0    <- count
  index 6 -> 1.0        <- percentile

Seven entries: one status followed by the six documented return values. This matches the official GIMP Python tutorial, which gates on if result.index(0) is Gimp.PDBStatusType.SUCCESS: before reading results.

The existing comment in the function already lists the six names in the right order, so only the leading status slot was overlooked.

Changes

  • read indices 1 through 5 for mean, std-dev, median, pixels and count
  • compare index 0 against SUCCESS, so a failed procedure call reports an error instead of silently returning zeros through the _safe() fallback. Happy to drop this guard if you would rather keep the diff to the index shift alone.

Test plan

  • Reproduced and confirmed fixed against live GIMP 3.2.4 on macOS using the uniform-canvas case above
  • Verified the exact expressions this patch introduces in the running GIMP: result.index(0) is a PDBStatusType and an int subclass, != Gimp.PDBStatusType.SUCCESS evaluates False on a good call, and indices 1-5 return the correct statistics
  • ruff check clean at the version CI pins (0.15.11)
  • py_compile clean under Python 3.10.21, matching the interpreter GIMP bundles to run plug-ins
  • run_tests.py against l 8000 ive GIMP: 59/59 PASSED

Worth noting for coverage: run_tests.py line 74 does call get_histogram, but t() only asserts status == 'success', so the wrong numbers passed. I did not add an assertion there to keep this focused, but it would be a reasonable follow-up.

There is one more instance of the same misread at gimp-mcp-plugin.py:1620, where filtr = result.index(0) takes the status instead of the filter object. It is wrapped in a try/except that quietly falls back to the exec path, so it fails invisibly. I left it out deliberately, since that fallback deserves its own testing - noted in #47.

Prepared with AI assistance; every result above was executed against a real GIMP 3.2.4 install rather than inferred.

Summary by CodeRabbit

  • Bug Fixes
    • Improved histogram analysis reliability by detecting unsuccessful operations and returning an appropriate error.
    • Corrected histogram statistics reporting to ensure values such as mean, standard deviation, median, pixel count, and sample count are displayed accurately.

proc.run() hands back a ValueArray whose index 0 is the PDB status, so reading
from index 0 attached every label to its predecessor's value.

On a uniform 300x200 #808080 canvas, get_histogram reported mean 3 (which is
the GIMP_PDB_SUCCESS enum), std_dev 128, median 0, pixels 128 and count 60000.
The correct values for that image are mean 128, std_dev 0, median 128 and
pixels 60000 -- note that pixels was reported as 128 for a 60,000 pixel image,
and std_dev as 128 for a perfectly uniform fill.

Read indices 1 through 5 instead, and compare index 0 against SUCCESS so a
failed call reports an error rather than silently returning zeros.
@coderabbitai
coderabbitai Bot commented Aug 31, 2026 •
Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 80e93700-6fb1-42a5-a7ab-e6889b09cb06

📥 Commits

Reviewing files that changed from the base of the PR and between 09bfb2d and c29a8e1.

📒 Files selected for processing (1)
  • gimp-mcp-plugin.py

Included review availability: Your plan provides up to 2 included reviews per hour; 0 remain after this review.


Walkthrough

_get_histogram now validates the histogram procedure status and reads each statistic from its corrected position.

Changes

Histogram result handling

Layer / File(s) Summary
Validate status and correct statistic indices
gimp-mcp-plugin.py
The histogram procedure now returns an error when its PDB status is not successful. The mean, standard deviation, median, pixel count, and count values now use indices 1 through 5.

Estimated code review effort: 2 (Simple) | ~10 minutes

Merge Risk: ⚪ Minimal · up to c29a8

This localized fix corrects histogram field mapping and reports failed procedure calls instead of silently returning fallback values; no actionable merge-blocking risk remains after normal checks and review.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 2 functions across 1 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the primary change: correcting get_histogram to skip the PDB status slot when reading return values.
Linked Issues check ✅ Passed The changes satisfy issue #47 by validating index 0 as the PDB status and reading the histogram statistics from indices 1 through 5. The related occurrence at line 1620 remains excluded as requested.
Out of Scope Changes check ✅ Passed The changes are limited to the get_histogram return-value handling described in issue #47. No unrelated code changes are identified.
  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@tarikkircali-amg
Copy link
Copy Markdown

Independently reproduced the same off-by-one on GIMP 3.2.6 — not by running this PR's code, but by exercising gimp-drawable-histogram directly.

Test: a 256×4 8-bit grayscale horizontal ramp (column values 0–255, 1024 px total), channel=VALUE, range 0.0–1.0. proc.run(cfg) returns a 7-element GimpValueArray:

slot value
index 0 PDB status (== Gimp.PDBStatusType.SUCCESS)
index 1 — mean 127.5
index 2 — std_dev ≈ 73.90
index 3 — median 128
index 4 — pixels 1024
index 5 — count 1024

So index 0 is the status slot and the documented statistics start at index 1 — exactly the offset this PR corrects. The pre-fix code reads index 0, i.e. it returns the status enum where the mean is expected.

The added result.index(0) != Gimp.PDBStatusType.SUCCESS guard is worth keeping as well: it's more robust than a bare index shift, since it fails loudly when the PDB call itself doesn't succeed instead of silently passing a status value through as histogram data.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working
4F35

Projects

None yet

Development

Successfully merging this pull request may close these issues.

get_histogram returns every field shifted by one: the PDB status is reported as 'mean'

3 participants

0