Skip to content

Commit

Permalink
Merge pull request #1807 from ANTsX/io_checks
Browse files Browse the repository at this point in the history
ENH: More image read checks

Provides a function to check if the image can be read.
  • Loading branch information
cookpa authored Nov 20, 2024
2 parents 1892fef + 851eb79 commit 4db64a7
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
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

0 comments on commit 4db64a7

Please sign in to comment.