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