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