Skip to content

Commit

Permalink
Get correct element size
Browse files Browse the repository at this point in the history
  • Loading branch information
sgerber-hf committed Nov 20, 2024
1 parent 5a2c827 commit 2d633b1
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 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,14 +269,15 @@ 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

#See https://github.com/Kitware/MetaIO/blob/1a031fd8223c4846e2f90f50216cbcb415d52018/src/metaImage.cxx#L2601 for reference
header_size = meta.get("HeaderSize") or 0
header_size = int( meta.get("HeaderSize") or 0 )
if header_size == -1:
data_quantity = np.prod(meta["DimSize"])
read_size = data_quantity * meta["ElementNumberOfChannels"] * meta["ElementSize"]
#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)
f.seek(meta_size + header_size, 0)
else:
raise ValueError(f"Invalid HeaderSize {header_size}")

Expand Down

0 comments on commit 2d633b1

Please sign in to comment.