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>
33 static typename TImage::Pointer
38 auto reader = ReaderType::New();
42 reader->SetImageIO(imageio);
44 reader->SetFileName(fileName.c_str());
51 std::cout <<
"Caught an exception: " << std::endl;
52 std::cout << err <<
' ' << __FILE__ <<
' ' << __LINE__ << std::endl;
57 std::cout <<
"Error while reading in image for patient " << fileName << std::endl;
61 typename TImage::Pointer image = reader->GetOutput();
64 double origin[TImage::ImageDimension];
65 for (
unsigned int i = 0; i < TImage::ImageDimension; ++i)
69 image->SetOrigin(origin);
74 template <
typename ImageType,
typename ImageIOType>
77 const std::string & filename,
78 typename ImageIOType::Pointer imageio =
nullptr)
80 const bool create_local_io_object{ imageio.IsNull() };
83 if (create_local_io_object)
85 imageio = ImageIOType::New();
87 auto writer = WriterType::New();
88 writer->SetImageIO(imageio);
89 writer->SetFileName(filename);
90 writer->SetInput(image);
97 std::cerr <<
"Exception Object caught: " << std::endl << err << std::endl;
103 imageio = imageio->Clone();
106 const std::string bad_root_path{
"/a_blatantly_obvious/bad_file_path/that/should/never/exist/on/the/computer/" };
107 const std::string bad_filename{ bad_root_path + filename };
108 bool exception_correctly_caught =
false;
110 auto writer = WriterType::New();
111 writer->SetImageIO(imageio);
112 writer->SetFileName(bad_filename);
113 writer->SetInput(image);
121 std::cout <<
"Correctly caught exception for attempting to write to an invalid file." << std::endl;
122 exception_correctly_caught =
true;
126 itkGenericExceptionMacro(
"IO library exception not converted to an itk::ExceptionObject.");
128 if (!exception_correctly_caught)
130 itkGenericExceptionMacro(
"Invalid file writing path did not throw an exception: " << bad_filename <<
" with "
131 << imageio->GetNameOfClass());
141 for (
unsigned int i = 0; i < 3; ++i)
147 template <
typename TPixel>
158 pix = (pix << 32) | randgen.lrand32();
162 RandomPix(vnl_random & randgen,
unsigned long long & pix)
164 pix = randgen.lrand32();
165 pix = (pix << 32) | randgen.lrand32();
183 return static_cast<bool>(itksys::SystemTools::RemoveFile(fname));
186 template <
typename ImageType>
190 typename ImageType::DirectionType dir;
192 im->SetDirection(dir);
195 template <
typename ImageType>
196 static typename ImageType::Pointer
198 const typename ImageType::SpacingType & spacing)
200 auto rval = ImageType::New();
202 rval->SetSpacing(spacing);
203 rval->SetRegions(region);
207 template <
typename ImageType>
208 static typename ImageType::Pointer
210 const typename ImageType::SpacingType & spacing,
213 auto rval = ImageType::New();
214 rval->SetSpacing(spacing);
215 rval->SetRegions(region);
216 rval->SetVectorLength(vecLength);
Standard exception handling object.
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 TImage::Pointer ReadImage(const std::string &fileName, const bool zeroOrigin=false, ImageIOBase::Pointer imageio=nullptr)
static void RandomPix(vnl_random &randgen, TPixel &pix)
static void RandomPix(vnl_random &randgen, long long &pix)
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.
SmartPointer< Self > Pointer
static constexpr T max(const T &)
Represent Red, Green and Blue components for color images.
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....