]> Creatis software - creaImageIO.git/blob - src/creaImageIODicomImageReader2.cpp
changes for X64 compilation.
[creaImageIO.git] / src / creaImageIODicomImageReader2.cpp
1 #include <creaImageIODicomImageReader2.h>
2 #include "gdcmStringFilter.h"
3
4
5 #include <creaImageIOSystem.h>
6 #include "boost/filesystem/path.hpp"
7
8 #include <creaImageIOTreeAttributeDescriptor.h>
9 #include <vtkStringArray.h>
10 #include <creaImageIOGimmick.h>
11
12
13 #ifdef _DEBUG
14 #define new DEBUG_NEW
15 #endif
16 namespace creaImageIO
17 {
18
19   //=====================================================================
20   DicomImageReader::DicomImageReader()
21   {
22       mReader =  vtkGDCMImageReader::New();
23       SetName ( "Dicom" );
24         
25   };
26   //=====================================================================
27   
28   //=====================================================================
29   DicomImageReader::~DicomImageReader()
30   {
31       mReader->Delete();
32   }
33   //=====================================================================
34
35   //=====================================================================  
36   bool DicomImageReader::CanRead(const std::string& filename)
37   { 
38       gdcm::Reader reader;
39       reader.SetFileName( filename.c_str() );
40       return  reader.Read();
41          
42   }
43   //=====================================================================
44
45   //=====================================================================
46   vtkImageData* DicomImageReader::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   void DicomImageReader::PushBackExtensions(std::vector<std::string>& v)
66   {
67     v.push_back("dcm");
68     v.push_back("");
69   }
70  
71   //========================================================================
72   std::string irclean(const std::string& str)
73   {
74           if(str.size() > 0)
75           {
76                 if (str == "GDCM::Unfound") 
77                   {
78                 return "";
79                   }
80                 if (str[str.size()-1]==' ')
81                   {
82                 return irclean(str.substr(0,str.size()-1));
83                   }
84                 if (str[str.size()-1]==0)
85                   {
86                 return irclean(str.substr(0,str.size()-1));
87                   }
88           }
89     
90     return str;
91   }
92   //========================================================================
93   //=====================================================================
94   
95
96         void DicomImageReader::getAttributes(const std::string filename, 
97                 std::map <std::string , std::string> &infos, std::vector<std::string> i_attr)
98         {
99                 gdcm::Reader reader;
100                 reader.SetFileName( filename.c_str() );
101                 if (reader.Read())
102                 {
103                         std::vector<std::string>::iterator it = i_attr.begin();
104                         for(;it != i_attr.end(); it++)
105                         {
106                                 unsigned short el;
107                                 unsigned short gr;
108                                 sscanf((*it).c_str(),"D%04hx_%04hx",&gr,&el);
109                                 if ( ( gr!=0 ) && ( el!=0 ) )
110                                 {
111                                         infos[(*it)] =  ( GetStringValueFromTag(reader.GetFile().GetDataSet().GetDataElement(gdcm::Tag(gr,el))) );
112                                 }
113                         }
114                 }
115         }
116
117   //=====================================================================
118   void DicomImageReader::ReadAttributes(const std::string& filename, 
119                       std::map<std::string,std::string>& attr)
120   {
121     GimmickMessage(2,"Reading attributes from DICOM file '"
122                    <<filename<<"'"<<std::endl);
123
124    
125       gdcm::Reader reader;
126       reader.SetFileName( filename.c_str() );
127       if (reader.Read())
128       {
129                   gdcm::StringFilter sf;
130                   sf.SetFile(reader.GetFile());
131                   std::map<std::string,std::string>::iterator i;
132                   for (i=attr.begin();i!=attr.end();++i)
133               {
134                           if ( i->first == "D0004_1500" )
135                           {
136                                   boost::filesystem::path full_path(filename);
137                                   std::string f = full_path.leaf();
138                                   i->second = f;
139                           }
140                           else if ( i->first == "FullFileName" )
141                           {
142                                 i->second = filename;
143                           }
144                           else if ( i->first == "FullFileDirectory" )
145                           {
146                                  std::string::size_type last_pos = filename.find_last_of("//");
147                                  //find first separator
148                                  i->second = filename.substr(0, last_pos);
149                           }
150                           else
151                           {
152                                   uint16_t el;
153                                   uint16_t gr;
154                                   tree::AttributeDescriptor::GetDicomGroupElementFromKey(i->first,gr,el);
155                                   //if ( ( gr!=0 ) && ( el!=0 ) )
156                                   const gdcm::Tag tag(gr, el);
157                                   if( reader.GetFile().GetDataSet().FindDataElement( tag ) )
158                                   {
159                                           i->second = irclean(sf.ToString(tag));
160                                   }
161                                   else
162                                   {
163                                           i->second = "";
164                                   }
165                           }
166                   }
167           }
168   }
169
170 void DicomImageReader::ReadAttributes2(const std::string& filename, 
171                       std::map<std::string,std::string>& attr)
172   {
173         if(!b_loaded)
174         {
175                 std::map<std::string,std::string>::iterator i;
176                 for (i=attr.begin();i!=attr.end();++i)
177                   {
178                         if ( i->first == "D0004_1500" ||  i->first == "FullFileName" || i->first == "FullFileDirectory" )
179                         {
180                         
181                         }
182                         else
183                         {
184                         uint16_t el;
185                         uint16_t gr;
186                         
187                         tree::AttributeDescriptor::GetDicomGroupElementFromKey(i->first,gr,el);
188                         mscan.AddTag(gdcm::Tag(gr,el) );
189                         }
190                 }
191                 b_loaded = true;
192         }
193          bool b = mscan.IsKey(filename.c_str());
194      if( b )
195       {
196                   const gdcm::Scanner::TagToValue &mapping = mscan.GetMapping(filename.c_str());
197                   gdcm::Scanner::TagToValue::const_iterator it = mapping.begin();
198                   std::map<std::string, std::string>::iterator i;
199                 for (i=attr.begin();i!=attr.end();++i, ++it)
200                 {
201                         if ( i->first == "D0004_1500" )
202                         {
203                                 boost::filesystem::path full_path(filename);
204                                 std::string f = full_path.leaf();
205                                 i->second = f;
206                         }
207                         else if ( i->first == "FullFileName" )
208                         {
209                                 i->second = filename;
210                         }
211                         else if ( i->first == "FullFileDirectory" )
212                         {
213                                   std::string::size_type last_pos = filename.find_last_of("//");
214                                   //find first separator
215                                   i->second = filename.substr(0, last_pos);
216                         }
217                         else
218                         {
219                                 const char *value = it->second;
220                                 i->second = irclean(it->second);
221                         }
222                 }
223          }
224 }
225
226 const std::string DicomImageReader::GetStringValueFromTag(const gdcm::DataElement& de)
227 {
228   static std::string buffer;
229   buffer = "";  // cleanup previous call
230
231
232     const gdcm::ByteValue *bv = de.GetByteValue();
233     if( bv ) // Can be Type 2
234       {
235       buffer = std::string( bv->GetPointer(), bv->GetLength() );
236       // Will be padded with at least one \0
237       }
238
239   // Since return is a const char* the very first \0 will be considered
240   return buffer.c_str();
241 }
242   //=====================================================================
243   
244 } // namespace creaImageIO
245