]> Creatis software - bbtk.git/blob - packages/gdcmvtk/src/bbgdcmvtkGetXCoherentInfoGdcmReader.cxx
Convert bbtk from GDCM to GDCM2 (cmakefiles and cxx files).
[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 USE_GDCM
99
100 #if defined USE_GDCM2
101 void GetXCoherentInfoGdcmReader::Process()
102 {
103  // Read the *first* image file (a SET of file names is given as input) 
104         gdcm::Reader *read = new gdcm::Reader();
105         reader->SetFileName( bbGetInputIn()[0].c_str());
106   
107         bool res = read->Read();  
108    if ( !res )
109    {
110       delete read;
111       bbSetOutputOut(0);
112       return;
113    }
114     
115  // Get info from the first image file
116    const gdcm::File &f = read->GetFile();
117    int i;
118    std::vector<double> v_iop;
119    const gdcm::DataElement &deIop = f.GetDataSet().GetDataElement(gdcm::Tag(0x0020, 0x0037));
120    std::stringstream ss;
121    deIop.GetValue().Print(ss);
122    gdcm::Element<gdcm::VR::DS,gdcm::VM::VM6> iop;
123    iop.Read( ss );
124    for(i=0; i< 6; i++)
125       v_iop.push_back((float)(iop[i]));
126    bbSetOutputIOP(v_iop );
127  
128   std::vector<double> v_ipp;
129    const gdcm::DataElement &deIpp = f.GetDataSet().GetDataElement(gdcm::Tag((0x0020,0x0032)));
130    deIpp.GetValue().Print(ss);
131    gdcm::Element<gdcm::VR::DS,gdcm::VM::VM3> ipp;
132    ipp.Read( ss );
133           for(i=0; i< 3; i++)
134                 v_ipp.push_back((float)(ipp[i]));
135    bbSetOutputIPP(v_ipp );
136
137 // Add *all the files* to the IPPsorter
138      gdcm::IPPSorter s;
139          s.SetComputeZSpacing( true );
140      s.SetZSpacingTolerance( 1e-3 );
141      std::vector<std::string> gii = bbGetInputIn();
142      s.Sort(gii);
143
144          //l = sh->GetFirstSingleSerieUIDFileSet();
145          // no Test if we have multiple series
146          //s.GetFilenames();
147          //int nbFiles;
148
149         double zspacing = 0.;
150         zspacing = s.GetZSpacing();
151         std::vector<double> v_pixelspacing = gdcm::ImageHelper::GetSpacingValue(f);
152         v_pixelspacing.push_back( v_pixelspacing[0] );
153         v_pixelspacing.push_back( v_pixelspacing[1] );
154         v_pixelspacing.push_back( zspacing );
155         bbSetOutputPixelSpacing(v_pixelspacing);
156
157    if (reader!=NULL)
158    { 
159       reader->Delete();
160       reader=NULL;
161    }
162    reader = vtkGDCMImageReader::New();
163    vtkStringArray *files = vtkStringArray::New();
164    std::vector< std::string >::const_iterator it = s.GetFilenames().begin();
165    for( ; it != s.GetFilenames().end(); ++it)
166    {
167           const std::string &f = *it;
168       files->InsertNextValue( f.c_str() );
169     }
170    reader->SetFileNames(files);
171    reader->Update();
172    reader->GetOutput();
173    bbSetOutputOut( reader->GetOutput() );
174 }
175 #endif USE_GDCM2
176
177 void GetXCoherentInfoGdcmReader::bbUserSetDefaultValues()
178 {
179    std::vector<std::string> init;
180    init.push_back("");
181    bbSetInputIn(init);  
182    reader=NULL;
183 }
184
185 void GetXCoherentInfoGdcmReader::bbUserInitializeProcessing()
186 {
187         
188 }
189
190 #if defined USE_GDCM
191 void GetXCoherentInfoGdcmReader::bbUserFinalizeProcessing()
192 {
193    if(reader)
194       reader->Delete();
195    if(f)
196       f->Delete();
197    if(sh)
198       sh->Delete();      
199 }
200 #endif
201
202 #if defined USE_GDCM2
203 void GetXCoherentInfoGdcmReader::bbUserFinalizeProcessing()
204 {
205    if(reader)
206       reader->Delete();
207 }
208 #endif
209
210 }
211 // EO namespace bbgdcmvtk