]> Creatis software - clitk.git/blob - vv/vvImageReader.txx
added the new headers
[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 #ifndef vvImageReader_TXX
19 #define vvImageReader_TXX
20 #include<string>
21
22 #include <itkImageFileReader.h>
23 #include <itkImageSeriesReader.h>
24
25 #include "clitkCommon.h"
26 #include "itkImageToVTKImageFilter.h"
27 #include "vvFromITK.h"
28 #include "vvConstants.h"
29
30 template<unsigned int VImageDimension>
31 void vvImageReader::UpdateWithDim(std::string InputPixelType)
32 {
33     if (mType == VECTORFIELD)
34         UpdateWithDimAndInputPixelType<itk::Vector<float,3>,VImageDimension>();
35     else if (InputPixelType == "short")
36         UpdateWithDimAndInputPixelType<short,VImageDimension>();
37     else if (InputPixelType == "unsigned_short")
38         UpdateWithDimAndInputPixelType<unsigned short,VImageDimension>();
39     else if (InputPixelType == "char")
40         UpdateWithDimAndInputPixelType<char,VImageDimension>();
41     else if (InputPixelType == "unsigned_char")
42         UpdateWithDimAndInputPixelType<unsigned char,VImageDimension>();
43     else if (InputPixelType == "int")
44         UpdateWithDimAndInputPixelType<int,VImageDimension>();
45     else if (InputPixelType == "double")
46         UpdateWithDimAndInputPixelType<double,VImageDimension>();
47     else if (InputPixelType == "float")
48         UpdateWithDimAndInputPixelType<float,VImageDimension>();
49     else
50         std::cerr << "Error, input pixel type : " << InputPixelType << " unknown !" << std::endl;
51 }
52 //====================================================================
53
54 template<class InputPixelType, unsigned int VImageDimension>
55 void vvImageReader::UpdateWithDimAndInputPixelType()
56 {
57     if (mType == MERGEDWITHTIME)   // In this case we can load the images
58                                    // one at the time to avoid excessive
59                                    // memory use
60     {
61         typedef itk::Image< InputPixelType, VImageDimension-1 > InputImageType;
62         typedef itk::ImageFileReader<InputImageType> ReaderType;
63         typename ReaderType::Pointer reader = ReaderType::New();
64         typedef itk::ImageToVTKImageFilter <InputImageType> ConnectorType;
65         typename ConnectorType::Pointer connector = ConnectorType::New();
66         connector->SetInput(reader->GetOutput());
67         mImage=vvImage::New();
68         for (std::vector<std::string>::const_iterator i=mInputFilenames.begin();i!=mInputFilenames.end();i++)
69         {
70             std::cout << (*i) << std::endl;
71             reader->SetFileName(*i);
72             try {
73                 reader->Update();
74             }
75             catch ( itk::ExceptionObject & err ) {
76                 std::cerr << "Error while reading " << mInputFilenames[0].c_str()
77                     << " " << err << std::endl;
78                 std::stringstream error;
79                 error << err;
80                 mLastError = error.str();
81                 return;
82             }
83             try {
84                 connector->Update();
85             }
86             catch ( itk::ExceptionObject & err ) {
87                 std::cerr << "Error while setting vvImage from ITK (MERGEDWITHTIME)"
88                     << " " << err << std::endl;
89             }
90             vtkImageData *image = vtkImageData::New();
91             image->DeepCopy(connector->GetOutput());
92             mImage->AddImage(image);
93         }
94     }
95     else
96     {
97         if (mInputFilenames.size() > 1)
98         {
99             typedef itk::Image< InputPixelType, VImageDimension > InputImageType;
100             typedef itk::ImageSeriesReader<InputImageType> ReaderType;
101             typename ReaderType::Pointer reader = ReaderType::New();
102             for (std::vector<std::string>::const_iterator i=mInputFilenames.begin();i!=mInputFilenames.end();i++)
103                 std::cout << (*i) << std::endl;
104             reader->SetFileNames(mInputFilenames);
105             //if (mUseAnObserver) {
106                 //reader->AddObserver(itk::ProgressEvent(), mObserver);
107             //}
108             try {
109                 reader->Update();
110             }
111             catch ( itk::ExceptionObject & err ) {
112                 std::cerr << "Error while reading image series:" << err << std::endl;
113                 std::stringstream error;
114                 error << err;
115                 mLastError = error.str();
116                 return;
117             }
118             if (mType == IMAGEWITHTIME)
119                 mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput(),true);
120             else
121                 mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput());
122         }
123         else
124         {
125             typedef itk::Image< InputPixelType, VImageDimension > InputImageType;
126             typedef itk::ImageFileReader<InputImageType> ReaderType;
127             typename ReaderType::Pointer reader = ReaderType::New();
128             reader->SetFileName(mInputFilenames[0]);
129             //if (mUseAnObserver) {
130                 //reader->AddObserver(itk::ProgressEvent(), mObserver);
131             //}
132             try {
133                 reader->Update();
134             }
135             catch ( itk::ExceptionObject & err ) {
136                 std::cerr << "Error while reading " << mInputFilenames[0].c_str()
137                     << " " << err << std::endl;
138                 std::stringstream error;
139                 error << err;
140                 mLastError = error.str();
141                 return;
142             }
143             if (mType == IMAGEWITHTIME)
144                 mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput(),true);
145             else
146                 mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput());
147         }
148     }
149 }
150 //====================================================================
151
152
153 #endif /* end #define vvImageReader_TXX */
154