ITK  4.3.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 <string>
21 
22 #include "itksys/SystemTools.hxx"
23 #include "itkImageFileWriter.h"
24 #include "itkImageFileReader.h"
25 #include "vnl/vnl_random.h"
26 namespace itk
27 {
29 {
30 public:
31  template <typename TImage>
32  static typename TImage::Pointer ReadImage( const std::string &fileName,
33  const bool zeroOrigin = false )
34  {
35  typedef itk::ImageFileReader<TImage> ReaderType;
36 
37  typename ReaderType::Pointer reader = ReaderType::New();
38  {
39  reader->SetFileName( fileName.c_str() );
40  try
41  {
42  reader->Update();
43  }
44  catch( itk::ExceptionObject & err )
45  {
46  std::cout << "Caught an exception: " << std::endl;
47  std::cout << err << " " << __FILE__ << " " << __LINE__ << std::endl;
48  throw err;
49  }
50  catch(...)
51  {
52  std::cout << "Error while reading in image for patient " << fileName << std::endl;
53  throw;
54  }
55  }
56  typename TImage::Pointer image = reader->GetOutput();
57  if(zeroOrigin)
58  {
59  double origin[TImage::ImageDimension];
60  for(unsigned int i = 0; i < TImage::ImageDimension; i++)
61  {
62  origin[i]=0;
63  }
64  image->SetOrigin(origin);
65  }
66  return image;
67  }
68 
69  template <class ImageType,class ImageIOType>
70  static void
71  WriteImage(typename ImageType::Pointer &image ,
72  const std::string &filename)
73  {
74 
75  typedef itk::ImageFileWriter< ImageType > WriterType;
76  typename WriterType::Pointer writer = WriterType::New();
77 
78  typename ImageIOType::Pointer imageio(ImageIOType::New());
79 
80  writer->SetImageIO(imageio);
81 
82  writer->SetFileName(filename.c_str());
83 
84  writer->SetInput(image);
85 
86  try
87  {
88  writer->Update();
89  }
90  catch (itk::ExceptionObject &err) {
91  std::cerr << "Exception Object caught: " << std::endl
92  << err << std::endl;
93  throw;
94  }
95  }
96 
97 //
98 // generate random pixels of various types
99  static void
100  RandomPix(vnl_random &randgen,itk::RGBPixel<unsigned char> &pix)
101  {
102  for(unsigned int i = 0; i < 3; i++)
103  {
104  pix[i] = randgen.lrand32(itk::NumericTraits<unsigned char>::max());
105  }
106  }
107 
108  template <typename TPixel>
109  static void
110  RandomPix(vnl_random &randgen, TPixel &pix)
111  {
112  pix = randgen.lrand32(itk::NumericTraits<TPixel>::max());
113  }
114 
115  static void
116  RandomPix(vnl_random &randgen, double &pix)
117  {
118  pix = randgen.drand64(itk::NumericTraits<double>::max());
119  }
120 
121  static void
122  RandomPix(vnl_random &randgen, float &pix)
123  {
124  pix = randgen.drand64(itk::NumericTraits<float>::max());
125  }
126 
127  static int Remove(const char *fname)
128  {
129  return itksys::SystemTools::RemoveFile(fname);
130  }
131 
132  template <class ImageType>
133  static void SetIdentityDirection(typename ImageType::Pointer &im)
134  {
135  typename ImageType::DirectionType dir;
136  dir.SetIdentity();
137  im->SetDirection(dir);
138  }
139 
140  template <class ImageType>
141  static typename ImageType::Pointer
142  AllocateImageFromRegionAndSpacing(const typename ImageType::RegionType &region,
143  const typename ImageType::SpacingType &spacing)
144  {
145  typename ImageType::Pointer rval = ImageType::New();
146  SetIdentityDirection<ImageType>(rval);
147  rval->SetSpacing(spacing);
148  rval->SetRegions(region);
149  rval->Allocate();
150  return rval;
151  }
152  template <class ImageType>
153  static typename ImageType::Pointer
154  AllocateImageFromRegionAndSpacing(const typename ImageType::RegionType &region,
155  const typename ImageType::SpacingType &spacing,
156  int vecLength)
157  {
158  typename ImageType::Pointer rval = ImageType::New();
159  rval->SetSpacing(spacing);
160  rval->SetRegions(region);
161  rval->SetVectorLength(vecLength);
162  rval->Allocate();
163  return rval;
164  }
165 };
166 }
167 #endif // __itkIOTestHelper_h
168