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