def test_getter_is_little_endian(self): """Test DicomIO.is_little_endian getter""" fp = DicomIO() fp.is_little_endian = True assert fp.is_little_endian fp.is_little_endian = False assert not fp.is_little_endian
def _write_dataset(fp: DicomIO, dataset: Dataset, write_like_original: bool) -> None: """Write the Data Set to a file-like. Assumes the file meta information, if any, has been written. """ # if we want to write with the same endianness and VR handling as # the read dataset we want to preserve raw data elements for # performance reasons (which is done by get_item); # otherwise we use the default converting item getter if dataset.is_original_encoding: get_item = Dataset.get_item else: get_item = Dataset.__getitem__ # type: ignore[assignment] # WRITE DATASET # The transfer syntax used to encode the dataset can't be changed # within the dataset. # Write any Command Set elements now as elements must be in tag order # Mixing Command Set with other elements is non-conformant so we # require `write_like_original` to be True command_set = get_item(dataset, slice(0x00000000, 0x00010000)) if command_set and write_like_original: fp.is_implicit_VR = True fp.is_little_endian = True write_dataset(fp, command_set) # Set file VR and endianness. MUST BE AFTER writing META INFO (which # requires Explicit VR Little Endian) and COMMAND SET (which requires # Implicit VR Little Endian) fp.is_implicit_VR = cast(bool, dataset.is_implicit_VR) fp.is_little_endian = cast(bool, dataset.is_little_endian) # Write non-Command Set elements now write_dataset(fp, get_item(dataset, slice(0x00010000, None)))
def test_getter_is_little_endian(self): """Test DicomIO.is_little_endian getter""" fp = DicomIO() fp.is_little_endian = True assert fp.is_little_endian fp.is_little_endian = False assert not fp.is_little_endian
def test_setter_is_little_endian(self): """Test DicomIO.is_little_endian setter""" fp = DicomIO() fp.is_little_endian = True assert fp.read_US == fp.read_leUS assert fp.read_UL == fp.read_leUL assert fp.write_US == fp.write_leUS assert fp.write_UL == fp.write_leUL assert fp.read_tag == fp.read_le_tag fp.is_little_endian = False assert fp.read_US == fp.read_beUS assert fp.read_UL == fp.read_beUL assert fp.write_US == fp.write_beUS assert fp.write_UL == fp.write_beUL assert fp.read_tag == fp.read_be_tag
def test_setter_is_little_endian(self): """Test DicomIO.is_little_endian setter""" fp = DicomIO() fp.is_little_endian = True assert fp.read_US == fp.read_leUS assert fp.read_UL == fp.read_leUL assert fp.write_US == fp.write_leUS assert fp.write_UL == fp.write_leUL assert fp.read_tag == fp.read_le_tag fp.is_little_endian = False assert fp.read_US == fp.read_beUS assert fp.read_UL == fp.read_beUL assert fp.write_US == fp.write_beUS assert fp.write_UL == fp.write_beUL assert fp.read_tag == fp.read_be_tag
def _harmonize_properties(ds: Dataset, fp: DicomIO) -> None: """Make sure the properties in the dataset and the file pointer are consistent, so the user can set both with the same effect. Properties set on the destination file object always have preference. """ # ensure preference of fp over dataset if hasattr(fp, 'is_little_endian'): ds.is_little_endian = fp.is_little_endian if hasattr(fp, 'is_implicit_VR'): ds.is_implicit_VR = fp.is_implicit_VR # write the properties back to have a consistent state fp.is_implicit_VR = cast(bool, ds.is_implicit_VR) fp.is_little_endian = cast(bool, ds.is_little_endian)