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