creaImageIO_lib
creaImageIODicomImageReader2.cpp
Go to the documentation of this file.
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 
31 #include "gdcmStringFilter.h"
32 #include "gdcmUIDs.h"
33 #include "boost/filesystem/path.hpp"
34 
35 #include "vtkGDCMImageReader.h"
36 #include <vtkStringArray.h>
37 #if defined(_WIN32)
38 #pragma warning(disable: 4996)
39 #endif
40 
41 #ifdef _DEBUG
42 #define new DEBUG_NEW
43 #endif
44 namespace creaImageIO
45 {
46 
47  //=====================================================================
49  {
50  mReader = vtkGDCMImageReader::New();
51  SetName ( "Dicom" );
52 
53  };
54  //=====================================================================
55 
56  //=====================================================================
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 
160 
161  gdcm::Reader reader;
162  reader.SetFileName( filename.c_str() );
163  if (reader.Read())
164  {
165  gdcm::StringFilter sf;
166  sf.SetFile(reader.GetFile());
167  std::map<std::string,std::string>::iterator i;
168  for (i=attr.begin();i!=attr.end();++i)
169  {
170  if ( i->first == "D0004_1500" )
171  {
172  boost::filesystem::path full_path(filename);
173  std::string f = full_path.leaf().string();
174  i->second = f;
175  }
176  else if ( i->first == "FullFileName" )
177  {
178  i->second = filename;
179  }
180  else if ( i->first == "FullFileDirectory" )
181  {
182  std::string::size_type last_pos = filename.find_last_of("//");
183  //find first separator
184  i->second = filename.substr(0, last_pos);
185  }
186  else
187  {
188  uint16_t el;
189  uint16_t gr;
190  {
191  gdcm::Attribute< 0x0008,0x103e> at;
192  std::string sr;
193  at.SetValue(sr);
194 
195  }
197  //if ( ( gr!=0 ) && ( el!=0 ) )
198  const gdcm::Tag tag(gr, el);
199  if( reader.GetFile().GetDataSet().FindDataElement( tag ) )
200  {
201  i->second = irclean(sf.ToString(tag));
202  }
203  else
204  {
205  i->second = "";
206  }
207  }
208  }
209  }
210  }
211 
212 void DicomImageReader::ReadAttributes2(const std::string& filename,
213  std::map<std::string,std::string>& attr)
214  {
215  if(!b_loaded)
216  {
217  std::map<std::string,std::string>::iterator i;
218  for (i=attr.begin();i!=attr.end();++i)
219  {
220  if ( i->first == "D0004_1500" || i->first == "FullFileName" || i->first == "FullFileDirectory" )
221  {
222 
223  }
224  else
225  {
226  uint16_t el;
227  uint16_t gr;
228 
230  mscan.AddTag(gdcm::Tag(gr,el) );
231  }
232  }
233  b_loaded = true;
234  }
235  bool b = mscan.IsKey(filename.c_str());
236  if( b )
237  {
238  const gdcm::Scanner::TagToValue &mapping = mscan.GetMapping(filename.c_str());
239  gdcm::Scanner::TagToValue::const_iterator it = mapping.begin();
240  std::map<std::string, std::string>::iterator i;
241  for (i=attr.begin();i!=attr.end();++i, ++it)
242  {
243  if ( i->first == "D0004_1500" )
244  {
245  boost::filesystem::path full_path(filename);
246  std::string f = full_path.leaf().string();
247  i->second = f;
248  }
249  else if ( i->first == "FullFileName" )
250  {
251  i->second = filename;
252  }
253  else if ( i->first == "FullFileDirectory" )
254  {
255  std::string::size_type last_pos = filename.find_last_of("//");
256  //find first separator
257  i->second = filename.substr(0, last_pos);
258  }
259  else
260  {
261  const char *value = it->second;
262  i->second = irclean(it->second);
263  }
264  }
265  }
266 }
267 
268 const std::string DicomImageReader::GetStringValueFromTag(const gdcm::DataElement& de)
269 {
270  static std::string buffer;
271  buffer = ""; // cleanup previous call
272 
273 
274  const gdcm::ByteValue *bv = de.GetByteValue();
275  if( bv ) // Can be Type 2
276  {
277  buffer = std::string( bv->GetPointer(), bv->GetLength() );
278  // Will be padded with at least one \0
279  }
280 
281  // Since return is a const char* the very first \0 will be considered
282  return buffer.c_str();
283 }
284  //=====================================================================
285 
286 } // namespace creaImageIO
287