Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions src/highdicom/pixels.py
Original file line number Diff line number Diff line change
Expand Up @@ -527,11 +527,13 @@ def _expand_segmented_lut(
"""
expanded_lut_values = []
i = 0
while i < len(segmented_lut_data):
# All segments are at least 3 values long, so a single trailing value
# is the padding byte of odd length 8-bit data
while i + 1 < len(segmented_lut_data):
opcode = segmented_lut_data[i]
if opcode == 0:
# Discrete segment type (literal)
length = segmented_lut_data[i + 1]
length = int(segmented_lut_data[i + 1])
expanded_lut_values.extend(
segmented_lut_data[i + 2:i + 2 + length].tolist()
)
Expand All @@ -547,7 +549,7 @@ def _expand_segmented_lut(
"not be used as the first segment."
)

length = segmented_lut_data[i + 1]
length = int(segmented_lut_data[i + 1])

# Need signed integers for subsequent calculations
start_value = int(expanded_lut_values[-1])
Expand Down Expand Up @@ -597,11 +599,13 @@ def _get_expanded_lut_length(
"""
total_length = 0
i = 0
while i < len(segmented_lut_data):
# All segments are at least 3 values long, so a single trailing value
# is the padding byte of odd length 8-bit data
while i + 1 < len(segmented_lut_data):
opcode = segmented_lut_data[i]
if opcode == 0:
# Discrete segment type (literal)
length = segmented_lut_data[i + 1]
length = int(segmented_lut_data[i + 1])
total_length += length
i += (length + 2)
elif opcode == 1:
Expand All @@ -615,7 +619,7 @@ def _get_expanded_lut_length(
"not be used as the first segment."
)

length = segmented_lut_data[i + 1]
length = int(segmented_lut_data[i + 1])
total_length += length
i += 3
elif opcode == 2:
Expand Down
80 changes: 79 additions & 1 deletion tests/test_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
from pydicom.sequence import Sequence as DataElementSequence
from pydicom.sr.codedict import codes
from pydicom.sr.coding import Code
from pydicom.data import get_testdata_file, get_testdata_files
from pydicom.data import (
get_palette_files,
get_testdata_file,
get_testdata_files,
)

import numpy as np

Expand Down Expand Up @@ -2389,6 +2393,80 @@ def test_construction_8bit(self):
with pytest.raises(TypeError):
lut.apply(arr, dtype=dtype)

def test_construction_long_segments(self):
# Segment lengths close to the maximum value of the data type
for dtype in [np.uint8, np.uint16]:
max_val = int(np.iinfo(dtype).max)

segmented_lut_data = np.array(
[
0, 1, 0, # literal [0]
1, max_val, max_val, # ramp up to the maximum value
],
dtype=dtype
)
lut = SegmentedPaletteColorLUT(
0,
segmented_lut_data,
color='red'
)
lut_data = np.arange(max_val + 1, dtype=dtype)
assert lut.number_of_entries == lut_data.shape[0]
assert np.array_equal(lut.lut_data, lut_data)

# Literal [7], then a literal segment that ends at the last of
# the max_val + 1 values of the segmented data
literal = np.arange(max_val - 4, dtype=dtype)
segmented_lut_data = np.concatenate(
[np.array([0, 1, 7, 0, max_val - 4], dtype=dtype), literal]
)
assert segmented_lut_data.shape[0] == max_val + 1
lut = SegmentedPaletteColorLUT(
0,
segmented_lut_data,
color='red'
)
lut_data = np.concatenate([np.array([7], dtype=dtype), literal])
assert lut.number_of_entries == lut_data.shape[0]
assert np.array_equal(lut.lut_data, lut_data)

def test_well_known_palettes(self):
# 8-bit segmented LUTs of well-known color palettes from the standard.
# Some of them have odd length and therefore a trailing padding byte
expected = {
'spring': {
'red': np.full(256, 255),
'green': np.arange(256),
'blue': np.arange(255, -1, -1),
},
'fall': {
'red': np.full(256, 255),
'green': np.arange(255, -1, -1),
'blue': np.zeros(256),
},
}
for name in ['spring', 'summer', 'fall', 'winter']:
ds = dcmread(get_palette_files(f'{name}.dcm')[0])
for color in ['red', 'green', 'blue']:
lut = SegmentedPaletteColorLUT.extract_from_dataset(
ds,
color=color,
)
lut_data = lut.lut_data
assert lut_data.dtype == np.uint8
assert lut_data.shape == (256, )
if name in expected:
assert np.array_equal(lut_data, expected[name][color])

# Odd length data: literal [0], ramp to 0 in 127 steps, ramp up to 254
# in 128 steps
ds = dcmread(get_palette_files('summer.dcm')[0])
assert len(ds.SegmentedBluePaletteColorLookupTableData) == 10
lut = SegmentedPaletteColorLUT.extract_from_dataset(ds, color='blue')
lut_data = lut.lut_data
assert np.array_equal(lut_data[:128], np.zeros(128))
assert lut_data[-5:].tolist() == [246, 248, 250, 252, 254]

def test_construction_invalid_opcode(self):
# Example with an invalid opcode (4)
segmented_lut_data = np.array([4, 0, 0], dtype=np.uint8)
Expand Down
Loading