]> Creatis software - creaImageIO.git/blob - src/creaImageIOSimpleView.cpp
0219927b6bdbb4a654a222abfa66aea9dda2c5a7
[creaImageIO.git] / src / creaImageIOSimpleView.cpp
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
28 #include "boost/filesystem/operations.hpp"
29 #include "boost/filesystem/fstream.hpp"
30
31 #include "creaImageIOSimpleView.h"
32 #include "creaImageIOUnMosaicVtkImageData.h"
33
34 namespace creaImageIO
35 {
36         bool SimpleView::readFile(      std::vector<std::string> i_filenames,                                                                   // in information names
37                                                                                 std::vector<std::string> i_attr,                                                                // in information attributes names
38                                                                                 std::vector<std::map <std::string , std::string> > &i_imgAttr,  // out information attributes names-values
39                                                                                 std::vector<vtkImageData *> &i_img)                                                     // out information vtkImageData
40         {
41
42                   //Mosaic Attribute for Simens         
43                         i_attr.push_back("D0019_100a");  // Number of images in mosaic (Simens)
44                         i_attr.push_back("D0018_0088");  // Spacing Between Slices
45                 
46
47
48                         creaImageIOUnMosaicVtkImageData Mosaic;
49                         vtkImageData* tmpImg1;
50                         vtkImageData* tmpImg2;
51                         int numberOfImagesInMosaic;
52                         double spc[3];
53                         double spcZ;
54
55                         bool bresult, bfinal = true;
56                         ImageReader *mReader = new ImageReader();
57                         std::vector<std::string>::iterator it = i_filenames.begin();
58                         for (; it != i_filenames.end(); it++)
59                         {
60                                 bresult = mReader->CanRead((*it).c_str());
61                                 if(bresult)
62                                 {
63                                         std::map <std::string , std::string> mapAttr;
64                                         mReader->getAttributes( (*it).c_str() , mapAttr, i_attr ); //  filename, outMapAttr-NameValue, inVectAttributeName
65                                         i_imgAttr.push_back( mapAttr );
66
67 //EED borrame
68 //                                      i_img.push_back( mReader->ReadImage( (*it).c_str() ) );
69
70
71                                         // EED 18 octt 2014
72                                         std::map <std::string , std::string>::iterator itMapAttr;
73                                         std::string numberOfImagesInMosaicStr="";
74                                         std::string spaceBetweenSliceStr="";
75
76                                         itMapAttr = mapAttr.find("D0019_100a");
77                                         if ( itMapAttr!=mapAttr.end() ) numberOfImagesInMosaicStr = itMapAttr->second;
78
79 //                                      std::string numberOfImagesInMosaicStr = mapAttr.find("D0019_100a")->second;
80                                         itMapAttr = mapAttr.find("D0018_0088");
81                                         if ( itMapAttr!=mapAttr.end() ) spaceBetweenSliceStr = itMapAttr->second;
82
83 //                                      std::string spaceBetweenSliceStr = mapAttr.find("D0018_0088")->second;
84                                         if (numberOfImagesInMosaicStr=="")
85                                         {
86                                                 i_img.push_back( mReader->ReadImage( (*it).c_str() ) );
87                                         } else {
88                                                 numberOfImagesInMosaic = atoi(numberOfImagesInMosaicStr.c_str());
89                                                 spcZ = atoi(spaceBetweenSliceStr.c_str());
90                                                 tmpImg1 = mReader->ReadImage( (*it).c_str() ) ;
91                                                 tmpImg1->GetSpacing( spc );     
92                                                 tmpImg2 = Mosaic.unMosaic( tmpImg1 , numberOfImagesInMosaic );
93                                                 tmpImg2->SetSpacing( spc[0], spc[1], spcZ );    
94                                                 i_img.push_back( tmpImg2 );
95                                         }// elemet
96
97                                 } else {
98                                         printf("ERROR. Impossible to read file %s\n", (*it).c_str() );
99                                         bfinal = false;
100                                 } // if
101                         } // for
102                         delete mReader;
103
104                         return bfinal;  
105         }
106
107
108         bool SimpleView::readDirectory( const std::string i_pathname,                                                                                   // in information names
109                                                                                                 std::vector<std::string> i_attr,                                                                                        // in information attributes names
110                                                                                                 std::vector<std::map <std::string , std::string> > &i_imgAttr,  // out information attributes names-values
111                                                                                                 std::vector<vtkImageData *> &i_imgs)                                                                    // out information vtkImageData
112         {
113                         bool bresult = true;
114                         ImageReader *mReader = new ImageReader();
115                         std::vector<std::string> names;
116                         bresult = boost::filesystem::exists( i_pathname );
117                         if (bresult)
118                         {
119                                 boost::filesystem::directory_iterator itr(i_pathname);
120                                 boost::filesystem::directory_iterator end_itr; 
121                                 for(;itr != end_itr; ++itr)
122                                 {
123                                         if (!boost::filesystem::is_directory(itr->status()))
124                                         {
125                                                 if( mReader->CanRead(itr->path().string()) )
126                                                 {
127                                                         names.push_back(itr->path().string()); 
128                                                 }
129                                         }
130                                 }
131                                 std::sort (names.begin(), names.end()); // make sure names are in lexicographical order
132                                 int lgr = (int)names.size();
133                                 
134                                 for(int i=0; i<lgr; i++)
135                                 {
136                std::cout << names[i] << std::endl;
137
138                                         std::map <std::string , std::string> mapAttr;
139                                         mReader->getAttributes( names[i] , mapAttr, i_attr ); //  filename, outMapAttr-NameValue, inVectAttributeName
140                                         i_imgAttr.push_back( mapAttr );
141
142                                         //UnMosaic step..
143
144                                    i_imgs.push_back( mReader->ReadImage(names[i]) );  
145                                 }                       
146                         }
147                         return bresult;
148         }
149
150         
151         vtkImageData* SimpleView::getVolumeSelected( std::vector<vtkImageData*> lstImages)
152         {
153                 vtkImageData *result;
154
155 //        creaImageIO::WxSimpleDlg dlg(0,crea::std2wx(bbGetInputTitle()),"localdatabase_Descriptor.dscp","Local Database"); 
156 //              dlg.SetAttrDicomTags( bbGetInputDicomTags() );
157 //              dlg.ShowModal(); 
158 //              bbSetOutputDicomInfo( dlg.getDicomInfoImagesSelected() );
159
160                 if (lstImages.size()!=0){ 
161 //                bbSetOutputFileName( std::string( dlg.getInfoImage().mb_str() ) );
162                   if(lstImages.size() ==1)
163                   {
164                         result = lstImages[0] ;  
165                   } else {
166                                 // FCY: it will be a big problem if we have several kind of data in the same folder.
167                                 // creation of a huge vtkImageData!!!!
168                                 vtkImageData* first = lstImages[0];
169                                 int dim[3];
170                                 first->GetDimensions(dim);
171                                 if (dim[2]==1)
172                                 {
173                                         vtkImageData *out = vtkImageData::New();
174                                         int ext[6];
175 //EED 2017-01-01 Migration VTK7
176 #if VTK_MAJOR_VERSION <= 5
177                                         first->GetWholeExtent(ext); 
178 #else
179                                         first->GetExtent(ext); 
180 #endif
181                                         if(ext[5] == 0)
182                                         {
183                                            ext[5] = lstImages.size()-1;
184                                          } else {
185                                                 ext[5] = ext[5] * lstImages.size()-1; // to deal with multiframes 
186                                         } // ext
187                                         out->SetExtent(ext);
188                                         double spac[3];
189                                         first->GetDimensions(dim);
190                                         first->GetSpacing(spac);
191                                         if (spac[0]==spac[1])
192                                         {
193                                                 spac[2]=spac[0];  
194                                 }
195                                         out->SetSpacing(spac);
196                                         out->SetDimensions(dim[0], dim[1], lstImages.size() );
197 //EED 2017-01-01 Migration VTK7
198 #if VTK_MAJOR_VERSION <= 5
199
200                                         out->SetScalarType(first->GetScalarType());
201                                         out->SetNumberOfScalarComponents(first->GetNumberOfScalarComponents());
202                                         out->AllocateScalars();
203                                         out->Update();
204 #else
205                                         out->AllocateScalars(first->GetScalarType(),first->GetNumberOfScalarComponents());
206 #endif
207
208
209                                         unsigned long imsize = dim[0] * dim[1];
210                                         imsize = imsize * dim[2];  // deal with multiframes here
211                                         // differents formats char , short, etc...
212                                         // differents components 1..3  ex. jpg ->RGB 3
213                                         imsize = imsize * first->GetScalarSize() * first->GetNumberOfScalarComponents();
214                                 int slice,sizeImageVector=lstImages.size();
215                                         for (slice=0 ; slice<sizeImageVector ; slice++)
216                                         {
217                                                 vtkImageData *img = lstImages[slice];
218                                                 memcpy(out->GetScalarPointer(0,0,slice), img->GetScalarPointer(0,0,0), imsize);
219                                                 //img->Delete();
220                                         } // for slice  
221                                         out->Modified();
222 //EED This is NOT working. The iteration over the pointer vtkImageData
223 //                              int slice=0;
224 //                              std::vector<vtkImageData*>::iterator it;
225 //                              for (it=dlg.getImagesSelected().begin(); it!=dlg.getImagesSelected().end(); ++it) 
226 //                                      {
227 //                                                memcpy(out->GetScalarPointer(0,0,slice), (*it)->GetScalarPointer(0,0,0), imsize);
228 //                                                slice++;
229 //                                      }       
230 //                              //for (it=dlg.getImagesSelected().begin(); it!=dlg.getImagesSelected().end(); ++it) 
231 //                                      //{
232 //                                      //              (*it)->Delete();
233 //                                      //}
234
235                                         result =out;
236
237                                 } else {
238                                         result = first;
239                                 } // dim
240                         } // if getImagesSelected size
241                 } else { 
242                 result = NULL;  
243                 } // getImagesSelected().size()!=0
244                         result->Modified();
245         return result;
246         }
247         
248         
249 }