ITK 6.0.0
Insight Toolkit
 
Loading...
Searching...
No Matches
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 * https://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"
27namespace itk
28{
30{
31public:
32 template <typename TImage>
33 static typename TImage::Pointer
34 ReadImage(const std::string & fileName, const bool zeroOrigin = false, ImageIOBase::Pointer imageio = nullptr)
35 {
36 using ReaderType = itk::ImageFileReader<TImage>;
37
38 auto reader = ReaderType::New();
39 {
40 if (imageio)
41 {
42 reader->SetImageIO(imageio);
43 }
44 reader->SetFileName(fileName.c_str());
45 try
46 {
47 reader->Update();
48 }
49 catch (const itk::ExceptionObject & err)
50 {
51 std::cout << "Caught an exception: " << std::endl;
52 std::cout << err << ' ' << __FILE__ << ' ' << __LINE__ << std::endl;
53 throw;
54 }
55 catch (...)
56 {
57 std::cout << "Error while reading in image for patient " << fileName << std::endl;
58 throw;
59 }
60 }
61 typename TImage::Pointer image = reader->GetOutput();
62 if (zeroOrigin)
63 {
64 double origin[TImage::ImageDimension];
65 for (unsigned int i = 0; i < TImage::ImageDimension; ++i)
66 {
67 origin[i] = 0;
68 }
69 image->SetOrigin(origin);
70 }
71 return image;
72 }
73
74 template <typename ImageType, typename ImageIOType>
75 static void
76 WriteImage(typename ImageType::Pointer image,
77 const std::string & filename,
78 typename ImageIOType::Pointer imageio = nullptr)
79 {
80 const bool create_local_io_object{ imageio.IsNull() };
81 using WriterType = itk::ImageFileWriter<ImageType>;
82 { // Test valid filename writing
83 if (create_local_io_object)
84 {
85 imageio = ImageIOType::New();
86 }
87 auto writer = WriterType::New();
88 writer->SetImageIO(imageio);
89 writer->SetFileName(filename);
90 writer->SetInput(image);
91 try
92 {
93 writer->Update();
94 }
95 catch (const itk::ExceptionObject & err)
96 {
97 std::cerr << "Exception Object caught: " << std::endl << err << std::endl;
98 throw;
99 }
100 }
101
102 { // Test if writing to an invalid location causes exception to be thrown:
103 imageio = imageio->Clone(); // A new io object is needed because the HDF5 io object is single use. A new IO
104 // object is needed to re-initialize the internal state.
105
106 const std::string bad_root_path{ "/a_blatantly_obvious/bad_file_path/that/should/never/exist/on/the/computer/" };
107 const std::string bad_filename{ bad_root_path + filename };
108 bool exception_correctly_caught = false;
109
110 auto writer = WriterType::New();
111 writer->SetImageIO(imageio);
112 writer->SetFileName(bad_filename);
113 writer->SetInput(image);
114 try
115 {
116 writer->Update();
117 }
118 catch (const itk::ExceptionObject & /* err */)
119 {
120 // This is the correct behavior
121 std::cout << "Correctly caught exception for attempting to write to an invalid file." << std::endl;
122 exception_correctly_caught = true;
123 }
124 catch (...)
125 {
126 itkGenericExceptionMacro("IO library exception not converted to an itk::ExceptionObject.");
127 }
128 if (!exception_correctly_caught)
129 {
130 itkGenericExceptionMacro("Invalid file writing path did not throw an exception: " << bad_filename << " with "
131 << imageio->GetNameOfClass());
132 }
133 }
134 }
135
136 //
137 // generate random pixels of various types
138 static void
139 RandomPix(vnl_random & randgen, itk::RGBPixel<unsigned char> & pix)
140 {
141 for (unsigned int i = 0; i < 3; ++i)
142 {
143 pix[i] = randgen.lrand32(itk::NumericTraits<unsigned char>::max());
144 }
145 }
146
147 template <typename TPixel>
148 static void
149 RandomPix(vnl_random & randgen, TPixel & pix)
150 {
151 pix = randgen.lrand32(itk::NumericTraits<TPixel>::max());
152 }
153
154 static void
155 RandomPix(vnl_random & randgen, long long & pix)
156 {
157 pix = randgen.lrand32(itk::NumericTraits<int>::max());
158 pix = (pix << 32) | randgen.lrand32();
159 }
160
161 static void
162 RandomPix(vnl_random & randgen, unsigned long long & pix)
163 {
164 pix = randgen.lrand32();
165 pix = (pix << 32) | randgen.lrand32();
166 }
167
168 static void
169 RandomPix(vnl_random & randgen, double & pix)
170 {
171 pix = randgen.drand64(itk::NumericTraits<double>::max());
172 }
173
174 static void
175 RandomPix(vnl_random & randgen, float & pix)
176 {
177 pix = randgen.drand64(itk::NumericTraits<float>::max());
178 }
179
180 static int
181 Remove(const char * fname)
182 {
183 return static_cast<bool>(itksys::SystemTools::RemoveFile(fname));
184 }
185
186 template <typename ImageType>
187 static void
188 SetIdentityDirection(typename ImageType::Pointer & im)
189 {
190 typename ImageType::DirectionType dir;
191 dir.SetIdentity();
192 im->SetDirection(dir);
193 }
194
195 template <typename ImageType>
196 static typename ImageType::Pointer
197 AllocateImageFromRegionAndSpacing(const typename ImageType::RegionType & region,
198 const typename ImageType::SpacingType & spacing)
199 {
200 auto rval = ImageType::New();
202 rval->SetSpacing(spacing);
203 rval->SetRegions(region);
204 rval->Allocate();
205 return rval;
206 }
207 template <typename ImageType>
208 static typename ImageType::Pointer
209 AllocateImageFromRegionAndSpacing(const typename ImageType::RegionType & region,
210 const typename ImageType::SpacingType & spacing,
211 int vecLength)
212 {
213 auto rval = ImageType::New();
214 rval->SetSpacing(spacing);
215 rval->SetRegions(region);
216 rval->SetVectorLength(vecLength);
217 rval->Allocate();
218 return rval;
219 }
220};
221} // namespace itk
222#endif // itkIOTestHelper_h
Standard exception handling object.
static ImageType::Pointer AllocateImageFromRegionAndSpacing(const typename ImageType::RegionType &region, const typename ImageType::SpacingType &spacing)
static void SetIdentityDirection(typename ImageType::Pointer &im)
static ImageType::Pointer AllocateImageFromRegionAndSpacing(const typename ImageType::RegionType &region, const typename ImageType::SpacingType &spacing, int vecLength)
static void RandomPix(vnl_random &randgen, double &pix)
static int Remove(const char *fname)
static TImage::Pointer ReadImage(const std::string &fileName, const bool zeroOrigin=false, ImageIOBase::Pointer imageio=nullptr)
static void RandomPix(vnl_random &randgen, TPixel &pix)
static void RandomPix(vnl_random &randgen, long long &pix)
static void WriteImage(typename ImageType::Pointer image, const std::string &filename, typename ImageIOType::Pointer imageio=nullptr)
static void RandomPix(vnl_random &randgen, float &pix)
static void RandomPix(vnl_random &randgen, unsigned long long &pix)
static void RandomPix(vnl_random &randgen, itk::RGBPixel< unsigned char > &pix)
Data source that reads image data from a single file.
Writes image data to a single file.
SmartPointer< Self > Pointer
static constexpr T max(const T &)
Represent Red, Green and Blue components for color images.
Definition itkRGBPixel.h:59
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....