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