Medical Imaging Interaction Toolkit  2018.4.99-3e3f1a6e
Medical Imaging Interaction Toolkit
mitkBaseDICOMReaderService.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 
14 
15 #include <mitkCustomMimeType.h>
16 #include <mitkIOMimeTypes.h>
18 #include <mitkImage.h>
19 #include <mitkDICOMFilesHelper.h>
21 #include <mitkDICOMProperty.h>
24 #include <mitkLocaleSwitch.h>
25 #include "mitkIPropertyProvider.h"
26 #include "mitkPropertyNameHelper.h"
27 
28 #include <iostream>
29 
30 
31 #include <itksys/SystemTools.hxx>
32 #include <itksys/Directory.hxx>
33 
34 namespace mitk {
35 
36  BaseDICOMReaderService::BaseDICOMReaderService(const std::string& description)
37  : AbstractFileReader(CustomMimeType(IOMimeTypes::DICOM_MIMETYPE()), description)
38 {
39 }
40 
41  BaseDICOMReaderService::BaseDICOMReaderService(const mitk::CustomMimeType& customType, const std::string& description)
42  : AbstractFileReader(customType, description)
43  {
44  }
45 
46 std::vector<itk::SmartPointer<BaseData> > BaseDICOMReaderService::Read()
47 {
48  std::vector<BaseData::Pointer> result;
49 
50 
51  std::string fileName = this->GetLocalFileName();
52  //special handling of Philips 3D US DICOM.
53  //Copied from DICOMSeriesReaderService
55  {
56  MITK_INFO << "it is a Philips3D US Dicom file" << std::endl;
57  mitk::LocaleSwitch localeSwitch("C");
58  std::locale previousCppLocale(std::cin.getloc());
59  std::locale l("C");
60  std::cin.imbue(l);
61 
64  stringvec.push_back(fileName);
65  if (DicomSeriesReader::LoadDicomSeries(stringvec, *node))
66  {
67  BaseData::Pointer data = node->GetData();
68  StringProperty::Pointer nameProp = StringProperty::New(itksys::SystemTools::GetFilenameName(fileName));
69  data->GetPropertyList()->SetProperty("name", nameProp);
70  result.push_back(data);
71  }
72  std::cin.imbue(previousCppLocale);
73  return result;
74  }
75 
76  //Normal DICOM handling (It wasn't a Philips 3D US)
77  mitk::StringList relevantFiles = this->GetRelevantFiles();
78 
79  // check whether directory or file
80  // if directory try to find first file within it instead
81  // We only support this for a single directory at once
82  if (relevantFiles.empty())
83  {
84  bool pathIsDirectory = itksys::SystemTools::FileIsDirectory(this->GetLocalFileName());
85  if (pathIsDirectory)
86  {
87  itksys::Directory input;
88  input.Load(this->GetLocalFileName().c_str());
89 
90  std::vector<std::string> files;
91  for (unsigned long idx = 0; idx<input.GetNumberOfFiles(); idx++)
92  {
93  if (!itksys::SystemTools::FileIsDirectory(input.GetFile(idx)))
94  {
95  std::string fullpath = this->GetLocalFileName() + "/" + std::string(input.GetFile(idx));
96  files.push_back(fullpath.c_str());
97  }
98  }
99  relevantFiles = files;
100  }
101  }
102 
103  if (relevantFiles.empty())
104  {
105  MITK_INFO << "DICOMReader service found no relevant files in specified location. No data is loaded. Location: "<<fileName;
106  }
107  else
108  {
109  mitk::DICOMFileReader::Pointer reader = this->GetReader(relevantFiles);
110 
111  if(reader.IsNull())
112  {
113  MITK_INFO << "DICOMReader service found no suitable reader configuration for relevant files.";
114  }
115  else
116  {
117  const unsigned int ntotalfiles = relevantFiles.size();
118 
119  for( unsigned int i=0; i< ntotalfiles; i++)
120  {
121  m_ReadFiles.push_back( relevantFiles.at(i) );
122  }
123 
124  reader->SetAdditionalTagsOfInterest(mitk::GetCurrentDICOMTagsOfInterest());
125  reader->SetTagLookupTableToPropertyFunctor(mitk::GetDICOMPropertyForDICOMValuesFunctor);
126  reader->SetInputFiles(relevantFiles);
127 
128  mitk::DICOMDCMTKTagScanner::Pointer scanner = mitk::DICOMDCMTKTagScanner::New();
129  scanner->AddTagPaths(reader->GetTagsOfInterest());
130  scanner->SetInputFiles(relevantFiles);
131  scanner->Scan();
132 
133  reader->SetTagCache(scanner->GetScanCache());
134  reader->AnalyzeInputFiles();
135  reader->LoadImages();
136 
137  for (unsigned int i = 0; i < reader->GetNumberOfOutputs(); ++i)
138  {
139  const mitk::DICOMImageBlockDescriptor& desc = reader->GetOutput(i);
140  mitk::BaseData::Pointer data = desc.GetMitkImage().GetPointer();
141 
142  std::string nodeName = GenerateNameFromDICOMProperties(&desc);
143 
144  StringProperty::Pointer nameProp = StringProperty::New(nodeName);
145  data->SetProperty("name", nameProp);
146 
147  result.push_back(data);
148  }
149  }
150  }
151 
152  return result;
153 }
154 
156 {
157  std::string fileName = this->GetLocalFileName();
158 
159  mitk::StringList relevantFiles = mitk::GetDICOMFilesInSameDirectory(fileName);
160 
161  return relevantFiles;
162 }
163 
165 {
167 
168  if (Unsupported == abstractConfidence)
169  {
170  if (itksys::SystemTools::FileIsDirectory(this->GetInputLocation().c_str()))
171  {
172  // In principle we support dicom directories
173  return Supported;
174  }
175  }
176 
177  return abstractConfidence;
178 }
179 
181 {
182  std::string nodeName = mitk::DataNode::NO_NAME_VALUE();
183 
184  auto studyProp = provider->GetConstProperty(mitk::GeneratePropertyNameForDICOMTag(0x0008, 0x1030).c_str());
185  if (studyProp.IsNotNull())
186  {
187  nodeName = studyProp->GetValueAsString();
188  }
189 
190  auto seriesProp = provider->GetConstProperty(mitk::GeneratePropertyNameForDICOMTag(0x0008, 0x103E).c_str());
191 
192  if (seriesProp.IsNotNull())
193  {
194  if (studyProp.IsNotNull())
195  {
196  nodeName += " / ";
197  }
198  else
199  {
200  nodeName = "";
201 
202  }
203  nodeName += seriesProp->GetValueAsString();
204  }
205 
206  return nodeName;
207 };
208 
209 }
The IOMimeTypes class.
DICOMTagPathMapType MITKDICOMREADER_EXPORT GetCurrentDICOMTagsOfInterest()
#define MITK_INFO
Definition: mitkLogMacros.h:18
ConfidenceLevel GetConfidenceLevel() const override
The confidence level of the reader or writer implementation.
std::vector< itk::SmartPointer< BaseData > > Read() override
DICOMFilePathList GetDICOMFilesInSameDirectory(const std::string &filePath)
std::vector< std::string > StringContainer
Lists of filenames.
std::string MITKCORE_EXPORT GeneratePropertyNameForDICOMTag(unsigned int group, unsigned int element)
Image::Pointer GetMitkImage() const
the 3D mitk::Image that is loaded from the DICOM files of a DICOMImageFrameList
static DataNode::Pointer LoadDicomSeries(const StringContainer &filenames, bool sort=true, bool load4D=true, bool correctGantryTilt=true, UpdateCallBackMethod callback=nullptr, itk::SmartPointer< Image > preLoadedImageBlock=nullptr)
DataCollection - Class to facilitate loading/accessing structured data.
virtual BaseProperty::ConstPointer GetConstProperty(const std::string &propertyKey, const std::string &contextName="", bool fallBackOnDefaultContext=true) const =0
Get property by its key.
virtual mitk::DICOMFileReader::Pointer GetReader(const mitk::StringList &relevantFiles) const =0
std::vector< std::string > m_ReadFiles
MITKDICOMREADER_EXPORT mitk::BaseProperty::Pointer GetDICOMPropertyForDICOMValuesFunctor(const DICOMCachedValueLookupTable &cacheLookupTable)
BaseDICOMReaderService(const std::string &description)
static std::string NO_NAME_VALUE()
Definition: mitkDataNode.h:394
The CustomMimeType class represents a custom mime-type which may be registered as a service object...
std::string GetLocalFileName() const
Get a local file name for reading.
ConfidenceLevel
A confidence level describing the confidence of the reader or writer in handling the given data...
Definition: mitkIFileIO.h:45
Convenience class to temporarily change the current locale.
static Pointer New()
Output descriptor for DICOMFileReader.
IFileReader::ConfidenceLevel GetConfidenceLevel() const override
The confidence level of the reader or writer implementation.
std::string MITKDICOMREADER_EXPORT GenerateNameFromDICOMProperties(const mitk::IPropertyProvider *provider)
static bool IsPhilips3DDicom(const std::string &filename)
Checks if a specific file is a Philips3D ultrasound DICOM file.
std::vector< std::string > StringList
Base class for creating mitk::BaseData objects from files or streams.
std::string GetInputLocation() const override
Get the current input location.
static Pointer New()