Repository navigation
Fix expansion of segmented LUTs with long segments or a padding byte - #541
Merged
CPBridge merged 1 commit intoDec 3, 2026
Merged
Conversation
Segment lengths were read as NumPy scalars, so with NumPy 2 the arithmetic on them overflowed the data type of the LUT data: an 8-bit linear segment of length 255 expanded to nothing and the expanded length wrapped around to 0. Odd length 8-bit data read from a dataset also has a trailing padding byte, which was read as the opcode of a further segment. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Collaborator
|
Hi @HEOJUNFO, just a note that, in retrospect, this wasn't ready for merge. I realised after merging that It allowed through padding bytes, which is good, but also allowed through various other incorrect situations. This is particularly important since the methods are used to check user's input data upon object creation, not just to parse existing data, and highdicom places emphasis on validating user inputs and making sure the objects it creates are correct. Rather than revert, I just made some follow up changes in #543, FYI |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #540.
SegmentedPaletteColorLUTcould not expand the 8-bit segmented LUTs of theDICOM well-known color palettes (spring, summer, fall, winter) when NumPy 2 is
installed:
Two changes in
_expand_segmented_lut()and_get_expanded_lut_length()(
src/highdicom/pixels.py):int. It was a NumPy scalar, sowith NumPy 2
range(1, length + 1),i + 2 + lengthandtotal_length += lengthwere computed in the data type of the LUT data andwrapped around: a linear segment of length 255 produced no values and the
expanded length of a 256 entry LUT became 0, which the constructor wrote to
the descriptor. With NumPy 1.26 the old code returns 256 entries. A 16-bit
segment of length 65535 had the same problem.
a dataset ends with a padding byte, which was read as the opcode of another
segment.
With the change the snippet prints
256 (256,),[ 0 100 255],[256, 0, 8]and the summer LUT has 256 entries. The expansions of all twelveLUTs in the four palettes are equal to the output of pydicom's
_expand_segmented_lut()for the same data.Tests:
test_construction_long_segments(8 and 16-bit) andtest_well_known_palettesintests/test_content.py. Both fail onmasterand pass here.
pytest tests/test_content.py tests/test_pr.py: 244 passed.flake8is clean on both files. I did not run the full suite.🤖 Generated with Claude Code