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