Skip to content

Commit 0378272

Browse files
committed
NF: add guard for unexpected bit widths
From comment by Eric L
1 parent 722358a commit 0378272

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

nibabel/parrec.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,11 @@ def __init__(self, info, image_defs, permit_truncated=False):
567567
# functionality
568568
# dtype
569569
bitpix = self._get_unique_image_prop('image pixel size')
570-
# REC data always little endian?
570+
if bitpix not in (8, 16):
571+
raise PARRECError('Only 8- and 16-bit data supported (not %s)'
572+
'please report this to the nibabel developers'
573+
% bitpix)
574+
# REC data always little endian
571575
dt = np.dtype('uint' + str(bitpix)).newbyteorder('<')
572576
Header.__init__(self,
573577
data_dtype=dt,

nibabel/tests/test_parrec.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,3 +534,11 @@ class TestPARRECImage(tsi.MmapImageMixin):
534534

535535
def write_image(self):
536536
return parrec.load(EG_PAR), EG_PAR
537+
538+
539+
def test_bitpix():
540+
# Check errors for other than 8, 16 bit
541+
hdr_defs = HDR_DEFS.copy()
542+
for pix_size in (24, 32):
543+
hdr_defs['image pixel size'] = pix_size
544+
assert_raises(PARRECError, PARRECHeader, HDR_INFO, hdr_defs)

0 commit comments

Comments
 (0)