ITK  5.2.0
Insight Toolkit
itkIOTestHelper.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright NumFOCUS
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef itkIOTestHelper_h
19 #define itkIOTestHelper_h
20 #include "ITKIOImageBaseExport.h"
21 #include <string>
22 
23 #include "itksys/SystemTools.hxx"
24 #include "itkImageFileWriter.h"
25 #include "itkImageFileReader.h"
26 #include "vnl/vnl_random.h"
27 namespace itk
28 {
30 {
31 public:
32  template <typename TImage>
33  static typename TImage::Pointer
34  ReadImage(const std::string & fileName,
35  const bool zeroOrigin = false,
36  typename ImageIOBase::Pointer imageio = nullptr)
37  {
38  using ReaderType = itk::ImageFileReader<TImage>;
39 
40  typename ReaderType::Pointer reader = ReaderType::New();
41  {
42  if (imageio)
43  {
44  reader->SetImageIO(imageio);
45  }
46  reader->SetFileName(fileName.c_str());
47  try
48  {
49  reader->Update();
50  }
51  catch (const itk::ExceptionObject & err)
52  {
53  std::cout << "Caught an exception: " << std::endl;
54  std::cout << err << " " << __FILE__ << " " << __LINE__ << std::endl;
55  throw err;
56  }
57  catch (...)
58  {
59  std::cout << "Error while reading in image for patient " << fileName << std::endl;
60  throw;
61  }
62  }
63  typename TImage::Pointer image = reader->GetOutput();
64  if (zeroOrigin)
65  {
66  double origin[TImage::ImageDimension];
67  for (unsigned int i = 0; i < TImage::ImageDimension; i++)
68  {
69  origin[i] = 0;
70  }
71  image->SetOrigin(origin);
72  }
73  return image;
74  }
75 
76  template <typename ImageType, typename ImageIOType>
77  static void
78  WriteImage(typename ImageType::Pointer & image,
79  const std::string & filename,
80  typename ImageIOType::Pointer imageio = nullptr)
81  {
82 
83  using WriterType = itk::ImageFileWriter<ImageType>;
84  typename WriterType::Pointer writer = WriterType::New();
85 
86  if (imageio.IsNull())
87  {
88  imageio = ImageIOType::New();
89  }
90 
91  writer->SetImageIO(imageio);
92 
93  writer->SetFileName(filename.c_str());
94 
95  writer->SetInput(image);
96 
97  try
98  {
99  writer->Update();
100  }
101  catch (const itk::ExceptionObject & err)
102  {
103  std::cerr << "Exception Object caught: " << std::endl << err << std::endl;
104  throw;
105  }
106  }
107 
108  //
109  // generate random pixels of various types
110  static void
111  RandomPix(vnl_random & randgen, itk::RGBPixel<unsigned char> & pix)
112  {
113  for (unsigned int i = 0; i < 3; i++)
114  {
115  pix[i] = randgen.lrand32(itk::NumericTraits<unsigned char>::max());
116  }
117  }
118 
119  template <typename TPixel>
120  static void
121  RandomPix(vnl_random & randgen, TPixel & pix)
122  {
123  pix = randgen.lrand32(itk::NumericTraits<TPixel>::max());
124  }
125 
126  static void
127  RandomPix(vnl_random & randgen, long long & pix)
128  {
129  pix = randgen.lrand32(itk::NumericTraits<int>::max());
130  pix = (pix << 32) | randgen.lrand32();
131  }
132 
133  static void
134  RandomPix(vnl_random & randgen, unsigned long long & pix)
135  {
136  pix = randgen.lrand32();
137  pix = (pix << 32) | randgen.lrand32();
138  }
139 
140  static void
141  RandomPix(vnl_random & randgen, double & pix)
142  {
143  pix = randgen.drand64(itk::NumericTraits<double>::max());
144  }
145 
146  static void
147  RandomPix(vnl_random & randgen, float & pix)
148  {
149  pix = randgen.drand64(itk::NumericTraits<float>::max());
150  }
151 
152  static int
153  Remove(const char * fname)
154  {
155  return itksys::SystemTools::RemoveFile(fname);
156  }
157 
158  template <typename ImageType>
159  static void
160  SetIdentityDirection(typename ImageType::Pointer & im)
161  {
162  typename ImageType::DirectionType dir;
163  dir.SetIdentity();
164  im->SetDirection(dir);
165  }
166 
167  template <typename ImageType>
168  static typename ImageType::Pointer
170  const typename ImageType::SpacingType & spacing)
171  {
172  typename ImageType::Pointer rval = ImageType::New();
173  SetIdentityDirection<ImageType>(rval);
174  rval->SetSpacing(spacing);
175  rval->SetRegions(region);
176  rval->Allocate();
177  return rval;
178  }
179  template <typename ImageType>
180  static typename ImageType::Pointer
182  const typename ImageType::SpacingType & spacing,
183  int vecLength)
184  {
185  typename ImageType::Pointer rval = ImageType::New();
186  rval->SetSpacing(spacing);
187  rval->SetRegions(region);
188  rval->SetVectorLength(vecLength);
189  rval->Allocate();
190  return rval;
191  }
192 };
193 } // namespace itk
194 #endif // itkIOTestHelper_h
itk::Matrix::SetIdentity
void SetIdentity()
Definition: itkMatrix.h:192
itk::RGBPixel
Represent Red, Green and Blue components for color images.
Definition: itkRGBPixel.h:58
itk::GTest::TypedefsAndConstructors::Dimension2::DirectionType
ImageBaseType::DirectionType DirectionType
Definition: itkGTestTypedefsAndConstructors.h:52
itk::IOTestHelper::RandomPix
static void RandomPix(vnl_random &randgen, double &pix)
Definition: itkIOTestHelper.h:141
itkImageFileReader.h
itk::SmartPointer< Self >
itk::IOTestHelper::WriteImage
static void WriteImage(typename ImageType::Pointer &image, const std::string &filename, typename ImageIOType::Pointer imageio=nullptr)
Definition: itkIOTestHelper.h:78
itk::IOTestHelper::AllocateImageFromRegionAndSpacing
static ImageType::Pointer AllocateImageFromRegionAndSpacing(const typename ImageType::RegionType &region, const typename ImageType::SpacingType &spacing)
Definition: itkIOTestHelper.h:169
itk::ImageFileReader
Data source that reads image data from a single file.
Definition: itkImageFileReader.h:75
itk::IOTestHelper::RandomPix
static void RandomPix(vnl_random &randgen, long long &pix)
Definition: itkIOTestHelper.h:127
itk::IOTestHelper::RandomPix
static void RandomPix(vnl_random &randgen, unsigned long long &pix)
Definition: itkIOTestHelper.h:134
itk::ImageFileWriter
Writes image data to a single file.
Definition: itkImageFileWriter.h:88
itk::IOTestHelper::RandomPix
static void RandomPix(vnl_random &randgen, TPixel &pix)
Definition: itkIOTestHelper.h:121
itk::GTest::TypedefsAndConstructors::Dimension2::RegionType
ImageBaseType::RegionType RegionType
Definition: itkGTestTypedefsAndConstructors.h:54
itk::IOTestHelper
Definition: itkIOTestHelper.h:29
itk::IOTestHelper::RandomPix
static void RandomPix(vnl_random &randgen, float &pix)
Definition: itkIOTestHelper.h:147
itkImageFileWriter.h
itk::IOTestHelper::RandomPix
static void RandomPix(vnl_random &randgen, itk::RGBPixel< unsigned char > &pix)
Definition: itkIOTestHelper.h:111
itk::NumericTraits
Define additional traits for native types such as int or float.
Definition: itkNumericTraits.h:58
itk::IOTestHelper::Remove
static int Remove(const char *fname)
Definition: itkIOTestHelper.h:153
itk::IOTestHelper::ReadImage
static TImage::Pointer ReadImage(const std::string &fileName, const bool zeroOrigin=false, typename ImageIOBase::Pointer imageio=nullptr)
Definition: itkIOTestHelper.h:34
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itk::IOTestHelper::SetIdentityDirection
static void SetIdentityDirection(typename ImageType::Pointer &im)
Definition: itkIOTestHelper.h:160
itk::IOTestHelper::AllocateImageFromRegionAndSpacing
static ImageType::Pointer AllocateImageFromRegionAndSpacing(const typename ImageType::RegionType &region, const typename ImageType::SpacingType &spacing, int vecLength)
Definition: itkIOTestHelper.h:181
fileName
std::string fileName
Definition: itkTestDriverInclude.h:118