diff --git a/Core/Code/IO/mitkAbstractFileReader.cpp b/Core/Code/IO/mitkAbstractFileReader.cpp index 58595f8dc2..afc2851720 100644 --- a/Core/Code/IO/mitkAbstractFileReader.cpp +++ b/Core/Code/IO/mitkAbstractFileReader.cpp @@ -1,158 +1,155 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #include #include #include #include #include #include mitk::AbstractFileReader::AbstractFileReader() : m_Priority (0) , m_PrototypeFactory(NULL) { } mitk::AbstractFileReader::~AbstractFileReader() { delete m_PrototypeFactory; } mitk::AbstractFileReader::AbstractFileReader(const mitk::AbstractFileReader& other) - : m_FileName(other.m_FileName) - , m_FilePrefix(other.m_FilePrefix) - , m_FilePattern(other.m_FilePattern) - , m_Extension(other.m_Extension) + : m_Extension(other.m_Extension) , m_Description(other.m_Description) , m_Priority(other.m_Priority) , m_Options(other.m_Options) , m_PrototypeFactory(NULL) { } mitk::AbstractFileReader::AbstractFileReader(const std::string& extension, const std::string& description) : m_Extension (extension) , m_Description (description) , m_Priority (0) , m_PrototypeFactory(NULL) { } ////////////////////// Reading ///////////////////////// std::list< itk::SmartPointer > mitk::AbstractFileReader::Read(const std::string& path, mitk::DataStorage* /*ds*/) { if (! itksys::SystemTools::FileExists(path.c_str())) mitkThrow() << "File '" + path + "' not found."; std::ifstream stream; stream.open(path.c_str()); return this->Read(stream); } //////////// µS Registration & Properties ////////////// us::ServiceRegistration mitk::AbstractFileReader::RegisterService(us::ModuleContext* context) { if (m_PrototypeFactory) return us::ServiceRegistration(); struct PrototypeFactory : public us::PrototypeServiceFactory { mitk::AbstractFileReader* const m_Prototype; PrototypeFactory(mitk::AbstractFileReader* prototype) : m_Prototype(prototype) {} us::InterfaceMap GetService(us::Module* /*module*/, const us::ServiceRegistrationBase& /*registration*/) { return us::MakeInterfaceMap(m_Prototype->Clone()); } void UngetService(us::Module* /*module*/, const us::ServiceRegistrationBase& /*registration*/, const us::InterfaceMap& service) { delete us::ExtractInterface(service); } }; m_PrototypeFactory = new PrototypeFactory(this); us::ServiceProperties props = this->ConstructServiceProperties(); return context->RegisterService(m_PrototypeFactory, props); } us::ServiceProperties mitk::AbstractFileReader::ConstructServiceProperties() { if ( m_Extension.empty() ) MITK_WARN << "Registered a Reader with no extension defined (m_Extension is empty). Reader will not be found by calls from ReaderManager.)"; if ( m_Description.empty() ) MITK_WARN << "Registered a Reader with no description defined (m_Description is empty). Reader will have no human readable extension information in FileDialogs.)"; std::transform(m_Extension.begin(), m_Extension.end(), m_Extension.begin(), ::tolower); us::ServiceProperties result; result[mitk::IFileReader::PROP_EXTENSION] = m_Extension; result[mitk::IFileReader::PROP_DESCRIPTION] = m_Description; result[us::ServiceConstants::SERVICE_RANKING()] = m_Priority; for (std::list::const_iterator it = m_Options.begin(); it != m_Options.end(); ++it) { result[*it] = std::string("true"); } return result; } //////////////////////// Options /////////////////////// std::list< std::string > mitk::AbstractFileReader::GetSupportedOptions() const { return m_Options; } ////////////////// MISC ////////////////// bool mitk::AbstractFileReader::CanRead(const std::string& path) const { // Default implementation only checks if extension is correct std::string pathEnd = path.substr( path.length() - m_Extension.length(), m_Extension.length() ); return (m_Extension == pathEnd); } float mitk::AbstractFileReader::GetProgress() const { // Default implementation always returns 1 (finished) return 1; } ////////////////// µS related Getters ////////////////// int mitk::AbstractFileReader::GetPriority() const { return m_Priority; } std::string mitk::AbstractFileReader::GetExtension() const { return m_Extension; } std::string mitk::AbstractFileReader::GetDescription() const { return m_Description; } diff --git a/Core/Code/IO/mitkAbstractFileReader.h b/Core/Code/IO/mitkAbstractFileReader.h index ba71438b65..fd22d9ece6 100644 --- a/Core/Code/IO/mitkAbstractFileReader.h +++ b/Core/Code/IO/mitkAbstractFileReader.h @@ -1,132 +1,93 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #ifndef AbstractFileReader_H_HEADER_INCLUDED_C1E7E521 #define AbstractFileReader_H_HEADER_INCLUDED_C1E7E521 // Macro #include // MITK #include #include // Microservices #include #include #include namespace us { struct PrototypeServiceFactory; } namespace mitk { //##Documentation //## @brief Interface class of readers that read from files //## @ingroup Process class MITK_CORE_EXPORT AbstractFileReader : public mitk::IFileReader { public: - //##Documentation - //## @brief Get the specified file prefix for the file(s) to load. - //## - //## You should specify either a FileName or FilePrefix. Use FilePrefix if - //## the data is stored in multiple files. - // File Pattern mechanic is currently deactivated until decision has been reached on how to handle patterns - //virtual std::string GetFilePrefix() const; - - //##Documentation - //## @brief Specify file prefix for the file(s) to load. - //## - //## You should specify either a FileName or FilePrefix. Use FilePrefix if - //## the data is stored in multiple files. - // File Pattern mechanic is currently deactivated until decision has been reached on how to handle patterns - // virtual void SetFilePrefix(const std::string& aFilePrefix); - - //##Documentation - //## @brief Get the specified file pattern for the file(s) to load. The - //## sprintf format used to build filename from FilePrefix and number. - //## - //## You should specify either a FileName or FilePrefix. Use FilePrefix if - //## the data is stored in multiple files. - // File Pattern mechanic is currently deactivated until decision has been reached on how to handle patterns - //virtual std::string GetFilePattern() const; - - /** - @brief Specified file pattern for the file(s) to load. The sprintf - format used to build filename from FilePrefix and number. - - You should specify either a FileName or FilePrefix. Use FilePrefix if - the data is stored in multiple files. */ - // File Pattern mechanic is currently deactivated until decision has been reached on how to handle patterns - //virtual void SetFilePattern(const std::string& aFilePattern); - virtual std::list< itk::SmartPointer > Read(const std::string& path, mitk::DataStorage *ds = 0 ); virtual std::list< itk::SmartPointer > Read(const std::istream& stream, mitk::DataStorage *ds = 0 ) = 0; virtual int GetPriority() const; virtual std::string GetExtension() const; virtual std::string GetDescription() const; virtual std::list< std::string > GetSupportedOptions() const; virtual bool CanRead(const std::string& path) const; virtual float GetProgress() const; us::ServiceRegistration RegisterService(us::ModuleContext* context = us::GetModuleContext()); protected: AbstractFileReader(); ~AbstractFileReader(); AbstractFileReader(const AbstractFileReader& other); AbstractFileReader(const std::string& extension, const std::string& description); - // Filenames etc.. - std::string m_FileName; - std::string m_FilePrefix; - std::string m_FilePattern; - // Minimal Service Properties: ALWAYS SET THESE IN CONSTRUCTOR OF DERIVED CLASSES! std::string m_Extension; std::string m_Description; int m_Priority; std::list< std::string > m_Options; // Options supported by this reader. Can be left emtpy if no special options are required virtual us::ServiceProperties ConstructServiceProperties(); private: us::PrototypeServiceFactory* m_PrototypeFactory; virtual mitk::IFileReader* Clone() const = 0; }; } // namespace mitk // This is the microservice declaration. Do not meddle! US_DECLARE_SERVICE_INTERFACE(mitk::AbstractFileReader, "org.mitk.services.FileReader") #endif /* AbstractFileReader_H_HEADER_INCLUDED_C1E7E521 */ diff --git a/Core/Code/IO/mitkAbstractFileWriter.cpp b/Core/Code/IO/mitkAbstractFileWriter.cpp index c425b91954..aa1f2f770d 100644 --- a/Core/Code/IO/mitkAbstractFileWriter.cpp +++ b/Core/Code/IO/mitkAbstractFileWriter.cpp @@ -1,167 +1,166 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #include #include #include #include #include #include #include mitk::AbstractFileWriter::AbstractFileWriter() : m_Priority (0) , m_PrototypeFactory(NULL) { } mitk::AbstractFileWriter::~AbstractFileWriter() { delete m_PrototypeFactory; } mitk::AbstractFileWriter::AbstractFileWriter(const mitk::AbstractFileWriter& other) - : m_FileName(other.m_FileName) - , m_Extension(other.m_Extension) + : m_Extension(other.m_Extension) , m_BasedataType(other.m_BasedataType) , m_Description(other.m_Description) , m_Priority(other.m_Priority) , m_Options(other.m_Options) , m_PrototypeFactory(NULL) { } mitk::AbstractFileWriter::AbstractFileWriter(const std::string& basedataType, const std::string& extension, const std::string& description) : m_Extension (extension) , m_BasedataType(basedataType) , m_Description (description) , m_Priority (0) , m_PrototypeFactory(NULL) { } ////////////////////// Writing ///////////////////////// void mitk::AbstractFileWriter::Write(const BaseData* data, const std::string& path) { if (! itksys::SystemTools::FileExists(path.c_str())) mitkThrow() << "File '" + path + "' not found."; std::ofstream stream; stream.open(path.c_str()); this->Write(data, stream); } //////////// µS Registration & Properties ////////////// us::ServiceRegistration mitk::AbstractFileWriter::RegisterService(us::ModuleContext* context) { if (m_PrototypeFactory) return us::ServiceRegistration(); struct PrototypeFactory : public us::PrototypeServiceFactory { mitk::AbstractFileWriter* const m_Prototype; PrototypeFactory(mitk::AbstractFileWriter* prototype) : m_Prototype(prototype) {} us::InterfaceMap GetService(us::Module* /*module*/, const us::ServiceRegistrationBase& /*registration*/) { return us::MakeInterfaceMap(m_Prototype->Clone()); } void UngetService(us::Module* /*module*/, const us::ServiceRegistrationBase& /*registration*/, const us::InterfaceMap& service) { delete us::ExtractInterface(service); } }; m_PrototypeFactory = new PrototypeFactory(this); us::ServiceProperties props = this->ConstructServiceProperties(); return context->RegisterService(m_PrototypeFactory, props); } us::ServiceProperties mitk::AbstractFileWriter::ConstructServiceProperties() { if ( m_Extension.empty() ) MITK_WARN << "Registered a Writer with no extension defined (m_Extension is empty). Writer will not be found by calls from WriterManager.)"; if ( m_BasedataType.empty() ) MITK_WARN << "Registered a Writer with no BasedataType defined (m_BasedataType is empty). Writer will not be found by calls from WriterManager.)"; if ( m_Description.empty() ) MITK_WARN << "Registered a Writer with no description defined (m_Description is empty). Writer will have no human readable extension information in FileDialogs.)"; us::ServiceProperties result; result[mitk::IFileWriter::PROP_EXTENSION] = m_Extension; result[mitk::IFileWriter::PROP_DESCRIPTION] = m_Description; result[mitk::IFileWriter::PROP_BASEDATA_TYPE] = m_BasedataType; result[us::ServiceConstants::SERVICE_RANKING()] = m_Priority; for (std::list::const_iterator it = m_Options.begin(); it != m_Options.end(); ++it) { result[*it] = std::string("true"); } return result; } //////////////////////// Options /////////////////////// std::list< std::string > mitk::AbstractFileWriter::GetSupportedOptions() const { return m_Options; } ////////////////// MISC ////////////////// bool mitk::AbstractFileWriter::CanWrite(const BaseData* data) const { // Default implementation only checks if basedatatype is correct std::string externalDataType = data->GetNameOfClass(); return (externalDataType == m_BasedataType); } float mitk::AbstractFileWriter::GetProgress() const { // Default implementation always returns 1 (finished) return 1; } ////////////////// µS related Getters ////////////////// int mitk::AbstractFileWriter::GetPriority() const { return m_Priority; } std::string mitk::AbstractFileWriter::GetExtension() const { return m_Extension; } std::string mitk::AbstractFileWriter::GetDescription() const { return m_Description; } std::string mitk::AbstractFileWriter::GetSupportedBasedataType() const { return m_BasedataType; } diff --git a/Core/Code/IO/mitkAbstractFileWriter.h b/Core/Code/IO/mitkAbstractFileWriter.h index 11828508a4..cec156c99a 100644 --- a/Core/Code/IO/mitkAbstractFileWriter.h +++ b/Core/Code/IO/mitkAbstractFileWriter.h @@ -1,167 +1,162 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #ifndef AbstractFileWriter_H_HEADER_INCLUDED_C1E7E521 #define AbstractFileWriter_H_HEADER_INCLUDED_C1E7E521 // Macro #include // MITK #include // Microservices #include #include #include namespace us { struct PrototypeServiceFactory; } namespace mitk { /** * @brief This abstract class gives a meaningful default implementations to most methods of mitkIFileWriter.h. * * In general, all FileWriters should derive from this class, this way it is made sure that the new implementation is * exposed to the Microservice-Framework and that is automatically available troughout MITK. * The default implementation only requires the two write * methods and the Clone() method to be implemented. Be sure to set all important members in the constructor. These are: *
    *
  • m_Extension: To define which file extension can be written. *
  • m_Description: To give a human readable name for this writer to be displayed in FileDialogs. *
  • m_BasedataType: To define which type of Basedata this fileWriter can handle. *
  • (Optional) m_Priority: To make this writer rank higher when choosing writers automatically *
  • (Optional) m_SupportedOptions: To define which options this writer can handle. Options can modify writing behaviour (e.g. set a compression) *
