]> Creatis software - bbtk.git/blob - packages/gdcmvtk/src/bbgdcmvtkGetXCoherentInfoGdcmReader.cxx
avoid warnings
[bbtk.git] / packages / gdcmvtk / src / bbgdcmvtkGetXCoherentInfoGdcmReader.cxx
1 #include "bbgdcmvtkGetXCoherentInfoGdcmReader.h"
2 #include "bbgdcmvtkPackage.h"
3
4 #include "gdcmFile.h"
5 #include "vtkImageData.h"
6 #include <vtkIndent.h>
7 #if defined USE_GDCM
8 #include "gdcmFileHelper.h"
9 #endif
10 #if defined USE_GDCM2
11         #include <gdcmReader.h>
12         #include <gdcmImageHelper.h>
13         #include <gdcmIPPSorter.h>
14         #include <vtkGDCMImageReader.h>
15         #include "vtkStringArray.h"
16 #endif
17
18 namespace bbgdcmvtk
19 {
20
21 BBTK_ADD_BLACK_BOX_TO_PACKAGE(gdcmvtk,GetXCoherentInfoGdcmReader)
22 BBTK_BLACK_BOX_IMPLEMENTATION(GetXCoherentInfoGdcmReader,bbtk::AtomicBlackBox);
23 #if defined USE_GDCM
24 void GetXCoherentInfoGdcmReader::Process()
25 {
26  // Read the *first* image file (a SET of file names is given as input) 
27    f = GDCM_NAME_SPACE::File::New();
28    f->SetFileName( bbGetInputIn()[0] );
29    bool res = f->Load();  
30    if ( !res )
31    {
32       f->Delete();
33       bbSetOutputOut(0);
34       return;
35    }
36  // Get info from the first image file
37    int i;
38    std::vector<double> v_iop;
39    float iop[6];
40    f->GetImageOrientationPatient(iop);
41    
42    for(i=0; i< 6; i++)
43       v_iop.push_back(iop[i]);
44    bbSetOutputIOP(v_iop );
45  
46    std::vector<double> v_ipp;
47    float ipp[3];
48    f->GetImagePositionPatient(ipp);
49    
50    for(i=0; i< 3; i++)
51       v_ipp.push_back(ipp[i]);
52    bbSetOutputIPP(v_ipp );
53
54 // Add *all the files* to the SerieHelper
55    sh = GDCM_NAME_SPACE::SerieHelper::New();
56    std::vector<std::string> gii = bbGetInputIn();
57    
58    for(std::vector<std::string>::iterator it = gii.begin();
59                                           it != gii.end();
60                                         ++it)
61    {
62       sh->AddFileName(*it);
63    }
64
65    GDCM_NAME_SPACE::FileList::const_iterator it;
66    GDCM_NAME_SPACE::FileList *l;
67
68    // Should only contain one!
69    l = sh->GetFirstSingleSerieUIDFileSet();
70
71    int nbFiles;
72    double zspacing = 0.;
73    nbFiles = l->size() ;
74    sh->OrderFileList(l); // this one should compute the *actual* Z Spacing!
75    zspacing = sh->GetZSpacing();
76    std::vector<double> v_pixelspacing;
77    v_pixelspacing.push_back( f->GetXSpacing() );
78    v_pixelspacing.push_back( f->GetYSpacing() );
79    v_pixelspacing.push_back( zspacing );
80 //   if (f->GetZSize() != 1) {
81 //      v_pixelspacing.push_back(f->GetZSpacing());
82 //   }
83    bbSetOutputPixelSpacing(v_pixelspacing);
84
85    if (reader!=NULL)
86    { 
87       reader->Delete();
88       reader=NULL;
89    }
90    reader = vtkGdcmReader::New();
91    //reader->SetFileName( bbGetInputIn().c_str() );
92    reader->SetCoherentFileList(l);
93    reader->Update();
94    reader->GetOutput();
95
96    bbSetOutputOut( reader->GetOutput() );
97 }
98 #endif
99 // endif USE_GDCM
100
101 #if defined USE_GDCM2
102 void GetXCoherentInfoGdcmReader::Process()
103 {
104  // Read the *first* image file (a SET of file names is given as input) 
105         gdcm::Reader *read = new gdcm::Reader();
106         reader->SetFileName( bbGetInputIn()[0].c_str());
107   
108         bool res = read->Read();  
109    if ( !res )
110    {
111       delete read;
112       bbSetOutputOut(0);
113       return;
114    }
115     
116  // Get info from the first image file
117    const gdcm::File &f = read->GetFile();
118    int i;
119    std::vector<double> v_iop;
120    const gdcm::DataElement &deIop = f.GetDataSet().GetDataElement(gdcm::Tag(0x0020, 0x0037));
121    std::stringstream ss;
122    deIop.GetValue().Print(ss);
123    gdcm::Element<gdcm::VR::DS,gdcm::VM::VM6> iop;
124    iop.Read( ss );
125    for(i=0; i< 6; i++)
126       v_iop.push_back((float)(iop[i]));
127    bbSetOutputIOP(v_iop );
128  
129   std::vector<double> v_ipp;
130    const gdcm::DataElement &deIpp = f.GetDataSet().GetDataElement(gdcm::Tag((0x0020,0x0032)));
131    deIpp.GetValue().Print(ss);
132    gdcm::Element<gdcm::VR::DS,gdcm::VM::VM3> ipp;
133    ipp.Read( ss );
134           for(i=0; i< 3; i++)
135                 v_ipp.push_back((float)(ipp[i]));
136    bbSetOutputIPP(v_ipp );
137
138 // Add *all the files* to the IPPsorter
139      gdcm::IPPSorter s;
140          s.SetComputeZSpacing( true );
141      s.SetZSpacingTolerance( 1e-3 );
142      std::vector<std::string> gii = bbGetInputIn();
143      s.Sort(gii);
144
145          //l = sh->GetFirstSingleSerieUIDFileSet();
146          // no Test if we have multiple series
147          //s.GetFilenames();
148          //int nbFiles;
149
150         double zspacing = 0.;
151         zspacing = s.GetZSpacing();
152         std::vector<double> v_pixelspacing = gdcm::ImageHelper::GetSpacingValue(f);
153         v_pixelspacing.push_back( v_pixelspacing[0] );
154         v_pixelspacing.push_back( v_pixelspacing[1] );
155         v_pixelspacing.push_back( zspacing );
156         bbSetOutputPixelSpacing(v_pixelspacing);
157
158    if (reader!=NULL)
159    { 
160       reader->Delete();
161       reader=NULL;
162    }
163    reader = vtkGDCMImageReader::New();
164    vtkStringArray *files = vtkStringArray::New();
165    std::vector< std::string >::const_iterator it = s.GetFilenames().begin();
166    for( ; it != s.GetFilenames().end(); ++it)
167    {
168           const std::string &f = *it;
169       files->InsertNextValue( f.c_str() );
170     }
171    reader->SetFileNames(files);
172    reader->Update();
173    reader->GetOutput();
174    bbSetOutputOut( reader->GetOutput() );
175 }
176 #endif
177 // endif USE_GDCM2
178
179 void GetXCoherentInfoGdcmReader::bbUserSetDefaultValues()
180 {
181    std::vector<std::string> init;
182    init.push_back("");
183    bbSetInputIn(init);  
184    reader=NULL;
185 }
186
187 void GetXCoherentInfoGdcmReader::bbUserInitializeProcessing()
188 {
189         
190 }
191
192 #if defined USE_GDCM
193 void GetXCoherentInfoGdcmReader::bbUserFinalizeProcessing()
194 {
195    if(reader)
196       reader->Delete();
197    if(f)
198       f->Delete();
199    if(sh)
200       sh->Delete();      
201 }
202 #endif
203
204 #if defined USE_GDCM2
205 void GetXCoherentInfoGdcmReader::bbUserFinalizeProcessing()
206 {
207    if(reader)
208       reader->Delete();
209 }
210 #endif
211
212 }
213 // EO namespace bbgdcmvtk