Skip to content

fix(core): re-apply invert after colormap when scrolling a stack#2814

Merged
sedghi merged 2 commits into
mainfrom
fix/stack-invert-lost-on-scroll
Jul 17, 2026
Merged

fix(core): re-apply invert after colormap when scrolling a stack#2814
sedghi merged 2 commits into
mainfrom
fix/stack-invert-lost-on-scroll

Conversation

@sedghi

@sedghi sedghi commented Jul 17, 2026

Copy link
Copy Markdown
Member

Context

Regression report: OHIF/Viewers#6153"After a reset, Invert is lost when scrolling between slices."

Repro: open a stack study → Invert → Reset → Invert again (the current slice inverts) → scroll to another slice → the invert is gone.

Root cause

Bisected to #2679, which added setColormap(colormap) (and VOILUTFunction/sharpening/smoothing) to StackViewport._setPropertiesFromCache() — the method that runs on every stack scroll.

  • _resetProperties() calls setColormap(matchedColormap), so after a Reset this.colormap is defined (before any reset it is undefined).
  • Every subsequent scroll then calls setColormap(this.colormap) inside _setPropertiesFromCache, which rebuilds the RGB transfer function in its default, non-inverted state.
  • The setInvertColor(invert) that follows is a no-op: setInvertColorGPU only flips the transfer function when the requested value differs from this.invert ((!this.invert && invert) || (this.invert && !invert)), and this.invert still holds the carried-over true. So the freshly rebuilt TF stays non-inverted and the inversion is silently dropped.

This is exactly why the loss appears only after a reset (that's what makes the colormap defined), and only did so after #2679 added the colormap re-application on scroll.

Fix

In _setPropertiesFromCache, clear this.invert right after setColormap(...) so the tracked flag matches the rebuilt (non-inverted) transfer function. The subsequent setInvertColor(invert) — called with the desired value captured before the mutation — then actually re-applies the inversion instead of being skipped by the no-op guard. #2679's colormap preservation is retained.

Testing

  • Manual: Invert → Reset → Invert → scroll — inversion now persists across slices.
  • Regression checks: Reset still restores defaults; a colormap set by the user still persists across scrolling.

Summary by CodeRabbit

  • Bug Fixes
    • Fixed an issue where image inversion could be lost when restoring cached viewport settings, ensuring inversion is correctly reapplied after colormap/transfer function restoration.
    • Added a regression test to confirm inversion remains enabled (and transfer-function colors stay consistent) when switching slices after resetProperties().

_setPropertiesFromCache runs on every stack scroll and (since #2679) calls
setColormap, which rebuilds the RGB transfer function in its default,
non-inverted state. The setInvertColor call that follows was then skipped by
its no-op guard (setInvertColorGPU only flips the TF when the requested value
differs from this.invert) because this.invert still held the carried-over
value, so the freshly rebuilt TF stayed non-inverted and the inversion was lost.

This surfaced as "invert is lost when scrolling after a reset": resetProperties
applies a colormap, so this.colormap becomes defined and is then re-applied on
every subsequent scroll, clobbering the inversion.

Clear this.invert right after setColormap so the tracked flag matches the
rebuilt (non-inverted) TF and setInvertColor actually re-applies the inversion.
Colormap preservation from #2679 is retained.

Fixes OHIF/Viewers#6153
@coderabbitai

coderabbitai Bot commented Jul 17, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 317f51d0-d3e1-4282-9480-608c636ace29

📥 Commits

Reviewing files that changed from the base of the PR and between 90c1145 and 9aa94cd.

📒 Files selected for processing (1)
  • packages/core/test/stackViewport_gpu_render_test.js

📝 Walkthrough

Walkthrough

StackViewport now resets its tracked inversion state during cache restoration. A GPU test verifies that inversion and RGB transfer-function colors remain consistent after resetProperties() and scrolling to another slice.

Changes

Cached inversion restoration

Layer / File(s) Summary
Reset inversion tracking during cache restoration
packages/core/src/RenderingEngine/StackViewport.ts
Cache restoration clears the tracked inversion flag before applying cached colormap and inversion properties.
Validate inversion continuity after scrolling
packages/core/test/stackViewport_gpu_render_test.js
GPU coverage verifies inversion and transfer-function colors remain unchanged after resetting properties and changing the displayed slice.

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

Possibly related PRs

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The narrative covers context, root cause, fix, and testing, but the required checklist and tested-environment details are incomplete. Add the completed checklist items and fill in OS, Node, and browser details; include any missing Changes & Results content if needed.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title is concise, semantic-release style, and accurately summarizes the main fix.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/stack-invert-lost-on-scroll

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.

@sedghi
sedghi merged commit 0040715 into main Jul 17, 2026
17 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