]> Creatis software - clitk.git/blob - common/vvImageReader.txx
GateAsciiImageIO is now cross-platform using itksys::RegularExpression
[clitk.git] / common / vvImageReader.txx
1 /*=========================================================================
2   Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
3
4   Authors belong to:
5   - University of LYON              http://www.universite-lyon.fr/
6   - Léon Bérard cancer center       http://www.centreleonberard.fr
7   - CREATIS CNRS laboratory         http://www.creatis.insa-lyon.fr
8
9   This software is distributed WITHOUT ANY WARRANTY; without even
10   the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11   PURPOSE.  See the copyright notices for more information.
12
13   It is distributed under dual licence
14
15   - BSD        See included LICENSE.txt file
16   - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17   ===========================================================================**/
18
19 #ifndef VVIMAGEREADER_TXX
20 #define VVIMAGEREADER_TXX
21
22 #include <string>
23 #include <itkImageFileReader.h>
24 #include <itkImageSeriesReader.h>
25 #include <itkImageToVTKImageFilter.h>
26 #include <itkAnalyzeImageIO.h>
27
28 #include <vtkTransform.h>
29
30 #include "clitkCommon.h"
31 #include "clitkConfiguration.h"
32 #include "vvFromITK.h"
33 //----------------------------------------------------------------------------
34 template<unsigned int VImageDimension>
35 void vvImageReader::UpdateWithDim(std::string InputPixelType)
36 {
37   if (mType == VECTORFIELD || mType == VECTORFIELDWITHTIME)
38     UpdateWithDimAndInputPixelType<itk::Vector<float,3>,VImageDimension>();
39   else if (InputPixelType == "short")
40     UpdateWithDimAndInputPixelType<short,VImageDimension>();
41   else if (InputPixelType == "unsigned_short")
42     UpdateWithDimAndInputPixelType<unsigned short,VImageDimension>();
43   else if (InputPixelType == "char")
44     UpdateWithDimAndInputPixelType<char,VImageDimension>();
45   else if (InputPixelType == "unsigned_char")
46     UpdateWithDimAndInputPixelType<unsigned char,VImageDimension>();
47   else if (InputPixelType == "int")
48     UpdateWithDimAndInputPixelType<int,VImageDimension>();
49   else if (InputPixelType == "unsigned_int")
50     UpdateWithDimAndInputPixelType<unsigned int,VImageDimension>();
51   else if (InputPixelType == "double")
52     UpdateWithDimAndInputPixelType<double,VImageDimension>();
53   else if (InputPixelType == "float")
54     UpdateWithDimAndInputPixelType<float,VImageDimension>();
55   else
56     std::cerr << "Error, input pixel type : " << InputPixelType << " unknown !" << std::endl;
57
58   if (CLITK_EXPERIMENTAL && mLastError.size()==0) {
59     //ReadNkiImageTransform();
60     ReadMatImageTransform();
61   }
62 }
63 //----------------------------------------------------------------------------
64
65
66 //----------------------------------------------------------------------------
67 template<class InputPixelType, unsigned int VImageDimension>
68 void vvImageReader::UpdateWithDimAndInputPixelType()
69 {
70   itk::AnalyzeImageIO *analyzeImageIO = NULL;
71
72   if (mType == MERGEDWITHTIME)   // In this case we can load the images
73     // one at the time to avoid excessive
74     // memory use
75   {
76     mImage=vvImage::New();
77
78     for (std::vector<std::string>::const_iterator i=mInputFilenames.begin(); i!=mInputFilenames.end(); i++) {
79       typedef itk::Image< InputPixelType, VImageDimension-1 > InputImageType;
80       typedef itk::ImageFileReader<InputImageType> ReaderType;
81       typename ReaderType::Pointer reader = ReaderType::New();
82       reader->ReleaseDataFlagOn();
83       reader->SetFileName(*i);
84       try {
85         mImage->AddItkImage<InputImageType>(reader->GetOutput());
86       } catch ( itk::ExceptionObject & err ) {
87         std::cerr << "Error while reading " << mInputFilenames[0].c_str()
88                   << " " << err << std::endl;
89         std::stringstream error;
90         error << err;
91         mLastError = error.str();
92         return;
93       }
94       analyzeImageIO = dynamic_cast<itk::AnalyzeImageIO*>( reader->GetImageIO() );
95     }
96   } else if (mType == SLICED) {
97     mImage=vvImage::New();
98     typedef itk::Image< InputPixelType, VImageDimension > InputImageType;
99     typedef itk::ImageFileReader<InputImageType> ReaderType;
100     typename ReaderType::Pointer reader = ReaderType::New();
101     reader->SetFileName(mInputFilenames[0]);
102     reader->UpdateOutputInformation();
103
104     typedef itk::Image< InputPixelType, VImageDimension-1 > SlicedImageType;
105     typedef itk::ExtractImageFilter<InputImageType, SlicedImageType> FilterType;
106
107     typename InputImageType::RegionType inputRegion = reader->GetOutput()->GetLargestPossibleRegion();
108     typename InputImageType::SizeType inputSize = inputRegion.GetSize();
109     typename InputImageType::IndexType start = inputRegion.GetIndex();
110     typename InputImageType::SizeType extractedRegionSize = inputSize;
111     typename InputImageType::RegionType extractedRegion;
112     extractedRegionSize[VImageDimension - 1] = 0;
113     extractedRegion.SetSize(extractedRegionSize);
114     start[VImageDimension - 1] = mSlice;
115     extractedRegion.SetIndex(start);
116
117     typename FilterType::Pointer filter = FilterType::New();
118     filter->SetExtractionRegion(extractedRegion);
119     filter->SetInput(reader->GetOutput());
120     filter->ReleaseDataFlagOn();
121 #if ITK_VERSION_MAJOR == 4
122     filter->SetDirectionCollapseToSubmatrix();
123 #endif
124     try {
125       mImage->AddItkImage<SlicedImageType>(filter->GetOutput());
126     } catch ( itk::ExceptionObject & err ) {
127       std::cerr << "Error while slicing " << mInputFilenames[0].c_str()
128                 << "(slice #" << mSlice << ") " << err << std::endl;
129       return;
130     }
131     analyzeImageIO = dynamic_cast<itk::AnalyzeImageIO*>( reader->GetImageIO() );
132   } else {
133     if (mInputFilenames.size() > 1) {
134       typedef itk::Image< InputPixelType, VImageDimension > InputImageType;
135       typedef itk::ImageSeriesReader<InputImageType> ReaderType;
136       typename ReaderType::Pointer reader = ReaderType::New();
137       reader->SetFileNames(mInputFilenames);
138       reader->ReleaseDataFlagOn();
139
140       try {
141         if (mType == IMAGEWITHTIME)
142         {
143           std::cerr << "We should never come here:" << std::endl
144             << "  Calling vvImageReader with multiple images and IMAGEWITHTIME is undefined." << std::endl
145             << "  You are probably looking for MERGEDWITHTIME Type." << std::endl;
146           return;
147         }
148         else
149           mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput());
150       } catch ( itk::ExceptionObject & err ) {
151         std::cerr << "Error while reading image series:" << err << std::endl;
152         std::stringstream error;
153         error << err;
154         mLastError = error.str();
155         return;
156       }
157     } else {
158       typedef itk::Image< InputPixelType, VImageDimension > InputImageType;
159       typedef itk::ImageFileReader<InputImageType> ReaderType;
160       typename ReaderType::Pointer reader = ReaderType::New();
161       reader->SetFileName(mInputFilenames[0]);
162       reader->ReleaseDataFlagOn();
163
164       try {
165         if (mType == IMAGEWITHTIME || mType == VECTORFIELDWITHTIME)
166           mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput(),true);
167         else
168           mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput());
169       } catch ( itk::ExceptionObject & err ) {
170         std::cerr << "Error while reading " << mInputFilenames[0].c_str()
171                   << " " << err << std::endl;
172         std::stringstream error;
173         error << err;
174         mLastError = error.str();
175         return;
176       }
177       analyzeImageIO = dynamic_cast<itk::AnalyzeImageIO*>( reader->GetImageIO() );
178     }
179   }
180
181   // For unknown analyze orientations, we set identity
182   if(analyzeImageIO) {
183     const double m[16] = {1.,0.,0.,0.,
184                           0.,0.,1.,0.,
185                           0.,-1.,0.,0.,
186                           0.,0.,0.,1.};
187     int i;
188     for(i=0; i<16 && m[i]==mImage->GetTransform()->GetMatrix()->GetElement(i%4, i/4); i++);
189     if(i==16) {
190       itkWarningMacro(<< "Analyze image file format detected with unknown orientation. "
191                       << "Forcing identity orientation, use other file format if not ok.");
192       mImage->GetTransform()->Identity();
193     }
194   }
195 }
196 //----------------------------------------------------------------------------
197
198
199 #endif /* end #define vvImageReader_TXX */
200