2 # ---------------------------------------------------------------------
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
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
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.
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
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 # ------------------------------------------------------------------------
29 #include "creaImageIODicomImageReader2.h"
30 #include <creaImageIOTreeAttributeDescriptor.h>
31 #include "gdcmStringFilter.h"
33 #include "boost/filesystem/path.hpp"
35 #include "vtkGDCMImageReader.h"
36 #include <vtkStringArray.h>
38 #pragma warning(disable: 4996)
47 //=====================================================================
48 DicomImageReader::DicomImageReader()
50 mReader = vtkGDCMImageReader::New();
54 //=====================================================================
56 //=====================================================================
57 DicomImageReader::~DicomImageReader()
61 //=====================================================================
63 //=====================================================================
64 bool DicomImageReader::CanRead(const std::string& filename)
67 reader.SetFileName( filename.c_str() );
71 //=====================================================================
73 //=====================================================================
74 vtkImageData* DicomImageReader::ReadImage(const std::string& filename)
79 mReader->SetFileName(filename.c_str());
81 im = vtkImageData::New();
82 im->ShallowCopy(mReader->GetOutput());
86 if (im!=0) im->Delete();
90 //gdcm::Tag grouplength(0x0000,0x0000);
91 //gdcm::DataElement de(gdcm::Tag(0x0000, 0x0000));
92 //de.SetByteValue("0",gdcm::VR::UL);
95 //gdcm::UIDs::GetUIDString(gdcm::UIDS::uid_1_2_840_10008_1_1),gdcm::VR::UI);
102 //=====================================================================
103 void DicomImageReader::PushBackExtensions(std::vector<std::string>& v)
109 //========================================================================
110 std::string irclean(const std::string& str)
114 if (str == "GDCM::Unfound")
118 if (str[str.size()-1]==' ')
120 return irclean(str.substr(0,str.size()-1));
122 if (str[str.size()-1]==0)
124 return irclean(str.substr(0,str.size()-1));
130 //========================================================================
131 //=====================================================================
134 void DicomImageReader::getAttributes(const std::string filename,
135 std::map <std::string , std::string> &infos, std::vector<std::string> i_attr)
138 reader.SetFileName( filename.c_str() );
141 std::vector<std::string>::iterator it = i_attr.begin();
142 for(;it != i_attr.end(); it++)
146 sscanf((*it).c_str(),"D%04hx_%04hx",&gr,&el);
147 if ( ( gr!=0 ) && ( el!=0 ) )
149 infos[(*it)] = ( GetStringValueFromTag(reader.GetFile().GetDataSet().GetDataElement(gdcm::Tag(gr,el))) );
155 //=====================================================================
156 void DicomImageReader::ReadAttributes(const std::string& filename,
157 std::map<std::string,std::string>& attr)
162 reader.SetFileName( filename.c_str() );
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)
170 if ( i->first == "D0004_1500" )
172 boost::filesystem::path full_path(filename);
173 std::string f = full_path.leaf().string();
176 else if ( i->first == "FullFileName" )
178 i->second = filename;
180 else if ( i->first == "FullFileDirectory" )
182 std::string::size_type last_pos = filename.find_last_of("//");
183 //find first separator
184 i->second = filename.substr(0, last_pos);
191 gdcm::Attribute< 0x0008,0x103e> at;
196 tree::AttributeDescriptor::GetDicomGroupElementFromKey(i->first,gr,el);
197 //if ( ( gr!=0 ) && ( el!=0 ) )
198 const gdcm::Tag tag(gr, el);
199 if( reader.GetFile().GetDataSet().FindDataElement( tag ) )
201 i->second = irclean(sf.ToString(tag));
212 void DicomImageReader::ReadAttributes2(const std::string& filename,
213 std::map<std::string,std::string>& attr)
217 std::map<std::string,std::string>::iterator i;
218 for (i=attr.begin();i!=attr.end();++i)
220 if ( i->first == "D0004_1500" || i->first == "FullFileName" || i->first == "FullFileDirectory" )
229 tree::AttributeDescriptor::GetDicomGroupElementFromKey(i->first,gr,el);
230 mscan.AddTag(gdcm::Tag(gr,el) );
235 bool b = mscan.IsKey(filename.c_str());
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)
243 if ( i->first == "D0004_1500" )
245 boost::filesystem::path full_path(filename);
246 std::string f = full_path.leaf().string();
249 else if ( i->first == "FullFileName" )
251 i->second = filename;
253 else if ( i->first == "FullFileDirectory" )
255 std::string::size_type last_pos = filename.find_last_of("//");
256 //find first separator
257 i->second = filename.substr(0, last_pos);
261 const char *value = it->second;
262 i->second = irclean(it->second);
268 const std::string DicomImageReader::GetStringValueFromTag(const gdcm::DataElement& de)
270 printf("EED DicomImageReader::GetStringValueFromTag Start\n");
271 static std::string buffer;
272 buffer = ""; // cleanup previous call
275 const gdcm::ByteValue *bv = de.GetByteValue();
276 if( bv!=NULL ) // Can be Type 2
278 buffer = std::string( bv->GetPointer(), bv->GetLength() );
279 // Will be padded with at least one \0
282 // Since return is a const char* the very first \0 will be considered
283 printf("EED DicomImageReader::GetStringValueFromTag END\n");
284 return buffer.c_str();
286 //=====================================================================
288 } // namespace creaImageIO