]> Creatis software - bbtk.git/blob - packages/gdcmvtk/src/bbgdcmvtkGetXCoherentInfoGdcmReader.cxx
#2959 BBTK Feature New Normal - new complex box RescaleSlopeIntercept_Interface.bbg
[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  // Read the *first* image file (a SET of file names is given as input) 
84    f = GDCM_NAME_SPACE::File::New();
85    f->SetFileName( bbGetInputIn()[0] );
86    bool res = f->Load();  
87    if ( !res )
88    {
89                 printf("EED GetXCoherentInfoGdcmReader::Process resultImage NULL\n");
90         f->Delete();
91         bbSetOutputOut( CreateDefaultImage() );
92         return;
93    }
94  // Get info from the first image file
95    int i;
96    std::vector<double> v_iop;
97    float iop[6];
98    f->GetImageOrientationPatient(iop);
99    
100    for(i=0; i< 6; i++)
101       v_iop.push_back(iop[i]);
102    bbSetOutputIOP(v_iop );
103  
104    std::vector<double> v_ipp;
105    float ipp[3];
106    f->GetImagePositionPatient(ipp);
107    
108    for(i=0; i< 3; i++)
109       v_ipp.push_back(ipp[i]);
110    bbSetOutputIPP(v_ipp );
111
112 // Add *all the files* to the SerieHelper
113    sh = GDCM_NAME_SPACE::SerieHelper::New();
114    std::vector<std::string> gii = bbGetInputIn();
115    
116    for(std::vector<std::string>::iterator it = gii.begin();
117                                           it != gii.end();
118                                         ++it)
119    {
120       sh->AddFileName(*it);
121    }
122
123    GDCM_NAME_SPACE::FileList::const_iterator it;
124    GDCM_NAME_SPACE::FileList *l;
125
126    // Should only contain one!
127    l = sh->GetFirstSingleSerieUIDFileSet();
128
129    int nbFiles;
130    double zspacing = 0.;
131    nbFiles = l->size() ;
132    sh->OrderFileList(l); // this one should compute the *actual* Z Spacing!
133    zspacing = sh->GetZSpacing();
134    std::vector<double> v_pixelspacing;
135    v_pixelspacing.push_back( f->GetXSpacing() );
136    v_pixelspacing.push_back( f->GetYSpacing() );
137    v_pixelspacing.push_back( zspacing );
138 //   if (f->GetZSize() != 1) {
139 //      v_pixelspacing.push_back(f->GetZSpacing());
140 //   }
141    bbSetOutputPixelSpacing(v_pixelspacing);
142
143    if (reader!=NULL)
144    { 
145       reader->Delete();
146       reader=NULL;
147    }
148    reader = vtkGdcmReader::New();
149         
150         //EED 21 mars 2012  FLIP probleme  ..PLOP..
151         reader->SetFlipY(false);
152
153         
154    //reader->SetFileName( bbGetInputIn().c_str() );
155    reader->SetCoherentFileList(l);
156    reader->Update();
157    reader->GetOutput();
158
159    bbSetOutputOut( reader->GetOutput() );
160 }
161 #endif
162 // endif USE_GDCM
163
164 #if defined USE_GDCM2
165 void GetXCoherentInfoGdcmReader::Process()
166 {
167  // Read the *first* image file (a SET of file names is given as input) 
168         gdcm::Reader *read = new gdcm::Reader();
169         reader->SetFileName( bbGetInputIn()[0].c_str() );
170   
171         bool res = read->Read();  
172         if ( !res )
173         {
174         delete read;
175         bbSetOutputOut(NULL);
176         return;
177         }
178     
179
180
181  // Get info from the first image file
182    const gdcm::File &f = read->GetFile();
183    int i;
184    std::vector<double> v_iop;
185    const gdcm::DataElement &deIop = f.GetDataSet().GetDataElement(gdcm::Tag(0x0020, 0x0037));
186    std::stringstream ss;
187    deIop.GetValue().Print(ss);
188    gdcm::Element<gdcm::VR::DS,gdcm::VM::VM6> iop;
189    iop.Read( ss );
190    for(i=0; i< 6; i++)
191       v_iop.push_back((float)(iop[i]));
192    bbSetOutputIOP(v_iop );
193  
194   std::vector<double> v_ipp;
195    const gdcm::DataElement &deIpp = f.GetDataSet().GetDataElement(gdcm::Tag((0x0020,0x0032)));
196    deIpp.GetValue().Print(ss);
197    gdcm::Element<gdcm::VR::DS,gdcm::VM::VM3> ipp;
198    ipp.Read( ss );
199           for(i=0; i< 3; i++)
200                 v_ipp.push_back((float)(ipp[i]));
201    bbSetOutputIPP(v_ipp );
202
203 // Add *all the files* to the IPPsorter
204      gdcm::IPPSorter s;
205          s.SetComputeZSpacing( true );
206      s.SetZSpacingTolerance( 1e-3 );
207      std::vector<std::string> gii = bbGetInputIn();
208      s.Sort(gii);
209
210          //l = sh->GetFirstSingleSerieUIDFileSet();
211          // no Test if we have multiple series
212          //s.GetFilenames();
213          //int nbFiles;
214
215         double zspacing = 0.;
216         zspacing = s.GetZSpacing();
217         std::vector<double> v_pixelspacing = gdcm::ImageHelper::GetSpacingValue(f);
218         v_pixelspacing.push_back( v_pixelspacing[0] );
219         v_pixelspacing.push_back( v_pixelspacing[1] );
220         v_pixelspacing.push_back( zspacing );
221         bbSetOutputPixelSpacing(v_pixelspacing);
222
223    if (reader!=NULL)
224    { 
225       reader->Delete();
226       reader=NULL;
227    }
228    reader = vtkGDCMImageReader::New();
229    vtkStringArray *files = vtkStringArray::New();
230    std::vector< std::string >::const_iterator it = s.GetFilenames().begin();
231    for( ; it != s.GetFilenames().end(); ++it)
232    {
233           const std::string &f = *it;
234       files->InsertNextValue( f.c_str() );
235    }
236    reader->SetFileNames(files);
237    reader->Update();
238    bbSetOutputOut( reader->GetOutput() );
239 }
240 #endif
241 // endif USE_GDCM2
242
243 void GetXCoherentInfoGdcmReader::bbUserSetDefaultValues()
244 {
245    std::vector<std::string> init;
246    init.push_back("");
247    bbSetInputIn(init);  
248    //reader=NULL;   /// \TODO fixme JPR
249 }
250
251 void GetXCoherentInfoGdcmReader::bbUserInitializeProcessing()
252 {
253         
254 }
255
256 #if defined USE_GDCM
257 void GetXCoherentInfoGdcmReader::bbUserFinalizeProcessing()
258 {
259    if(reader)
260       reader->Delete();
261    if(f)
262       f->Delete();
263    if(sh)
264       sh->Delete();      
265 }
266 #endif
267
268 #if defined USE_GDCM2
269 void GetXCoherentInfoGdcmReader::bbUserFinalizeProcessing()
270 {
271    if(reader)
272       reader->Delete();
273 }
274 #endif
275
276 }
277 // EO namespace bbgdcmvtk