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