diff --git a/Core/Code/IO/mitkAbstractFileReader.cpp b/Core/Code/IO/mitkAbstractFileReader.cpp index 9a2af0ffce..f981bb0f6a 100644 --- a/Core/Code/IO/mitkAbstractFileReader.cpp +++ b/Core/Code/IO/mitkAbstractFileReader.cpp @@ -1,161 +1,161 @@ /*=================================================================== 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_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->GetServiceProperties(); return context->RegisterService(m_PrototypeFactory, props); } us::ServiceProperties mitk::AbstractFileReader::GetServiceProperties() { 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< mitk::IFileReader::FileServiceOption >::const_iterator it = m_Options.begin(); it != m_Options.end(); ++it) { + for (mitk::IFileReader::OptionList::const_iterator it = m_Options.begin(); it != m_Options.end(); ++it) { if (it->second) result[it->first] = std::string("true"); else result[it->first] = std::string("false"); } return result; } //////////////////////// Options /////////////////////// -std::list< mitk::IFileReader::FileServiceOption > mitk::AbstractFileReader::GetOptions() const +mitk::IFileReader::OptionList mitk::AbstractFileReader::GetOptions() const { return m_Options; } -void mitk::AbstractFileReader::SetOptions(std::list< mitk::IFileReader::FileServiceOption > options) +void mitk::AbstractFileReader::SetOptions(const mitk::IFileReader::OptionList& options) { if (options.size() != m_Options.size()) MITK_WARN << "Number of set Options differs from Number of available Options, which is a sign of false usage. Please consult documentation"; m_Options = 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 49f82ec3ea..05e6ac69e0 100644 --- a/Core/Code/IO/mitkAbstractFileReader.h +++ b/Core/Code/IO/mitkAbstractFileReader.h @@ -1,97 +1,97 @@ /*=================================================================== 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: 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< mitk::IFileReader::FileServiceOption > GetOptions() const; + virtual mitk::IFileReader::OptionList GetOptions() const; - virtual void SetOptions(std::list< mitk::IFileReader::FileServiceOption > options); + virtual void SetOptions(const mitk::IFileReader::OptionList& options); 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); // Minimal Service Properties: ALWAYS SET THESE IN CONSTRUCTOR OF DERIVED CLASSES! std::string m_Extension; std::string m_Description; int m_Priority; /** * \brief Options supported by this reader. Set sensible default values! * * Can be left emtpy if no special options are required. */ - std::list< mitk::IFileReader::FileServiceOption > m_Options; + mitk::IFileReader::OptionList m_Options; virtual us::ServiceProperties GetServiceProperties(); 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 90d4f39cc2..e9fb7d1df9 100644 --- a/Core/Code/IO/mitkAbstractFileWriter.cpp +++ b/Core/Code/IO/mitkAbstractFileWriter.cpp @@ -1,171 +1,171 @@ /*=================================================================== 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_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->GetServiceProperties(); return context->RegisterService(m_PrototypeFactory, props); } us::ServiceProperties mitk::AbstractFileWriter::GetServiceProperties() { 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) + for (mitk::IFileWriter::OptionList::const_iterator it = m_Options.begin(); it != m_Options.end(); ++it) { result[it->first] = std::string("true"); } return result; } //////////////////////// Options /////////////////////// -std::list< mitk::IFileWriter::FileServiceOption > mitk::AbstractFileWriter::GetOptions() const +mitk::IFileWriter::OptionList mitk::AbstractFileWriter::GetOptions() const { return m_Options; } -void mitk::AbstractFileWriter::SetOptions(std::list< mitk::IFileWriter::FileServiceOption > options) +void mitk::AbstractFileWriter::SetOptions(const OptionList& options) { if (options.size() != m_Options.size()) MITK_WARN << "Number of set Options differs from Number of available Options, which is a sign of false usage. Please consult documentation"; m_Options = 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 16e405775f..9951cf9ca3 100644 --- a/Core/Code/IO/mitkAbstractFileWriter.h +++ b/Core/Code/IO/mitkAbstractFileWriter.h @@ -1,160 +1,160 @@ /*=================================================================== 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< mitk::IFileWriter::FileServiceOption > GetOptions() const; + virtual OptionList GetOptions() const; - virtual void SetOptions(std::list< mitk::IFileWriter::FileServiceOption > options); + virtual void SetOptions(const OptionList& options); /** * \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); // 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< mitk::IFileWriter::FileServiceOption > m_Options; // Options supported by this Writer. Can be left emtpy if no special options are required + OptionList m_Options; // Options supported by this Writer. Can be left emtpy if no special options are required virtual us::ServiceProperties GetServiceProperties(); private: us::PrototypeServiceFactory* m_PrototypeFactory; virtual mitk::IFileWriter* Clone() const = 0; }; } // namespace mitk #endif /* AbstractFileWriter_H_HEADER_INCLUDED_C1E7E521 */ diff --git a/Core/Code/IO/mitkFileReaderRegistry.cpp b/Core/Code/IO/mitkFileReaderRegistry.cpp index 7ed2287718..84210eb685 100644 --- a/Core/Code/IO/mitkFileReaderRegistry.cpp +++ b/Core/Code/IO/mitkFileReaderRegistry.cpp @@ -1,222 +1,227 @@ /*=================================================================== 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 "mitkFileReaderRegistry.h" // Microservices #include #include #include #include // Legacy Support #include //const std::string mitk::IFileWriter::PROP_EXTENSION = "org.mitk.services.FileWriter.Extension"; mitk::FileReaderRegistry::FileReaderRegistry() { } mitk::FileReaderRegistry::~FileReaderRegistry() { for (std::map >::iterator iter = m_ServiceObjects.begin(), end = m_ServiceObjects.end(); iter != end; ++iter) { iter->second.UngetService(iter->first); } } //////////////////// READING DIRECTLY //////////////////// std::list< mitk::BaseData::Pointer > mitk::FileReaderRegistry::Read(const std::string& path, us::ModuleContext* context) { // Find extension std::string extension = path; extension.erase(0, path.find_last_of('.')); // Get best Reader mitk::IFileReader* reader = GetReader(extension, context); // Throw exception if no compatible reader was found if (reader == NULL) mitkThrow() << "Tried to directly read a file of type '" + extension + "' via FileReaderRegistry, but no reader supporting this filetype was found."; return reader->Read(path); } std::list< mitk::BaseData::Pointer > mitk::FileReaderRegistry::ReadAll(const std::list paths, std::list* unreadableFiles, us::ModuleContext* context) { std::list< mitk::BaseData::Pointer > result; for (std::list::const_iterator iterator = paths.begin(), end = paths.end(); iterator != end; ++iterator) { try { std::list baseDataList = Read( *iterator, context ); result.merge(baseDataList); } catch (...) { if (unreadableFiles) unreadableFiles->push_back( *iterator ); } } return result; } //////////////////// GETTING READERS //////////////////// mitk::IFileReader* mitk::FileReaderRegistry::GetReader(const std::string& extension, us::ModuleContext* context ) { std::vector results = GetReaders(extension, context); if (results.empty()) return NULL; return results.front(); } std::vector mitk::FileReaderRegistry::GetReaders(const std::string& extension, us::ModuleContext* context ) { std::vector result; const std::vector > refs = GetReaderList(extension, context); result.reserve(refs.size()); // Translate List of ServiceRefs to List of Pointers for (std::vector >::const_iterator iter = refs.begin(), end = refs.end(); iter != end; ++iter) { us::ServiceObjects serviceObjects = context->GetServiceObjects(*iter); mitk::IFileReader* reader = serviceObjects.GetService(); m_ServiceObjects.insert(std::make_pair(reader, serviceObjects)); result.push_back(reader); } return result; } -mitk::IFileReader* mitk::FileReaderRegistry::GetReader(const std::string& extension, const std::list& options, us::ModuleContext* context ) +mitk::IFileReader* mitk::FileReaderRegistry::GetReader( + const std::string& extension, const mitk::IFileReader::OptionNames& options, + us::ModuleContext* context ) { std::vector matching = mitk::FileReaderRegistry::GetReaders(extension, options, context); if (matching.empty()) return NULL; return matching.front(); } -std::vector mitk::FileReaderRegistry::GetReaders(const std::string& extension, const std::list& options, us::ModuleContext* context ) +std::vector mitk::FileReaderRegistry::GetReaders( + const std::string& extension, const mitk::IFileReader::OptionNames& options, + us::ModuleContext* context ) { const std::vector allReaders = mitk::FileReaderRegistry::GetReaders(extension, context); std::vector result; result.reserve(allReaders.size()); // the list is already sorted by priority. Now find reader that supports all options for (std::vector ::const_iterator iter = allReaders.begin(), end = allReaders.end(); iter != end; ++iter) { mitk::IFileReader * currentReader = *iter; // Now see if this reader supports all options. If yes, push to results if ( mitk::FileReaderRegistry::ReaderSupportsOptions(currentReader, options) ) { result.push_back(currentReader); } } return result; } void mitk::FileReaderRegistry::UngetReader(mitk::IFileReader* reader) { std::map >::iterator readerIter = m_ServiceObjects.find(reader); if (readerIter != m_ServiceObjects.end()) { readerIter->second.UngetService(reader); m_ServiceObjects.erase(readerIter); } } void mitk::FileReaderRegistry::UngetReaders(const std::vector& readers) { for (std::vector::const_iterator iter = readers.begin(), end = readers.end(); iter != end; ++iter) { this->UngetReader(*iter); } } //////////////////// GENERIC INFORMATION //////////////////// std::string mitk::FileReaderRegistry::GetSupportedExtensions(const std::string& extension) { us::ModuleContext* context = us::GetModuleContext(); const std::vector > refs = GetReaderList(extension, context); std::vector entries; // Will contain Description + Extension (Human readable) entries.reserve(refs.size()); std::string knownExtensions; // Will contain plain list of all known extensions (for the QFileDialog entry "All Known Extensions") for (std::vector >::const_iterator iterator = refs.begin(), end = refs.end(); iterator != end; ++iterator) { // Generate List of Extensions if (iterator == refs.begin()) // First entry without semicolon knownExtensions += "*" + iterator->GetProperty(mitk::IFileReader::PROP_EXTENSION).ToString(); else // Ad semicolon for each following entry knownExtensions += "; *" + iterator->GetProperty(mitk::IFileReader::PROP_EXTENSION).ToString(); // Generate List of human readable entries composed of Description + Extension std::string entry = iterator->GetProperty(mitk::IFileReader::PROP_DESCRIPTION).ToString() + "(*" + iterator->GetProperty(mitk::IFileReader::PROP_EXTENSION).ToString() + ");;"; entries.push_back(entry); } std::sort(entries.begin(), entries.end()); std::string result = "Known Extensions (" + knownExtensions + ");;All (*)"; for (std::vector::const_iterator iterator = entries.begin(), end = entries.end(); iterator != end; ++iterator) { result += ";;" + *iterator; } return result; } //////////////////// INTERNAL CODE //////////////////// -bool mitk::FileReaderRegistry::ReaderSupportsOptions(mitk::IFileReader* reader, const std::list& options ) +bool mitk::FileReaderRegistry::ReaderSupportsOptions(mitk::IFileReader* reader, + const mitk::IFileReader::OptionNames& options ) { - const std::list< mitk::IFileReader::FileServiceOption > readerOptions = reader->GetOptions(); + const mitk::IFileReader::OptionList readerOptions = reader->GetOptions(); if (options.empty()) return true; // if no options were requested, return true unconditionally if (readerOptions.empty()) return false; // if options were requested and reader supports no options, return false // For each of the strings in requested options, check if option is available in reader - for(std::list< std::string >::const_iterator options_i = options.begin(), i_end = options.end(); options_i != i_end; ++options_i) + for(mitk::IFileReader::OptionNames::const_iterator options_i = options.begin(), i_end = options.end(); options_i != i_end; ++options_i) { { bool optionFound = false; // Iterate over each available option from reader to check if one of them matches the current option - for(std::list::const_iterator options_j = readerOptions.begin(), j_end = readerOptions.end(); + for(mitk::IFileReader::OptionList::const_iterator options_j = readerOptions.begin(), j_end = readerOptions.end(); options_j != j_end; ++options_j) { if ( *options_i == options_j->first ) optionFound = true; } if (optionFound == false) return false; // If one option was not found, leave method and return false } } return true; // if all options have been found, return true } //////////////////// uS-INTERACTION //////////////////// std::vector< us::ServiceReference > mitk::FileReaderRegistry::GetReaderList(const std::string& extension, us::ModuleContext* context ) { // filter for class and extension std::string filter; if (!extension.empty()) { filter = us::LDAPProp(mitk::IFileReader::PROP_EXTENSION) == extension; } std::vector > result = context->GetServiceReferences(filter); std::sort(result.begin(), result.end()); std::reverse(result.begin(), result.end()); return result; } diff --git a/Core/Code/IO/mitkFileReaderRegistry.h b/Core/Code/IO/mitkFileReaderRegistry.h index fc4203b7a0..9af2033c2d 100644 --- a/Core/Code/IO/mitkFileReaderRegistry.h +++ b/Core/Code/IO/mitkFileReaderRegistry.h @@ -1,108 +1,107 @@ /*=================================================================== 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 FileReaderRegistry_H_HEADER_INCLUDED_C1E7E521 #define FileReaderRegistry_H_HEADER_INCLUDED_C1E7E521 #include #include +#include + // Microservices #include #include #include -namespace mitk { - struct IFileReader; -} namespace mitk { /** * @ingroup Process * * Provides convenient access to mitk::IFileReader instances and reading * files into mitk::BaseData types. * * \note The life-time of all mitk::IFileReader objects returned by an * instance of this class ends with the destruction of that instance. */ class MITK_CORE_EXPORT FileReaderRegistry { public: FileReaderRegistry(); ~FileReaderRegistry(); /** * Reads the file located at path and returns the * contents as a DataNode. * * This method will select the best available reader in the service * registry for the task. * * UnsupportedFileException: If no compatible reader was found * FileNotFoundException: If no file was found at path * FileReadException: If the selected reader failed to read the file **/ std::list< itk::SmartPointer > Read(const std::string& path, us::ModuleContext* context = us::GetModuleContext()); std::list< mitk::BaseData::Pointer > ReadAll(const std::list paths, std::list* unreadableFiles = 0, us::ModuleContext* context = us::GetModuleContext()); template itk::SmartPointer Read(const std::string& path, us::ModuleContext* context = us::GetModuleContext()) { std::list basedatas = Read(path, context); T* result = dynamic_cast (basedatas.front().GetPointer()); return result; } /** * Returns a compatible Reader to the given file extension **/ mitk::IFileReader* GetReader(const std::string& extension, us::ModuleContext* context = us::GetModuleContext() ); - mitk::IFileReader* GetReader(const std::string& extension, const std::list& options, us::ModuleContext* context = us::GetModuleContext() ); + mitk::IFileReader* GetReader(const std::string& extension, const mitk::IFileReader::OptionNames& options, us::ModuleContext* context = us::GetModuleContext() ); std::vector GetReaders(const std::string& extension, us::ModuleContext* context = us::GetModuleContext() ); - std::vector GetReaders(const std::string& extension, const std::list& options, us::ModuleContext* context = us::GetModuleContext() ); + std::vector GetReaders(const std::string& extension, const mitk::IFileReader::OptionNames& options, us::ModuleContext* context = us::GetModuleContext() ); void UngetReader(mitk::IFileReader* reader); void UngetReaders(const std::vector& readers); std::string GetSupportedExtensions(const std::string& extension = ""); protected: std::vector< us::ServiceReference > GetReaderList(const std::string& extension, us::ModuleContext* context); - bool ReaderSupportsOptions(mitk::IFileReader* reader, const std::list& options); + bool ReaderSupportsOptions(mitk::IFileReader* reader, const mitk::IFileReader::OptionNames& options); private: // purposely not implemented FileReaderRegistry(const FileReaderRegistry&); FileReaderRegistry& operator=(const FileReaderRegistry&); std::map > m_ServiceObjects; }; } // namespace mitk #endif /* FileReaderRegistry_H_HEADER_INCLUDED_C1E7E521 */ diff --git a/Core/Code/IO/mitkFileWriterRegistry.cpp b/Core/Code/IO/mitkFileWriterRegistry.cpp index 8bfd47b178..cf4e2fc972 100644 --- a/Core/Code/IO/mitkFileWriterRegistry.cpp +++ b/Core/Code/IO/mitkFileWriterRegistry.cpp @@ -1,202 +1,202 @@ /*=================================================================== 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 "mitkFileWriterRegistry.h" // MITK #include // Microservices #include #include #include #include mitk::FileWriterRegistry::FileWriterRegistry() { } mitk::FileWriterRegistry::~FileWriterRegistry() { for (std::map >::iterator iter = m_ServiceObjects.begin(), end = m_ServiceObjects.end(); iter != end; ++iter) { iter->second.UngetService(iter->first); } } //////////////////// WRITING DIRECTLY //////////////////// void mitk::FileWriterRegistry::Write(const mitk::BaseData* data, const std::string& path, us::ModuleContext* context) { // Find extension std::string extension = path; extension.erase(0, path.find_last_of('.')); // Get best Writer mitk::IFileWriter* Writer = GetWriter(extension, context); // Throw exception if no compatible Writer was found if (Writer == NULL) mitkThrow() << "Tried to directly Write a file of type '" + extension + "' via FileWriterRegistry, but no Writer supporting this filetype was found."; Writer->Write(data, path); } //////////////////// GETTING WRITERS //////////////////// mitk::IFileWriter* mitk::FileWriterRegistry::GetWriter(const std::string& extension, us::ModuleContext* context ) { std::vector results = GetWriters(extension, context); if (results.empty()) return NULL; return results.front(); } std::vector mitk::FileWriterRegistry::GetWriters(const std::string& extension, us::ModuleContext* context ) { std::vector result; const std::vector > refs = GetWriterList(extension, context); result.reserve(refs.size()); // Translate List of ServiceRefs to List of Pointers for (std::vector >::const_iterator iter = refs.begin(), end = refs.end(); iter != end; ++iter) { us::ServiceObjects serviceObjects = context->GetServiceObjects(*iter); mitk::IFileWriter* writer = serviceObjects.GetService(); m_ServiceObjects.insert(std::make_pair(writer, serviceObjects)); result.push_back(writer); } return result; } mitk::IFileWriter* mitk::FileWriterRegistry::GetWriter(const std::string& extension, const std::list& options, us::ModuleContext* context ) { const std::vector matching = mitk::FileWriterRegistry::GetWriters(extension, options, context); if (matching.empty()) return NULL; return matching.front(); } std::vector mitk::FileWriterRegistry::GetWriters(const std::string& extension, const std::list& options, us::ModuleContext* context ) { const std::vector allWriters = mitk::FileWriterRegistry::GetWriters(extension, context); std::vector result; // the list is always sorted by priority. Now find Writer that supports all options for (std::vector ::const_iterator iter = allWriters.begin(), end = allWriters.end(); iter != end; ++iter) { // Now see if this Writer supports all options. If yes, push to results if ( mitk::FileWriterRegistry::WriterSupportsOptions(*iter, options) ) { result.push_back(*iter); } } return result; } //////////////////// GENERIC INFORMATION //////////////////// std::string mitk::FileWriterRegistry::GetSupportedExtensions(const std::string& extension, us::ModuleContext* context) { const std::vector > refs = GetWriterList(extension, context); return CreateFileDialogString(refs); } std::string mitk::FileWriterRegistry::GetSupportedWriters(const std::string& basedataType, us::ModuleContext* context) { const std::vector > refs = GetWriterListByBasedataType(basedataType, context); return CreateFileDialogString(refs); } //////////////////// INTERNAL CODE //////////////////// -bool mitk::FileWriterRegistry::WriterSupportsOptions(mitk::IFileWriter* writer, const std::list& options ) +bool mitk::FileWriterRegistry::WriterSupportsOptions(mitk::IFileWriter* writer, const mitk::IFileWriter::OptionNames& options ) { - const std::list< mitk::IFileWriter::FileServiceOption > writerOptions = writer->GetOptions(); + const mitk::IFileWriter::OptionList writerOptions = writer->GetOptions(); if (options.empty()) return true; // if no options were requested, return true unconditionally if (writerOptions.empty()) return false; // if options were requested and reader supports no options, return false // For each of the strings in requested options, check if option is available in reader - for(std::list< std::string >::const_iterator options_i = options.begin(), i_end = options.end(); options_i != i_end; ++options_i) + for(mitk::IFileWriter::OptionNames::const_iterator options_i = options.begin(), i_end = options.end(); options_i != i_end; ++options_i) { { bool optionFound = false; // Iterate over each available option from reader to check if one of them matches the current option - for(std::list::const_iterator options_j = writerOptions.begin(), j_end = writerOptions.end(); - options_j != j_end; ++options_j) + for(mitk::IFileWriter::OptionList::const_iterator options_j = writerOptions.begin(), j_end = writerOptions.end(); + options_j != j_end; ++options_j) { if ( *options_i == options_j->first ) optionFound = true; } if (optionFound == false) return false; // If one option was not found, leave method and return false } } return true; // if all options have been found, return true } std::string mitk::FileWriterRegistry::CreateFileDialogString(const std::vector >& refs) { std::vector entries; // Will contain Description + Extension (Human readable) entries.reserve(refs.size()); std::string knownExtensions; // Will contain plain list of all known extensions (for the QFileDialog entry "All Known Extensions") for (std::vector >::const_iterator iterator = refs.begin(), end = refs.end(); iterator != end; ++iterator) { // Generate List of Extensions if (iterator == refs.begin()) // First entry without semicolon knownExtensions += "*" + iterator->GetProperty(mitk::IFileWriter::PROP_EXTENSION).ToString(); else // Ad semicolon for each following entry knownExtensions += "; *" + iterator->GetProperty(mitk::IFileWriter::PROP_EXTENSION).ToString(); // Generate List of human readable entries composed of Description + Extension std::string entry = iterator->GetProperty(mitk::IFileWriter::PROP_DESCRIPTION).ToString() + "(*" + iterator->GetProperty(mitk::IFileWriter::PROP_EXTENSION).ToString() + ");;"; entries.push_back(entry); } std::sort(entries.begin(), entries.end()); std::string result = "Known Extensions (" + knownExtensions + ");;All (*)"; for (std::vector::const_iterator iterator = entries.begin(), end = entries.end(); iterator != end; ++iterator) { result += ";;" + *iterator; } return result; } //////////////////// uS-INTERACTION //////////////////// std::vector< us::ServiceReference > mitk::FileWriterRegistry::GetWriterList(const std::string& extension, us::ModuleContext* context ) { // filter for class and extension std::string filter; if (!extension.empty()) { filter = "(" + mitk::IFileWriter::PROP_EXTENSION + "=" + extension + ")"; } std::vector > result = context->GetServiceReferences(filter); std::sort(result.begin(), result.end()); std::reverse(result.begin(), result.end()); return result; } std::vector< us::ServiceReference > mitk::FileWriterRegistry::GetWriterListByBasedataType(const std::string& basedataType, us::ModuleContext* context ) { // filter for class and extension std::string filter = us::LDAPProp(mitk::IFileWriter::PROP_BASEDATA_TYPE) == basedataType; std::vector > result = context->GetServiceReferences(filter); std::sort(result.begin(), result.end()); std::reverse(result.begin(), result.end()); return result; } diff --git a/Core/Code/IO/mitkFileWriterRegistry.h b/Core/Code/IO/mitkFileWriterRegistry.h index e0f1af66de..116e6c23ae 100644 --- a/Core/Code/IO/mitkFileWriterRegistry.h +++ b/Core/Code/IO/mitkFileWriterRegistry.h @@ -1,98 +1,99 @@ /*=================================================================== 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 FileWriterRegistry_H_HEADER_INCLUDED_C1E7E521 #define FileWriterRegistry_H_HEADER_INCLUDED_C1E7E521 #include // Microservices #include #include #include +#include "mitkIFileWriter.h" + // Temporarily disable warning until PIMPL pattern is implemented here #ifdef _MSC_VER # pragma warning(push) # pragma warning(disable: 4251) #endif namespace mitk { class BaseData; - struct IFileWriter; } namespace mitk { /** * @ingroup Process * * Provides convenient access to mitk::IFileWriter instances and writing * files from mitk::BaseData types. * * \note The life-time of all mitk::IFileWriter objects returned by an * instance of this class ends with the destruction of that instance. */ class MITK_CORE_EXPORT FileWriterRegistry { public: FileWriterRegistry(); ~FileWriterRegistry(); void Write(const mitk::BaseData* data, const std::string& path, us::ModuleContext* context = us::GetModuleContext()); /** * Returns a compatible Writer to the given file extension */ mitk::IFileWriter* GetWriter(const std::string& extension, us::ModuleContext* context = us::GetModuleContext() ); mitk::IFileWriter* GetWriter(const std::string& extension, const std::list& options, us::ModuleContext* context = us::GetModuleContext() ); std::vector GetWriters(const std::string& extension, us::ModuleContext* context = us::GetModuleContext() ); std::vector GetWriters(const std::string& extension, const std::list& options, us::ModuleContext* context = us::GetModuleContext() ); void UngetWriter(mitk::IFileWriter* writer); void UngetWriters(const std::vector& writers); std::string GetSupportedExtensions(const std::string& extension = std::string(), us::ModuleContext* context = us::GetModuleContext()); std::string GetSupportedWriters(const std::string& basedataType, us::ModuleContext* context = us::GetModuleContext()); protected: std::vector< us::ServiceReference > GetWriterList(const std::string& extension, us::ModuleContext* context); std::vector< us::ServiceReference > GetWriterListByBasedataType(const std::string& basedataType, us::ModuleContext* context); std::string CreateFileDialogString(const std::vector >& refs); - bool WriterSupportsOptions(mitk::IFileWriter* writer, const std::list& options); + bool WriterSupportsOptions(mitk::IFileWriter* writer, const mitk::IFileWriter::OptionNames& options); private: // purposely not implemented FileWriterRegistry(const FileWriterRegistry&); FileWriterRegistry& operator=(const FileWriterRegistry&); std::map > m_ServiceObjects; }; } // namespace mitk #ifdef _MSC_VER # pragma warning(pop) #endif #endif /* FileWriterRegistry_H_HEADER_INCLUDED_C1E7E521 */ diff --git a/Core/Code/Interfaces/mitkIFileReader.h b/Core/Code/Interfaces/mitkIFileReader.h index 0d866c87e6..26174ac512 100644 --- a/Core/Code/Interfaces/mitkIFileReader.h +++ b/Core/Code/Interfaces/mitkIFileReader.h @@ -1,139 +1,141 @@ /*=================================================================== 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 IFileReader_H_HEADER_INCLUDED_C1E7E521 #define IFileReader_H_HEADER_INCLUDED_C1E7E521 // Macro #include #include // Microservices #include // STL #include namespace mitk { class BaseData; class DataStorage; } namespace itk { template class SmartPointer; } namespace mitk { /** * \brief The common interface of all FileReader. * * This interface defines the Methods necessary for the FileReaderManager * to interact with its FileReaders. To implement a new Filereader, it is * recommended to derive from FileReaderAbstract instead of from the Interface, * as the abstract class already implements most of the functions and also makes sure * that your reader will be managed by the FileReaderManager. */ struct MITK_CORE_EXPORT IFileReader { virtual ~IFileReader(); typedef std::pair FileServiceOption; + typedef std::vector OptionNames; + typedef std::vector OptionList; /** * \brief Reads the specified file and returns its contents. */ virtual std::list< itk::SmartPointer > Read(const std::string& path, mitk::DataStorage* ds = 0) = 0; /** * \brief Reads the specified input stream and returns its contents. */ virtual std::list< itk::SmartPointer > Read(const std::istream& stream, mitk::DataStorage* ds = 0) = 0; /** * \brief Returns the priority which defined how 'good' the FileReader 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. */ virtual int GetPriority() const = 0 ; /** * \brief returns the file extension that this FileReader is able to handle. * * Please enter 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 a human readable description of the file format. * * This will be used in FileDialogs for example. */ virtual std::string GetDescription() const = 0; /** * \brief returns a list of the supported Options * * Inititally, the reader contains a set of standard options. These can be retrieved, * manipulated and set again. To activate or deactivate an option, just set it's bool * value accordingly. All supported options are in the default set, it does not * make sense to add strings artificially after retrieving a reader - the specific * implementation decides which options it wants to support. * If no options are supported, an empty list is returned. */ - virtual std::list< mitk::IFileReader::FileServiceOption > GetOptions() const = 0; + virtual OptionList GetOptions() const = 0; /** * \brief Sets the options for this reader * * The best way to use this method is to retireve the options via GetOptions, manipulate the bool values, * and then set the options again. */ - virtual void SetOptions(std::list< mitk::IFileReader::FileServiceOption > options) = 0; + virtual void SetOptions(const OptionList& options) = 0; /** * \brief Returns true if this writer can confirm that it can read this file and false otherwise. */ virtual bool CanRead(const std::string& path) const = 0; /** * \brief Returns a value between 0 and 1 depending on the progress of the read. * 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_DESCRIPTION; static const std::string PROP_IS_LEGACY; // Microservice names for defined properties static const std::string OPTION_READ_AS_BINARY; static const std::string OPTION_READ_MULTIPLE_FILES; protected: }; } // namespace mitk // This is the microservice declaration. Do not meddle! US_DECLARE_SERVICE_INTERFACE(mitk::IFileReader, "org.mitk.IFileReader") #endif /* IFileReader_H_HEADER_INCLUDED_C1E7E521 */ diff --git a/Core/Code/Interfaces/mitkIFileWriter.h b/Core/Code/Interfaces/mitkIFileWriter.h index 2ff6d62309..7e96b18f05 100644 --- a/Core/Code/Interfaces/mitkIFileWriter.h +++ b/Core/Code/Interfaces/mitkIFileWriter.h @@ -1,100 +1,102 @@ /*=================================================================== 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(); typedef std::pair FileServiceOption; + typedef std::vector OptionList; + typedef std::vector OptionNames; virtual void Write(const BaseData* data, const std::string& path ) = 0; virtual void Write(const BaseData* data, std::ostream& stream ) = 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< mitk::IFileWriter::FileServiceOption > GetOptions() const = 0; + virtual OptionList GetOptions() const = 0; - virtual void SetOptions(std::list< mitk::IFileWriter::FileServiceOption > options) = 0; + virtual void SetOptions(const OptionList& options) = 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 */ diff --git a/Core/Code/Testing/mitkFileReaderManagerTest.cpp b/Core/Code/Testing/mitkFileReaderManagerTest.cpp index 656b0498d5..b247cd99e4 100644 --- a/Core/Code/Testing/mitkFileReaderManagerTest.cpp +++ b/Core/Code/Testing/mitkFileReaderManagerTest.cpp @@ -1,230 +1,218 @@ /*=================================================================== 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 "mitkTestingMacros.h" #include "mitkAbstractFileReader.h" #include "mitkIFileReader.h" #include "mitkFileReaderRegistry.h" #include #include #include #include #include #include #include class DummyReader : public mitk::AbstractFileReader { public: DummyReader(const DummyReader& other) : mitk::AbstractFileReader(other) { } DummyReader(const std::string& extension, int priority) : mitk::AbstractFileReader(extension, "This is a dummy description") { m_Priority = priority; m_ServiceReg = this->RegisterService(); } ~DummyReader() { if (m_ServiceReg) m_ServiceReg.Unregister(); } using mitk::AbstractFileReader::Read; virtual std::list< itk::SmartPointer > Read(const std::istream& /*stream*/, mitk::DataStorage* /*ds*/ = 0) { std::list result; return result; } - virtual void SetOptions(const std::list< mitk::FileServiceOption >& options ) - { - m_Options = options; - //m_Registration.SetProperties(ConstructServiceProperties()); - } - private: DummyReader* Clone() const { return new DummyReader(*this); } us::ServiceRegistration m_ServiceReg; }; // End of internal dummy reader class DummyReader2 : public mitk::AbstractFileReader { public: DummyReader2(const DummyReader2& other) : mitk::AbstractFileReader(other) { } DummyReader2(const std::string& extension, int priority) : mitk::AbstractFileReader(extension, "This is a second dummy description") { m_Priority = priority; m_ServiceReg = this->RegisterService(); } ~DummyReader2() { if (m_ServiceReg) m_ServiceReg.Unregister(); } using mitk::AbstractFileReader::Read; virtual std::list< itk::SmartPointer > Read(const std::istream& /*stream*/, mitk::DataStorage* /*ds*/ = 0) { std::list result; return result; } - virtual void SetOptions(const std::list< mitk::FileServiceOption >& options ) - { - m_Options = options; - //m_Registration.SetProperties(ConstructServiceProperties()); - } - private: DummyReader2* Clone() const { return new DummyReader2(*this); } us::ServiceRegistration m_ServiceReg; }; // End of internal dummy reader 2 /** * TODO */ int mitkFileReaderManagerTest(int /*argc*/ , char* /*argv*/[]) { // always start with this! MITK_TEST_BEGIN("FileReaderManager"); // mitk::FileReaderRegistry::Pointer frm = mitk::FileReaderRegistry::New(); // MITK_TEST_CONDITION_REQUIRED(argc == 2,"Testing FileReaderRegistry instantiation"); DummyReader testDR("test",1); DummyReader otherDR("other",1); MITK_TEST_CONDITION_REQUIRED(testDR.CanRead("/this/is/a/folder/file.test"),"Positive test of default CanRead() implementation"); MITK_TEST_CONDITION_REQUIRED(!testDR.CanRead("/this/is/a/folder/file.tes"),"Negative test of default CanRead() implementation"); mitk::FileReaderRegistry* readerManager = new mitk::FileReaderRegistry; mitk::IFileReader* returned = readerManager->GetReader("test"); MITK_TEST_CONDITION_REQUIRED(returned && &static_cast(testDR) != returned,"Testing correct retrieval of FileReader 1/2"); returned = readerManager->GetReader("other"); MITK_TEST_CONDITION_REQUIRED(returned && &static_cast(otherDR) != returned,"Testing correct retrieval of FileReader 2/2"); DummyReader mediocreTestDR("test", 20); DummyReader prettyFlyTestDR("test", 50); DummyReader2 awesomeTestDR("test", 100); returned = readerManager->GetReader("test"); MITK_TEST_CONDITION_REQUIRED(dynamic_cast(returned), "Testing correct priorized retrieval of FileReader: Best reader"); // Now to give those readers some options, then we will try again - std::list< mitk::FileServiceOption > options; - options.push_front(std::make_pair("isANiceGuy", true)); + mitk::IFileReader::OptionList options; + options.push_back(std::make_pair("isANiceGuy", true)); mediocreTestDR.SetOptions(options); options.clear(); - options.push_front(std::make_pair("canFly", true)); + options.push_back(std::make_pair("canFly", true)); prettyFlyTestDR.SetOptions(options); - options.push_front(std::make_pair("isAwesome", true)); + options.push_back(std::make_pair("isAwesome", true)); awesomeTestDR.SetOptions(options); //note: awesomeReader canFly and isAwesome // Reset Options, use to define what we want the reader to do options.clear(); - std::list optionsFilter; - optionsFilter.push_front("canFly"); + mitk::IFileReader::OptionNames optionsFilter; + optionsFilter.push_back("canFly"); returned = readerManager->GetReader("test", optionsFilter); MITK_TEST_CONDITION_REQUIRED(returned && &static_cast(awesomeTestDR) != returned, "Testing correct retrieval of FileReader with Options: Best reader with options"); - optionsFilter.push_front("isAwesome"); + optionsFilter.push_back("isAwesome"); returned = readerManager->GetReader("test", optionsFilter); MITK_TEST_CONDITION_REQUIRED(returned && &static_cast(awesomeTestDR) != returned, "Testing correct retrieval of FileReader with multiple Options: Best reader with options"); optionsFilter.clear(); - optionsFilter.push_front("isANiceGuy"); + optionsFilter.push_back("isANiceGuy"); returned = readerManager->GetReader("test", optionsFilter); MITK_TEST_CONDITION_REQUIRED(returned && &static_cast(mediocreTestDR) != returned, "Testing correct retrieval of specific FileReader with Options: Low priority reader with specific option"); - optionsFilter.push_front("canFly"); + optionsFilter.push_back("canFly"); returned = readerManager->GetReader("test", optionsFilter); MITK_TEST_CONDITION_REQUIRED(returned == NULL, "Testing correct return of 0 value when no matching reader was found"); // Onward to test the retrieval of multiple readers std::vector< mitk::IFileReader* > returnedList; returnedList = readerManager->GetReaders("test", optionsFilter); MITK_TEST_CONDITION_REQUIRED(returnedList.empty(), "Testing correct return of zero readers when no matching reader was found, asking for all compatibles"); optionsFilter.clear(); optionsFilter.push_back("canFly"); returnedList = readerManager->GetReaders("test", optionsFilter); MITK_TEST_CONDITION_REQUIRED(returnedList.size() == 2, "Testing correct return of two readers when two matching reader was found, asking for all compatibles"); MITK_TEST_CONDITION_REQUIRED(dynamic_cast(returnedList.front()), "Testing correct priorization of returned Readers with options 1/2"); optionsFilter.clear(); optionsFilter.push_back("isAwesome"); returnedList = readerManager->GetReaders("test", optionsFilter); MITK_TEST_CONDITION_REQUIRED(returnedList.size() == 1, "Testing correct return of one readers when one matching reader was found, asking for all compatibles"); MITK_TEST_CONDITION_REQUIRED(dynamic_cast(returnedList.front()), "Testing correctness of result from former query"); // And now to verify a working read chain for a mps file: //mitk::PointSetReader::Pointer psr = mitk::PointSetReader::New(); //std::list basedata; //basedata = mitk::FileReaderRegistry::Read("F://Build//MITK-Data//pointSet.mps"); //MITK_TEST_CONDITION_REQUIRED(basedata.size() > 0, "Testing correct read of PointSet"); // Need to instanciate the CoreObjectFactory, so legacy Readers are available mitk::CoreObjectFactory::GetInstance(); // Testing templated call to ReaderManager //mitk::PointSet::Pointer pointset = mitk::FileReaderRegistry::Read< mitk::PointSet >("F://Build//MITK-Data//pointSet.mps"); //MITK_TEST_CONDITION_REQUIRED(pointset.IsNotNull(), "Testing templated call of Read()"); // And now for something completely different... (Debug) // mitk::LegacyFileReaderService::Pointer lfr = mitk::LegacyFileReaderService::New(".nrrd", "Nearly Raw Raster Data"); //returned = mitk::FileReaderRegistry::GetReader(".nrrd"); //MITK_TEST_CONDITION_REQUIRED(lfr == returned, "Testing correct retrieval of specific FileReader with Options: Low priority reader with specific option"); //std::list image = mitk::FileReaderRegistry::Read("F://Build//MITK-Data//Pic2DplusT.nrrd"); //MITK_TEST_CONDITION_REQUIRED(image.size() > 0, "Testing whether image was returned or not"); //mitk::Image::Pointer image2 = dynamic_cast (image.front().GetPointer()); //MITK_TEST_CONDITION_REQUIRED(image2.IsNotNull(), "Testing if BaseData is an image"); // Delete this here because it will call the PrototypeServiceFactory::Unget() method // of the dummy readers. delete readerManager; // always end with this! MITK_TEST_END(); } diff --git a/Core/Code/Testing/mitkFileWriterManagerTest.cpp b/Core/Code/Testing/mitkFileWriterManagerTest.cpp index c3648f6e13..e2e951169d 100644 --- a/Core/Code/Testing/mitkFileWriterManagerTest.cpp +++ b/Core/Code/Testing/mitkFileWriterManagerTest.cpp @@ -1,230 +1,230 @@ /*=================================================================== 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 #include #include #include #include #include class DummyWriter : public mitk::AbstractFileWriter { public: DummyWriter(const DummyWriter& other) : mitk::AbstractFileWriter(other) { } DummyWriter(const std::string& basedataType, const std::string& extension, int priority) : mitk::AbstractFileWriter(basedataType, extension, "This is a dummy description") { m_Priority = priority; m_ServiceReg = this->RegisterService(); } ~DummyWriter() { if (m_ServiceReg) m_ServiceReg.Unregister(); } using AbstractFileWriter::Write; virtual void Write(const mitk::BaseData* /*data*/, std::ostream& /*stream*/ ) { } virtual void SetOptions(const std::list< std::string >& options ) { m_Options = options; //m_Registration.SetProperties(ConstructServiceProperties()); } private: DummyWriter* Clone() const { return new DummyWriter(*this); } us::ServiceRegistration m_ServiceReg; }; // End of internal dummy Writer class DummyWriter2 : public mitk::AbstractFileWriter { public: DummyWriter2(const DummyWriter2& other) : mitk::AbstractFileWriter(other) { } DummyWriter2(const std::string& basedataType, const std::string& extension, int priority) : mitk::AbstractFileWriter(basedataType, extension, "This is a dummy description") { m_Priority = priority; m_ServiceReg = this->RegisterService(); } ~DummyWriter2() { if (m_ServiceReg) m_ServiceReg.Unregister(); } using AbstractFileWriter::Write; virtual void Write(const mitk::BaseData* /*data*/, std::ostream& /*stream*/ ) { } virtual void SetOptions(const std::list< std::string >& options ) { m_Options = options; //m_Registration.SetProperties(ConstructServiceProperties()); } private: DummyWriter2* Clone() const { return new DummyWriter2(*this); } us::ServiceRegistration m_ServiceReg; }; // End of internal dummy Writer 2 /** * TODO */ int mitkFileWriterManagerTest(int argc , char* argv[]) { // always start with this! MITK_TEST_BEGIN("FileWriterManager"); // mitk::FileWriterRegistry::Pointer frm = mitk::FileWriterRegistry::New(); // MITK_TEST_CONDITION_REQUIRED(argc == 2,"Testing FileWriterRegistry instantiation"); DummyWriter testDR("testdata", "test", 1); DummyWriter otherDR("testdata", "other", 1); // MITK_TEST_CONDITION_REQUIRED(testDR->CanWrite("/this/is/a/folder/file.test"),"Positive test of default CanRead() implementation"); // MITK_TEST_CONDITION_REQUIRED(!testDR->CanWrite("/this/is/a/folder/file.tes"),"Negative test of default CanRead() implementation"); mitk::FileWriterRegistry* writerManager = new mitk::FileWriterRegistry; mitk::IFileWriter* returned = writerManager->GetWriter("test"); MITK_TEST_CONDITION_REQUIRED(returned && &static_cast(testDR) != returned,"Testing correct retrieval of FileWriter 1/2"); returned = writerManager->GetWriter("other"); MITK_TEST_CONDITION_REQUIRED(returned && &static_cast(otherDR) != returned,"Testing correct retrieval of FileWriter 2/2"); DummyWriter mediocreTestDR("testdata", "test", 20); DummyWriter prettyFlyTestDR("testdata", "test", 50); DummyWriter2 awesomeTestDR("testdata", "test", 100); returned = writerManager->GetWriter("test"); MITK_TEST_CONDITION_REQUIRED(dynamic_cast(&awesomeTestDR), "Testing correct priorized retrieval of FileWriter: Best Writer"); // Now to give those Writers some options, then we will try again - std::list options; - options.push_front("isANiceGuy"); + mitk::IFileWriter::OptionList options; + options.push_back(std::make_pair("isANiceGuy", true)); mediocreTestDR.SetOptions(options); options.clear(); - options.push_front("canFly"); + options.push_back(std::make_pair("canFly", true)); prettyFlyTestDR.SetOptions(options); - options.push_front("isAwesome"); + options.push_back(std::make_pair("isAwesome", true)); awesomeTestDR.SetOptions(options); //note: awesomeWriter canFly and isAwesome // Reset Options, use to define what we want the Writer to do - options.clear(); - options.push_front("canFly"); + mitk::IFileWriter::OptionNames optionFilter; + optionFilter.push_back("canFly"); returned = writerManager->GetWriter("test", options); MITK_TEST_CONDITION_REQUIRED(returned && &static_cast(awesomeTestDR) != returned, "Testing correct retrieval of FileWriter with Options: Best Writer with options"); - options.push_front("isAwesome"); + optionFilter.push_back("isAwesome"); returned = writerManager->GetWriter("test", options); MITK_TEST_CONDITION_REQUIRED(returned && &static_cast(awesomeTestDR) != returned, "Testing correct retrieval of FileWriter with multiple Options: Best Writer with options"); - options.clear(); - options.push_front("isANiceGuy"); + optionFilter.clear(); + optionFilter.push_back("isANiceGuy"); returned = writerManager->GetWriter("test", options); MITK_TEST_CONDITION_REQUIRED(returned && &static_cast(mediocreTestDR) != returned, "Testing correct retrieval of specific FileWriter with Options: Low priority Writer with specific option"); - options.push_front("canFly"); + optionFilter.push_back("canFly"); returned = writerManager->GetWriter("test", options); MITK_TEST_CONDITION_REQUIRED(returned == NULL, "Testing correct return of 0 value when no matching Writer was found"); // Onward to test the retrieval of multiple Writers std::vector< mitk::IFileWriter* > returnedList; returnedList = writerManager->GetWriters("test", options); MITK_TEST_CONDITION_REQUIRED(returnedList.empty(), "Testing correct return of zero Writers when no matching Writer was found, asking for all compatibles"); - options.clear(); - options.push_back("canFly"); + optionFilter.clear(); + optionFilter.push_back("canFly"); returnedList = writerManager->GetWriters("test", options); MITK_TEST_CONDITION_REQUIRED(returnedList.size() == 2, "Testing correct return of two Writers when two matching Writer was found, asking for all compatibles"); MITK_TEST_CONDITION_REQUIRED(dynamic_cast(returnedList.front()), "Testing correct priorization of returned Writers with options 1/2"); - options.clear(); - options.push_back("isAwesome"); + optionFilter.clear(); + optionFilter.push_back("isAwesome"); returnedList = writerManager->GetWriters("test", options); MITK_TEST_CONDITION_REQUIRED(returnedList.size() == 1, "Testing correct return of one Writers when one matching Writer was found, asking for all compatibles"); MITK_TEST_CONDITION_REQUIRED(dynamic_cast(returnedList.front()), "Testing correctness of result from former query"); mitk::CoreObjectFactory::GetInstance(); //mitk::FileReaderManager readerManager; //mitk::Image::Pointer image = readerManager.Read("F://Build//MITK-Data//Pic2DplusT.nrrd"); //writerManager->Write(image.GetPointer(), "F://Build//MITK-Data//Pic2DplusTcopy.nrrd"); //// And now to verify a working read chain for a mps file: //mitk::PointSetWriter::Pointer psr = mitk::PointSetWriter::New(); //mitk::BaseData::Pointer basedata; //basedata = mitk::FileWriterRegistry::Read("F://Build//MITK-Data//pointSet.mps"); //MITK_TEST_CONDITION_REQUIRED(basedata.IsNotNull(), "Testing correct read of PointSet"); //// Testing templated call to WriterManager //mitk::PointSet::Pointer pointset = mitk::FileWriterRegistry::Read< mitk::PointSet >("F://Build//MITK-Data//pointSet.mps"); //MITK_TEST_CONDITION_REQUIRED(pointset.IsNotNull(), "Testing templated call of Read()"); //// And now for something completely different... (Debug) //mitk::LegacyFileWriterService::Pointer lfr = mitk::LegacyFileWriterService::New(".nrrd", "Nearly Raw Raster Data"); //returned = mitk::FileWriterRegistry::GetWriter(".nrrd"); //MITK_TEST_CONDITION_REQUIRED(lfr == returned, "Testing correct retrieval of specific FileWriter with Options: Low priority Writer with specific option"); //mitk::BaseData::Pointer image = mitk::FileWriterRegistry::Read("F://Build//MITK-Data//Pic2DplusT.nrrd"); //MITK_TEST_CONDITION_REQUIRED(image.IsNotNull(), "Testing whether BaseData is empty or not"); //mitk::Image::Pointer image2 = dynamic_cast (image.GetPointer()); //MITK_TEST_CONDITION_REQUIRED(image2.IsNotNull(), "Testing if BaseData is an image"); // Delete this here because it will call the PrototypeServiceFactory::Unget() method // of the dummy writers. delete writerManager; //// always end with this! MITK_TEST_END(); }