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