18#ifndef itkIOTestHelper_h
19#define itkIOTestHelper_h
20#include "ITKIOImageBaseExport.h"
23#include "itksys/SystemTools.hxx"
37 template <
typename TImage>
38 static typename TImage::Pointer
43 auto reader = ReaderType::New();
47 reader->SetImageIO(imageio);
49 reader->SetFileName(fileName.c_str());
56 std::cout <<
"Caught an exception: " << std::endl;
57 std::cout << err <<
' ' << __FILE__ <<
' ' << __LINE__ << std::endl;
62 std::cout <<
"Error while reading in image for patient " << fileName << std::endl;
66 typename TImage::Pointer image = reader->GetOutput();
69 double origin[TImage::ImageDimension];
70 for (
unsigned int i = 0; i < TImage::ImageDimension; ++i)
74 image->SetOrigin(origin);
79 template <
typename ImageType,
typename ImageIOType>
82 const std::string & filename,
83 typename ImageIOType::Pointer imageio =
nullptr)
85 const bool create_local_io_object{ imageio.IsNull() };
88 if (create_local_io_object)
90 imageio = ImageIOType::New();
92 auto writer = WriterType::New();
93 writer->SetImageIO(imageio);
94 writer->SetFileName(filename);
95 writer->SetInput(image);
102 std::cerr <<
"Exception Object caught: " << std::endl << err << std::endl;
108 imageio = imageio->Clone();
111 const std::string bad_root_path{
"/a_blatantly_obvious/bad_file_path/that/should/never/exist/on/the/computer/" };
112 const std::string bad_filename{ bad_root_path + filename };
113 bool exception_correctly_caught =
false;
115 auto writer = WriterType::New();
116 writer->SetImageIO(imageio);
117 writer->SetFileName(bad_filename);
118 writer->SetInput(image);
126 std::cout <<
"Correctly caught exception for attempting to write to an invalid file." << std::endl;
127 exception_correctly_caught =
true;
131 itkGenericExceptionMacro(
"IO library exception not converted to an itk::ExceptionObject.");
133 if (!exception_correctly_caught)
135 itkGenericExceptionMacro(
"Invalid file writing path did not throw an exception: " << bad_filename <<
" with "
136 << imageio->GetNameOfClass());
144 template <
typename T>
148 static_assert(std::is_arithmetic_v<T>,
"BoundedRandom: T must be arithmetic");
149 if constexpr (std::is_floating_point_v<T>)
151 std::uniform_real_distribution<T> dist{
static_cast<T
>(0), bound };
152 return dist(randomNumberEngine);
156 using U = std::make_unsigned_t<T>;
158 if constexpr (
sizeof(T) >
sizeof(std::mt19937::result_type))
161 raw = (
static_cast<U
>(randomNumberEngine()) << 32) |
static_cast<U
>(randomNumberEngine());
165 raw =
static_cast<U
>(randomNumberEngine());
167 const U bound_u =
static_cast<U
>(bound);
170 if (bound_u == std::numeric_limits<U>::max())
172 return static_cast<T
>(raw);
174 return static_cast<T
>(raw % (bound_u + U{ 1 }));
178 template <
typename TPixel>
184 static_assert(std::is_arithmetic_v<TPixel>,
"RandomPix: TPixel must be arithmetic");
188 template <
typename T>
194 for (
unsigned int i = 0; i < 3; ++i)
200 template <
typename T,
unsigned int VLength>
206 for (
unsigned int i = 0; i < VLength; ++i)
215 return static_cast<bool>(itksys::SystemTools::RemoveFile(fname));
218 template <
typename ImageType>
222 typename ImageType::DirectionType dir;
224 im->SetDirection(dir);
227 template <
typename ImageType>
228 static typename ImageType::Pointer
230 const typename ImageType::SpacingType & spacing)
232 auto rval = ImageType::New();
234 rval->SetSpacing(spacing);
235 rval->SetRegions(region);
239 template <
typename ImageType>
240 static typename ImageType::Pointer
242 const typename ImageType::SpacingType & spacing,
245 auto rval = ImageType::New();
246 rval->SetSpacing(spacing);
247 rval->SetRegions(region);
248 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(std::mt19937 &randomNumberEngine, itk::Vector< T, VLength > &pix, double _max=static_cast< double >(itk::NumericTraits< T >::max()))
static int Remove(const char *fname)
static TImage::Pointer ReadImage(const std::string &fileName, const bool zeroOrigin=false, ImageIOBase::Pointer imageio=nullptr)
static T BoundedRandom(std::mt19937 &randomNumberEngine, T bound)
static void RandomPix(std::mt19937 &randomNumberEngine, TPixel &pix, double _max=static_cast< double >(itk::NumericTraits< TPixel >::max()))
static void WriteImage(typename ImageType::Pointer image, const std::string &filename, typename ImageIOType::Pointer imageio=nullptr)
static void RandomPix(std::mt19937 &randomNumberEngine, itk::RGBPixel< T > &pix, double _max=static_cast< double >(itk::NumericTraits< T >::max()))
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.
A templated class holding a n-Dimensional vector.
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....