* You can also use the protected constructor for this. * * @ingroup Process */ class MITK_CORE_EXPORT AbstractFileWriter : public mitk::IFileWriter { public: /** * \brief Write the data in data to the the location specified in path */ virtual void Write(const BaseData* data, const std::string& path); /** * \brief Write the data in data to the the stream specified in stream */ virtual void Write(const BaseData* data, std::ostream& stream ) = 0; /** * \brief Returns the priority which defined how 'good' the FileWriter can handle it's file format. * * Default is zero and should only be chosen differently for a reason. * The priority is intended to be used by the MicroserviceFramework to determine * which reader to use if several equivalent readers have been found. * It may be used to replace a default reader from MITK in your own project. * E.g. if you want to use your own reader for *.nrrd files instead of the default, * implement it and give it a higher priority than zero. * * This method has a default implementation and need not be reimplemented, simply set m_Priority in the constructor. */ virtual int GetPriority() const; /** * \brief returns the file extension that this FileWriter is able to handle. * * Please enter only the characters after the fullstop, e.g "nrrd" is correct * while "*.nrrd" and ".nrrd" are incorrect. * * This method has a default implementation and need not be reimplemented, simply set m_Extension in the constructor. */ virtual std::string GetExtension() const; /** * \brief returns the name of the itk Basedata that this FileWriter is able to handle. * * The correct value is the one given as the first parameter in the ItkNewMacro of that Basedata derivate. * You can also retrieve it by calling GetNameOfClass() on an instance of said data. * * This method has a default implementation and need not be reimplemented, simply set m_BaseDataType in the constructor. */ virtual std::string GetSupportedBasedataType() const; /** * \brief Returns a human readable description of the file format. * * This will be used in FileDialogs for example. * This method has a default implementation and need not be reimplemented, simply set m_BaseDataType in the constructor. */ virtual std::string GetDescription() const; /** * \brief returns a list of the supported Options * * Options are strings that are treated as flags when passed to the read method. */ virtual std::list< std::string > GetSupportedOptions() const; /** * \brief Return true if this writer can confirm that it can read this file and false otherwise. * * The default implementation of AbstractFileWriter checks if the supplied filename is of the same extension as m_extension. * Overwrite this method if you require more specific behaviour */ virtual bool CanWrite(const BaseData* data) const; /** * \brief Returns a value between 0 and 1 depending on the progress of the writing process. * This method need not necessarily be implemented, always returning zero is accepted. */ virtual float GetProgress() const; us::ServiceRegistration RegisterService(us::ModuleContext* context = us::GetModuleContext()); protected: AbstractFileWriter(); ~AbstractFileWriter(); AbstractFileWriter(const AbstractFileWriter& other); AbstractFileWriter(const std::string& basedataType, const std::string& extension, const std::string& description); - // Filenames etc.. - std::string m_FileName; - //std::string m_FilePrefix; - //std::string m_FilePattern; - // Minimal Service Properties: ALWAYS SET THESE IN CONSTRUCTOR OF DERIVED CLASSES! std::string m_Extension; std::string m_BasedataType; std::string m_Description; int m_Priority; std::list< std::string > m_Options; // Options supported by this Writer. Can be left emtpy if no special options are required virtual us::ServiceProperties ConstructServiceProperties(); private: us::PrototypeServiceFactory* m_PrototypeFactory; virtual mitk::IFileWriter* Clone() const = 0; }; } // namespace mitk #endif /* AbstractFileWriter_H_HEADER_INCLUDED_C1E7E521 */ diff --git a/Core/Code/Interfaces/mitkIFileWriter.h b/Core/Code/Interfaces/mitkIFileWriter.h index cce13daf77..b7f1681b39 100644 --- a/Core/Code/Interfaces/mitkIFileWriter.h +++ b/Core/Code/Interfaces/mitkIFileWriter.h @@ -1,122 +1,103 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #ifndef IFileWriter_H_HEADER_INCLUDED_C1E7E521 #define IFileWriter_H_HEADER_INCLUDED_C1E7E521 // Macro #include #include // Microservices #include // STL #include namespace mitk { class BaseData; } namespace mitk { /** * \brief The common interface of all FileWriters. * * This interface defines the methods necessary for the FileWriterManager * to interact with its FileWriters. To implement a new FileWriter, it is * recommended to derive from FileWriterAbstract instead of directly from this Interface, * as the abstract class already implements most of the methods and also makes sure that your writer * will be managed by the FileWriterManager. */ struct MITK_CORE_EXPORT IFileWriter { virtual ~IFileWriter(); virtual void Write(const BaseData* data, const std::string& path ) = 0; virtual void Write(const BaseData* data, std::ostream& stream ) = 0; - /** - * \brief Returns the priority which defines how 'good' the FileWriter can handle it's file format. - * - * Default is zero and should only be chosen differently for a reason. - * The priority is intended to be used by the MicroserviceFramework to determine - * which reader to use if several equivalent readers have been found. - * It may be used to replace a default reader from MITK in your own project. - * If you want to use your own reader for *.nrrd files instead of the default, - * just implement it and give it a higher priority than zero. - */ - virtual int GetPriority() const = 0 ; - - /** - * \brief Returns a human readable description of the file format to be written. - * - * This will be used in FileDialogs for example. - */ - virtual std::string GetDescription() const = 0 ; - /** * \brief returns the file extension that this FileWriter is able to handle. * * Please return only the characters after the fullstop, e.g "nrrd" is correct * while "*.nrrd" and ".nrrd" are incorrect. */ virtual std::string GetExtension() const = 0; /** * \brief returns the itk classname that this FileWriter is able to handle. */ virtual std::string GetSupportedBasedataType() const = 0; /** * \brief returns a list of the supported Options * * Options are strings that are treated as flags when passed to the write method. */ virtual std::list< std::string > GetSupportedOptions() const = 0; /** * \brief Returns true if this reader can confirm that it can write \c data and false otherwise. */ virtual bool CanWrite(const BaseData* data) const = 0; /** * \brief Returns a value between 0 and 1 depending on the progress of the writing process. * This method need not necessarily be implemented meaningfully, always returning zero is accepted. */ virtual float GetProgress() const = 0; // Microservice properties static const std::string PROP_EXTENSION; static const std::string PROP_BASEDATA_TYPE; static const std::string PROP_DESCRIPTION; static const std::string PROP_IS_LEGACY; protected: }; } // namespace mitk // This is the microservice declaration. Do not meddle! US_DECLARE_SERVICE_INTERFACE(mitk::IFileWriter, "org.mitk.IFileWriter") #endif /* IFileWriter_H_HEADER_INCLUDED_C1E7E521 */