From bbb764eff5167a519ba17c74984796c1a8aefee2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Fri, 29 Mar 2013 17:56:53 +0100 Subject: [PATCH] Base for image converter plugins. --- modules/FindMagnum.cmake | 7 ++ src/CMakeLists.txt | 1 + src/Trade/AbstractImageConverter.cpp | 56 +++++++++++++ src/Trade/AbstractImageConverter.h | 119 +++++++++++++++++++++++++++ src/Trade/CMakeLists.txt | 1 + src/Trade/Trade.h | 1 + 6 files changed, 185 insertions(+) create mode 100644 src/Trade/AbstractImageConverter.cpp create mode 100644 src/Trade/AbstractImageConverter.h diff --git a/modules/FindMagnum.cmake b/modules/FindMagnum.cmake index e1e9561ad..cde956bc7 100644 --- a/modules/FindMagnum.cmake +++ b/modules/FindMagnum.cmake @@ -8,6 +8,7 @@ # MAGNUM_INCLUDE_DIRS - Root include dir and include dirs of # dependencies # MAGNUM_PLUGINS_FONT_DIR - Directory with font plugins +# MAGNUM_PLUGINS_IMAGECONVERTER_DIR - Directory with image converter plugins # MAGNUM_PLUGINS_IMPORTER_DIR - Directory with importer plugins # This command will try to find only the base library, not the optional # components. The base library depends on Corrade, OpenGL and GLEW @@ -63,6 +64,8 @@ # MAGNUM_PLUGINS_INSTALL_DIR - Plugin installation directory # MAGNUM_PLUGINS_FONT_INSTALL_DIR - Font plugin installation # directory +# MAGNUM_PLUGINS_IMAGECONVERTER_INSTALL_DIR - Image converter plugin +# installation directory # MAGNUM_PLUGINS_IMPORTER_INSTALL_DIR - Importer plugin installation # directory # MAGNUM_CMAKE_MODULE_INSTALL_DIR - Installation dir for CMake @@ -323,6 +326,7 @@ endif() set(MAGNUM_LIBRARY_INSTALL_DIR ${CMAKE_INSTALL_PREFIX}/lib${LIB_SUFFIX}) set(MAGNUM_PLUGINS_INSTALL_DIR ${MAGNUM_LIBRARY_INSTALL_DIR}/magnum) set(MAGNUM_PLUGINS_FONT_INSTALL_DIR ${MAGNUM_PLUGINS_INSTALL_DIR}/fonts) +set(MAGNUM_PLUGINS_IMAGECONVERTER_INSTALL_DIR ${MAGNUM_PLUGINS_INSTALL_DIR}/imageconverters) set(MAGNUM_PLUGINS_IMPORTER_INSTALL_DIR ${MAGNUM_PLUGINS_INSTALL_DIR}/importers) set(MAGNUM_CMAKE_MODULE_INSTALL_DIR ${CMAKE_ROOT}/Modules) set(MAGNUM_INCLUDE_INSTALL_DIR ${CMAKE_INSTALL_PREFIX}/include/Magnum) @@ -333,6 +337,7 @@ mark_as_advanced(FORCE MAGNUM_LIBRARY_INSTALL_DIR MAGNUM_PLUGINS_INSTALL_DIR MAGNUM_PLUGINS_FONT_INSTALL_DIR + MAGNUM_PLUGINS_IMAGECONVERTER_INSTALL_DIR MAGNUM_PLUGINS_IMPORTER_INSTALL_DIR MAGNUM_CMAKE_MODULE_INSTALL_DIR MAGNUM_INCLUDE_INSTALL_DIR @@ -341,8 +346,10 @@ mark_as_advanced(FORCE # Plugin directories if(NOT WIN32) set(MAGNUM_PLUGINS_FONT_DIR ${MAGNUM_PLUGINS_INSTALL_DIR}/fonts) + set(MAGNUM_PLUGINS_IMAGECONVERTER_DIR ${MAGNUM_PLUGINS_INSTALL_DIR}/imageconverters) set(MAGNUM_PLUGINS_IMPORTER_DIR ${MAGNUM_PLUGINS_INSTALL_DIR}/importers) else() set(MAGNUM_PLUGINS_FONT_DIR fonts) + set(MAGNUM_PLUGINS_IMAGECONVERTER_DIR imageconverters) set(MAGNUM_PLUGINS_IMPORTER_DIR importers) endif() diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 589b97694..61c7a059b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -67,6 +67,7 @@ set(Magnum_SRCS Implementation/BufferState.cpp Implementation/State.cpp + Trade/AbstractImageConverter.cpp Trade/AbstractImporter.cpp Trade/AbstractMaterialData.cpp Trade/MeshData2D.cpp diff --git a/src/Trade/AbstractImageConverter.cpp b/src/Trade/AbstractImageConverter.cpp new file mode 100644 index 000000000..23b3efeac --- /dev/null +++ b/src/Trade/AbstractImageConverter.cpp @@ -0,0 +1,56 @@ +/* + This file is part of Magnum. + + Copyright © 2010, 2011, 2012, 2013 Vladimír Vondruš + + Permission is hereby granted, free of charge, to any person obtaining a + copy of this software and associated documentation files (the "Software"), + to deal in the Software without restriction, including without limitation + the rights to use, copy, modify, merge, publish, distribute, sublicense, + and/or sell copies of the Software, and to permit persons to whom the + Software is furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included + in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + DEALINGS IN THE SOFTWARE. +*/ + +#include "AbstractImageConverter.h" + +#include + +namespace Magnum { namespace Trade { + +AbstractImageConverter::AbstractImageConverter() = default; + +AbstractImageConverter::AbstractImageConverter(Corrade::PluginManager::AbstractPluginManager* manager, std::string plugin): AbstractPlugin(manager, std::move(plugin)) {} + +Image2D* AbstractImageConverter::convertToImage(const Image2D* const) const { + CORRADE_ASSERT(features() & Feature::ConvertToImage, + "Trade::AbstractImageConverter::convertToImage(): feature advertised but not implemented", nullptr); + + CORRADE_ASSERT(false, "Trade::AbstractImageConverter::convertToImage(): feature not implemented", nullptr); +} + +std::pair AbstractImageConverter::convertToData(const Image2D* const) const { + CORRADE_ASSERT(features() & Feature::ConvertToData, + "Trade::AbstractImageConverter::convertToData(): feature advertised but not implemented", std::make_pair(nullptr, 0)); + + CORRADE_ASSERT(false, "Trade::AbstractImageConverter::convertToData(): feature not implemented", std::make_pair(nullptr, 0)); +} + +bool AbstractImageConverter::convertToFile(const Image2D* const, const std::string&) const { + CORRADE_ASSERT(features() & Feature::ConvertToFile, + "Trade::AbstractImageConverter::convertToFile(): feature advertised but not implemented", false); + + CORRADE_ASSERT(false, "Trade::AbstractImageConverter::convertToFile(): feature not implemented", false); +} + +}} diff --git a/src/Trade/AbstractImageConverter.h b/src/Trade/AbstractImageConverter.h new file mode 100644 index 000000000..5434618db --- /dev/null +++ b/src/Trade/AbstractImageConverter.h @@ -0,0 +1,119 @@ +#ifndef Magnum_Trade_AbstractImageConverter_h +#define Magnum_Trade_AbstractImageConverter_h +/* + This file is part of Magnum. + + Copyright © 2010, 2011, 2012, 2013 Vladimír Vondruš + + Permission is hereby granted, free of charge, to any person obtaining a + copy of this software and associated documentation files (the "Software"), + to deal in the Software without restriction, including without limitation + the rights to use, copy, modify, merge, publish, distribute, sublicense, + and/or sell copies of the Software, and to permit persons to whom the + Software is furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included + in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + DEALINGS IN THE SOFTWARE. +*/ + +/** @file + * @brief Class Magnum::Trade::AbstractImageConverter + */ + +#include + +#include "Magnum.h" +#include "Text/Text.h" +#include "magnumVisibility.h" + +namespace Magnum { namespace Trade { + +/** +@brief Base for image converter plugins + +Provides functionality for converting images between various internal formats +or compressing them. + +@section AbstractImageConverter-subclassing Subclassing + +Plugin implements function features() and one or more of convertToImage(), +convertToData() or convertToFile() functions based on what features are +supported. +*/ +class MAGNUM_EXPORT AbstractImageConverter: public Corrade::PluginManager::AbstractPlugin { + PLUGIN_INTERFACE("cz.mosra.magnum.Trade.AbstractImageConverter/0.1") + + public: + /** + * @brief Features supported by this converter + * + * @see Features, features() + */ + enum class Feature: UnsignedByte { + /** Converting to image with different format with convertToImage() */ + ConvertToImage = 1 << 0, + + /** Converting to data with convertToData() */ + ConvertToData = 1 << 1, + + /** Converting to file with convertToFile() */ + ConvertToFile = 1 << 2 + }; + + /** + * @brief Features supported by this converter + * + * @see features() + */ + typedef Corrade::Containers::EnumSet Features; + + /** @brief Default constructor */ + explicit AbstractImageConverter(); + + /** @brief Plugin manager constructor */ + explicit AbstractImageConverter(Corrade::PluginManager::AbstractPluginManager* manager, std::string plugin); + + /** @brief Features supported by this converter */ + virtual Features features() const = 0; + + /** + * @brief Convert image to different format + * + * Available only if @ref Feature "Feature::ConvertToImage" is supported. + * Returns converted image on success, `nullptr` otherwise. + * @see features(), convertToData(), convertToFile() + */ + virtual Image2D* convertToImage(const Image2D* const image) const; + + /** + * @brief Convert image to raw data + * + * Available only if @ref Feature "Feature::ConvertToData" is supported. + * Returns data pointer and size on success, `nullptr` otherwise. + * @see features(), convertToImage(), convertToFile() + */ + virtual std::pair convertToData(const Image2D* const image) const; + + /** + * @brief Convert image and save it to file + * + * Available only if @ref Feature "Feature::ConvertToFile" is supported. + * Returns `true` on success, `false` otherwise. + * @see features(), convertToImage(), convertToData() + */ + virtual bool convertToFile(const Image2D* const image, const std::string& filename) const; +}; + +CORRADE_ENUMSET_OPERATORS(AbstractImageConverter::Features) + +}} + +#endif diff --git a/src/Trade/CMakeLists.txt b/src/Trade/CMakeLists.txt index 3e541fe28..954527c36 100644 --- a/src/Trade/CMakeLists.txt +++ b/src/Trade/CMakeLists.txt @@ -24,6 +24,7 @@ set(MagnumTrade_HEADERS AbstractImporter.h + AbstractImageConverter.h AbstractMaterialData.h CameraData.h ImageData.h diff --git a/src/Trade/Trade.h b/src/Trade/Trade.h index 37dc2ba1a..a12b47f79 100644 --- a/src/Trade/Trade.h +++ b/src/Trade/Trade.h @@ -34,6 +34,7 @@ namespace Magnum { namespace Trade { /** @todoc Remove `ifndef` when Doxygen is sane again */ #ifndef DOXYGEN_GENERATING_OUTPUT +class AbstractImageConverter; class AbstractImporter; class AbstractMaterialData; class CameraData;