Skip to content

refactor: split idisp() into _idisp_matplotlib()/_idisp_opencv() helpers - #97

Merged
petercorke merged 5 commits into
mainfrom
refactor/idisp-split-mpl-opencv-helpers
Aug 16, 2026
Merged

refactor: split idisp() into _idisp_matplotlib()/_idisp_opencv() helpers#97
petercorke merged 5 commits into
mainfrom
refactor/idisp-split-mpl-opencv-helpers

Conversation

@petercorke

Copy link
Copy Markdown
Owner

Summary

  • idisp() dispatched on matplotlib=True/False inside one ~750-line function body. Split the two backend bodies into separate private helpers (_idisp_matplotlib(), _idisp_opencv()) so each can be read, tested and fixed in isolation.
  • Prompted by a recent investigation (see fix: Image() drops maxintval when dtype= is also given, silently truncating #95) that had to carefully reason about which of idisp()'s two branches a uint16 display artifact could have come from -- turned out to be neither (the corruption was upstream, in Image.__init__), but the exercise made the case for splitting the backends apart.
  • Purely mechanical. The shared preamble (plain/block/fps handling) and the full public signature/docstring stay on idisp(), which now just dispatches to the two helpers with the branch bodies unchanged. Verify with git diff -w on this PR: only ~100 non-whitespace lines actually changed -- the rest of the diff is the extracted bodies' indentation shifting one level in from the removed if matplotlib: / else:.

Tests added

tests/base/test_idisp_display_readback.py -- genuine pixel-content readback tests for both backends across a dtype matrix (uint8, uint16 full-range, uint16 12-bit narrow-range, float32, bool), complementing the existing "doesn't crash" style coverage already in test_io.py.

  • Matplotlib path: reads back real rendered pixels via the Agg backend's off-screen buffer_rgba() -- headless-safe and deterministic on every platform, no virtual display needed. Sanity-checked manually that this is genuinely sensitive (not a vacuous pass): a 0/128/255 uint8 test image reads back as exactly [0,0,0,255] / [128,128,128,255] / [255,255,255,255] at the corresponding data coordinates.
  • OpenCV path: there's no portable cross-platform API to read back a native HighGUI window's actual rendered content (Linux/macOS/Windows all differ, CI runs all three). Instead intercepts the array handed to cv2.imshow() -- since cv2.imshow's own internal 16-bit conversion was independently verified correct via real on-screen pixel readback (screenshot + template matching, during the fix: Image() drops maxintval when dtype= is also given, silently truncating #95 investigation), asserting the correct array reaches it is equivalent to asserting the correct image is displayed.

Test plan

  • Full test suite: 1007 passed, 15 skipped, no regressions (996 without the new file's 11 tests -- matches baseline).
  • ruff check on the refactored file: same 15 pre-existing findings as origin/main, no new ones introduced.
  • New readback tests pass, including the narrow-range 12-bit case that mirrors the real-world bug found in fix: Image() drops maxintval when dtype= is also given, silently truncating #95.

🤖 Generated with Claude Code

idisp() dispatched on matplotlib=True/False inside one 750-line function
body. Split the two backend bodies out into separate private helpers so
each can be read, tested and fixed in isolation -- prompted by a recent
investigation (see fix/image-ctor-drops-maxintval-before-dtype-cast) that
had to reason carefully about which of idisp()'s two branches a uint16
display artifact could have come from.

Purely mechanical: the shared preamble (plain/block/fps handling) and the
full public signature/docstring stay on idisp(), which now just dispatches
to _idisp_matplotlib()/_idisp_opencv() with the branch bodies unchanged
(verify with `git diff -w`: only ~100 non-whitespace lines actually
changed, the rest is the extracted bodies' indentation shifting one level).

Also adds tests/base/test_idisp_display_readback.py: genuine pixel-content
readback tests for both backends across a dtype matrix (uint8, uint16
full-range, uint16 12-bit narrow-range, float32, bool), complementing the
existing "doesn't crash" style coverage in test_io.py. Matplotlib path
reads back real rendered pixels via the Agg backend's off-screen buffer;
OpenCV path intercepts the array handed to cv2.imshow(), since there's no
portable way to read back a native HighGUI window's content across
Linux/macOS/Windows CI.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@codacy-production

codacy-production Bot commented Aug 16, 2026

Copy link
Copy Markdown

Not up to standards ⛔

🔴 Issues 4 high

Alerts:
⚠ 4 issues (≤ 0 issues of at least minor severity)

Results:
4 new issues

Category Results
ErrorProne 3 high
Security 1 high

View in Codacy

🟢 Metrics 207 complexity · 8 duplication

Metric Results
Complexity 207
Duplication 8

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@petercorke
petercorke merged commit 776cda0 into main Aug 16, 2026
30 of 31 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant