Skip to content

Fix expansion of segmented LUTs with long segments or a padding byte - #541

Merged
CPBridge merged 1 commit into
ImagingDataCommons:masterfrom
HEOJUNFO:bugfix/segmented-lut-expansion
Dec 3, 2026
Merged

CPBridge merged 1 commit into
ImagingDataCommons:masterfrom
HEOJUNFO:bugfix/segmented-lut-expansion

Conversation

@HEOJUNFO

@HEOJUNFO HEOJUNFO commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Fixes #540.

SegmentedPaletteColorLUT could not expand the 8-bit segmented LUTs of the
DICOM well-known color palettes (spring, summer, fall, winter) when NumPy 2 is
installed:

import numpy as np
import highdicom as hd
from pydicom import dcmread
from pydicom.data import get_palette_files

ds = dcmread(get_palette_files('spring.dcm')[0])  # green: 0, 1, 0, 1, 255, 255
lut = hd.SegmentedPaletteColorLUT.extract_from_dataset(ds, color='green')
print(lut.number_of_entries, lut.lut_data.shape)  # 256 (1,)
print(lut.apply(np.array([0, 100, 255])))         # [0 0 0]

lut = hd.SegmentedPaletteColorLUT(
    0, np.array([0, 1, 0, 1, 255, 255], dtype=np.uint8), color='green'
)
print(lut.GreenPaletteColorLookupTableDescriptor)  # [0, 0, 8]

ds = dcmread(get_palette_files('summer.dcm')[0])  # blue: 9 values + padding byte
hd.SegmentedPaletteColorLUT.extract_from_dataset(ds, color='blue').lut_data
# IndexError: index 10 is out of bounds for axis 0 with size 10

Two changes in _expand_segmented_lut() and _get_expanded_lut_length()
(src/highdicom/pixels.py):

  • The segment length is converted to a Python int. It was a NumPy scalar, so
    with NumPy 2 range(1, length + 1), i + 2 + length and
    total_length += length were computed in the data type of the LUT data and
    wrapped 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.
  • The loop stops when a single value is left. Odd length 8-bit data read from
    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 twelve
LUTs 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) and
test_well_known_palettes in tests/test_content.py. Both fail on master
and pass here. pytest tests/test_content.py tests/test_pr.py: 244 passed.
flake8 is clean on both files. I did not run the full suite.

🤖 Generated with Claude Code

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>

@CPBridge CPBridge left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@CPBridge
CPBridge merged commit 44cce02 into ImagingDataCommons:master Dec 3, 2026
13 checks passed
@CPBridge

CPBridge commented Dec 4, 2026

Copy link
Copy Markdown
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

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.

SegmentedPaletteColorLUT expands the standard 8-bit palettes to a single entry with NumPy 2

2 participants