]> Creatis software - bbtk.git/blob - packages/gdcmvtk/src/bbgdcmvtkGetXCoherentInfoGdcmReader.cxx
#3309 BBTK Bug New Normal Bug gdcmvtk - GetXCoherentInfoGdcmReader std - FilesFromDir...
[bbtk.git] / packages / gdcmvtk / src / bbgdcmvtkGetXCoherentInfoGdcmReader.cxx
1 /*
2 # ---------------------------------------------------------------------
3 #
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
5 #                        pour la SantÈ)
6 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9 #
10 #  This software is governed by the CeCILL-B license under French law and
11 #  abiding by the rules of distribution of free software. You can  use,
12 #  modify and/ or redistribute the software under the terms of the CeCILL-B
13 #  license as circulated by CEA, CNRS and INRIA at the following URL
14 #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
15 #  or in the file LICENSE.txt.
16 #
17 #  As a counterpart to the access to the source code and  rights to copy,
18 #  modify and redistribute granted by the license, users are provided only
19 #  with a limited warranty  and the software's author,  the holder of the
20 #  economic rights,  and the successive licensors  have only  limited
21 #  liability.
22 #
23 #  The fact that you are presently reading this means that you have had
24 #  knowledge of the CeCILL-B license and that you accept its terms.
25 # ------------------------------------------------------------------------ */
26
27 #include "bbgdcmvtkGetXCoherentInfoGdcmReader.h"
28 #include "bbgdcmvtkPackage.h"
29
30 #include "gdcmFile.h"
31 #include "vtkImageData.h"
32 #include <vtkIndent.h>
33
34 #if defined USE_GDCM
35         #include "gdcmFileHelper.h"
36 #endif
37
38 #if defined USE_GDCM2
39         #include <gdcmReader.h>
40         #include <gdcmImageHelper.h>
41         #include <gdcmIPPSorter.h>
42         #include <vtkGDCMImageReader.h>
43         #include "vtkStringArray.h"
44 #endif
45
46 namespace bbgdcmvtk
47 {
48
49 BBTK_ADD_BLACK_BOX_TO_PACKAGE(gdcmvtk,GetXCoherentInfoGdcmReader)
50 BBTK_BLACK_BOX_IMPLEMENTATION(GetXCoherentInfoGdcmReader,bbtk::AtomicBlackBox);
51
52
53 vtkImageData* GetXCoherentInfoGdcmReader::CreateDefaultImage()
54 {
55         int i;
56         int sizeX, sizeY, sizeZ;
57         sizeX = 200;
58         sizeY = sizeX;
59         sizeZ = 1;
60         vtkImageData *newImage = vtkImageData::New();
61         newImage->Initialize();
62         newImage->SetSpacing( 1,1,1 );
63         newImage->SetDimensions(  sizeX,sizeY,sizeZ );
64         newImage->SetExtent(0,  sizeX-1,0,sizeY-1,0,sizeZ-1 );
65
66 //EED 2017-01-01 Migration VTK7
67 #if VTK_MAJOR_VERSION <= 5
68         newImage->SetWholeExtent(0,  sizeX-1,0,sizeY-1,0,sizeZ-1 );
69         newImage->SetScalarTypeToUnsignedChar();
70         newImage->SetNumberOfScalarComponents(1);
71         newImage->AllocateScalars();
72         newImage->Update();
73 #else
74         newImage->AllocateScalars(VTK_UNSIGNED_CHAR,1);
75 #endif
76
77         memset ( (void*)newImage->GetScalarPointer(), 0, sizeX*sizeY*1 );
78         for (i=0; i<sizeX; i++)
79         {
80                 newImage->SetScalarComponentFromDouble(i,i,0, 0, 255 );
81                 newImage->SetScalarComponentFromDouble(i,sizeY-1-i,0, 0, 255 );
82         } // for i
83         return newImage;
84
85
86
87 #if defined USE_GDCM
88 void GetXCoherentInfoGdcmReader::Process()
89 {
90 printf("EED GetXCoherentInfoGdcmReader::Process Start\n");
91  // Read the *first* image file (a SET of file names is given as input) 
92    f = GDCM_NAME_SPACE::File::New();
93
94         if ( bbGetInputIn().size()!=0 )
95         {
96                 f->SetFileName( bbGetInputIn()[0] );
97         } else {
98                 f->SetFileName( "" );
99         } // if
100 printf("EED GetXCoherentInfoGdcmReader::Process 1\n");
101
102    bool res = f->Load();  
103    if ( !res )
104    {
105                 printf("EED GetXCoherentInfoGdcmReader::Process resultImage NULL\n");
106         f->Delete();
107         bbSetOutputOut( CreateDefaultImage() );
108         return;
109    } // if res
110  // Get info from the first image file
111    int i;
112    std::vector<double> v_iop;
113    float iop[6];
114    f->GetImageOrientationPatient(iop);
115    
116    for(i=0; i< 6; i++)
117         {
118       v_iop.push_back(iop[i]);
119         } // for i
120    bbSetOutputIOP(v_iop );
121 printf("EED GetXCoherentInfoGdcmReader::Process 2\n");
122  
123    std::vector<double> v_ipp;
124    float ipp[3];
125    f->GetImagePositionPatient(ipp);
126    
127    for(i=0; i< 3; i++)
128         {
129       v_ipp.push_back(ipp[i]);
130         } // for i
131    bbSetOutputIPP(v_ipp );
132
133 // Add *all the files* to the SerieHelper
134    sh = GDCM_NAME_SPACE::SerieHelper::New();
135    std::vector<std::string> gii = bbGetInputIn();
136
137 printf("EED GetXCoherentInfoGdcmReader::Process 3\n");
138    
139    for(std::vector<std::string>::iterator it = gii.begin();
140                                           it != gii.end();
141                                         ++it)
142    {
143       sh->AddFileName(*it);
144    } //for vector
145
146    GDCM_NAME_SPACE::FileList::const_iterator it;
147    GDCM_NAME_SPACE::FileList *l;
148
149 printf("EED GetXCoherentInfoGdcmReader::Process 4\n");
150    // Should only contain one!
151    l = sh->GetFirstSingleSerieUIDFileSet();
152 printf("EED GetXCoherentInfoGdcmReader::Process 5\n");
153
154         if (l==NULL)
155         {       
156                 printf("EED GetXCoherentInfoGdcmReader::Process Warnning not coherent list/file \n");
157                 return;
158         }
159 printf("EED GetXCoherentInfoGdcmReader::Process 6\n");
160
161    double zspacing = 0.;
162    sh->OrderFileList(l); // this one should compute the *actual* Z Spacing!
163    zspacing             = sh->GetZSpacing();
164    std::vector<double> v_pixelspacing;
165    v_pixelspacing.push_back( f->GetXSpacing() );
166    v_pixelspacing.push_back( f->GetYSpacing() );
167    v_pixelspacing.push_back( zspacing );
168 //   if (f->GetZSize() != 1) {
169 //      v_pixelspacing.push_back(f->GetZSpacing());
170 //   }
171    bbSetOutputPixelSpacing(v_pixelspacing);
172
173         VectorMapInfoDicom vectormapinfodicom;
174         uint16_t group;
175         uint16_t elem;
176         std::string key;
177         std::string strTagValue;  // read on disc
178         int iTag,sizeDicomTagsVector=bbGetInputDicomTags().size();
179         GDCM_NAME_SPACE::FileList::const_iterator iitt = l->begin();
180         //iitt ++;
181
182         for ( ; iitt != l->end(); ++iitt)
183         {
184                 MapInfoDicom mapinfodicom;
185                 for (iTag=0; iTag<sizeDicomTagsVector; iTag++)
186                 {
187                         key = bbGetInputDicomTags()[iTag];
188                         group = elem = 0;
189                         if ( (key.size()==10) && (key[0] == 'D') && (key[5] == '_') )
190                         {
191                                 sscanf(key.c_str(),"D%04hx_%04hx ",&group,&elem);  
192                         }// if key
193                         strTagValue = (*iitt)->GetEntryString(group,elem);
194                         mapinfodicom.insert ( std::pair<std::string,std::string>(key,strTagValue) );
195                 } // for iTag
196
197                 vectormapinfodicom.push_back( mapinfodicom );
198    } // for iitt
199    bbSetOutputDicomInfo( vectormapinfodicom );
200
201    if (reader!=NULL)
202    { 
203       reader->Delete();
204       reader = NULL;
205    }
206    reader = vtkGdcmReader::New();
207         //EED 21 mars 2012  FLIP probleme  ..PLOP..
208         reader->SetFlipY(false);
209    //reader->SetFileName( bbGetInputIn().c_str() );
210    reader->SetCoherentFileList(l);
211    reader->Update();
212    reader->GetOutput();
213    bbSetOutputOut( reader->GetOutput() );
214 }
215 #endif
216 // endif USE_GDCM
217
218 #if defined USE_GDCM2
219 void GetXCoherentInfoGdcmReader::Process()
220 {
221  // Read the *first* image file (a SET of file names is given as input) 
222         gdcm::Reader *read = new gdcm::Reader();
223         reader->SetFileName( bbGetInputIn()[0].c_str() );
224         bool res = read->Read();  
225         if ( !res )
226         {
227         delete read;
228         bbSetOutputOut(NULL);
229         return;
230         }
231     
232  // Get info from the first image file
233    const gdcm::File &f = read->GetFile();
234    int i;
235    std::vector<double> v_iop;
236    const gdcm::DataElement &deIop = f.GetDataSet().GetDataElement(gdcm::Tag(0x0020, 0x0037));
237    std::stringstream ss;
238    deIop.GetValue().Print(ss);
239    gdcm::Element<gdcm::VR::DS,gdcm::VM::VM6> iop;
240    iop.Read( ss );
241    for(i=0; i< 6; i++)
242    {
243         v_iop.push_back((float)(iop[i]));
244    }
245    bbSetOutputIOP(v_iop );
246  
247    std::vector<double> v_ipp;
248    const gdcm::DataElement &deIpp = f.GetDataSet().GetDataElement(gdcm::Tag((0x0020,0x0032)));
249    deIpp.GetValue().Print(ss);
250    gdcm::Element<gdcm::VR::DS,gdcm::VM::VM3> ipp;
251    ipp.Read( ss );
252    for(i=0; i< 3; i++)
253         {
254                 v_ipp.push_back((float)(ipp[i]));
255         } // for 3
256    bbSetOutputIPP(v_ipp );
257
258 // Add *all the files* to the IPPsorter
259      gdcm::IPPSorter s;
260          s.SetComputeZSpacing( true );
261      s.SetZSpacingTolerance( 1e-3 );
262      std::vector<std::string> gii = bbGetInputIn();
263      s.Sort(gii);
264
265          //l = sh->GetFirstSingleSerieUIDFileSet();
266          // no Test if we have multiple series
267          //s.GetFilenames();
268          //int nbFiles;
269
270         double zspacing = 0.;
271         zspacing = s.GetZSpacing();
272         std::vector<double> v_pixelspacing = gdcm::ImageHelper::GetSpacingValue(f);
273         v_pixelspacing.push_back( v_pixelspacing[0] );
274         v_pixelspacing.push_back( v_pixelspacing[1] );
275         v_pixelspacing.push_back( zspacing );
276         bbSetOutputPixelSpacing(v_pixelspacing);
277
278    if (reader!=NULL)
279    { 
280       reader->Delete();
281       reader=NULL;
282    }
283    reader = vtkGDCMImageReader::New();
284    vtkStringArray *files = vtkStringArray::New();
285    std::vector< std::string >::const_iterator it = s.GetFilenames().begin();
286    for( ; it != s.GetFilenames().end(); ++it)
287    {
288           const std::string &f = *it;
289       files->InsertNextValue( f.c_str() );
290    }
291    reader->SetFileNames(files);
292    reader->Update();
293    bbSetOutputOut( reader->GetOutput() );
294 }
295 #endif
296 // endif USE_GDCM2
297
298 void GetXCoherentInfoGdcmReader::bbUserSetDefaultValues()
299 {
300    reader=NULL;
301    std::vector<std::string> init;
302    init.push_back("");
303    bbSetInputIn(init);  
304    //reader=NULL;   /// \TODO fixme JPR
305 }
306
307 void GetXCoherentInfoGdcmReader::bbUserInitializeProcessing()
308 {
309         
310 }
311
312 #if defined USE_GDCM
313 void GetXCoherentInfoGdcmReader::bbUserFinalizeProcessing()
314 {
315    if(reader)
316         {
317       reader->Delete();
318         } // if reader
319    if(f)
320         {
321       f->Delete();
322         } // if f
323    if(sh)
324         {
325       sh->Delete();      
326         } // if sh
327 }
328 #endif
329
330 #if defined USE_GDCM2
331 void GetXCoherentInfoGdcmReader::bbUserFinalizeProcessing()
332 {
333    if(reader)
334         {
335       reader->Delete();
336         }
337 }
338 #endif
339
340 }
341 // EO namespace bbgdcmvtk