You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Corrupted nifti image loading if same filename is used with different extensions in the same folder #2243
ITK-5.1
(reproducible in various versions of Slicer, MITK, ITK-Snap, etc.)
Environment
Windows, Linux
Additional Information
It is just so frustrating that there are so many troubles with nifti images and people often keep using it (it only makes sense for neuroimaging but probably they should give up as well and move to a new, simpler and better format).
This is a problem with nifti library itself. Function nifti_findimgname ignores the actual given filename, and searches through a list of supported extensions:
@hjmjohnson@lassoan these days, many OSes, like macOS in my case, sandboxes processes from the filesystem and allows them only to access files the user has granted permission to access. The way nifti_clib second guesses and tries different filename extensions and uppercase/lowercase changes is antithetical to that.
I'm thinking it might be a good idea to add a new API to nifti_clib to supplement this existing one (which ITK currently calls):
nifti_image *nifti_image_read( const char *hname , int read_data )
In this new function we'd just not do any of that second guessing (trying different extensions, changing uppercase/lowercase) and just use the exact file paths given. For .hdr+.img you'd need to pass both, for .nii you'd only need to pass one.
I would find it more intuitive to add an optional data_file_path parameter to the existing nifti_image_read method.
If this new parameter can be passed to the reader, how would you determine the data file path?
The proper solution would be to store the relative path to the data file in the header file, as it is done in other file formats, from nrrd to obj. Could the NIFTI file format be changed to store the data file path? If not, then it is yet another reason to discourage everyone from using this file format.
I would find it more intuitive to add an optional data_file_path parameter to the existing nifti_image_read method.
That would be a backwards-incompatible change to a public API. Upstream is very conservative, and though I'm less so, I don't think that's a good idea either. Hence me suggesting a new API. It would allow the existing API to do what it's always done, and for anyone that doesn't like that to opt-in to new behaviour with new API.
If this new parameter can be passed to the reader, how would you determine the data file path?
In the case of sandboxed apps on macOS, the user must select both the .hdr and .img in the system file panel for the sandbox to allow access to said files. The file panel API then hands back the paths to both files.
In other cases, it could be the caller's responsibility to find the other half of .img/.hdr, then pass that to my proposed API.
Could the NIFTI file format be changed to store the data file path? If not, then it is yet another reason to discourage everyone from using this file format.
Do you mean nifti_image *nifti_image_read( const char *hname , int read_data, const char *data_file_path=nullptr) would be considered as backward incompatible? Adding a new method would be OK, too, maybe just use something more specific suffix than _new (e.g., _with_datafile, _explicit - meaning no heuristics, ...)
That's way out of scope. :)
I know, but I had to say it. NIFTI (and probably the governance structure around it) is just so broken...
Activity
dzenanz commentedon Jan 8, 2021
This is a problem with nifti library itself. Function
nifti_findimgnameignores the actual given filename, and searches through a list of supported extensions:ITK/Modules/ThirdParty/NIFTI/src/nifti/niftilib/nifti1_io.c
Lines 2846 to 2914 in 5e28b1d
dzenanz commentedon Jan 8, 2021
It looks like it was not rectified upstream either. It would be good if @vanossj propagated the issue to nifti_clib.
stale commentedon Jun 11, 2021
This issue has been automatically marked as stale because it has not had recent activity. Thank you for your contributions.
seanm commentedon Jan 23, 2025
@hjmjohnson @lassoan these days, many OSes, like macOS in my case, sandboxes processes from the filesystem and allows them only to access files the user has granted permission to access. The way nifti_clib second guesses and tries different filename extensions and uppercase/lowercase changes is antithetical to that.
I'm thinking it might be a good idea to add a new API to nifti_clib to supplement this existing one (which ITK currently calls):
nifti_image *nifti_image_read( const char *hname , int read_data )into:
nifti_image *nifti_image_read_new( const char *header_file_path , const char *image_file_path, int read_data )In this new function we'd just not do any of that second guessing (trying different extensions, changing uppercase/lowercase) and just use the exact file paths given. For .hdr+.img you'd need to pass both, for .nii you'd only need to pass one.
Thoughts?
lassoan commentedon Jan 23, 2025
I would find it more intuitive to add an optional
data_file_pathparameter to the existingnifti_image_readmethod.If this new parameter can be passed to the reader, how would you determine the data file path?
The proper solution would be to store the relative path to the data file in the header file, as it is done in other file formats, from nrrd to obj. Could the NIFTI file format be changed to store the data file path? If not, then it is yet another reason to discourage everyone from using this file format.
seanm commentedon Jan 23, 2025
That would be a backwards-incompatible change to a public API. Upstream is very conservative, and though I'm less so, I don't think that's a good idea either. Hence me suggesting a new API. It would allow the existing API to do what it's always done, and for anyone that doesn't like that to opt-in to new behaviour with new API.
In the case of sandboxed apps on macOS, the user must select both the .hdr and .img in the system file panel for the sandbox to allow access to said files. The file panel API then hands back the paths to both files.
In other cases, it could be the caller's responsibility to find the other half of .img/.hdr, then pass that to my proposed API.
That's way out of scope. :)
lassoan commentedon Jan 23, 2025
Do you mean
nifti_image *nifti_image_read( const char *hname , int read_data, const char *data_file_path=nullptr)would be considered as backward incompatible? Adding a new method would be OK, too, maybe just use something more specific suffix than_new(e.g.,_with_datafile,_explicit- meaning no heuristics, ...)I know, but I had to say it. NIFTI (and probably the governance structure around it) is just so broken...
hjmjohnson commentedon Jan 23, 2025
InsightSoftwareConsortium/nifti_clib#9