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
13 changes: 11 additions & 2 deletions src/highdicom/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -697,12 +697,18 @@ def read_frame_raw(self, index: int) -> bytes:

Raises
------
ValueError
When `index` is less than 0 or greater than or equal to the number
of frames.
OSError
When frame could not be read

"""
if index > self.number_of_frames:
raise ValueError('Frame index exceeds number of frames in image.')
if index < 0 or index >= self.number_of_frames:
raise ValueError(
'Frame index must be greater than or equal to 0 and less '
'than the number of frames in the image.'
)
logger.debug(f'read frame #{index}')

frame_offset = self._offset_table[index]
Expand Down Expand Up @@ -755,6 +761,9 @@ def read_frame(self, index: int, correct_color: bool = True) -> np.ndarray:

Raises
------
ValueError
When `index` is less than 0 or greater than or equal to the number
of frames.
OSError
When frame could not be read

Expand Down
8 changes: 8 additions & 0 deletions tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ def test_read_float_pixel_data(self):
pixel_array[frame_index],
)

def test_reject_out_of_range_frame_indices(self):
filename = str(self._test_dir.joinpath('ct_image.dcm'))
with ImageFileReader(filename) as reader:
with pytest.raises(ValueError, match="Frame index"):
reader.read_frame_raw(-1)
with pytest.raises(ValueError, match="Frame index"):
reader.read_frame_raw(reader.number_of_frames)

def test_read_multi_frame_ct_image_native(self):
filename = str(get_testdata_file('eCT_Supplemental.dcm'))
dataset = dcmread(filename)
Expand Down