ITK  4.9.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  typedef itk::ImageFileReader<TImage> ReaderType;
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  typedef itk::ImageFileWriter< ImageType > WriterType;
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, double &pix)
118  {
119  pix = randgen.drand64(itk::NumericTraits<double>::max());
120  }
121 
122  static void
123  RandomPix(vnl_random &randgen, float &pix)
124  {
125  pix = randgen.drand64(itk::NumericTraits<float>::max());
126  }
127 
128  static int Remove(const char *fname)
129  {
130  return itksys::SystemTools::RemoveFile(fname);
131  }
132 
133  template <typename ImageType>
134  static void SetIdentityDirection(typename ImageType::Pointer &im)
135  {
136  typename ImageType::DirectionType dir;
137  dir.SetIdentity();
138  im->SetDirection(dir);
139  }
140 
141  template <typename ImageType>
142  static typename ImageType::Pointer
143  AllocateImageFromRegionAndSpacing(const typename ImageType::RegionType &region,
144  const typename ImageType::SpacingType &spacing)
145  {
146  typename ImageType::Pointer rval = ImageType::New();
147  SetIdentityDirection<ImageType>(rval);
148  rval->SetSpacing(spacing);
149  rval->SetRegions(region);
150  rval->Allocate();
151  return rval;
152  }
153  template <typename ImageType>
154  static typename ImageType::Pointer
155  AllocateImageFromRegionAndSpacing(const typename ImageType::RegionType &region,
156  const typename ImageType::SpacingType &spacing,
157  int vecLength)
158  {
159  typename ImageType::Pointer rval = ImageType::New();
160  rval->SetSpacing(spacing);
161  rval->SetRegions(region);
162  rval->SetVectorLength(vecLength);
163  rval->Allocate();
164  return rval;
165  }
166 };
167 }
168 #endif // itkIOTestHelper_h
static void RandomPix(vnl_random &randgen, TPixel &pix)
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 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)
Define additional traits for native types such as int or float.
static int Remove(const char *fname)