]> Creatis software - bbtk.git/blob - packages/gdcmvtk/src/bbgdcmvtkGetXCoherentInfoGdcmReader.cxx
#3309 BBTK Bug New Normal - Bug gdcmvtk - GetXCoherentInfoGdcmReader std - FilesFro...
[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  // Read the *first* image file (a SET of file names is given as input) 
91    f = GDCM_NAME_SPACE::File::New();
92
93         if ( bbGetInputIn().size()!=0 )
94         {
95                 f->SetFileName( bbGetInputIn()[0] );
96         } else {
97                 f->SetFileName( "" );
98         } // if
99
100    bool res = f->Load();  
101    if ( !res )
102    {
103                 printf("EED GetXCoherentInfoGdcmReader::Process resultImage NULL\n");
104         f->Delete();
105         bbSetOutputOut( CreateDefaultImage() );
106         return;
107    } // if res
108  // Get info from the first image file
109    int i;
110    std::vector<double> v_iop;
111    float iop[6];
112    f->GetImageOrientationPatient(iop);
113    
114    for(i=0; i< 6; i++)
115         {
116       v_iop.push_back(iop[i]);
117         } // for i
118    bbSetOutputIOP(v_iop );
119  
120    std::vector<double> v_ipp;
121    float ipp[3];
122    f->GetImagePositionPatient(ipp);
123    
124    for(i=0; i< 3; i++)
125         {
126       v_ipp.push_back(ipp[i]);
127         } // for i
128    bbSetOutputIPP(v_ipp );
129
130 // Add *all the files* to the SerieHelper
131    sh = GDCM_NAME_SPACE::SerieHelper::New();
132    std::vector<std::string> gii = bbGetInputIn();
133    
134    for(std::vector<std::string>::iterator it = gii.begin();
135                                           it != gii.end();
136                                         ++it)
137    {
138       sh->AddFileName(*it);
139    } //for vector
140
141    GDCM_NAME_SPACE::FileList::const_iterator it;
142    GDCM_NAME_SPACE::FileList *l;
143
144    // Should only contain one!
145    l = sh->GetFirstSingleSerieUIDFileSet();
146
147    double zspacing = 0.;
148    sh->OrderFileList(l); // this one should compute the *actual* Z Spacing!
149    zspacing             = sh->GetZSpacing();
150    std::vector<double> v_pixelspacing;
151    v_pixelspacing.push_back( f->GetXSpacing() );
152    v_pixelspacing.push_back( f->GetYSpacing() );
153    v_pixelspacing.push_back( zspacing );
154 //   if (f->GetZSize() != 1) {
155 //      v_pixelspacing.push_back(f->GetZSpacing());
156 //   }
157    bbSetOutputPixelSpacing(v_pixelspacing);
158
159         VectorMapInfoDicom vectormapinfodicom;
160         uint16_t group;
161         uint16_t elem;
162         std::string key;
163         std::string strTagValue;  // read on disc
164         int iTag,sizeDicomTagsVector=bbGetInputDicomTags().size();
165         GDCM_NAME_SPACE::FileList::const_iterator iitt = l->begin();
166         //iitt ++;
167
168         for ( ; iitt != l->end(); ++iitt)
169         {
170                 MapInfoDicom mapinfodicom;
171
172                 for (iTag=0; iTag<sizeDicomTagsVector; iTag++)
173                 {
174                         key = bbGetInputDicomTags()[iTag];
175                         group = elem = 0;
176                         if ( (key.size()==10) && (key[0] == 'D') && (key[5] == '_') )
177                         {
178                                 sscanf(key.c_str(),"D%04hx_%04hx ",&group,&elem);  
179                         }// if key
180                         strTagValue = (*iitt)->GetEntryString(group,elem);
181                         mapinfodicom.insert ( std::pair<std::string,std::string>(key,strTagValue) );
182                 } // for iTag
183
184                 vectormapinfodicom.push_back( mapinfodicom );
185    } // for iitt
186    bbSetOutputDicomInfo( vectormapinfodicom );
187
188    if (reader!=NULL)
189    { 
190       reader->Delete();
191       reader = NULL;
192    }
193    reader = vtkGdcmReader::New();
194         //EED 21 mars 2012  FLIP probleme  ..PLOP..
195         reader->SetFlipY(false);
196    //reader->SetFileName( bbGetInputIn().c_str() );
197    reader->SetCoherentFileList(l);
198    reader->Update();
199    reader->GetOutput();
200    bbSetOutputOut( reader->GetOutput() );
201 }
202 #endif
203 // endif USE_GDCM
204
205 #if defined USE_GDCM2
206 void GetXCoherentInfoGdcmReader::Process()
207 {
208  // Read the *first* image file (a SET of file names is given as input) 
209         gdcm::Reader *read = new gdcm::Reader();
210         reader->SetFileName( bbGetInputIn()[0].c_str() );
211   
212         bool res = read->Read();  
213         if ( !res )
214         {
215         delete read;
216         bbSetOutputOut(NULL);
217         return;
218         }
219     
220
221
222  // Get info from the first image file
223    const gdcm::File &f = read->GetFile();
224    int i;
225    std::vector<double> v_iop;
226    const gdcm::DataElement &deIop = f.GetDataSet().GetDataElement(gdcm::Tag(0x0020, 0x0037));
227    std::stringstream ss;
228    deIop.GetValue().Print(ss);
229    gdcm::Element<gdcm::VR::DS,gdcm::VM::VM6> iop;
230    iop.Read( ss );
231    for(i=0; i< 6; i++)
232         {
233         v_iop.push_back((float)(iop[i]));
234         }
235    bbSetOutputIOP(v_iop );
236  
237   std::vector<double> v_ipp;
238    const gdcm::DataElement &deIpp = f.GetDataSet().GetDataElement(gdcm::Tag((0x0020,0x0032)));
239    deIpp.GetValue().Print(ss);
240    gdcm::Element<gdcm::VR::DS,gdcm::VM::VM3> ipp;
241    ipp.Read( ss );
242         for(i=0; i< 3; i++)
243         {
244                 v_ipp.push_back((float)(ipp[i]));
245         } // for 3
246    bbSetOutputIPP(v_ipp );
247
248 // Add *all the files* to the IPPsorter
249      gdcm::IPPSorter s;
250          s.SetComputeZSpacing( true );
251      s.SetZSpacingTolerance( 1e-3 );
252      std::vector<std::string> gii = bbGetInputIn();
253      s.Sort(gii);
254
255          //l = sh->GetFirstSingleSerieUIDFileSet();
256          // no Test if we have multiple series
257          //s.GetFilenames();
258          //int nbFiles;
259
260         double zspacing = 0.;
261         zspacing = s.GetZSpacing();
262         std::vector<double> v_pixelspacing = gdcm::ImageHelper::GetSpacingValue(f);
263         v_pixelspacing.push_back( v_pixelspacing[0] );
264         v_pixelspacing.push_back( v_pixelspacing[1] );
265         v_pixelspacing.push_back( zspacing );
266         bbSetOutputPixelSpacing(v_pixelspacing);
267
268    if (reader!=NULL)
269    { 
270       reader->Delete();
271       reader=NULL;
272    }
273    reader = vtkGDCMImageReader::New();
274    vtkStringArray *files = vtkStringArray::New();
275    std::vector< std::string >::const_iterator it = s.GetFilenames().begin();
276    for( ; it != s.GetFilenames().end(); ++it)
277    {
278           const std::string &f = *it;
279       files->InsertNextValue( f.c_str() );
280    }
281    reader->SetFileNames(files);
282    reader->Update();
283    bbSetOutputOut( reader->GetOutput() );
284 }
285 #endif
286 // endif USE_GDCM2
287
288 void GetXCoherentInfoGdcmReader::bbUserSetDefaultValues()
289 {
290         reader=NULL;
291    std::vector<std::string> init;
292    init.push_back("");
293    bbSetInputIn(init);  
294    //reader=NULL;   /// \TODO fixme JPR
295 }
296
297 void GetXCoherentInfoGdcmReader::bbUserInitializeProcessing()
298 {
299         
300 }
301
302 #if defined USE_GDCM
303 void GetXCoherentInfoGdcmReader::bbUserFinalizeProcessing()
304 {
305    if(reader)
306         {
307       reader->Delete();
308         } // if reader
309    if(f)
310         {
311       f->Delete();
312         } // if f
313    if(sh)
314         {
315       sh->Delete();      
316         } // if sh
317 }
318 #endif
319
320 #if defined USE_GDCM2
321 void GetXCoherentInfoGdcmReader::bbUserFinalizeProcessing()
322 {
323    if(reader)
324         {
325       reader->Delete();
326         }
327 }
328 #endif
329
330 }
331 // EO namespace bbgdcmvtk