Medical Imaging Interaction Toolkit  2018.4.99-87d68d9f
Medical Imaging Interaction Toolkit
mitkDICOMDCMTKTagScanner.cpp
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
15 
16 #include <dcmtk/dcmdata/dcfilefo.h>
17 #include <dcmtk/dcmdata/dcpath.h>
18 
20 {
21 }
22 
24 {
25 }
26 
28 {
29  m_ScannedTags.insert( DICOMTagPath(tag) );
30 }
31 
33 {
34  for ( auto tagIter = tags.cbegin(); tagIter != tags.cend(); ++tagIter )
35  {
36  this->AddTag( *tagIter );
37  }
38 }
39 
41 {
42  m_ScannedTags.insert(path);
43 }
44 
46 {
47  for (const auto& path : paths)
48  {
49  this->AddTagPath(path);
50  }
51 }
52 
54 {
55  m_InputFilenames = filenames;
56 }
57 
59 {
60  mitk::DICOMTagPath result;
61 
62  OFListConstIterator(DcmPathNode*) it = dcmpath->begin();
63  OFListConstIterator(DcmPathNode*) endOfList = dcmpath->end();
64  OFString pathStr; DcmEVR vr; DcmObject* obj;
65 
66  while (it != endOfList)
67  {
68  if (((*it) == nullptr) || ((*it)->m_obj == nullptr))
69  {
70  mitkThrow() << "Error in DcmPathToTagPath(). Invalid search result";
71  }
72  obj = (*it)->m_obj;
73  vr = obj->ident();
74 
75  if ((vr == EVR_SQ) || (obj->isLeaf()))
76  {
77  result.AddElement(obj->getTag().getGroup(), obj->getTag().getElement());
78  }
79  else if ((vr == EVR_item) || (vr == EVR_dataset))
80  {
81  if (result.Size() > 0)
82  {
84  result.GetLastNode().selection = (*it)->m_itemNo;
85  }
86  else
87  {
88  mitkThrow() << "Error in DcmPathToTagPath(). DCMTK path is illegal due to toplevel sequence item.";
89  }
90  }
91  else
92  {
94  }
95  ++it;
96  }
97 
98  return result;
99 }
100 
102 {
103  this->PushLocale();
104 
105  try
106  {
107  DcmPathProcessor processor;
108  processor.setItemWildcardSupport(true);
109 
110  DICOMGenericTagCache::Pointer newCache = DICOMGenericTagCache::New();
111 
112  for (const auto& fileName : this->m_InputFilenames)
113  {
114  DcmFileFormat dfile;
115  OFCondition cond = dfile.loadFile(fileName.c_str());
116  if (cond.bad())
117  {
118  MITK_ERROR << "Error when scanning for tags. Cannot open given file. File: " << fileName;
119  }
120  else
121  {
122  DICOMGenericImageFrameInfo::Pointer info = DICOMGenericImageFrameInfo::New(fileName);
123 
124  for (const auto& path : this->m_ScannedTags)
125  {
126  std::string tagPath = DICOMTagPathToDCMTKSearchPath(path);
127  cond = processor.findOrCreatePath(dfile.getDataset(), tagPath.c_str());
128  if (cond.good())
129  {
130  OFList< DcmPath * > findings;
131  processor.getResults(findings);
132  for (const auto& finding : findings)
133  {
134  auto element = dynamic_cast<DcmElement*>(finding->back()->m_obj);
135  if (!element)
136  {
137  auto item = dynamic_cast<DcmItem*>(finding->back()->m_obj);
138  if (item)
139  {
140  element = item->getElement(finding->back()->m_itemNo);
141  }
142  }
143 
144  if (element)
145  {
146  OFString value;
147  cond = element->getOFStringArray(value);
148  if (cond.good())
149  {
150  info->SetTagValue(DcmPathToTagPath(finding), std::string(value.c_str()));
151  }
152  }
153  }
154  }
155  }
156  newCache->AddFrameInfo(info);
157  }
158  }
159 
160  m_Cache = newCache;
161 
162  this->PopLocale();
163  }
164  catch (...)
165  {
166  this->PopLocale();
167  throw;
168  }
169 }
170 
171 mitk::DICOMTagCache::Pointer
173 {
174  return m_Cache.GetPointer();
175 }
176 
178 {
179  if (m_Cache.IsNotNull())
180  {
181  return m_Cache->GetFrameInfoList();
182  }
184 }
DICOMTagPath & AddElement(unsigned int group, unsigned int element)
Class is used to identify (nested) attributes in a DICOM dataset. In contrast to the class DICOMTag...
void SetInputFiles(const StringList &filenames) override
Define the list of files to scan. This does not ADD to an internal list, but it replaces the whole li...
DICOMGenericTagCache::Pointer m_Cache
#define MITK_ERROR
Definition: mitkLogMacros.h:20
std::vector< DICOMTag > DICOMTagList
Definition: mitkDICOMTag.h:59
Representation of a DICOM tag.
Definition: mitkDICOMTag.h:32
PathIndexType AddNode(const NodeInfo &newNode)
void Scan() override
Start the scanning process. Calling Scan() will invalidate previous scans, forgetting all about files...
static void info(const char *fmt,...)
Definition: svm.cpp:86
void AddTagPath(const DICOMTagPath &tag) override
Add this tag path to the scanning process.
PathIndexType Size() const
void AddTags(const DICOMTagList &tags) override
Add a list of tags to the scanning process.
void AddTag(const DICOMTag &tag) override
Add this tag to the scanning process.
DICOMTagCache::Pointer GetScanCache() const override
Retrieve Pointer to the complete cache of the scan.
#define mitkThrow()
std::vector< std::string > StringList
std::vector< DICOMTagPath > DICOMTagPathList
void PopLocale() const
Activate last remembered locale from locale stack "C" locale is required for correct parsing of numbe...
void PushLocale() const
Remember current locale on stack, activate "C" locale. "C" locale is required for correct parsing of ...
std::vector< DICOMDatasetAccessingImageFrameInfo::Pointer > DICOMDatasetAccessingImageFrameList
void AddTagPaths(const DICOMTagPathList &tags) override
Add a list of tag pathes to the scanning process.
std::set< DICOMTagPath > m_ScannedTags
ItemSelectionIndex selection
DICOMDatasetAccessingImageFrameList GetFrameInfoList() const override
Retrieve a result list for file-by-file tag access.
MITKDICOMREADER_EXPORT std::string DICOMTagPathToDCMTKSearchPath(const DICOMTagPath &tagPath)
mitk::DICOMTagPath DcmPathToTagPath(DcmPath *dcmpath)