def test_jpeg_rgb_wrong_photometric_interpretation(self): with pytest.raises(ValueError): decode_frame(value=b'', transfer_syntax_uid=JPEGBaseline, rows=16, columns=32, samples_per_pixel=3, bits_allocated=8, bits_stored=8, photometric_interpretation='MONOCHROME', pixel_representation=0, planar_configuration=0)
def read_frame(self, index: int, correct_color: bool = True) -> np.ndarray: """Reads and decodes the pixel data of an individual frame item. Parameters ---------- index: int Zero-based frame index correct_color: bool, optional Whether colors should be corrected by applying an ICC transformation. Will only be performed if metadata contain an ICC Profile. Returns ------- numpy.ndarray Array of decoded pixels of the frame with shape (Rows x Columns) in case of a monochrome image or (Rows x Columns x SamplesPerPixel) in case of a color image. Raises ------ IOError When frame could not be read """ frame_data = self.read_frame_raw(index) logger.debug(f'decode frame #{index}') if self.metadata.BitsAllocated == 1: unpacked_frame = unpack_bits(frame_data) rows, columns = self.metadata.Rows, self.metadata.Columns n_pixels = self._pixels_per_frame pixel_offset = int(((index * n_pixels / 8) % 1) * 8) pixel_array = unpacked_frame[pixel_offset:pixel_offset + n_pixels] return pixel_array.reshape(rows, columns) frame_array = decode_frame( frame_data, rows=self.metadata.Rows, columns=self.metadata.Columns, samples_per_pixel=self.metadata.SamplesPerPixel, transfer_syntax_uid=self.metadata.file_meta.TransferSyntaxUID, bits_allocated=self.metadata.BitsAllocated, bits_stored=self.metadata.BitsStored, photometric_interpretation=self.metadata.PhotometricInterpretation, pixel_representation=self.metadata.PixelRepresentation, planar_configuration=getattr(self.metadata, 'PlanarConfiguration', None)) # We don't use the color_correct_frame() function here, since we cache # the ICC transform on the reader instance for improved performance. if correct_color and self._color_manager is not None: logger.debug(f'correct color of frame #{index}') return self._color_manager.transform_frame(frame_array) return frame_array
def test_jpeg_rgb(self): filepath = str(self._test_files_dir.joinpath('frame_rgb_empty.jpeg')) with open(filepath, 'br') as fp: compressed_frame = fp.read() rows = 16 columns = 32 samples_per_pixel = 3 bits_allocated = 8 frame = decode_frame(value=compressed_frame, transfer_syntax_uid=JPEGBaseline, rows=rows, columns=columns, samples_per_pixel=samples_per_pixel, bits_allocated=bits_allocated, bits_stored=bits_allocated, photometric_interpretation='YBR_FULL', pixel_representation=0, planar_configuration=0) assert frame.shape[0] == rows assert frame.shape[1] == columns assert frame.shape[2] == samples_per_pixel assert str(frame.dtype) == f'uint{bits_allocated}'
def test_jpeg2000_monochrome(self): bits_allocated = 16 frame = np.zeros((16, 32), dtype=np.dtype(f'uint{bits_allocated}')) compressed_frame = encode_frame( frame, transfer_syntax_uid=JPEG2000Lossless, bits_allocated=bits_allocated, bits_stored=bits_allocated, photometric_interpretation='MONOCHROME1', pixel_representation=0, ) assert compressed_frame.startswith(b'\x00\x00\x00\x0C\x6A\x50\x20') assert compressed_frame.endswith(b'\xFF\xD9') decoded_frame = decode_frame(value=compressed_frame, transfer_syntax_uid=JPEG2000Lossless, rows=frame.shape[0], columns=frame.shape[1], samples_per_pixel=1, bits_allocated=bits_allocated, bits_stored=bits_allocated, photometric_interpretation='MONOCHROME1', pixel_representation=0, planar_configuration=0) np.testing.assert_array_equal(frame, decoded_frame)
def test_jpeg2000_rgb(self): bits_allocated = 8 frame = np.ones((16, 32, 3), dtype=np.dtype(f'uint{bits_allocated}')) frame *= 255 compressed_frame = encode_frame(frame, transfer_syntax_uid=JPEG2000Lossless, bits_allocated=bits_allocated, bits_stored=bits_allocated, photometric_interpretation='YBR_FULL', pixel_representation=0, planar_configuration=0) assert compressed_frame.startswith(b'\x00\x00\x00\x0C\x6A\x50\x20') assert compressed_frame.endswith(b'\xFF\xD9') decoded_frame = decode_frame(value=compressed_frame, transfer_syntax_uid=JPEG2000Lossless, rows=frame.shape[0], columns=frame.shape[1], samples_per_pixel=frame.shape[2], bits_allocated=bits_allocated, bits_stored=bits_allocated, photometric_interpretation='YBR_FULL', pixel_representation=0, planar_configuration=0) np.testing.assert_array_equal(frame, decoded_frame)