2 # ---------------------------------------------------------------------
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
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
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.
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
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 # ------------------------------------------------------------------------
28 #include "boost/filesystem/operations.hpp"
29 #include "boost/filesystem/fstream.hpp"
31 #include "creaImageIOSimpleView.h"
32 #include "creaImageIOUnMosaicVtkImageData.h"
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
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
48 creaImageIOUnMosaicVtkImageData Mosaic;
49 vtkImageData* tmpImg1;
50 vtkImageData* tmpImg2;
51 int numberOfImagesInMosaic;
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++)
60 bresult = mReader->CanRead((*it).c_str());
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 );
68 // i_img.push_back( mReader->ReadImage( (*it).c_str() ) );
72 std::map <std::string , std::string>::iterator itMapAttr;
73 std::string numberOfImagesInMosaicStr="";
74 std::string spaceBetweenSliceStr="";
76 itMapAttr = mapAttr.find("D0019_100a");
77 if ( itMapAttr!=mapAttr.end() ) numberOfImagesInMosaicStr = itMapAttr->second;
79 // std::string numberOfImagesInMosaicStr = mapAttr.find("D0019_100a")->second;
80 itMapAttr = mapAttr.find("D0018_0088");
81 if ( itMapAttr!=mapAttr.end() ) spaceBetweenSliceStr = itMapAttr->second;
83 // std::string spaceBetweenSliceStr = mapAttr.find("D0018_0088")->second;
84 if (numberOfImagesInMosaicStr=="")
86 i_img.push_back( mReader->ReadImage( (*it).c_str() ) );
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 );
98 printf("ERROR. Impossible to read file %s\n", (*it).c_str() );
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
114 ImageReader *mReader = new ImageReader();
115 std::vector<std::string> names;
116 bresult = boost::filesystem::exists( i_pathname );
119 boost::filesystem::directory_iterator itr(i_pathname);
120 boost::filesystem::directory_iterator end_itr;
121 for(;itr != end_itr; ++itr)
123 if (!boost::filesystem::is_directory(itr->status()))
125 if( mReader->CanRead(itr->path().string()) )
127 names.push_back(itr->path().string());
131 std::sort (names.begin(), names.end()); // make sure names are in lexicographical order
132 int lgr = (int)names.size();
134 for(int i=0; i<lgr; i++)
136 std::cout << names[i] << std::endl;
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 );
144 i_imgs.push_back( mReader->ReadImage(names[i]) );
151 vtkImageData* SimpleView::getVolumeSelected( std::vector<vtkImageData*> lstImages)
153 vtkImageData *result;
155 // creaImageIO::WxSimpleDlg dlg(0,crea::std2wx(bbGetInputTitle()),"localdatabase_Descriptor.dscp","Local Database");
156 // dlg.SetAttrDicomTags( bbGetInputDicomTags() );
158 // bbSetOutputDicomInfo( dlg.getDicomInfoImagesSelected() );
160 if (lstImages.size()!=0){
161 // bbSetOutputFileName( std::string( dlg.getInfoImage().mb_str() ) );
162 if(lstImages.size() ==1)
164 result = lstImages[0] ;
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];
170 first->GetDimensions(dim);
173 vtkImageData *out = vtkImageData::New();
175 //EED 2017-01-01 Migration VTK7
176 #if VTK_MAJOR_VERSION <= 5
177 first->GetWholeExtent(ext);
179 first->GetExtent(ext);
183 ext[5] = lstImages.size()-1;
185 ext[5] = ext[5] * lstImages.size()-1; // to deal with multiframes
189 first->GetDimensions(dim);
190 first->GetSpacing(spac);
191 if (spac[0]==spac[1])
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
200 out->SetScalarType(first->GetScalarType());
201 out->SetNumberOfScalarComponents(first->GetNumberOfScalarComponents());
202 out->AllocateScalars();
205 out->AllocateScalars(first->GetScalarType(),first->GetNumberOfScalarComponents());
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++)
217 vtkImageData *img = lstImages[slice];
218 memcpy(out->GetScalarPointer(0,0,slice), img->GetScalarPointer(0,0,0), imsize);
222 //EED This is NOT working. The iteration over the pointer vtkImageData
224 // std::vector<vtkImageData*>::iterator it;
225 // for (it=dlg.getImagesSelected().begin(); it!=dlg.getImagesSelected().end(); ++it)
227 // memcpy(out->GetScalarPointer(0,0,slice), (*it)->GetScalarPointer(0,0,0), imsize);
230 // //for (it=dlg.getImagesSelected().begin(); it!=dlg.getImagesSelected().end(); ++it)
232 // // (*it)->Delete();
240 } // if getImagesSelected size
243 } // getImagesSelected().size()!=0