@@ -390,9 +390,52 @@ class Nifti1DicomExtension(Nifti1Extension):
390
390
def __init__ (self , code , content ):
391
391
self ._code = code
392
392
self ._raw_content = content
393
- self ._is_implicit_VR = self . _guess_implicit_VR ( )
393
+ self ._bio = BytesIO ( content )
394
394
self ._content = self ._unmangle (content )
395
395
396
+ def _check_encoding (self ):
397
+ """DICOM Data can be stored in the header either as a valid DICOM
398
+ object, with a preamble, meta info, transfer syntax etc., or as a
399
+ set of naked tags. Check for meta info and transfer synatx here
400
+ and fall back to heuristics if metainfo is missing."""
401
+ self ._bio .seek (0 )
402
+ self ._preamble = read_preamble (self ._bio ,True ) # Attempt to read preamble,
403
+ # skip if missing w/o error
404
+
405
+ if self ._preamble :
406
+ self ._meta ,_is_implicit_VR ,_is_little_endian = self ._check_meta ()
407
+ else :
408
+ self ._meta = None
409
+ _is_implicit_VR = self ._guess_implicit_VR ()
410
+ _is_little_endian = self ._guess_little_endian ()
411
+ return _is_implicit_VR ,_is_little_endian
412
+
413
+
414
+ def _check_meta (self ):
415
+ """Check the DICOM Transfer Syntax and set encoding appropriately.
416
+ Extracted from dicom.filereader.read_partial, see there for detail"""
417
+ file_meta_dataset = _read_file_meta_info (self ._bio )
418
+ transfer_syntax = file_meta_dataset .TransferSyntaxUID
419
+ if transfer_syntax == dicom .UID .ImplicitVRLittleEndian :
420
+ is_implicit_VR = True
421
+ is_little_endian = True
422
+ elif transfer_syntax == dicom .UID .ExplicitVRLittleEndian :
423
+ is_implicit_VR = False
424
+ is_little_endian = True
425
+ elif transfer_syntax == dicom .UID .ExplicitVRBigEndian :
426
+ is_implicit_VR = False
427
+ is_little_endian = False
428
+ elif transfer_syntax == dicom .UID .DeflatedExplicitVRLittleEndian :
429
+ zipped = fileobj .read ()
430
+ unzipped = zlib .decompress (zipped , - zlib .MAX_WBITS )
431
+ self ._bio = BytesIO (unzipped ) # a file-like object
432
+ is_implicit_VR = False
433
+ is_little_endian = True
434
+ else :
435
+ is_implicit_VR = False
436
+ is_little_endian = True
437
+ return file_meta_dataset , is_implicit_VR , is_little_endian
438
+
396
439
def _guess_implicit_VR (self ):
397
440
"""Without a DICOM Transfer Syntax, it's difficult to tell if Value
398
441
Representations (VRs) are included in the DICOM encoding or not.
@@ -405,28 +448,30 @@ def _guess_implicit_VR(self):
405
448
implicit_VR = True
406
449
return implicit_VR
407
450
408
- def _is_little_endian (self ):
451
+ def _guess_little_endian (self ):
409
452
return True
410
453
411
454
def _unmangle (self ,value ):
412
- raw_io = BytesIO (value )
413
- ds = read_dataset (raw_io ,self ._is_implicit_VR ,self ._is_little_endian )
414
- return ds
455
+ self ._is_implicit_VR , self ._is_little_endian = self ._check_encoding ()
456
+
457
+ ds = read_dataset (self ._bio ,self ._is_implicit_VR ,self ._is_little_endian )
458
+ content = FileDataset (
459
+ self ._bio ,ds ,self ._preamble ,self ._meta ,self ._is_implicit_VR ,self ._is_little_endian
460
+ )
461
+ return content
415
462
416
463
def _mangle (self , value ):
417
- raw_io = BytesIO ()
418
- dio = DicomFileLike (raw_io )
419
- dio .is_implicit_VR = self ._is_implicit_VR
420
- dio .is_little_endian = self ._is_little_endian
421
- ds_len = write_dataset (dio ,value )
422
- dio .seek (0 )
423
- return dio .read (ds_len )
464
+ bio = BytesIO ()
465
+ write_file (bio ,value )
466
+ bio .seek (0 )
467
+ return bio .read ()
424
468
425
469
try :
426
- from dicom .filereader import read_dataset
427
- from dicom .filewriter import write_dataset
428
- from dicom .filebase import DicomFileLike
470
+ from dicom .dataset import FileDataset
471
+ from dicom .filereader import read_dataset , read_preamble , _read_file_meta_info
472
+ from dicom .filewriter import write_file
429
473
from dicom .values import converters as dicom_converters
474
+ import dicom .UID
430
475
from io import BytesIO
431
476
except ImportError :
432
477
"""Fall back to standard reader if pydicom unavailable."""
0 commit comments