Skip to content

Commit

Permalink
handle Freesurfer Nifti1 header extension
Browse files Browse the repository at this point in the history
  write TAG_END_NIIHDREXTENSION at the end of extension data to avoid the data to be truncated:
    TAG_END_NIIHDREXTENSION (-1)  data-length (1) '*'

  note: If the extension data has trailing null characters or zeros at the end,
        nibabel.nifti1.Nifti1Extension.get_content() will truncate the data.
        See https://github.com/nipy/nibabel/blob/master/nibabel/nifti1.py#L629C1-L630C1,
        line 629:  'evalue = evalue.rstrip(b'\x00')'
  • Loading branch information
yhuang43 committed Apr 1, 2024
1 parent 9553feb commit cc9cdf2
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions surfa/io/fsnifti1extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,18 @@ def write(self, fileobj, content, countbytesonly=False):
iou.write_bytes(fileobj, content.warpmeta['format'], dtype='>i4')
iou.write_bytes(fileobj, content.warpmeta.get('spacing', 1), dtype='>i4')
iou.write_bytes(fileobj, content.warpmeta.get('exp_k', 0.0), dtype='>f4')

# write TAG_END_NIIHDREXTENSION at the end of extension data to avoid the data to be truncated:
# TAG_END_NIIHDREXTENSION (-1) data-length (1) '*'
# this needs to be the last tag.
tag = FSNifti1Extension.Tags.end_data
length = 1 # extra char '*'
num_bytes += length + addtaglength
print(f'[DEBUG] FSNifti1Extension.write(): +{length:5d}, +{addtaglength:d}, dlen = {num_bytes:6d}, TAG = {tag:2d}')
if (not countbytesonly):
FSNifti1Extension.write_tag(fileobj, tag, length)
extrachar = '*'
fileobj.write(extrachar.encode('utf-8'))

return num_bytes

Expand Down Expand Up @@ -258,6 +270,26 @@ def write(self, fileobj, content, countbytesonly=False):
iou.write_bytes(fileobj, content.scan_parameters['field_strength'], '>f4')
fileobj.write(content.scan_parameters['pedir'].encode('utf-8'))

# end_data (TAG_END_NIIHDREXTENSION = -1)
"""
write TAG_END_NIIHDREXTENSION at the end of extension data to avoid the data to be truncated:
TAG_END_NIIHDREXTENSION (-1) data-length (1) '*'
this needs to be the last tag.
If the extension data has trailing null characters or zeros at the end,
nibabel.nifti1.Nifti1Extension.get_content() will truncate the data.
See https://github.com/nipy/nibabel/blob/master/nibabel/nifti1.py#L629C1-L630C1,
line 629: 'evalue = evalue.rstrip(b'\x00')'
"""
tag = FSNifti1Extension.Tags.end_data
length = 1 # extra char '*'
num_bytes += length + addtaglength
print(f'[DEBUG] FSNifti1Extension.write(): +{length:5d}, +{addtaglength:d}, dlen = {num_bytes:6d}, TAG = {tag:2d}')
if (not countbytesonly):
FSNifti1Extension.write_tag(fileobj, tag, length)
extrachar = '*'
fileobj.write(extrachar.encode('utf-8'))

return num_bytes


Expand Down Expand Up @@ -384,6 +416,7 @@ class Tags:
gcamorph_geom = 10 # TAG_GCAMORPH_GEOM
gcamorph_meta = 13 # TAG_GCAMORPH_META
scan_parameters = 45 # TAG_SCAN_PARAMETERS
end_data = -1 # TAG_END_NIIHDREXTENSION


class Content:
Expand Down

0 comments on commit cc9cdf2

Please sign in to comment.