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