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