]> Creatis software - creaImageIO.git/blob - src/creaImageIODicomImageReader2.cpp
additional document for output
[creaImageIO.git] / src / creaImageIODicomImageReader2.cpp
1 #include <creaImageIODicomImageReader2.h>
2
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         std::map<std::string,std::string>::iterator i;
130         for (i=attr.begin();i!=attr.end();++i)
131           {
132             if ( i->first == "D0004_1500" )
133               {
134                 boost::filesystem::path full_path(filename);
135                 std::string f = full_path.leaf();
136                 i->second = f;
137               }
138             else if ( i->first == "FullFileName" )
139               {
140                 i->second = filename;
141               }
142                   else if ( i->first == "FullFileDirectory" )
143               {
144                           std::string::size_type last_pos = filename.find_last_of("//");
145                           //find first separator
146                           i->second = filename.substr(0, last_pos);
147               }
148             else
149               {
150                 uint16_t el;
151                 uint16_t gr;
152
153                 tree::AttributeDescriptor::GetDicomGroupElementFromKey(i->first,gr,el);
154                 if ( ( gr!=0 ) && ( el!=0 ) )
155                   {
156                          
157                            std::string val = GetStringValueFromTag(reader.GetFile().GetDataSet().GetDataElement(gdcm::Tag(gr,el)));
158                     i->second = irclean(val);
159                   }
160               }
161           }
162       }
163   }
164
165 void DicomImageReader::ReadAttributes2(const std::string& filename, 
166                       std::map<std::string,std::string>& attr)
167   {
168         if(!b_loaded)
169         {
170                 std::map<std::string,std::string>::iterator i;
171                 for (i=attr.begin();i!=attr.end();++i)
172                   {
173                         if ( i->first == "D0004_1500" ||  i->first == "FullFileName" || i->first == "FullFileDirectory" )
174                         {
175                         
176                         }
177                         else
178                         {
179                         uint16_t el;
180                         uint16_t gr;
181                         
182                         tree::AttributeDescriptor::GetDicomGroupElementFromKey(i->first,gr,el);
183                         mscan.AddTag(gdcm::Tag(gr,el) );
184                         }
185                 }
186                 b_loaded = true;
187         }
188          bool b = mscan.IsKey(filename.c_str());
189      if( b )
190       {
191                   const gdcm::Scanner::TagToValue &mapping = mscan.GetMapping(filename.c_str());
192                   gdcm::Scanner::TagToValue::const_iterator it = mapping.begin();
193                   std::map<std::string, std::string>::iterator i;
194                 for (i=attr.begin();i!=attr.end();++i, ++it)
195                 {
196                         if ( i->first == "D0004_1500" )
197                         {
198                                 boost::filesystem::path full_path(filename);
199                                 std::string f = full_path.leaf();
200                                 i->second = f;
201                         }
202                         else if ( i->first == "FullFileName" )
203                         {
204                                 i->second = filename;
205                         }
206                         else if ( i->first == "FullFileDirectory" )
207                         {
208                                   std::string::size_type last_pos = filename.find_last_of("//");
209                                   //find first separator
210                                   i->second = filename.substr(0, last_pos);
211                         }
212                         else
213                         {
214                                 const char *value = it->second;
215                                 i->second = irclean(it->second);
216                         }
217                 }
218          }
219 }
220
221 const std::string DicomImageReader::GetStringValueFromTag(const gdcm::DataElement& de)
222 {
223   static std::string buffer;
224   buffer = "";  // cleanup previous call
225
226
227     const gdcm::ByteValue *bv = de.GetByteValue();
228     if( bv ) // Can be Type 2
229       {
230       buffer = std::string( bv->GetPointer(), bv->GetLength() );
231       // Will be padded with at least one \0
232       }
233
234   // Since return is a const char* the very first \0 will be considered
235   return buffer.c_str();
236 }
237   //=====================================================================
238   
239 } // namespace creaImageIO
240