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