]> Creatis software - creaImageIO.git/blob - src/creaImageIODicomImageReader2.cpp
thread bug correction
[creaImageIO.git] / src / creaImageIODicomImageReader2.cpp
1 #include <creaImageIODicomImageReader2.h>
2 #include "gdcmStringFilter.h"
3 #include "vtkGDCMImageReader.h"
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 #include "gdcmUIDs.h"
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     //gdcm::Tag grouplength(0x0000,0x0000);
63     //gdcm::DataElement de(gdcm::Tag(0x0000, 0x0000));
64     //de.SetByteValue("0",gdcm::VR::UL);
65     //
66     //
67     //gdcm::UIDs::GetUIDString(gdcm::UIDS::uid_1_2_840_10008_1_1),gdcm::VR::UI);
68
69     //std::ostream os;
70     //de.Write(&os);
71
72   }
73
74   //=====================================================================
75   void DicomImageReader::PushBackExtensions(std::vector<std::string>& v)
76   {
77     v.push_back("dcm");
78     v.push_back("");
79   }
80  
81   //========================================================================
82   std::string irclean(const std::string& str)
83   {
84           if(str.size() > 0)
85           {
86                 if (str == "GDCM::Unfound") 
87                   {
88                 return "";
89                   }
90                 if (str[str.size()-1]==' ')
91                   {
92                 return irclean(str.substr(0,str.size()-1));
93                   }
94                 if (str[str.size()-1]==0)
95                   {
96                 return irclean(str.substr(0,str.size()-1));
97                   }
98           }
99     
100     return str;
101   }
102   //========================================================================
103   //=====================================================================
104   
105
106         void DicomImageReader::getAttributes(const std::string filename, 
107                 std::map <std::string , std::string> &infos, std::vector<std::string> i_attr)
108         {
109                 gdcm::Reader reader;
110                 reader.SetFileName( filename.c_str() );
111                 if (reader.Read())
112                 {
113                         std::vector<std::string>::iterator it = i_attr.begin();
114                         for(;it != i_attr.end(); it++)
115                         {
116                                 unsigned short el;
117                                 unsigned short gr;
118                                 sscanf((*it).c_str(),"D%04hx_%04hx",&gr,&el);
119                                 if ( ( gr!=0 ) && ( el!=0 ) )
120                                 {
121                                         infos[(*it)] =  ( GetStringValueFromTag(reader.GetFile().GetDataSet().GetDataElement(gdcm::Tag(gr,el))) );
122                                 }
123                         }
124                 }
125         }
126
127   //=====================================================================
128   void DicomImageReader::ReadAttributes(const std::string& filename, 
129                       std::map<std::string,std::string>& attr)
130   {
131     GimmickMessage(2,"Reading attributes from DICOM file '"
132                    <<filename<<"'"<<std::endl);
133
134    
135       gdcm::Reader reader;
136       reader.SetFileName( filename.c_str() );
137       if (reader.Read())
138       {
139                   gdcm::StringFilter sf;
140                   sf.SetFile(reader.GetFile());
141                   std::map<std::string,std::string>::iterator i;
142                   for (i=attr.begin();i!=attr.end();++i)
143               {
144                           if ( i->first == "D0004_1500" )
145                           {
146                                   boost::filesystem::path full_path(filename);
147                                   std::string f = full_path.leaf();
148                                   i->second = f;
149                           }
150                           else if ( i->first == "FullFileName" )
151                           {
152                                 i->second = filename;
153                           }
154                           else if ( i->first == "FullFileDirectory" )
155                           {
156                                  std::string::size_type last_pos = filename.find_last_of("//");
157                                  //find first separator
158                                  i->second = filename.substr(0, last_pos);
159                           }
160                           else
161                           {
162                                   uint16_t el;
163                                   uint16_t gr;
164                       {
165             gdcm::Attribute<  0x0008,0x103e> at;
166             std::string sr;
167             at.SetValue(sr);
168             
169         }
170                                   tree::AttributeDescriptor::GetDicomGroupElementFromKey(i->first,gr,el);
171                                   //if ( ( gr!=0 ) && ( el!=0 ) )
172                                   const gdcm::Tag tag(gr, el);
173                                   if( reader.GetFile().GetDataSet().FindDataElement( tag ) )
174                                   {
175                                           i->second = irclean(sf.ToString(tag));
176                                   }
177                                   else
178                                   {
179                                           i->second = "";
180                                   }
181                           }
182                   }
183           }
184   }
185
186 void DicomImageReader::ReadAttributes2(const std::string& filename, 
187                       std::map<std::string,std::string>& attr)
188   {
189         if(!b_loaded)
190         {
191                 std::map<std::string,std::string>::iterator i;
192                 for (i=attr.begin();i!=attr.end();++i)
193                   {
194                         if ( i->first == "D0004_1500" ||  i->first == "FullFileName" || i->first == "FullFileDirectory" )
195                         {
196                         
197                         }
198                         else
199                         {
200                         uint16_t el;
201                         uint16_t gr;
202                         
203                         tree::AttributeDescriptor::GetDicomGroupElementFromKey(i->first,gr,el);
204                         mscan.AddTag(gdcm::Tag(gr,el) );
205                         }
206                 }
207                 b_loaded = true;
208         }
209          bool b = mscan.IsKey(filename.c_str());
210      if( b )
211       {
212                   const gdcm::Scanner::TagToValue &mapping = mscan.GetMapping(filename.c_str());
213                   gdcm::Scanner::TagToValue::const_iterator it = mapping.begin();
214                   std::map<std::string, std::string>::iterator i;
215                 for (i=attr.begin();i!=attr.end();++i, ++it)
216                 {
217                         if ( i->first == "D0004_1500" )
218                         {
219                                 boost::filesystem::path full_path(filename);
220                                 std::string f = full_path.leaf();
221                                 i->second = f;
222                         }
223                         else if ( i->first == "FullFileName" )
224                         {
225                                 i->second = filename;
226                         }
227                         else if ( i->first == "FullFileDirectory" )
228                         {
229                                   std::string::size_type last_pos = filename.find_last_of("//");
230                                   //find first separator
231                                   i->second = filename.substr(0, last_pos);
232                         }
233                         else
234                         {
235                                 const char *value = it->second;
236                                 i->second = irclean(it->second);
237                         }
238                 }
239          }
240 }
241
242 const std::string DicomImageReader::GetStringValueFromTag(const gdcm::DataElement& de)
243 {
244   static std::string buffer;
245   buffer = "";  // cleanup previous call
246
247
248     const gdcm::ByteValue *bv = de.GetByteValue();
249     if( bv ) // Can be Type 2
250       {
251       buffer = std::string( bv->GetPointer(), bv->GetLength() );
252       // Will be padded with at least one \0
253       }
254
255   // Since return is a const char* the very first \0 will be considered
256   return buffer.c_str();
257 }
258   //=====================================================================
259   
260 } // namespace creaImageIO
261