]> Creatis software - clitk.git/blob - common/vvImageReader.txx
changes in license header
[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
27 #include <vtkTransform.h>
28
29 #include "clitkCommon.h"
30 #include "clitkConfiguration.h"
31 #include "vvFromITK.h"
32 //----------------------------------------------------------------------------
33 template<unsigned int VImageDimension>
34 void vvImageReader::UpdateWithDim(std::string InputPixelType)
35 {
36   if (mType == VECTORFIELD)
37     UpdateWithDimAndInputPixelType<itk::Vector<float,3>,VImageDimension>();
38   else if (InputPixelType == "short")
39     UpdateWithDimAndInputPixelType<short,VImageDimension>();
40   else if (InputPixelType == "unsigned_short")
41     UpdateWithDimAndInputPixelType<unsigned short,VImageDimension>();
42   else if (InputPixelType == "char")
43     UpdateWithDimAndInputPixelType<char,VImageDimension>();
44   else if (InputPixelType == "unsigned_char")
45     UpdateWithDimAndInputPixelType<unsigned char,VImageDimension>();
46   else if (InputPixelType == "int")
47     UpdateWithDimAndInputPixelType<int,VImageDimension>();
48   else if (InputPixelType == "unsigned_int")
49     UpdateWithDimAndInputPixelType<unsigned int,VImageDimension>();
50   else if (InputPixelType == "double")
51     UpdateWithDimAndInputPixelType<double,VImageDimension>();
52   else if (InputPixelType == "float")
53     UpdateWithDimAndInputPixelType<float,VImageDimension>();
54   else
55     std::cerr << "Error, input pixel type : " << InputPixelType << " unknown !" << std::endl;
56
57   if (CLITK_EXPERIMENTAL && mLastError.size()==0) {
58     //ReadNkiImageTransform();
59     ReadMatImageTransform();
60   }
61 }
62 //----------------------------------------------------------------------------
63
64
65 //----------------------------------------------------------------------------
66 template<class InputPixelType, unsigned int VImageDimension>
67 void vvImageReader::UpdateWithDimAndInputPixelType()
68 {
69   if (mType == MERGEDWITHTIME)   // In this case we can load the images
70     // one at the time to avoid excessive
71     // memory use
72   {
73     mImage=vvImage::New();
74
75     for (std::vector<std::string>::const_iterator i=mInputFilenames.begin(); i!=mInputFilenames.end(); i++) {
76       typedef itk::Image< InputPixelType, VImageDimension-1 > InputImageType;
77       typedef itk::ImageFileReader<InputImageType> ReaderType;
78       typename ReaderType::Pointer reader = ReaderType::New();
79       reader->ReleaseDataFlagOn();
80       reader->SetFileName(*i);
81       try {
82         mImage->AddItkImage<InputImageType>(reader->GetOutput());
83       } catch ( itk::ExceptionObject & err ) {
84         std::cerr << "Error while reading " << mInputFilenames[0].c_str()
85                   << " " << err << std::endl;
86         std::stringstream error;
87         error << err;
88         mLastError = error.str();
89         return;
90       }
91     }
92   } else if (mType == SLICED) {
93     mImage=vvImage::New();
94     typedef itk::Image< InputPixelType, VImageDimension > InputImageType;
95     typedef itk::ImageFileReader<InputImageType> ReaderType;
96     typename ReaderType::Pointer reader = ReaderType::New();
97     reader->SetFileName(mInputFilenames[0]);
98     reader->UpdateOutputInformation();
99
100     typedef itk::Image< InputPixelType, VImageDimension-1 > SlicedImageType;
101     typedef itk::ExtractImageFilter<InputImageType, SlicedImageType> FilterType;
102
103     typename InputImageType::RegionType inputRegion = reader->GetOutput()->GetLargestPossibleRegion();
104     typename InputImageType::SizeType inputSize = inputRegion.GetSize();
105     typename InputImageType::IndexType start = inputRegion.GetIndex();
106     typename InputImageType::SizeType extractedRegionSize = inputSize;
107     typename InputImageType::RegionType extractedRegion;
108     extractedRegionSize[VImageDimension - 1] = 0;
109     extractedRegion.SetSize(extractedRegionSize);
110     start[VImageDimension - 1] = mSlice;
111     extractedRegion.SetIndex(start);
112
113     typename FilterType::Pointer filter = FilterType::New();
114     filter->SetExtractionRegion(extractedRegion);
115     filter->SetInput(reader->GetOutput());
116     filter->ReleaseDataFlagOn();
117     try {
118       mImage->AddItkImage<SlicedImageType>(filter->GetOutput());
119     }
120     catch ( itk::ExceptionObject & err ) {
121       std::cerr << "Error while slicing " << mInputFilenames[0].c_str()
122                 << "(slice #" << mSlice << ") " << err << std::endl;
123       return;
124     }
125   } else {
126     if (mInputFilenames.size() > 1) {
127       typedef itk::Image< InputPixelType, VImageDimension > InputImageType;
128       typedef itk::ImageSeriesReader<InputImageType> ReaderType;
129       typename ReaderType::Pointer reader = ReaderType::New();
130       reader->SetFileNames(mInputFilenames);
131       reader->ReleaseDataFlagOn();
132
133       try {
134         if (mType == IMAGEWITHTIME)
135           mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput(),true);
136         else
137           mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput());
138       } catch ( itk::ExceptionObject & err ) {
139         std::cerr << "Error while reading image series:" << err << std::endl;
140         std::stringstream error;
141         error << err;
142         mLastError = error.str();
143         return;
144       }
145     } else {
146       typedef itk::Image< InputPixelType, VImageDimension > InputImageType;
147       typedef itk::ImageFileReader<InputImageType> ReaderType;
148       typename ReaderType::Pointer reader = ReaderType::New();
149       reader->SetFileName(mInputFilenames[0]);
150       reader->ReleaseDataFlagOn();
151
152       try {
153         if (mType == IMAGEWITHTIME)
154           mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput(),true);
155         else
156           mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput());
157       } catch ( itk::ExceptionObject & err ) {
158         std::cerr << "Error while reading " << mInputFilenames[0].c_str()
159                   << " " << err << std::endl;
160         std::stringstream error;
161         error << err;
162         mLastError = error.str();
163         return;
164       }
165     }
166   }
167 }
168 //----------------------------------------------------------------------------
169
170
171 #endif /* end #define vvImageReader_TXX */
172