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
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;
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++)
56 bresult = mReader->CanRead((*it).c_str());
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 );
64 // i_img.push_back( mReader->ReadImage( (*it).c_str() ) );
68 std::map <std::string , std::string>::iterator itMapAttr;
69 std::string numberOfImagesInMosaicStr="";
70 std::string spaceBetweenSliceStr="";
72 itMapAttr = mapAttr.find("D0019_100a");
73 if ( itMapAttr!=mapAttr.end() ) numberOfImagesInMosaicStr = itMapAttr->second;
75 // std::string numberOfImagesInMosaicStr = mapAttr.find("D0019_100a")->second;
76 itMapAttr = mapAttr.find("D0018_0088");
77 if ( itMapAttr!=mapAttr.end() ) spaceBetweenSliceStr = itMapAttr->second;
79 // std::string spaceBetweenSliceStr = mapAttr.find("D0018_0088")->second;
80 if (numberOfImagesInMosaicStr=="")
82 i_img.push_back( mReader->ReadImage( (*it).c_str() ) );
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 );
94 printf("ERROR. Impossible to read file %s\n", (*it).c_str() );
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
109 ImageReader *mReader = new ImageReader();
110 std::vector<std::string> names;
111 bresult = boost::filesystem::exists( i_pathname );
114 boost::filesystem::directory_iterator itr(i_pathname);
115 boost::filesystem::directory_iterator end_itr;
116 for(;itr != end_itr; ++itr)
118 if (!boost::filesystem::is_directory(itr->status()))
120 if( mReader->CanRead(itr->path().string()) )
122 names.push_back(itr->path().string());
126 std::sort (names.begin(), names.end()); // make sure names are in lexicographical order
127 int lgr = (int)names.size();
129 for(int i=0; i<lgr; i++)
131 std::cout << names[i] << std::endl;
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 );
139 i_imgs.push_back( mReader->ReadImage(names[i]) );
146 vtkImageData* SimpleView::getVolumeSelected( std::vector<vtkImageData*> lstImages)
148 vtkImageData *result;
150 // creaImageIO::WxSimpleDlg dlg(0,crea::std2wx(bbGetInputTitle()),"localdatabase_Descriptor.dscp","Local Database");
151 // dlg.SetAttrDicomTags( bbGetInputDicomTags() );
153 // bbSetOutputDicomInfo( dlg.getDicomInfoImagesSelected() );
155 if (lstImages.size()!=0)
157 // bbSetOutputFileName( std::string( dlg.getInfoImage().mb_str() ) );
158 if(lstImages.size() ==1)
160 result = lstImages[0] ;
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];
166 first->GetDimensions(dim);
169 vtkImageData *out = vtkImageData::New();
171 //EED 2017-01-01 Migration VTK7
172 #if VTK_MAJOR_VERSION <= 5
173 first->GetWholeExtent(ext);
175 first->GetExtent(ext);
179 ext[5] = lstImages.size()-1;
181 ext[5] = ext[5] * lstImages.size()-1; // to deal with multiframes
185 first->GetDimensions(dim);
186 first->GetSpacing(spac);
187 if (spac[0]==spac[1])
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
196 out->SetScalarType(first->GetScalarType());
197 out->SetNumberOfScalarComponents(first->GetNumberOfScalarComponents());
198 out->AllocateScalars();
201 out->AllocateScalars(first->GetScalarType(),first->GetNumberOfScalarComponents());
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++)
213 vtkImageData *img = lstImages[slice];
214 memcpy(out->GetScalarPointer(0,0,slice), img->GetScalarPointer(0,0,0), imsize);
218 //EED This is NOT working. The iteration over the pointer vtkImageData
220 // std::vector<vtkImageData*>::iterator it;
221 // for (it=dlg.getImagesSelected().begin(); it!=dlg.getImagesSelected().end(); ++it)
223 // memcpy(out->GetScalarPointer(0,0,slice), (*it)->GetScalarPointer(0,0,0), imsize);
226 // //for (it=dlg.getImagesSelected().begin(); it!=dlg.getImagesSelected().end(); ++it)
228 // // (*it)->Delete();
236 } // if getImagesSelected size
240 } // getImagesSelected().size()!=0