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