Skip to content

Commit

Permalink
[utils] Support MetaImage files with unspecified HeaderSize (#149)
Browse files Browse the repository at this point in the history
  • Loading branch information
sgerber-hf authored Nov 21, 2024
1 parent 6982927 commit e4a0d3f
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/deepali/utils/imageio/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,12 +218,13 @@ def read_meta_image_from_fileobj(f: io.BufferedReader) -> Tuple[np.ndarray, Meta
"ElementDataFile",
):
meta[key] = value
elif key == "HeaderSize":
meta[key] = np.intp(value)
elif key in (
"NDims",
"ID",
"ParentID",
"CompressedDataSize",
"HeaderSize",
"HeaderSizePerSlice",
"ElementNumberOfChannels",
):
Expand Down Expand Up @@ -268,8 +269,18 @@ def read_meta_image_from_fileobj(f: io.BufferedReader) -> Tuple[np.ndarray, Meta
element_size = np.dtype(meta["ElementType"]).itemsize
increment = np.prod(shape[1:], dtype=int) * element_size

f.seek(meta_size, 0)
f.seek((meta.get("HeaderSize") or 0), 1)
header_size = int( meta.get("HeaderSize") or 0 )
if header_size == -1:
#See https://github.com/Kitware/MetaIO/blob/1a031fd8223c4846e2f90f50216cbcb415d52018/src/metaImage.cxx#L2601 for reference
data_quantity = int(np.prod(meta["DimSize"])) #type: ignore
element_size = int(np.dtype(meta["ElementType"]).itemsize)
read_size = data_quantity * int(meta["ElementNumberOfChannels"]) * element_size #type: ignore
f.seek(-read_size, 2)
elif header_size >= 0:
f.seek(meta_size + header_size, 0)
else:
raise ValueError(f"Invalid HeaderSize {header_size}")

if meta.get("CompressedData"):
if meta["CompressedDataSize"] is None:
raise ValueError("CompressedDataSize needs to be specified when using CompressedData")
Expand Down

0 comments on commit e4a0d3f

Please sign in to comment.