ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkIOTestHelper.h
Go to the documentation of this file.
00001 /*=========================================================================
00002  *
00003  *  Copyright Insight Software Consortium
00004  *
00005  *  Licensed under the Apache License, Version 2.0 (the "License");
00006  *  you may not use this file except in compliance with the License.
00007  *  You may obtain a copy of the License at
00008  *
00009  *         http://www.apache.org/licenses/LICENSE-2.0.txt
00010  *
00011  *  Unless required by applicable law or agreed to in writing, software
00012  *  distributed under the License is distributed on an "AS IS" BASIS,
00013  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  *  See the License for the specific language governing permissions and
00015  *  limitations under the License.
00016  *
00017  *=========================================================================*/
00018 #ifndef __itkIOTestHelper_h
00019 #define __itkIOTestHelper_h
00020 #include <string>
00021 
00022 #include "itksys/SystemTools.hxx"
00023 #include "itkImageFileWriter.h"
00024 #include "itkImageFileReader.h"
00025 #include "vnl/vnl_random.h"
00026 namespace itk
00027 {
00028 class IOTestHelper
00029 {
00030 public:
00031   template <typename TImage>
00032   static typename TImage::Pointer ReadImage( const std::string &fileName,
00033                                              const bool zeroOrigin = false )
00034     {
00035       typedef itk::ImageFileReader<TImage> ReaderType;
00036 
00037       typename ReaderType::Pointer reader = ReaderType::New();
00038       {
00039       reader->SetFileName( fileName.c_str() );
00040       try
00041         {
00042         reader->Update();
00043         }
00044       catch( itk::ExceptionObject & err )
00045         {
00046         std::cout << "Caught an exception: " << std::endl;
00047         std::cout << err << " " << __FILE__ << " " << __LINE__ << std::endl;
00048         throw err;
00049         }
00050       catch(...)
00051         {
00052         std::cout << "Error while reading in image for patient " << fileName << std::endl;
00053         throw;
00054         }
00055       }
00056       typename TImage::Pointer image = reader->GetOutput();
00057       if(zeroOrigin)
00058         {
00059         double origin[TImage::ImageDimension];
00060         for(unsigned int i = 0; i < TImage::ImageDimension; i++)
00061           {
00062           origin[i]=0;
00063           }
00064         image->SetOrigin(origin);
00065         }
00066       return image;
00067     }
00068 
00069   template <class ImageType,class ImageIOType>
00070   static void
00071   WriteImage(typename ImageType::Pointer &image ,
00072              const std::string &filename)
00073     {
00074 
00075       typedef itk::ImageFileWriter< ImageType > WriterType;
00076       typename  WriterType::Pointer writer = WriterType::New();
00077 
00078       typename ImageIOType::Pointer imageio(ImageIOType::New());
00079 
00080       writer->SetImageIO(imageio);
00081 
00082       writer->SetFileName(filename.c_str());
00083 
00084       writer->SetInput(image);
00085 
00086       try
00087         {
00088         writer->Update();
00089         }
00090       catch (itk::ExceptionObject &err) {
00091       std::cerr << "Exception Object caught: " << std::endl
00092                 << err << std::endl;
00093       throw;
00094       }
00095     }
00096 
00097 //
00098 // generate random pixels of various types
00099   static void
00100   RandomPix(vnl_random &randgen,itk::RGBPixel<unsigned char> &pix)
00101     {
00102       for(unsigned int i = 0; i < 3; i++)
00103         {
00104         pix[i] = randgen.lrand32(itk::NumericTraits<unsigned char>::max());
00105         }
00106     }
00107 
00108   template <typename TPixel>
00109   static void
00110   RandomPix(vnl_random &randgen, TPixel &pix)
00111     {
00112       pix = randgen.lrand32(itk::NumericTraits<TPixel>::max());
00113     }
00114 
00115   static void
00116   RandomPix(vnl_random &randgen, double &pix)
00117     {
00118       pix = randgen.drand64(itk::NumericTraits<double>::max());
00119     }
00120 
00121   static void
00122   RandomPix(vnl_random &randgen, float &pix)
00123     {
00124       pix = randgen.drand64(itk::NumericTraits<float>::max());
00125     }
00126 
00127   static int Remove(const char *fname)
00128     {
00129       return itksys::SystemTools::RemoveFile(fname);
00130     }
00131 
00132   template <class ImageType>
00133   static void SetIdentityDirection(typename ImageType::Pointer &im)
00134     {
00135       typename ImageType::DirectionType dir;
00136       dir.SetIdentity();
00137       im->SetDirection(dir);
00138     }
00139 
00140   template <class ImageType>
00141   static typename ImageType::Pointer
00142   AllocateImageFromRegionAndSpacing(const typename ImageType::RegionType &region,
00143                                     const typename ImageType::SpacingType &spacing)
00144     {
00145       typename ImageType::Pointer rval = ImageType::New();
00146       SetIdentityDirection<ImageType>(rval);
00147       rval->SetSpacing(spacing);
00148       rval->SetRegions(region);
00149       rval->Allocate();
00150       return rval;
00151     }
00152   template <class ImageType>
00153   static typename ImageType::Pointer
00154   AllocateImageFromRegionAndSpacing(const typename ImageType::RegionType &region,
00155                                     const typename ImageType::SpacingType &spacing,
00156                                     int vecLength)
00157     {
00158       typename ImageType::Pointer rval = ImageType::New();
00159       rval->SetSpacing(spacing);
00160       rval->SetRegions(region);
00161       rval->SetVectorLength(vecLength);
00162       rval->Allocate();
00163       return rval;
00164     }
00165 };
00166 }
00167 #endif // __itkIOTestHelper_h
00168