]> Creatis software - creaImageIO.git/blob - src/creaImageIOVtkImageReader.cpp
Bug 1805:
[creaImageIO.git] / src / creaImageIOVtkImageReader.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
29 #include <creaImageIOVtkImageReader.h>
30 #include <vtkImageReader2.h>
31 #include <creaImageIOSystem.h>
32 #include "boost/filesystem/path.hpp"
33
34 namespace creaImageIO{
35   //=====================================================================
36   VtkImageReader::VtkImageReader(vtkImageReader2* r, 
37                                        const std::string& name,
38                                        const std::string& extensions)
39     : mReader(r), mExtensions(extensions)
40   {
41           
42         //EED 21 mars 2012  FLIP probleme  ..PLOP..
43         mReader->FileLowerLeftOn();
44           
45     if (name.size() == 0) 
46       {
47                   const char *test =mReader->GetDescriptiveName();
48                   //warning: comparison with string literal results in unspecified behaviour
49                   //if(test != "")
50                   std::string emptyString("");
51                   if(test != emptyString)
52                   {
53                         SetName ( "toto");// mReader->GetDescriptiveName());
54                   }
55       }
56     else 
57       {
58         SetName ( name );
59       }
60     GimmickDebugMessage(5,"Constructing vtkImageReader : "<<GetName()
61                         <<std::endl);
62       
63   }
64   //=====================================================================
65   
66   //=====================================================================
67   VtkImageReader::~VtkImageReader()
68   {
69
70     mReader->Delete();
71   }
72   //=====================================================================
73
74   //=====================================================================
75   bool VtkImageReader::CanRead(const std::string& filename)
76   { 
77
78     return (mReader->CanReadFile(filename.c_str())!=0);
79 /*        if(filename != "")
80           {
81                 return (mReader->CanReadFile(filename.c_str())!=0);
82           }
83           else
84           {
85                   return false;
86           }*/
87   }
88   //=====================================================================
89   void VtkImageReader::getAttributes(const std::string filename,
90                 std::map <std::string , std::string> &infos, std::vector<std::string> i_attr)
91    {
92    }
93   //=====================================================================
94   vtkImageData* VtkImageReader::ReadImage(const std::string& filename)
95   {
96     vtkImageData* im = 0;
97     try
98       {
99         mReader->SetFileName(filename.c_str());
100         mReader->Update();
101         im = vtkImageData::New();
102         im->ShallowCopy(mReader->GetOutput());
103       }
104     catch (...)
105       {
106         if (im!=0) im->Delete();
107         im = 0;
108       }
109     return im;
110   }
111   //=====================================================================
112   
113   //=====================================================================
114   void SplitExtensionsString ( const std::string& str, 
115                                const std::string& delimiters, 
116                                std::vector<std::string>& tokens)
117   {
118     // Skip delimiters at beginning.
119     std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
120     // Find first delimiter.
121     std::string::size_type pos     = str.find_first_of(delimiters, lastPos);
122     
123     while (std::string::npos != pos || std::string::npos != lastPos)
124       {
125         // Found a token, add it to the vector.
126         // SPECIFIC : REMOVE INITIAL DOT (lastPos + 1)
127         tokens.push_back(str.substr(lastPos+1, pos - lastPos));
128         // Skip delimiters.  Note the "not_of"
129         lastPos = str.find_first_not_of(delimiters, pos);
130         // Find next delimiter
131         pos = str.find_first_of(delimiters, lastPos);
132       }
133     
134     }
135   //=====================================================================
136   
137   //=====================================================================
138   void VtkImageReader::PushBackExtensions(std::vector<std::string>& v)
139   {
140     std::string ext = mExtensions;
141     if (ext.size()==0) ext = mReader->GetFileExtensions ();
142     
143     SplitExtensionsString(ext," ",v);
144   }
145   //=====================================================================
146  
147
148
149   //=====================================================================
150   void VtkImageReader::ReadAttributes(const std::string& filename, 
151                                       std::map<std::string,std::string>& attr)
152   {
153     GimmickMessage(2,"Reading attributes from '"<<filename<<std::endl);
154     // Get image dimensions
155     // How to get the image info without loading it in vtk ?
156     mReader->SetFileName(filename.c_str());
157     mReader->Update(); //OpenFile();
158     int ext[6];
159     mReader->GetDataExtent(ext);
160     // Columns
161     char cols[128];
162     sprintf(cols,"%i",ext[1]-ext[0]);
163     // Rows
164     char rows[128];
165     sprintf(rows,"%i",ext[3]-ext[2]);
166     // Planes 
167     char planes[128];
168     sprintf(planes,"%i",ext[5]-ext[4]);
169    
170         std::map<std::string,std::string>::iterator i;
171     if ( (i = attr.find("FullFileName")) != attr.end())
172       {
173         i->second = filename;
174       }
175     if ( (i = attr.find("D0004_1500")) != attr.end())
176       {
177         boost::filesystem::path full_path(filename);
178         std::string f = full_path.leaf().string();
179         i->second = f;
180       }
181     if ( (i = attr.find("D0028_0010")) != attr.end())
182       {
183         i->second = rows;
184       }
185     if ( (i = attr.find("D0028_0011")) != attr.end())
186       {
187         i->second = cols;
188       }
189     
190     if ( (i = attr.find("D0028_0012")) != attr.end())
191       {
192                 i->second = planes;
193       }
194           if ( (i = attr.find("FullFileDirectory")) != attr.end())
195       {
196          std::string::size_type last_pos = filename.find_last_of("//");
197                  i->second = filename.substr(0, last_pos);
198           }
199
200     GimmickMessage(2,"Attributes map:"<<std::endl<<attr<<std::endl);
201   }
202   //=====================================================================
203
204 } // namespace creaImageIO