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);
224 #endif // itkIOTestHelper_h