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 # ------------------------------------------------------------------------
28 #include <creaImageIODicomScanner.h>
30 #include <boost/filesystem.hpp>
31 #include "boost/algorithm/string.hpp"
32 #include "boost/filesystem/path.hpp"
36 #pragma warning(disable: 4996)
45 //=====================================================================
46 DicomImageScanner::DicomImageScanner()
48 mReader = vtkGDCMImageReader::New();
52 //=====================================================================
54 //=====================================================================
55 DicomImageScanner::~DicomImageScanner()
59 //=====================================================================
61 //=====================================================================
62 bool DicomImageScanner::addDirectory(std::string& filename, std::map<std::string,std::string>& attr)
67 std::map<std::string,std::string>::iterator i;
69 for (i=attr.begin();i!=attr.end();++i, j++)
71 if ( i->first == "D0004_1500" || i->first == "FullFileName" || i->first == "FullFileDirectory" )
80 tree::AttributeDescriptor::GetDicomGroupElementFromKey(i->first,gr,el);
81 mscan.AddTag(gdcm::Tag(gr,el) );
89 boost::algorithm::replace_all(filename,"\\", "/");
90 d.Load(filename.c_str(),true);
91 mscan.Scan(d.GetFilenames());
96 //=====================================================================
98 //=====================================================================
99 vtkImageData* DicomImageScanner::ReadImage(const std::string& filename)
101 vtkImageData* im = 0;
104 mReader->SetFileName(filename.c_str());
106 im = vtkImageData::New();
107 im->ShallowCopy(mReader->GetOutput());
111 if (im!=0) im->Delete();
118 //=====================================================================
120 //========================================================================
121 std::string DicomImageScanner::irclean(const std::string& str)
125 if (str == "GDCM::Unfound")
129 if (str[str.size()-1]==' ')
131 return irclean(str.substr(0,str.size()-1));
133 if (str[str.size()-1]==0)
135 return irclean(str.substr(0,str.size()-1));
141 //========================================================================
143 //=====================================================================
145 void DicomImageScanner::ReadAttributes(const std::string& filename,
146 std::map<std::string,std::string>& attr)
148 std::string name = "E:\\data-images\\dicoms\\CD1\\DICOM\\09112417\\37390000/31582235";
150 bool b = mscan.IsKey(filename.c_str());
152 const gdcm::Scanner::TagToValue &mapping = mscan.GetMapping(filename.c_str());
153 gdcm::Scanner::TagToValue::const_iterator it = mapping.begin();
155 std::map<std::string, std::string>::iterator i;
157 for (;it != mapping.end(); ++it)
159 sprintf(key,"D%04x_%04x", it->first.GetGroup(), it->first.GetElement());
160 attr[key] = irclean(it->second);
167 for (i=attr.begin();i!=attr.end();++i, j++)
169 if ( attr.find("D0004_1500") != attr.end())
171 boost::filesystem::path full_path(filename);
172 std::string f = full_path.leaf().string();
173 attr["D0004_1500"] = f;
175 if ( attr.find("FullFileName")!= attr.end())
177 attr["FullFileName"] = filename;
179 if ( attr.find("FullFileDirectory" )!= attr.end())
181 std::string::size_type last_pos = filename.find_last_of("//");
182 //find first separator
183 attr["FullFileDirectory" ] = filename.substr(0, last_pos);
191 // tree::AttributeDescriptor::GetDicomGroupElementFromKey(i->first,gr,el);
193 // mscan.AddTag(gdcm::Tag(gr,el) );
195 // // const char *value = it->second;
196 // i->second = irclean(it->second);
218 const std::string DicomImageScanner::GetStringValueFromTag(const gdcm::DataElement& de)
220 static std::string buffer;
221 buffer = ""; // cleanup previous call
224 const gdcm::ByteValue *bv = de.GetByteValue();
225 if( bv ) // Can be Type 2
227 buffer = std::string( bv->GetPointer(), bv->GetLength() );
228 // Will be padded with at least one \0
232 // Since return is a const char* the very first \0 will be considered
233 return buffer.c_str();
235 //=====================================================================
237 } // namespace creaImageIO