7 #include <dcmtk/config/osconfig.h> 9 #include "fwDcmtkIO/helper/DicomDir.hpp" 11 #include <fwCore/exceptionmacros.hpp> 14 #include <fwDcmtkTools/Dictionary.hpp> 16 #include <boost/algorithm/string/classification.hpp> 17 #include <boost/algorithm/string/regex.hpp> 18 #include <boost/algorithm/string/split.hpp> 19 #include <boost/filesystem/operations.hpp> 20 #include <boost/regex.h> 22 #include <dcmtk/dcmdata/dcdeftag.h> 23 #include <dcmtk/dcmdata/dcdicdir.h> 24 #include <dcmtk/dcmnet/diutil.h> 33 SLM_ASSERT(
"You must specify a valid directory.", ::boost::filesystem::is_directory(root));
40 ::boost::filesystem::path dicomDirPath = root /
"dicomdir";
41 bool dicomdirExists = ::boost::filesystem::exists(dicomDirPath) && !::boost::filesystem::is_directory(dicomDirPath);
45 dicomDirPath = root /
"DICOMDIR";
46 dicomdirExists = ::boost::filesystem::exists(dicomDirPath) && !::boost::filesystem::is_directory(dicomDirPath);
51 DcmDicomDir dicomDir(dicomDirPath.string().c_str());
52 DcmDirectoryRecord* rootRecord = &(dicomDir.getRootRecord());
53 DcmDirectoryRecord* patientRecord = NULL;
54 DcmDirectoryRecord* studyRecord = NULL;
55 DcmDirectoryRecord* seriesRecord = NULL;
56 DcmDirectoryRecord* fileRecord = NULL;
60 if (rootRecord != NULL)
67 while (((patientRecord = rootRecord->nextSub(patientRecord)) != NULL))
70 while (((studyRecord = patientRecord->nextSub(studyRecord)) != NULL))
73 while (((seriesRecord = studyRecord->nextSub(seriesRecord)) != NULL))
76 while ((fileRecord = seriesRecord->nextSub(fileRecord)) != NULL)
78 if (fileRecord->findAndGetOFStringArray(DCM_ReferencedFileID, tmpString).good())
80 ::boost::filesystem::path realPath = DicomDir::getRealFilename(root,
82 dicomFiles.push_back(realPath.string());
86 SLM_WARN(
"Dicom instance doesn't have a referenced file id.");
93 catch(std::exception e)
101 SLM_WARN(
"Root record is null, unable to get information from dicomdir file.");
107 SLM_WARN(
"Dicomdir file is missing");
115 std::string DicomDir::createRegex(std::string filename)
117 std::string regex =
"";
118 for(std::string::iterator it = filename.begin(); it != filename.end(); ++it)
121 char lower =
static_cast<char>(upper + 32);
123 std::stringstream ss;
126 if(upper >=
'A' && upper <=
'Z')
128 ss <<
"[" << upper << lower <<
"]";
144 ::boost::filesystem::path DicomDir::getRealFilename(
145 const ::boost::filesystem::path& root,
const std::string& filename)
147 ::boost::filesystem::path result = root;
148 std::vector<std::string> elements;
149 ::boost::split(elements, filename, ::boost::is_any_of(
"/\\"));
152 for(std::vector<std::string>::iterator it = elements.begin(); it != elements.end(); ++it)
154 std::string current = *it;
155 ::boost::regex regex(DicomDir::createRegex(current));
157 ::boost::filesystem::directory_iterator dirIt(result), dirItEnd;
159 bool matchFound =
false;
162 for (; dirIt != dirItEnd; ++dirIt)
164 child = dirIt->path().filename().string();
165 if (::boost::regex_match(child, regex))
167 FW_RAISE_IF(
"There is more than one file matching the uppercase filename stored " 168 "in the dicom instance: \""+filename+
"\"", matchFound);
175 FW_RAISE_IF(
"There is no file matching the uppercase filename stored in the dicom instance: \""+filename+
"\"",
static FWDCMTKIO_API bool readDicomDir(const ::boost::filesystem::path &root, std::vector< std::string > &dicomFiles)
Find Dicom instances in a DicomDir file.
#define SLM_WARN(message)
#define SLM_ASSERT(message, cond)
work like 'assert' from 'cassert', with in addition a message logged by spylog (with FATAL loglevel) ...
fwDcmtkIO contains classes used to pull Dicom images from a pacs using dcmtk library.
This file defines SpyLog macros. These macros are used to log messages to a file or to the console du...