ITK  5.0.0
Insight Segmentation and Registration Toolkit
itkIOTestHelper.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
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 ReadImage( const std::string &fileName,
34  const bool zeroOrigin = false )
35  {
36  using ReaderType = itk::ImageFileReader<TImage>;
37 
38  typename ReaderType::Pointer reader = ReaderType::New();
39  {
40  reader->SetFileName( fileName.c_str() );
41  try
42  {
43  reader->Update();
44  }
45  catch( itk::ExceptionObject & err )
46  {
47  std::cout << "Caught an exception: " << std::endl;
48  std::cout << err << " " << __FILE__ << " " << __LINE__ << std::endl;
49  throw err;
50  }
51  catch(...)
52  {
53  std::cout << "Error while reading in image for patient " << fileName << std::endl;
54  throw;
55  }
56  }
57  typename TImage::Pointer image = reader->GetOutput();
58  if(zeroOrigin)
59  {
60  double origin[TImage::ImageDimension];
61  for(unsigned int i = 0; i < TImage::ImageDimension; i++)
62  {
63  origin[i]=0;
64  }
65  image->SetOrigin(origin);
66  }
67  return image;
68  }
69 
70  template <typename ImageType,typename ImageIOType>
71  static void
72  WriteImage(typename ImageType::Pointer &image ,
73  const std::string &filename)
74  {
75 
76  using WriterType = itk::ImageFileWriter< ImageType >;
77  typename WriterType::Pointer writer = WriterType::New();
78 
79  typename ImageIOType::Pointer imageio(ImageIOType::New());
80 
81  writer->SetImageIO(imageio);
82 
83  writer->SetFileName(filename.c_str());
84 
85  writer->SetInput(image);
86 
87  try
88  {
89  writer->Update();
90  }
91  catch (itk::ExceptionObject &err) {
92  std::cerr << "Exception Object caught: " << std::endl
93  << err << std::endl;
94  throw;
95  }
96  }
97 
98 //
99 // generate random pixels of various types
100  static void
101  RandomPix(vnl_random &randgen,itk::RGBPixel<unsigned char> &pix)
102  {
103  for(unsigned int i = 0; i < 3; i++)
104  {
105  pix[i] = randgen.lrand32(itk::NumericTraits<unsigned char>::max());
106  }
107  }
108 
109  template <typename TPixel>
110  static void
111  RandomPix(vnl_random &randgen, TPixel &pix)
112  {
113  pix = randgen.lrand32(itk::NumericTraits<TPixel>::max());
114  }
115 
116  static void
117  RandomPix(vnl_random &randgen, long long &pix)
118  {
119  pix = randgen.lrand32(itk::NumericTraits<int>::max());
120  pix = (pix << 32) | randgen.lrand32();
121  }
122 
123  static void
124  RandomPix(vnl_random &randgen, unsigned long long &pix)
125  {
126  pix = randgen.lrand32();
127  pix = (pix << 32) | randgen.lrand32();
128  }
129 
130  static void
131  RandomPix(vnl_random &randgen, double &pix)
132  {
133  pix = randgen.drand64(itk::NumericTraits<double>::max());
134  }
135 
136  static void
137  RandomPix(vnl_random &randgen, float &pix)
138  {
139  pix = randgen.drand64(itk::NumericTraits<float>::max());
140  }
141 
142  static int Remove(const char *fname)
143  {
144  return itksys::SystemTools::RemoveFile(fname);
145  }
146 
147  template <typename ImageType>
148  static void SetIdentityDirection(typename ImageType::Pointer &im)
149  {
150  typename ImageType::DirectionType dir;
151  dir.SetIdentity();
152  im->SetDirection(dir);
153  }
154 
155  template <typename ImageType>
156  static typename ImageType::Pointer
158  const typename ImageType::SpacingType &spacing)
159  {
160  typename ImageType::Pointer rval = ImageType::New();
161  SetIdentityDirection<ImageType>(rval);
162  rval->SetSpacing(spacing);
163  rval->SetRegions(region);
164  rval->Allocate();
165  return rval;
166  }
167  template <typename ImageType>
168  static typename ImageType::Pointer
170  const typename ImageType::SpacingType &spacing,
171  int vecLength)
172  {
173  typename ImageType::Pointer rval = ImageType::New();
174  rval->SetSpacing(spacing);
175  rval->SetRegions(region);
176  rval->SetVectorLength(vecLength);
177  rval->Allocate();
178  return rval;
179  }
180 };
181 }
182 #endif // itkIOTestHelper_h
static void RandomPix(vnl_random &randgen, TPixel &pix)
static void RandomPix(vnl_random &randgen, unsigned long long &pix)
void SetIdentity()
Definition: itkMatrix.h:180
static void RandomPix(vnl_random &randgen, float &pix)
static void RandomPix(vnl_random &randgen, itk::RGBPixel< unsigned char > &pix)
static TImage::Pointer ReadImage(const std::string &fileName, const bool zeroOrigin=false)
static void RandomPix(vnl_random &randgen, long long &pix)
static ImageType::Pointer AllocateImageFromRegionAndSpacing(const typename ImageType::RegionType &region, const typename ImageType::SpacingType &spacing, int vecLength)
Standard exception handling object.
static void WriteImage(typename ImageType::Pointer &image, const std::string &filename)
static ImageType::Pointer AllocateImageFromRegionAndSpacing(const typename ImageType::RegionType &region, const typename ImageType::SpacingType &spacing)
Represent Red, Green and Blue components for color images.
Definition: itkRGBPixel.h:58
static void RandomPix(vnl_random &randgen, double &pix)
Writes image data to a single file.
Data source that reads image data from a single file.
static void SetIdentityDirection(typename ImageType::Pointer &im)
static int Remove(const char *fname)