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