Medical Imaging Interaction Toolkit  2016.11.0
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,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
19 
20 #include <dcfilefo.h>
21 #include <dcpath.h>
22 
24 {
25 }
26 
28 {
29 }
30 
32 {
33  m_ScannedTags.insert( DICOMTagPath(tag) );
34 }
35 
37 {
38  for ( auto tagIter = tags.cbegin(); tagIter != tags.cend(); ++tagIter )
39  {
40  this->AddTag( *tagIter );
41  }
42 }
43 
45 {
46  m_ScannedTags.insert(path);
47 }
48 
50 {
51  for (const auto& path : paths)
52  {
53  this->AddTagPath(path);
54  }
55 }
56 
58 {
59  m_InputFilenames = filenames;
60 }
61 
63 {
64  mitk::DICOMTagPath result;
65 
66  OFListConstIterator(DcmPathNode*) it = dcmpath->begin();
67  OFListConstIterator(DcmPathNode*) endOfList = dcmpath->end();
68  OFString pathStr; DcmEVR vr; DcmObject* obj;
69 
70  while (it != endOfList)
71  {
72  if (((*it) == NULL) || ((*it)->m_obj == NULL))
73  {
74  mitkThrow() << "Error in DcmPathToTagPath(). Invalid search result";
75  }
76  obj = (*it)->m_obj;
77  vr = obj->ident();
78 
79  if ((vr == EVR_SQ) || (obj->isLeaf()))
80  {
81  result.AddElement(obj->getTag().getGroup(), obj->getTag().getElement());
82  }
83  else if ((vr == EVR_item) || (vr == EVR_dataset))
84  {
85  if (result.Size() > 0)
86  {
88  result.GetLastNode().selection = (*it)->m_itemNo;
89  }
90  else
91  {
92  mitkThrow() << "Error in DcmPathToTagPath(). DCMTK path is illegal due to toplevel sequence item.";
93  }
94  }
95  else
96  {
98  }
99  ++it;
100  }
101 
102  return result;
103 }
104 
106 {
107  this->PushLocale();
108 
109  try
110  {
111  DcmPathProcessor processor;
112  processor.setItemWildcardSupport(true);
113 
115 
116  for (const auto& fileName : this->m_InputFilenames)
117  {
118  DcmFileFormat dfile;
119  OFCondition cond = dfile.loadFile(fileName.c_str());
120  if (cond.bad())
121  {
122  MITK_ERROR << "Error when scanning for tags. Cannot open given file. File: " << fileName;
123  }
124  else
125  {
127 
128  for (const auto& path : this->m_ScannedTags)
129  {
130  std::string tagPath = DICOMTagPathToDCMTKSearchPath(path);
131  cond = processor.findOrCreatePath(dfile.getDataset(), tagPath.c_str());
132  if (cond.good())
133  {
134  OFList< DcmPath * > findings;
135  processor.getResults(findings);
136  for (const auto& finding : findings)
137  {
138  auto element = dynamic_cast<DcmElement*>(finding->back()->m_obj);
139  if (!element)
140  {
141  auto item = dynamic_cast<DcmItem*>(finding->back()->m_obj);
142  if (item)
143  {
144  element = item->getElement(finding->back()->m_itemNo);
145  }
146  }
147 
148  if (element)
149  {
150  OFString value;
151  cond = element->getOFStringArray(value);
152  if (cond.good())
153  {
154  info->SetTagValue(DcmPathToTagPath(finding), std::string(value.c_str()));
155  }
156  }
157  }
158  }
159  }
160  newCache->AddFrameInfo(info);
161  }
162  }
163 
164  m_Cache = newCache;
165 
166  this->PopLocale();
167  }
168  catch (...)
169  {
170  this->PopLocale();
171  throw;
172  }
173 }
174 
177 {
178  return m_Cache.GetPointer();
179 }
180 
182 {
183  if (m_Cache.IsNotNull())
184  {
185  return m_Cache->GetFrameInfoList();
186  }
188 }
DICOMTagPath & AddElement(unsigned int group, unsigned int element)
itk::SmartPointer< Self > Pointer
Class is used to identify (nested) attributes in a DICOM dataset. In contrast to the class DICOMTag...
virtual 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...
#define MITK_ERROR
Definition: mitkLogMacros.h:24
std::vector< DICOMTag > DICOMTagList
Definition: mitkDICOMTag.h:64
Representation of a DICOM tag.
Definition: mitkDICOMTag.h:37
PathIndexType AddNode(const NodeInfo &newNode)
static void info(const char *fmt,...)
Definition: svm.cpp:100
virtual void AddTagPath(const DICOMTagPath &tag) override
Add this tag path to the scanning process.
PathIndexType Size() const
virtual void AddTags(const DICOMTagList &tags) override
Add a list of tags to the scanning process.
virtual void AddTag(const DICOMTag &tag) override
Add this tag to the scanning process.
virtual 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
std::vector< DICOMDatasetAccessingImageFrameInfo::Pointer > DICOMDatasetAccessingImageFrameList
virtual void AddTagPaths(const DICOMTagPathList &tags) override
Add a list of tag pathes to the scanning process.
ItemSelectionIndex selection
virtual DICOMDatasetAccessingImageFrameList GetFrameInfoList() const override
Retrieve a result list for file-by-file tag access.
virtual void Scan()
Start the scanning process. Calling Scan() will invalidate previous scans, forgetting all about files...
MITKDICOMREADER_EXPORT std::string DICOMTagPathToDCMTKSearchPath(const DICOMTagPath &tagPath)
mitk::DICOMTagPath DcmPathToTagPath(DcmPath *dcmpath)