Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly account for Cr and Cb subsampling for native YBR_FULL_422 #242

Merged
merged 2 commits into from
Nov 9, 2023
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
6 changes: 6 additions & 0 deletions src/highdicom/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,12 @@ def _bytes_per_frame_uncompressed(self) -> int:
# are packed into 12.5 -> 13 bytes
return n_pixels // 8 + (n_pixels % 8 > 0)
else:
if self.metadata.PhotometricInterpretation == 'YBR_FULL_422':
# Account for subsampling of CB and CR when calculating
# expected number of samples
# See https://dicom.nema.org/medical/dicom/current/output/chtml
# /part03/sect_C.7.6.3.html#sect_C.7.6.3.1.2
n_pixels = self.metadata.Rows * self.metadata.Columns * 2
return n_pixels * bits_allocated // 8

def close(self) -> None:
Expand Down
19 changes: 19 additions & 0 deletions tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,25 @@ def test_read_multi_frame_seg_image_sm_dots_bitpacked(self):
)
np.testing.assert_array_equal(frame, pixel_array[i, ...])

def test_read_ybr_422_native(self):
# Reading a frame using YBR_422 photometric interpretation and no
# compression
filename = str(get_testdata_file('SC_ybr_full_422_uncompressed.dcm'))
dataset = dcmread(filename)
pixel_array = dataset.pixel_array
with ImageFileReader(filename) as reader:
assert reader.number_of_frames == 1
frame = reader.read_frame(0, correct_color=False)
assert isinstance(frame, np.ndarray)
assert frame.ndim == 3
assert frame.dtype == np.uint8
assert frame.shape == (
reader.metadata.Rows,
reader.metadata.Columns,
reader.metadata.SamplesPerPixel,
)
np.testing.assert_array_equal(frame, pixel_array)

def test_read_single_frame_ct_image_dicom_bytes_io(self):
filename = str(self._test_dir.joinpath("ct_image.dcm"))
dcm = DicomBytesIO(open(filename, "rb").read())
Expand Down
Loading