Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: More image read checks #1807

Merged
merged 1 commit into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Utilities/ReadWriteData.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,26 @@
bool
ANTSFileExists(const std::string & strFilename)
{
// ITK checks file existence on all platforms, also read permissions on POSIX systems
return itksys::SystemTools::FileExists(strFilename);
}

bool
ANTSFileIsImage(const std::string &filename)
{
if (!ANTSFileExists(filename))
{
return false;
}

// Check if the file is recognized as a valid image
itk::ImageIOBase::Pointer imageIO = itk::ImageIOFactory::CreateImageIO(
filename.c_str(), itk::ImageIOFactory::IOFileModeEnum::ReadMode);
if (!imageIO)
{
return false;
}

// File passed both checks
return true;
}
36 changes: 36 additions & 0 deletions Utilities/ReadWriteData.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
extern bool
ANTSFileExists(const std::string & strFilename);

extern bool
ANTSFileIsImage(const std::string & filename);

// Nifti stores DTI values in lower tri format but itk uses upper tri
// currently, nifti io does nothing to deal with this. if this changes
// the function below should be modified/eliminated.
Expand Down Expand Up @@ -153,6 +156,14 @@ ReadTensorImage(itk::SmartPointer<TImageType> & target, const char * file, bool
if (!ANTSFileExists(std::string(file)))
{
std::cerr << " file " << std::string(file) << " does not exist . " << std::endl;
target = nullptr;
return;
}

if (!ANTSFileIsImage(file))
{
std::cerr << " file " << std::string(file) << " is not recognized as a supported image format . " << std::endl;
target = nullptr;
return;
}

Expand Down Expand Up @@ -286,6 +297,14 @@ ReadImage(itk::SmartPointer<TImageType> & target, const char * file)
target = nullptr;
return false;
}

if (!ANTSFileIsImage(file))
{
std::cerr << " file " << std::string(file) << " is not recognized as a supported image format . " << std::endl;
target = nullptr;
return false;
}

typedef TImageType ImageType;
typedef itk::ImageFileReader<ImageType> FileSourceType;

Expand Down Expand Up @@ -391,13 +410,15 @@ ReadLabeledPointSet(itk::SmartPointer<TPointSet> & target,
{
if (std::string(file).length() < 3)
{
std::cerr << " bad file name " << std::string(file) << std::endl;
target = nullptr;
return false;
}

if (!ANTSFileExists(std::string(file)))
{
std::cerr << " file " << std::string(file) << " does not exist . " << std::endl;
target = nullptr;
return false;
}

Expand All @@ -415,6 +436,7 @@ ReadLabeledPointSet(itk::SmartPointer<TPointSet> & target,
{
std::cerr << "Exception caught during point set reference file reading " << std::endl;
std::cerr << e << std::endl;
target = nullptr;
return false;
}

Expand Down Expand Up @@ -445,6 +467,13 @@ ReadImageIntensityPointSet(itk::SmartPointer<TPointSet> & target,
return false;
}

if (!ANTSFileIsImage(imageFile))
{
std::cerr << " file " << std::string(imageFile) << " is not recognized as a supported image format . " << std::endl;
target = nullptr;
return false;
}

if (std::string(maskFile).length() < 3)
{
std::cerr << " bad mask file name " << std::string(maskFile) << std::endl;
Expand All @@ -459,6 +488,13 @@ ReadImageIntensityPointSet(itk::SmartPointer<TPointSet> & target,
return false;
}

if (!ANTSFileIsImage(maskFile))
{
std::cerr << " file " << std::string(maskFile) << " is not recognized as a supported image format . " << std::endl;
target = nullptr;
return false;
}

if (neighborhoodRadius.size() != TImage::ImageDimension)
{
std::cerr << " size of the neighborhood radius is not equal to the image dimension." << std::endl;
Expand Down