]> Creatis software - creaImageIO.git/blob - src/creaImageIODicomImageReader2.cpp
Feature #1764
[creaImageIO.git] / src / creaImageIODicomImageReader2.cpp
1 /*
2 # ---------------------------------------------------------------------
3 #
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image 
5 #                        pour la Santé)
6 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9 #
10 #  This software is governed by the CeCILL-B license under French law and 
11 #  abiding by the rules of distribution of free software. You can  use, 
12 #  modify and/ or redistribute the software under the terms of the CeCILL-B 
13 #  license as circulated by CEA, CNRS and INRIA at the following URL 
14 #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
15 #  or in the file LICENSE.txt.
16 #
17 #  As a counterpart to the access to the source code and  rights to copy,
18 #  modify and redistribute granted by the license, users are provided only
19 #  with a limited warranty  and the software's author,  the holder of the
20 #  economic rights,  and the successive licensors  have only  limited
21 #  liability. 
22 #
23 #  The fact that you are presently reading this means that you have had
24 #  knowledge of the CeCILL-B license and that you accept its terms.
25 # ------------------------------------------------------------------------
26 */
27
28
29 #include <creaImageIODicomImageReader2.h>
30 #include "gdcmStringFilter.h"
31 #include "vtkGDCMImageReader.h"
32
33 #include <creaImageIOSystem.h>
34 #include "boost/filesystem/path.hpp"
35
36 #include <creaImageIOTreeAttributeDescriptor.h>
37 #include <vtkStringArray.h>
38 #include <creaImageIOGimmick.h>
39
40 #include "gdcmUIDs.h"
41 #ifdef _DEBUG
42 #define new DEBUG_NEW
43 #endif
44 namespace creaImageIO
45 {
46
47   //=====================================================================
48   DicomImageReader::DicomImageReader()
49   {
50       mReader =  vtkGDCMImageReader::New();
51       SetName ( "Dicom" );
52         
53   };
54   //=====================================================================
55   
56   //=====================================================================
57   DicomImageReader::~DicomImageReader()
58   {
59       mReader->Delete();
60   }
61   //=====================================================================
62
63   //=====================================================================  
64   bool DicomImageReader::CanRead(const std::string& filename)
65   { 
66       gdcm::Reader reader;
67       reader.SetFileName( filename.c_str() );
68       return  reader.Read();
69          
70   }
71   //=====================================================================
72
73   //=====================================================================
74   vtkImageData* DicomImageReader::ReadImage(const std::string& filename)
75   {
76     vtkImageData* im = 0;
77     try
78       {
79         mReader->SetFileName(filename.c_str());
80         mReader->Update();
81         im = vtkImageData::New();
82         im->ShallowCopy(mReader->GetOutput());
83       }
84     catch (...)
85       {
86         if (im!=0) im->Delete();
87           im = 0;
88       }
89     return im;
90     //gdcm::Tag grouplength(0x0000,0x0000);
91     //gdcm::DataElement de(gdcm::Tag(0x0000, 0x0000));
92     //de.SetByteValue("0",gdcm::VR::UL);
93     //
94     //
95     //gdcm::UIDs::GetUIDString(gdcm::UIDS::uid_1_2_840_10008_1_1),gdcm::VR::UI);
96
97     //std::ostream os;
98     //de.Write(&os);
99
100   }
101
102   //=====================================================================
103   void DicomImageReader::PushBackExtensions(std::vector<std::string>& v)
104   {
105     v.push_back("dcm");
106     v.push_back("");
107   }
108  
109   //========================================================================
110   std::string irclean(const std::string& str)
111   {
112           if(str.size() > 0)
113           {
114                 if (str == "GDCM::Unfound") 
115                   {
116                 return "";
117                   }
118                 if (str[str.size()-1]==' ')
119                   {
120                 return irclean(str.substr(0,str.size()-1));
121                   }
122                 if (str[str.size()-1]==0)
123                   {
124                 return irclean(str.substr(0,str.size()-1));
125                   }
126           }
127     
128     return str;
129   }
130   //========================================================================
131   //=====================================================================
132   
133
134         void DicomImageReader::getAttributes(const std::string filename, 
135                 std::map <std::string , std::string> &infos, std::vector<std::string> i_attr)
136         {
137                 gdcm::Reader reader;
138                 reader.SetFileName( filename.c_str() );
139                 if (reader.Read())
140                 {
141                         std::vector<std::string>::iterator it = i_attr.begin();
142                         for(;it != i_attr.end(); it++)
143                         {
144                                 unsigned short el;
145                                 unsigned short gr;
146                                 sscanf((*it).c_str(),"D%04hx_%04hx",&gr,&el);
147                                 if ( ( gr!=0 ) && ( el!=0 ) )
148                                 {
149                                         infos[(*it)] =  ( GetStringValueFromTag(reader.GetFile().GetDataSet().GetDataElement(gdcm::Tag(gr,el))) );
150                                 }
151                         }
152                 }
153         }
154
155   //=====================================================================
156   void DicomImageReader::ReadAttributes(const std::string& filename, 
157                       std::map<std::string,std::string>& attr)
158   {
159     GimmickMessage(2,"Reading attributes from DICOM file '"
160                    <<filename<<"'"<<std::endl);
161
162    
163       gdcm::Reader reader;
164       reader.SetFileName( filename.c_str() );
165       if (reader.Read())
166       {
167                   gdcm::StringFilter sf;
168                   sf.SetFile(reader.GetFile());
169                   std::map<std::string,std::string>::iterator i;
170                   for (i=attr.begin();i!=attr.end();++i)
171               {
172                           if ( i->first == "D0004_1500" )
173                           {
174                                   boost::filesystem::path full_path(filename);
175                                   std::string f = full_path.leaf();
176                                   i->second = f;
177                           }
178                           else if ( i->first == "FullFileName" )
179                           {
180                                 i->second = filename;
181                           }
182                           else if ( i->first == "FullFileDirectory" )
183                           {
184                                  std::string::size_type last_pos = filename.find_last_of("//");
185                                  //find first separator
186                                  i->second = filename.substr(0, last_pos);
187                           }
188                           else
189                           {
190                                   uint16_t el;
191                                   uint16_t gr;
192                       {
193             gdcm::Attribute<  0x0008,0x103e> at;
194             std::string sr;
195             at.SetValue(sr);
196             
197         }
198                                   tree::AttributeDescriptor::GetDicomGroupElementFromKey(i->first,gr,el);
199                                   //if ( ( gr!=0 ) && ( el!=0 ) )
200                                   const gdcm::Tag tag(gr, el);
201                                   if( reader.GetFile().GetDataSet().FindDataElement( tag ) )
202                                   {
203                                           i->second = irclean(sf.ToString(tag));
204                                   }
205                                   else
206                                   {
207                                           i->second = "";
208                                   }
209                           }
210                   }
211           }
212   }
213
214 void DicomImageReader::ReadAttributes2(const std::string& filename, 
215                       std::map<std::string,std::string>& attr)
216   {
217         if(!b_loaded)
218         {
219                 std::map<std::string,std::string>::iterator i;
220                 for (i=attr.begin();i!=attr.end();++i)
221                   {
222                         if ( i->first == "D0004_1500" ||  i->first == "FullFileName" || i->first == "FullFileDirectory" )
223                         {
224                         
225                         }
226                         else
227                         {
228                         uint16_t el;
229                         uint16_t gr;
230                         
231                         tree::AttributeDescriptor::GetDicomGroupElementFromKey(i->first,gr,el);
232                         mscan.AddTag(gdcm::Tag(gr,el) );
233                         }
234                 }
235                 b_loaded = true;
236         }
237          bool b = mscan.IsKey(filename.c_str());
238      if( b )
239       {
240                   const gdcm::Scanner::TagToValue &mapping = mscan.GetMapping(filename.c_str());
241                   gdcm::Scanner::TagToValue::const_iterator it = mapping.begin();
242                   std::map<std::string, std::string>::iterator i;
243                 for (i=attr.begin();i!=attr.end();++i, ++it)
244                 {
245                         if ( i->first == "D0004_1500" )
246                         {
247                                 boost::filesystem::path full_path(filename);
248                                 std::string f = full_path.leaf();
249                                 i->second = f;
250                         }
251                         else if ( i->first == "FullFileName" )
252                         {
253                                 i->second = filename;
254                         }
255                         else if ( i->first == "FullFileDirectory" )
256                         {
257                                   std::string::size_type last_pos = filename.find_last_of("//");
258                                   //find first separator
259                                   i->second = filename.substr(0, last_pos);
260                         }
261                         else
262                         {
263                                 const char *value = it->second;
264                                 i->second = irclean(it->second);
265                         }
266                 }
267          }
268 }
269
270 const std::string DicomImageReader::GetStringValueFromTag(const gdcm::DataElement& de)
271 {
272   static std::string buffer;
273   buffer = "";  // cleanup previous call
274
275
276     const gdcm::ByteValue *bv = de.GetByteValue();
277     if( bv ) // Can be Type 2
278       {
279       buffer = std::string( bv->GetPointer(), bv->GetLength() );
280       // Will be padded with at least one \0
281       }
282
283   // Since return is a const char* the very first \0 will be considered
284   return buffer.c_str();
285 }
286   //=====================================================================
287   
288 } // namespace creaImageIO
289