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 "gdcmStringFilter.h"
31 #include "vtkGDCMImageReader.h"
33 #include <creaImageIOSystem.h>
34 #include "boost/filesystem/path.hpp"
36 #include <creaImageIOTreeAttributeDescriptor.h>
37 #include <vtkStringArray.h>
38 #include <creaImageIOGimmick.h>
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)
159 GimmickMessage(2,"Reading attributes from DICOM file '"
160 <<filename<<"'"<<std::endl);
164 reader.SetFileName( filename.c_str() );
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)
172 if ( i->first == "D0004_1500" )
174 boost::filesystem::path full_path(filename);
175 std::string f = full_path.leaf().string();
178 else if ( i->first == "FullFileName" )
180 i->second = filename;
182 else if ( i->first == "FullFileDirectory" )
184 std::string::size_type last_pos = filename.find_last_of("//");
185 //find first separator
186 i->second = filename.substr(0, last_pos);
193 gdcm::Attribute< 0x0008,0x103e> at;
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 ) )
203 i->second = irclean(sf.ToString(tag));
214 void DicomImageReader::ReadAttributes2(const std::string& filename,
215 std::map<std::string,std::string>& attr)
219 std::map<std::string,std::string>::iterator i;
220 for (i=attr.begin();i!=attr.end();++i)
222 if ( i->first == "D0004_1500" || i->first == "FullFileName" || i->first == "FullFileDirectory" )
231 tree::AttributeDescriptor::GetDicomGroupElementFromKey(i->first,gr,el);
232 mscan.AddTag(gdcm::Tag(gr,el) );
237 bool b = mscan.IsKey(filename.c_str());
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)
245 if ( i->first == "D0004_1500" )
247 boost::filesystem::path full_path(filename);
248 std::string f = full_path.leaf().string();
251 else if ( i->first == "FullFileName" )
253 i->second = filename;
255 else if ( i->first == "FullFileDirectory" )
257 std::string::size_type last_pos = filename.find_last_of("//");
258 //find first separator
259 i->second = filename.substr(0, last_pos);
263 const char *value = it->second;
264 i->second = irclean(it->second);
270 const std::string DicomImageReader::GetStringValueFromTag(const gdcm::DataElement& de)
272 static std::string buffer;
273 buffer = ""; // cleanup previous call
276 const gdcm::ByteValue *bv = de.GetByteValue();
277 if( bv ) // Can be Type 2
279 buffer = std::string( bv->GetPointer(), bv->GetLength() );
280 // Will be padded with at least one \0
283 // Since return is a const char* the very first \0 will be considered
284 return buffer.c_str();
286 //=====================================================================
288 } // namespace creaImageIO