18#ifndef itkIOTestHelper_h
19#define itkIOTestHelper_h
20#include "ITKIOImageBaseExport.h"
23#include "itksys/SystemTools.hxx"
26#include "vnl/vnl_random.h"
32 template <
typename TImage>
35 const bool zeroOrigin =
false,
44 reader->SetImageIO(imageio);
46 reader->SetFileName(fileName.c_str());
51 catch (
const itk::ExceptionObject & err)
53 std::cout <<
"Caught an exception: " << std::endl;
54 std::cout << err <<
' ' << __FILE__ <<
' ' << __LINE__ << std::endl;
59 std::cout <<
"Error while reading in image for patient " << fileName << std::endl;
66 double origin[TImage::ImageDimension];
67 for (
unsigned int i = 0; i < TImage::ImageDimension; ++i)
71 image->SetOrigin(origin);
76 template <
typename ImageType,
typename ImageIOType>
79 const std::string & filename,
82 const bool create_local_io_object{ imageio.IsNull() };
85 if (create_local_io_object)
90 writer->SetImageIO(imageio);
91 writer->SetFileName(filename);
92 writer->SetInput(image);
97 catch (
const itk::ExceptionObject & err)
99 std::cerr <<
"Exception Object caught: " << std::endl << err << std::endl;
105 imageio = imageio->Clone();
108 const std::string bad_root_path{
"/a_blatantly_obvious/bad_file_path/that/should/never/exist/on/the/computer/" };
109 const std::string bad_filename{ bad_root_path + filename };
110 bool exception_correctly_caught =
false;
113 writer->SetImageIO(imageio);
114 writer->SetFileName(bad_filename);
115 writer->SetInput(image);
120 catch (
const itk::ExceptionObject & )
123 std::cout <<
"Correctly caught exception for attempting to write to an invalid file." << std::endl;
124 exception_correctly_caught =
true;
128 itkGenericExceptionMacro(
"IO library exception not converted to an itk::ExceptionObject.");
130 if (!exception_correctly_caught)
132 itkGenericExceptionMacro(
"Invalid file writing path did not throw an exception: " << bad_filename <<
" with "
133 << imageio->GetNameOfClass());
143 for (
unsigned int i = 0; i < 3; ++i)
149 template <
typename TPixel>
160 pix = (pix << 32) | randgen.lrand32();
164 RandomPix(vnl_random & randgen,
unsigned long long & pix)
166 pix = randgen.lrand32();
167 pix = (pix << 32) | randgen.lrand32();
185 return static_cast<bool>(itksys::SystemTools::RemoveFile(fname));
188 template <
typename ImageType>
194 im->SetDirection(dir);
197 template <
typename ImageType>
200 const typename ImageType::SpacingType & spacing)
203 SetIdentityDirection<ImageType>(rval);
204 rval->SetSpacing(spacing);
205 rval->SetRegions(region);
209 template <
typename ImageType>
212 const typename ImageType::SpacingType & spacing,
216 rval->SetSpacing(spacing);
217 rval->SetRegions(region);
218 rval->SetVectorLength(vecLength);
static ImageType::Pointer AllocateImageFromRegionAndSpacing(const typename ImageType::RegionType ®ion, const typename ImageType::SpacingType &spacing)
static void SetIdentityDirection(typename ImageType::Pointer &im)
static ImageType::Pointer AllocateImageFromRegionAndSpacing(const typename ImageType::RegionType ®ion, const typename ImageType::SpacingType &spacing, int vecLength)
static void RandomPix(vnl_random &randgen, double &pix)
static int Remove(const char *fname)
static void RandomPix(vnl_random &randgen, TPixel &pix)
static void RandomPix(vnl_random &randgen, long long &pix)
static TImage::Pointer ReadImage(const std::string &fileName, const bool zeroOrigin=false, typename ImageIOBase::Pointer imageio=nullptr)
static void WriteImage(typename ImageType::Pointer image, const std::string &filename, typename ImageIOType::Pointer imageio=nullptr)
static void RandomPix(vnl_random &randgen, float &pix)
static void RandomPix(vnl_random &randgen, unsigned long long &pix)
static void RandomPix(vnl_random &randgen, itk::RGBPixel< unsigned char > &pix)
Data source that reads image data from a single file.
Writes image data to a single file.
Define additional traits for native types such as int or float.
Represent Red, Green and Blue components for color images.
SmartPointer< Self > Pointer
ImageBaseType::DirectionType DirectionType
ImageBaseType::RegionType RegionType
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....