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