Medical Imaging Interaction Toolkit  2026.06.00
Medical Imaging Interaction Toolkit
mitkIOUtil.h
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 
13 #ifndef mitkIOUtil_h
14 #define mitkIOUtil_h
15 
16 #include <MitkCoreExports.h>
17 #include <mitkDataStorage.h>
18 #include <mitkFileSystem.h>
19 #include <mitkImage.h>
20 #include <mitkPointSet.h>
21 #include <mitkSurface.h>
22 
23 #include <mitkFileReaderSelector.h>
24 #include <mitkFileWriterSelector.h>
25 #include <mitkIFileReader.h>
26 #include <mitkIFileWriter.h>
27 
28 #include <fstream>
29 
30 #if !defined(MITK_WINDOWS_NO_UNDEF) && defined(GetTempPath)
31  #undef GetTempPath
32 #endif
33 
34 namespace us
35 {
36  class ModuleResource;
37 }
38 
39 namespace mitk
40 {
41  class PropertyList;
42 
51  {
52  public:
60  {
64  LoadInfo(const std::string &path);
65 
67  std::string m_Path;
69  std::vector<BaseData::Pointer> m_Output;
73  bool m_Cancel;
76  };
77 
85  {
90  virtual bool operator()(LoadInfo &loadInfo) const = 0;
91  };
92 
95  {
101  SaveInfo(const BaseData *baseData, const MimeType &mimeType, const std::string &path);
102 
104  bool operator<(const SaveInfo &other) const;
105 
108 
114  std::string m_Path;
116  bool m_Cancel;
117  };
118 
126  {
131  virtual bool operator()(SaveInfo &saveInfo) const = 0;
132  };
133 
139  static std::string GetProgramPath();
140 
141  enum class AppBundlePath
142  {
143  Parent,
144  Self
145  };
146 
154  static fs::path GetAppBundlePath(AppBundlePath path = AppBundlePath::Parent);
155 
161  static std::string GetTempPath();
162 
168  static char GetDirectorySeparator();
169 
190  static std::string CreateTemporaryFile(std::ofstream &tmpStream,
191  const std::string &templateName = "XXXXXX",
192  const std::string &path = std::string());
193 
216  static std::string CreateTemporaryFile(std::ofstream &tmpStream,
217  std::ios_base::openmode mode,
218  const std::string &templateName = "XXXXXX",
219  std::string path = std::string());
220 
241  static std::string CreateTemporaryFile(const std::string &templateName = "XXXXXX",
242  const std::string &path = std::string());
243 
260  static std::string CreateTemporaryDirectory(const std::string &templateName = "XXXXXX",
261  std::string path = std::string());
262 
279  static DataStorage::SetOfObjects::Pointer Load(const std::string &path, DataStorage &storage,
280  const ReaderOptionsFunctorBase *optionsCallback = nullptr);
281 
297  static DataStorage::SetOfObjects::Pointer Load(const std::string &path,
298  const IFileReader::Options &options,
299  DataStorage &storage);
300 
316  static std::vector<BaseData::Pointer> Load(const std::string &path,
317  const ReaderOptionsFunctorBase *optionsCallback = nullptr);
318 
327  template <typename T>
328  static typename T::Pointer Load(const std::string& path, const ReaderOptionsFunctorBase *optionsCallback = nullptr)
329  {
330  return dynamic_cast<T*>(Load(path, optionsCallback).at(0).GetPointer());
331  }
332 
347  static std::vector<BaseData::Pointer> Load(const std::string &path, const IFileReader::Options &options);
348 
354  template <typename T>
355  static typename T::Pointer Load(const std::string& path, const IFileReader::Options &options)
356  {
357  return dynamic_cast<T*>(Load(path, options).at(0).GetPointer());
358  }
359 
374  static DataStorage::SetOfObjects::Pointer Load(const std::vector<std::string> &paths, DataStorage &storage,
375  const ReaderOptionsFunctorBase *optionsCallback = nullptr);
376 
384  static std::vector<BaseData::Pointer> Load(const std::vector<std::string> &paths,
385  const ReaderOptionsFunctorBase *optionsCallback = nullptr);
386 
396  static std::vector<BaseData::Pointer> Load(const us::ModuleResource &usResource,
397  std::ios_base::openmode mode = std::ios_base::in);
398 
404  template <typename T>
405  static typename T::Pointer Load(const us::ModuleResource &usResource, std::ios_base::openmode mode = std::ios_base::in)
406  {
407  return dynamic_cast<T*>(Load(usResource, mode).at(0).GetPointer());
408  }
409 
416  static BaseData::Pointer Load(const std::string& path, const PropertyList* properties);
417 
428  static void Save(const mitk::BaseData *data, const std::string &path, bool setPathProperty = false);
429 
441  static void Save(const mitk::BaseData *data, const std::string &path, const IFileWriter::Options &options, bool setPathProperty = false);
442 
456  static void Save(const mitk::BaseData *data,
457  const std::string &mimeType,
458  const std::string &path,
459  bool addExtension = true,
460  bool setPathProperty = false);
461 
476  static void Save(const mitk::BaseData *data,
477  const std::string &mimeType,
478  const std::string &path,
479  const mitk::IFileWriter::Options &options,
480  bool addExtension = true,
481  bool setPathProperty = false);
482 
494  static void Save(std::vector<SaveInfo> &saveInfos, bool setPathProperty = false);
495 
496  protected:
505  static std::string Load(std::vector<LoadInfo> &loadInfos,
506  DataStorage::SetOfObjects *nodeResult,
507  DataStorage *ds,
508  const ReaderOptionsFunctorBase *optionsCallback);
509 
520  static std::string Save(const BaseData *data,
521  const std::string &mimeType,
522  const std::string &path,
523  WriterOptionsFunctorBase *optionsCallback,
524  bool addExtension,
525  bool setPathProperty);
526 
534  static std::string Save(std::vector<SaveInfo> &saveInfos,
535  WriterOptionsFunctorBase *optionsCallback,
536  bool setPathProperty);
537 
538  private:
539  struct Impl;
540  };
541 }
542 
543 #endif
#define MITKCORE_EXPORT
Base class of all data objects.
Definition: mitkBaseData.h:60
Abstract data management class that organizes DataNode objects in a directed acyclic graph.
itk::VectorContainer< unsigned int, DataNode::Pointer > SetOfObjects
Container type used as a result set of query operations such as GetSubset().
Selects the most suitable IFileReader for a given file path.
Selects the most suitable IFileWriter for a given BaseData object.
A utility class to load and save data from/to the local file system.
Definition: mitkIOUtil.h:51
static BaseData::Pointer Load(const std::string &path, const PropertyList *properties)
Load a file with additional properties for the reader.
static std::string Save(std::vector< SaveInfo > &saveInfos, WriterOptionsFunctorBase *optionsCallback, bool setPathProperty)
Internal save implementation that processes SaveInfo objects.
static void Save(const mitk::BaseData *data, const std::string &path, bool setPathProperty=false)
Save a mitk::BaseData instance.
static T::Pointer Load(const std::string &path, const ReaderOptionsFunctorBase *optionsCallback=nullptr)
Load a file, cast the first result to type T and return it.
Definition: mitkIOUtil.h:328
static std::string CreateTemporaryFile(std::ofstream &tmpStream, std::ios_base::openmode mode, const std::string &templateName="XXXXXX", std::string path=std::string())
static void Save(std::vector< SaveInfo > &saveInfos, bool setPathProperty=false)
Use SaveInfo objects to save BaseData instances.
static std::string CreateTemporaryFile(std::ofstream &tmpStream, const std::string &templateName="XXXXXX", const std::string &path=std::string())
static std::vector< BaseData::Pointer > Load(const us::ModuleResource &usResource, std::ios_base::openmode mode=std::ios_base::in)
Loads the contents of a us::ModuleResource and returns the corresponding mitk::BaseData.
static DataStorage::SetOfObjects::Pointer Load(const std::vector< std::string > &paths, DataStorage &storage, const ReaderOptionsFunctorBase *optionsCallback=nullptr)
Loads a list of file paths into the given DataStorage.
static std::string CreateTemporaryDirectory(const std::string &templateName="XXXXXX", std::string path=std::string())
static std::vector< BaseData::Pointer > Load(const std::string &path, const IFileReader::Options &options)
Load a file and return the loaded data.
static char GetDirectorySeparator()
static void Save(const mitk::BaseData *data, const std::string &mimeType, const std::string &path, const mitk::IFileWriter::Options &options, bool addExtension=true, bool setPathProperty=false)
Save a mitk::BaseData instance.
static std::vector< BaseData::Pointer > Load(const std::string &path, const ReaderOptionsFunctorBase *optionsCallback=nullptr)
Load a file and return the loaded data.
static void Save(const mitk::BaseData *data, const std::string &mimeType, const std::string &path, bool addExtension=true, bool setPathProperty=false)
Save a mitk::BaseData instance.
static DataStorage::SetOfObjects::Pointer Load(const std::string &path, const IFileReader::Options &options, DataStorage &storage)
Load a file into the given DataStorage given user defined IFileReader::Options.
static std::string CreateTemporaryFile(const std::string &templateName="XXXXXX", const std::string &path=std::string())
static T::Pointer Load(const us::ModuleResource &usResource, std::ios_base::openmode mode=std::ios_base::in)
Load a module resource, cast the first result to type T and return it.
Definition: mitkIOUtil.h:405
static void Save(const mitk::BaseData *data, const std::string &path, const IFileWriter::Options &options, bool setPathProperty=false)
Save a mitk::BaseData instance.
static fs::path GetAppBundlePath(AppBundlePath path=AppBundlePath::Parent)
static T::Pointer Load(const std::string &path, const IFileReader::Options &options)
Load a file with options, cast the first result to type T and return it.
Definition: mitkIOUtil.h:355
static std::string GetTempPath()
static std::vector< BaseData::Pointer > Load(const std::vector< std::string > &paths, const ReaderOptionsFunctorBase *optionsCallback=nullptr)
Load a list of file paths and return the loaded data.
static std::string GetProgramPath()
static std::string Load(std::vector< LoadInfo > &loadInfos, DataStorage::SetOfObjects *nodeResult, DataStorage *ds, const ReaderOptionsFunctorBase *optionsCallback)
Internal load implementation that processes LoadInfo objects.
static DataStorage::SetOfObjects::Pointer Load(const std::string &path, DataStorage &storage, const ReaderOptionsFunctorBase *optionsCallback=nullptr)
Load a file into the given DataStorage.
static std::string Save(const BaseData *data, const std::string &mimeType, const std::string &path, WriterOptionsFunctorBase *optionsCallback, bool addExtension, bool setPathProperty)
Internal save implementation with writer options callback.
Immutable, value-type wrapper for a registered mime-type.
Definition: mitkMimeType.h:45
Key-value list holding instances of BaseProperty.
Represents a resource (text file, image, etc.) embedded in a CppMicroServices module.
Portability header that provides a unified namespace alias for the C++ filesystem library.
Find image slices visible on a given plane.
std::map< std::string, us::Any > Options
Options for reading or writing data.
Definition: mitkIFileIO.h:80
Contains information about the current loading process.
Definition: mitkIOUtil.h:60
bool m_Cancel
Flag indicating if the load operation should be canceled.
Definition: mitkIOUtil.h:73
const PropertyList * m_Properties
Optional properties to pass to the file reader.
Definition: mitkIOUtil.h:75
std::vector< BaseData::Pointer > m_Output
The loaded BaseData objects (output).
Definition: mitkIOUtil.h:69
LoadInfo(const std::string &path)
Constructor.
std::string m_Path
The file path to load.
Definition: mitkIOUtil.h:67
FileReaderSelector m_ReaderSelector
The reader selector containing all suitable readers for the file.
Definition: mitkIOUtil.h:71
Base class for option callbacks used in load operations.
Definition: mitkIOUtil.h:85
virtual bool operator()(LoadInfo &loadInfo) const =0
Callback operator invoked during load to configure reader options.
Contains information about a save operation for a BaseData object.
Definition: mitkIOUtil.h:95
SaveInfo(const BaseData *baseData, const MimeType &mimeType, const std::string &path)
Constructor.
std::string m_Path
The path to write the BaseData object to.
Definition: mitkIOUtil.h:114
const BaseData * m_BaseData
The BaseData object to save.
Definition: mitkIOUtil.h:107
bool m_Cancel
Flag indicating if sub-sequent save operations are to be canceled.
Definition: mitkIOUtil.h:116
MimeType m_MimeType
The selected mime-type, used to restrict results from FileWriterSelector.
Definition: mitkIOUtil.h:112
FileWriterSelector m_WriterSelector
Contains a set of IFileWriter objects.
Definition: mitkIOUtil.h:110
bool operator<(const SaveInfo &other) const
Comparison operator for ordering SaveInfo objects.
Base class for option callbacks used in save operations.
Definition: mitkIOUtil.h:126
virtual bool operator()(SaveInfo &saveInfo) const =0
Callback operator invoked during save to configure writer options.