Medical Imaging Interaction Toolkit  2018.4.99-389bf124
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 (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 <mitkIOAdapter.h>
17 #include <mitkIOMimeTypes.h>
18 #include <mitkProgressBar.h>
19 
21  : mitk::AbstractFileReader(other)
22 {
23 }
24 
25 mitk::LegacyFileReaderService::LegacyFileReaderService(const std::vector<std::string> &extensions,
26  const std::string &category)
28 {
30 
31  CustomMimeType customMimeType;
32  customMimeType.SetCategory(category);
33 
34  for (auto extension : extensions)
35  {
36  if (!extension.empty() && extension[0] == '.')
37  {
38  extension.assign(extension.begin() + 1, extension.end());
39  }
40  customMimeType.AddExtension(extension);
41  }
42  this->SetDescription(category);
43  this->SetMimeType(customMimeType);
44 
45  m_ServiceReg = this->RegisterService();
46 }
47 
49 {
50 }
51 
53 
54 std::vector<itk::SmartPointer<mitk::BaseData>> mitk::LegacyFileReaderService::Read()
55 {
56  std::vector<BaseData::Pointer> result;
57 
58  std::list<IOAdapterBase::Pointer> possibleIOAdapter;
59  std::list<itk::LightObject::Pointer> allobjects = itk::ObjectFactoryBase::CreateAllInstance("mitkIOAdapter");
60 
61  for (auto i = allobjects.begin(); i != allobjects.end(); ++i)
62  {
63  auto *io = dynamic_cast<IOAdapterBase *>(i->GetPointer());
64  if (io)
65  {
66  possibleIOAdapter.push_back(io);
67  }
68  else
69  {
70  MITK_ERROR << "Error BaseDataIO factory did not return an IOAdapterBase: " << (*i)->GetNameOfClass() << std::endl;
71  }
72  }
73 
74  const std::string path = this->GetLocalFileName();
75  for (auto k = possibleIOAdapter.begin(); k != possibleIOAdapter.end(); ++k)
76  {
77  bool canReadFile = (*k)->CanReadFile(path, "", ""); // they could read the file
78 
79  if (canReadFile)
80  {
81  BaseDataSource::Pointer ioObject = (*k)->CreateIOProcessObject(path, "", "");
82  ioObject->Update();
83  auto numberOfContents = static_cast<int>(ioObject->GetNumberOfOutputs());
84 
85  if (numberOfContents > 0)
86  {
87  BaseData::Pointer baseData;
88  for (int i = 0; i < numberOfContents; ++i)
89  {
90  baseData = dynamic_cast<BaseData *>(ioObject->GetOutputs()[i].GetPointer());
91  if (baseData) // this is what's wanted, right?
92  {
93  result.push_back(baseData);
94  }
95  }
96  }
97 
98  break;
99  }
100  }
101 
102  if (result.empty())
103  {
104  mitkThrow() << "Could not read file '" << path << "'";
105  }
106 
107  return result;
108 }
109 
110 mitk::LegacyFileReaderService *mitk::LegacyFileReaderService::Clone() const
111 {
112  return new LegacyFileReaderService(*this);
113 }
float k(1.0)
Base of all data objects.
Definition: mitkBaseData.h:37
#define MITK_ERROR
Definition: mitkLogMacros.h:20
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...
std::string GetLocalFileName() const
Get a local file name for reading.
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:27
#define mitkThrow()
void SetDescription(const std::string &description)
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.