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