Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkLegacyFileReaderService.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 
18 
19 #include <mitkCustomMimeType.h>
20 #include <mitkDicomSeriesReader.h>
21 #include <mitkIOAdapter.h>
22 #include <mitkIOMimeTypes.h>
23 #include <mitkProgressBar.h>
24 
26  : mitk::AbstractFileReader(other)
27 {
28 }
29 
30 mitk::LegacyFileReaderService::LegacyFileReaderService(const std::vector<std::string> &extensions,
31  const std::string &category)
33 {
35 
36  CustomMimeType customMimeType;
37  customMimeType.SetCategory(category);
38 
39  for (auto extension : extensions)
40  {
41  if (!extension.empty() && extension[0] == '.')
42  {
43  extension.assign(extension.begin() + 1, extension.end());
44  }
45  customMimeType.AddExtension(extension);
46  }
47  this->SetDescription(category);
48  this->SetMimeType(customMimeType);
49 
50  m_ServiceReg = this->RegisterService();
51 }
52 
54 {
55 }
56 
58 
59 std::vector<itk::SmartPointer<mitk::BaseData>> mitk::LegacyFileReaderService::Read()
60 {
61  std::vector<BaseData::Pointer> result;
62 
63  std::list<IOAdapterBase::Pointer> possibleIOAdapter;
64  std::list<itk::LightObject::Pointer> allobjects = itk::ObjectFactoryBase::CreateAllInstance("mitkIOAdapter");
65 
66  for (std::list<itk::LightObject::Pointer>::iterator i = allobjects.begin(); i != allobjects.end(); ++i)
67  {
68  IOAdapterBase *io = dynamic_cast<IOAdapterBase *>(i->GetPointer());
69  if (io)
70  {
71  possibleIOAdapter.push_back(io);
72  }
73  else
74  {
75  MITK_ERROR << "Error BaseDataIO factory did not return an IOAdapterBase: " << (*i)->GetNameOfClass() << std::endl;
76  }
77  }
78 
79  const std::string path = this->GetLocalFileName();
80  for (std::list<IOAdapterBase::Pointer>::iterator k = possibleIOAdapter.begin(); k != possibleIOAdapter.end(); ++k)
81  {
82  bool canReadFile = (*k)->CanReadFile(path, "", ""); // they could read the file
83 
84  if (canReadFile)
85  {
86  BaseDataSource::Pointer ioObject = (*k)->CreateIOProcessObject(path, "", "");
87  ioObject->Update();
88  int numberOfContents = static_cast<int>(ioObject->GetNumberOfOutputs());
89 
90  if (numberOfContents > 0)
91  {
92  BaseData::Pointer baseData;
93  for (int i = 0; i < numberOfContents; ++i)
94  {
95  baseData = dynamic_cast<BaseData *>(ioObject->GetOutputs()[i].GetPointer());
96  if (baseData) // this is what's wanted, right?
97  {
98  result.push_back(baseData);
99  }
100  }
101  }
102 
103  break;
104  }
105  }
106 
107  if (result.empty())
108  {
109  mitkThrow() << "Could not read file '" << path << "'";
110  }
111 
112  return result;
113 }
114 
115 mitk::LegacyFileReaderService *mitk::LegacyFileReaderService::Clone() const
116 {
117  return new LegacyFileReaderService(*this);
118 }
Base of all data objects.
Definition: mitkBaseData.h:39
#define MITK_ERROR
Definition: mitkLogMacros.h:24
DataCollection - Class to facilitate loading/accessing structured data.
void SetMimeType(const CustomMimeType &mimeType)
The CustomMimeType class represents a custom mime-type which may be registered as a service object...
void SetMimeTypePrefix(const std::string &prefix)
us::ServiceRegistration< IFileReader > RegisterService(us::ModuleContext *context=us::GetModuleContext())
static std::string DEFAULT_BASE_NAME()
IOAdapterBase class is an abstract adapter class for IO process objects.
Definition: mitkIOAdapter.h:31
#define mitkThrow()
void SetDescription(const std::string &description)
virtual std::vector< itk::SmartPointer< BaseData > > Read() override
Reads a path or stream and creates a list of BaseData objects.
void AddExtension(const std::string &extension)
LegacyFileReaderService(const LegacyFileReaderService &other)
void SetCategory(const std::string &category)
Base class for creating mitk::BaseData objects from files or streams.