]> Creatis software - clitk.git/blob - vv/vvImageReader.cxx
- contours show/hide
[clitk.git] / vv / vvImageReader.cxx
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_CXX
19 #define VVIMAGEREADER_CXX
20
21 #include <QApplication>
22 #include <itkImageFileReader.h>
23 #include "vvImageReader.h"
24 #include "vvImageReader.txx"
25
26 //------------------------------------------------------------------------------
27 vvImageReader::vvImageReader()
28 {
29   mImage = NULL;
30   mInputFilenames.resize(0);
31   mLastError = "";
32   mType = UNDEFINEDIMAGETYPE;
33 }
34 //------------------------------------------------------------------------------
35
36
37 //------------------------------------------------------------------------------
38 vvImageReader::~vvImageReader() { }
39 //------------------------------------------------------------------------------
40
41
42 //------------------------------------------------------------------------------
43 void vvImageReader::Update()
44 {
45   Update(mType);
46 }
47 //------------------------------------------------------------------------------
48
49
50 //------------------------------------------------------------------------------
51 void vvImageReader::Update(LoadedImageType type)
52 {
53   itk::ImageIOBase::Pointer reader = itk::ImageIOFactory::CreateImageIO(mInputFilenames[0].c_str(), itk::ImageIOFactory::ReadMode);
54   if (!reader) {
55     mLastError="Unable to read file.";
56   } else {
57     reader->SetFileName(mInputFilenames[0]);
58     reader->ReadImageInformation();
59     if (mInputFilenames.size() > 1)
60       Update(reader->GetNumberOfDimensions()+1,reader->GetComponentTypeAsString(reader->GetComponentType()),type);
61     else
62       Update(reader->GetNumberOfDimensions(),reader->GetComponentTypeAsString(reader->GetComponentType()),type);
63   }
64 }
65 //------------------------------------------------------------------------------
66
67
68 //------------------------------------------------------------------------------
69 void vvImageReader::Update(int dim,std::string inputPixelType, LoadedImageType type)
70 {
71   //CALL_FOR_ALL_DIMS(dim,UpdateWithDim,inputPixelType);
72   mType = type;
73   mDim = dim;
74   mInputPixelType=inputPixelType;
75   this->start(); //Start heavy read operation in a separate thread
76   while (this->isRunning()) {
77     qApp->processEvents();
78     this->wait(50);
79   }
80 }
81 //------------------------------------------------------------------------------
82
83
84 //------------------------------------------------------------------------------
85 void vvImageReader::run()
86 {
87   switch(mDim)     {
88   case 2:
89     UpdateWithDim<2>(mInputPixelType);
90     break;;
91   case 3:
92     UpdateWithDim<3>(mInputPixelType);
93     break;;
94   case 4:
95     UpdateWithDim<4>(mInputPixelType);
96     break;;
97   default:
98     std::cerr << "dimension unknown in Update ! " << std::endl;
99   }
100 }
101 //------------------------------------------------------------------------------
102
103
104 //------------------------------------------------------------------------------
105 void vvImageReader::SetInputFilename(const std::string & filename)
106 {
107   mInputFilenames.resize(0);
108   mInputFilenames.push_back(filename);
109 }
110 //------------------------------------------------------------------------------
111
112
113 //------------------------------------------------------------------------------
114 void vvImageReader::SetInputFilenames(const std::vector<std::string> & filenames)
115 {
116   mInputFilenames = filenames;
117 }
118 //------------------------------------------------------------------------------
119
120
121 //------------------------------------------------------------------------------
122 //Read transformation in NKI format (Xdr, transposed, cm)
123 void vvImageReader::ReadNkiImageTransform()
124 {
125   bool bRead=true;
126   typedef itk::ImageFileReader< itk::Image< double, 2 > > MatrixReaderType;
127   MatrixReaderType::Pointer readerTransfo = MatrixReaderType::New();
128   readerTransfo->SetFileName(mInputFilenames[0]+".MACHINEORIENTATION");
129   try {
130     readerTransfo->Update();
131   } catch( itk::ExceptionObject & err ) {
132     bRead=false;
133   }
134
135   if (bRead) {
136     double mat[16];
137
138     //Transpose matrix (NKI format)
139     for(int j=0; j<4; j++)
140       for(int i=0; i<4; i++)
141         mat[4*j+i]=readerTransfo->GetOutput()->GetBufferPointer()[4*i+j];
142
143     //From cm to mm
144     for(int i=0; i<3; i++)
145       mat[4*i+3]*=10;
146
147     //Set Transformation
148     vtkSmartPointer<vtkTransform> pt = vtkSmartPointer<vtkTransform>::New();
149     pt->SetMatrix( mat );
150     pt->Inverse();
151     mImage->SetTransform( pt );
152   }
153 }
154 //------------------------------------------------------------------------------
155 #endif
156