]> Creatis software - clitk.git/blob - vv/vvImageReader.txx
remove antique RCS headers
[clitk.git] / vv / vvImageReader.txx
1 /*=========================================================================
2
3 Program:   vv
4 Language:  C++
5 Author :   Pierre Seroul (pierre.seroul@gmail.com)
6
7 Copyright (C) 2008
8 Léon Bérard cancer center http://oncora1.lyon.fnclcc.fr
9 CREATIS-LRMN http://www.creatis.insa-lyon.fr
10
11 This program is free software: you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation, version 3 of the License.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
23 =========================================================================*/
24 #ifndef vvImageReader_TXX
25 #define vvImageReader_TXX
26 #include<string>
27
28 #include <itkImageFileReader.h>
29 #include <itkImageSeriesReader.h>
30
31 #include "clitkCommon.h"
32 #include "itkImageToVTKImageFilter.h"
33 #include "vvFromITK.h"
34 #include "vvConstants.h"
35
36 template<unsigned int VImageDimension>
37 void vvImageReader::UpdateWithDim(std::string InputPixelType)
38 {
39     if (mType == VECTORFIELD)
40         UpdateWithDimAndInputPixelType<itk::Vector<float,3>,VImageDimension>();
41     else if (InputPixelType == "short")
42         UpdateWithDimAndInputPixelType<short,VImageDimension>();
43     else if (InputPixelType == "unsigned_short")
44         UpdateWithDimAndInputPixelType<unsigned short,VImageDimension>();
45     else if (InputPixelType == "char")
46         UpdateWithDimAndInputPixelType<char,VImageDimension>();
47     else if (InputPixelType == "unsigned_char")
48         UpdateWithDimAndInputPixelType<unsigned char,VImageDimension>();
49     else if (InputPixelType == "int")
50         UpdateWithDimAndInputPixelType<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 //====================================================================
59
60 template<class InputPixelType, unsigned int VImageDimension>
61 void vvImageReader::UpdateWithDimAndInputPixelType()
62 {
63     if (mType == MERGEDWITHTIME)   // In this case we can load the images
64                                    // one at the time to avoid excessive
65                                    // memory use
66     {
67         typedef itk::Image< InputPixelType, VImageDimension-1 > InputImageType;
68         typedef itk::ImageFileReader<InputImageType> ReaderType;
69         typename ReaderType::Pointer reader = ReaderType::New();
70         typedef itk::ImageToVTKImageFilter <InputImageType> ConnectorType;
71         typename ConnectorType::Pointer connector = ConnectorType::New();
72         connector->SetInput(reader->GetOutput());
73         mImage=vvImage::New();
74         for (std::vector<std::string>::const_iterator i=mInputFilenames.begin();i!=mInputFilenames.end();i++)
75         {
76             std::cout << (*i) << std::endl;
77             reader->SetFileName(*i);
78             try {
79                 reader->Update();
80             }
81             catch ( itk::ExceptionObject & err ) {
82                 std::cerr << "Error while reading " << mInputFilenames[0].c_str()
83                     << " " << err << std::endl;
84                 std::stringstream error;
85                 error << err;
86                 mLastError = error.str();
87                 return;
88             }
89             try {
90                 connector->Update();
91             }
92             catch ( itk::ExceptionObject & err ) {
93                 std::cerr << "Error while setting vvImage from ITK (MERGEDWITHTIME)"
94                     << " " << err << std::endl;
95             }
96             vtkImageData *image = vtkImageData::New();
97             image->DeepCopy(connector->GetOutput());
98             mImage->AddImage(image);
99         }
100     }
101     else
102     {
103         if (mInputFilenames.size() > 1)
104         {
105             typedef itk::Image< InputPixelType, VImageDimension > InputImageType;
106             typedef itk::ImageSeriesReader<InputImageType> ReaderType;
107             typename ReaderType::Pointer reader = ReaderType::New();
108             for (std::vector<std::string>::const_iterator i=mInputFilenames.begin();i!=mInputFilenames.end();i++)
109                 std::cout << (*i) << std::endl;
110             reader->SetFileNames(mInputFilenames);
111             //if (mUseAnObserver) {
112                 //reader->AddObserver(itk::ProgressEvent(), mObserver);
113             //}
114             try {
115                 reader->Update();
116             }
117             catch ( itk::ExceptionObject & err ) {
118                 std::cerr << "Error while reading image series:" << err << std::endl;
119                 std::stringstream error;
120                 error << err;
121                 mLastError = error.str();
122                 return;
123             }
124             if (mType == IMAGEWITHTIME)
125                 mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput(),true);
126             else
127                 mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput());
128         }
129         else
130         {
131             typedef itk::Image< InputPixelType, VImageDimension > InputImageType;
132             typedef itk::ImageFileReader<InputImageType> ReaderType;
133             typename ReaderType::Pointer reader = ReaderType::New();
134             reader->SetFileName(mInputFilenames[0]);
135             //if (mUseAnObserver) {
136                 //reader->AddObserver(itk::ProgressEvent(), mObserver);
137             //}
138             try {
139                 reader->Update();
140             }
141             catch ( itk::ExceptionObject & err ) {
142                 std::cerr << "Error while reading " << mInputFilenames[0].c_str()
143                     << " " << err << std::endl;
144                 std::stringstream error;
145                 error << err;
146                 mLastError = error.str();
147                 return;
148             }
149             if (mType == IMAGEWITHTIME)
150                 mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput(),true);
151             else
152                 mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput());
153         }
154     }
155 }
156 //====================================================================
157
158
159 #endif /* end #define vvImageReader_TXX */
160