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>
29 #include "creaImageIOSystem.h"
30 #include <boost/filesystem.hpp>
31 #include "boost/algorithm/string.hpp"
35 #include <creaImageIOSystem.h>
36 #include "boost/filesystem/path.hpp"
38 #include <creaImageIOTreeAttributeDescriptor.h>
39 #include <vtkStringArray.h>
40 #include <creaImageIOGimmick.h>
41 #include <gdcmDirectory.h>
52 //=====================================================================
53 DicomImageScanner::DicomImageScanner()
55 mReader = vtkGDCMImageReader::New();
59 //=====================================================================
61 //=====================================================================
62 DicomImageScanner::~DicomImageScanner()
66 //=====================================================================
68 //=====================================================================
69 bool DicomImageScanner::addDirectory(std::string& filename, std::map<std::string,std::string>& attr)
74 std::map<std::string,std::string>::iterator i;
76 for (i=attr.begin();i!=attr.end();++i, j++)
78 if ( i->first == "D0004_1500" || i->first == "FullFileName" || i->first == "FullFileDirectory" )
87 tree::AttributeDescriptor::GetDicomGroupElementFromKey(i->first,gr,el);
88 mscan.AddTag(gdcm::Tag(gr,el) );
96 boost::algorithm::replace_all(filename,"\\", "/");
97 d.Load(filename.c_str(),true);
98 mscan.Scan(d.GetFilenames());
103 //=====================================================================
105 //=====================================================================
106 vtkImageData* DicomImageScanner::ReadImage(const std::string& filename)
108 vtkImageData* im = 0;
111 mReader->SetFileName(filename.c_str());
113 im = vtkImageData::New();
114 im->ShallowCopy(mReader->GetOutput());
118 if (im!=0) im->Delete();
125 //=====================================================================
127 //========================================================================
128 std::string DicomImageScanner::irclean(const std::string& str)
132 if (str == "GDCM::Unfound")
136 if (str[str.size()-1]==' ')
138 return irclean(str.substr(0,str.size()-1));
140 if (str[str.size()-1]==0)
142 return irclean(str.substr(0,str.size()-1));
148 //========================================================================
150 //=====================================================================
152 void DicomImageScanner::ReadAttributes(const std::string& filename,
153 std::map<std::string,std::string>& attr)
155 std::string name = "E:\\data-images\\dicoms\\CD1\\DICOM\\09112417\\37390000/31582235";
157 bool b = mscan.IsKey(filename.c_str());
159 const gdcm::Scanner::TagToValue &mapping = mscan.GetMapping(filename.c_str());
160 gdcm::Scanner::TagToValue::const_iterator it = mapping.begin();
162 std::map<std::string, std::string>::iterator i;
164 for (;it != mapping.end(); ++it)
166 sprintf(key,"D%04x_%04x", it->first.GetGroup(), it->first.GetElement());
167 attr[key] = irclean(it->second);
174 for (i=attr.begin();i!=attr.end();++i, j++)
176 if ( attr.find("D0004_1500") != attr.end())
178 boost::filesystem::path full_path(filename);
179 std::string f = full_path.leaf().string();
180 attr["D0004_1500"] = f;
182 if ( attr.find("FullFileName")!= attr.end())
184 attr["FullFileName"] = filename;
186 if ( attr.find("FullFileDirectory" )!= attr.end())
188 std::string::size_type last_pos = filename.find_last_of("//");
189 //find first separator
190 attr["FullFileDirectory" ] = filename.substr(0, last_pos);
198 // tree::AttributeDescriptor::GetDicomGroupElementFromKey(i->first,gr,el);
200 // mscan.AddTag(gdcm::Tag(gr,el) );
202 // // const char *value = it->second;
203 // i->second = irclean(it->second);
225 const std::string DicomImageScanner::GetStringValueFromTag(const gdcm::DataElement& de)
227 static std::string buffer;
228 buffer = ""; // cleanup previous call
231 const gdcm::ByteValue *bv = de.GetByteValue();
232 if( bv ) // Can be Type 2
234 buffer = std::string( bv->GetPointer(), bv->GetLength() );
235 // Will be padded with at least one \0
239 // Since return is a const char* the very first \0 will be considered
240 return buffer.c_str();
242 //=====================================================================
244 } // namespace creaImageIO