]> Creatis software - creaImageIO.git/blob - src/creaImageIOSimpleView.cpp
#2842 creaImageIO Bug New Normal - Read images from directory
[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 }