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