diff --git a/src/MagnumPlugins/AnyImageConverter/AnyImageConverter.conf b/src/MagnumPlugins/AnyImageConverter/AnyImageConverter.conf new file mode 100644 index 000000000..e69de29bb diff --git a/src/MagnumPlugins/AnyImageConverter/AnyImageConverter.cpp b/src/MagnumPlugins/AnyImageConverter/AnyImageConverter.cpp new file mode 100644 index 000000000..d1f131bcf --- /dev/null +++ b/src/MagnumPlugins/AnyImageConverter/AnyImageConverter.cpp @@ -0,0 +1,80 @@ +/* + This file is part of Magnum. + + Copyright © 2010, 2011, 2012, 2013, 2014, 2015 + 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 "AnyImageConverter.h" + +#include +#include +#include + +#include "Magnum/Trade/ImageData.h" + +namespace Magnum { namespace Trade { + +AnyImageConverter::AnyImageConverter(PluginManager::Manager& manager): AbstractImageConverter{manager} {} + +AnyImageConverter::AnyImageConverter(PluginManager::AbstractManager& manager, std::string plugin): AbstractImageConverter{manager, std::move(plugin)} {} + +AnyImageConverter::~AnyImageConverter() = default; + +auto AnyImageConverter::doFeatures() const -> Features { + return Feature::ConvertFile|Feature::ConvertCompressedFile; +} + +bool AnyImageConverter::doExportToFile(const ImageView2D& image, const std::string& filename) { + CORRADE_INTERNAL_ASSERT(manager()); + + /* Detect type from extension */ + std::string plugin; + if(Utility::String::endsWith(filename, ".png")) + plugin = "PngImageConverter"; + else if(Utility::String::endsWith(filename, ".tga")) + plugin = "TgaImageConverter"; + else { + Error() << "Trade::AnyImageConverter::exportToFile(): cannot determine type of file" << filename; + return false; + } + + /* Try to load the plugin */ + if(!(manager()->load(plugin) & PluginManager::LoadState::Loaded)) { + Error() << "Trade::AnyImageConverter::exportToFile(): cannot load" << plugin << "plugin"; + return false; + } + + /* Try to convert the file (error output should be printed by the plugin + itself) */ + return static_cast*>(manager())->instance(plugin)->exportToFile(image, filename); +} + +bool AnyImageConverter::doExportToFile(const CompressedImageView2D&, const std::string& filename) { + CORRADE_INTERNAL_ASSERT(manager()); + + /* No file formats to store compressed data yet */ + + Error() << "Trade::AnyImageConverter::exportToFile(): cannot determine type of file" << filename << "to store compressed data"; + return false; +} + +}} diff --git a/src/MagnumPlugins/AnyImageConverter/AnyImageConverter.h b/src/MagnumPlugins/AnyImageConverter/AnyImageConverter.h new file mode 100644 index 000000000..c5f7d5e9f --- /dev/null +++ b/src/MagnumPlugins/AnyImageConverter/AnyImageConverter.h @@ -0,0 +1,80 @@ +#ifndef Magnum_Trade_AnyImageConverter_h +#define Magnum_Trade_AnyImageConverter_h +/* + This file is part of Magnum. + + Copyright © 2010, 2011, 2012, 2013, 2014, 2015 + 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 @ref Magnum::Trade::AnyImageConverter + */ + +#include + +namespace Magnum { namespace Trade { + +/** +@brief Any image converter plugin + +Detects file type based on file extension, loads corresponding plugin and then +tries to convert the file with it. + +This plugin is built if `WITH_ANYIMAGECONVERTER` is enabled when building +Magnum Plugins. To use dynamic plugin, you need to load `AnyImageConverter` +plugin from `MAGNUM_PLUGINS_IMPORTER_DIR`. To use static plugin, you need to +request `AnyImageConverter` component of `MagnumPlugins` package in CMake and +link to `${MAGNUMPLUGINS_ANYIMAGECONVERTER_LIBRARIES}`. To use this as a +dependency of another plugin, you additionally need to add +`${MAGNUMPLUGINS_ANYIMAGECONVERTER_INCLUDE_DIRS}` to include path. See +@ref building-plugins, @ref cmake-plugins and @ref plugins for more +information. + +Supported formats for uncompressed data: + +- PNG (`*.png`), loaded with any plugin that provides `PngImageConverer` +- TGA (`*.tga`), loaded with @ref TgaImageConverter or any other plugin that + provides it + +No supported formats for compressed data yet. + +Only exporting to files is supported. +*/ +class AnyImageConverter: public AbstractImageConverter { + public: + /** @brief Constructor with access to plugin manager */ + explicit AnyImageConverter(PluginManager::Manager& manager); + + /** @brief Plugin manager constructor */ + explicit AnyImageConverter(PluginManager::AbstractManager& manager, std::string plugin); + + ~AnyImageConverter(); + + private: + Features doFeatures() const override; + bool doExportToFile(const ImageView2D& image, const std::string& filename) override; + bool doExportToFile(const CompressedImageView2D& image, const std::string& filename) override; +}; + +}} + +#endif diff --git a/src/MagnumPlugins/AnyImageConverter/CMakeLists.txt b/src/MagnumPlugins/AnyImageConverter/CMakeLists.txt new file mode 100644 index 000000000..8bf5125d4 --- /dev/null +++ b/src/MagnumPlugins/AnyImageConverter/CMakeLists.txt @@ -0,0 +1,57 @@ +# +# This file is part of Magnum. +# +# Copyright © 2010, 2011, 2012, 2013, 2014, 2015 +# 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. +# + +set(AnyImageConverter_SRCS + AnyImageConverter.cpp) + +set(AnyImageConverter_HEADERS + AnyImageConverter.h) + +# Objects shared between plugin and test library +add_library(AnyImageConverterObjects OBJECT + ${AnyImageConverter_SRCS} + ${AnyImageConverter_HEADERS}) +if(NOT BUILD_STATIC OR BUILD_STATIC_PIC) + set_target_properties(AnyImageConverterObjects PROPERTIES POSITION_INDEPENDENT_CODE ON) +endif() + +# AnyImageConverter plugin +add_plugin(AnyImageConverter ${MAGNUM_PLUGINS_IMAGECONVERTER_DEBUG_INSTALL_DIR} ${MAGNUM_PLUGINS_IMAGECONVERTER_RELEASE_INSTALL_DIR} + AnyImageConverter.conf + $ + pluginRegistration.cpp) +if(BUILD_STATIC_PIC) + set_target_properties(AnyImageConverter PROPERTIES POSITION_INDEPENDENT_CODE ON) +endif() + +target_link_libraries(AnyImageConverter ${MAGNUM_LIBRARIES}) + +install(FILES ${AnyImageConverter_HEADERS} DESTINATION ${MAGNUM_PLUGINS_INCLUDE_INSTALL_DIR}/AnyImageConverter) + +if(BUILD_TESTS) + add_library(MagnumAnyImageConverterTestLib STATIC $) + target_link_libraries(MagnumAnyImageConverterTestLib ${MAGNUM_LIBRARIES}) + add_subdirectory(Test) +endif() diff --git a/src/MagnumPlugins/AnyImageConverter/Test/CMakeLists.txt b/src/MagnumPlugins/AnyImageConverter/Test/CMakeLists.txt new file mode 100644 index 000000000..c2f799158 --- /dev/null +++ b/src/MagnumPlugins/AnyImageConverter/Test/CMakeLists.txt @@ -0,0 +1,33 @@ +# +# This file is part of Magnum. +# +# Copyright © 2010, 2011, 2012, 2013, 2014, 2015 +# 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_directories(BEFORE ${CMAKE_CURRENT_BINARY_DIR}) + +set(PNG_OUTPUT_FILE ${CMAKE_CURRENT_BINARY_DIR}/output.png) + +configure_file(${CMAKE_CURRENT_SOURCE_DIR}/configure.h.cmake + ${CMAKE_CURRENT_BINARY_DIR}/configure.h) + +corrade_add_test(AnyImageConverterTest Test.cpp LIBRARIES MagnumAnyImageConverterTestLib) diff --git a/src/MagnumPlugins/AnyImageConverter/Test/Test.cpp b/src/MagnumPlugins/AnyImageConverter/Test/Test.cpp new file mode 100644 index 000000000..de288cddd --- /dev/null +++ b/src/MagnumPlugins/AnyImageConverter/Test/Test.cpp @@ -0,0 +1,92 @@ +/* + This file is part of Magnum. + + Copyright © 2010, 2011, 2012, 2013, 2014, 2015 + 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 +#include +#include +#include + +#include "Magnum/PixelFormat.h" +#include "Magnum/Trade/ImageData.h" + +#include "MagnumPlugins/AnyImageConverter/AnyImageConverter.h" + +#include "configure.h" + +namespace Magnum { namespace Trade { namespace Test { + +struct AnyImageConverterTest: TestSuite::Tester { + explicit AnyImageConverterTest(); + + void png(); + + void unknown(); + + private: + PluginManager::Manager _manager; +}; + +AnyImageConverterTest::AnyImageConverterTest(): _manager{MAGNUM_PLUGINS_IMAGECONVERTER_DIR} { + addTests({&AnyImageConverterTest::png, + + &AnyImageConverterTest::unknown}); +} + +namespace { + constexpr const char Data[] = { + 1, 2, 3, 2, 3, 4, 0, 0, + 3, 4, 5, 4, 5, 6, 0, 0, + 5, 6, 7, 6, 7, 8, 0, 0 + }; + + const ImageView2D Image{PixelFormat::RGB, PixelType::UnsignedByte, {2, 3}, Data}; +} + +void AnyImageConverterTest::png() { + if(_manager.loadState("PngImageConverter") == PluginManager::LoadState::NotFound) + CORRADE_SKIP("PngImageConverter plugin not found, cannot test"); + + if(Utility::Directory::fileExists(PNG_OUTPUT_FILE)) + CORRADE_VERIFY(Utility::Directory::rm(PNG_OUTPUT_FILE)); + + /* Just test that the exported file exists */ + AnyImageConverter converter{_manager}; + CORRADE_VERIFY(converter.exportToFile(Image, PNG_OUTPUT_FILE)); + CORRADE_VERIFY(Utility::Directory::fileExists(PNG_OUTPUT_FILE)); +} + +void AnyImageConverterTest::unknown() { + std::ostringstream output; + Error::setOutput(&output); + + AnyImageConverter converter{_manager}; + CORRADE_VERIFY(!converter.exportToFile(Image, "image.xcf")); + + CORRADE_COMPARE(output.str(), "Trade::AnyImageConverter::exportToFile(): cannot determine type of file image.xcf\n"); +} + +}}} + +CORRADE_TEST_MAIN(Magnum::Trade::Test::AnyImageConverterTest) diff --git a/src/MagnumPlugins/AnyImageConverter/Test/configure.h.cmake b/src/MagnumPlugins/AnyImageConverter/Test/configure.h.cmake new file mode 100644 index 000000000..a7472cc7a --- /dev/null +++ b/src/MagnumPlugins/AnyImageConverter/Test/configure.h.cmake @@ -0,0 +1,32 @@ +/* + This file is part of Magnum. + + Copyright © 2010, 2011, 2012, 2013, 2014, 2015 + 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. +*/ + +#ifdef CORRADE_IS_DEBUG_BUILD +#define MAGNUM_PLUGINS_IMAGECONVERTER_DIR "${MAGNUM_PLUGINS_IMAGECONVERTER_DEBUG_DIR}" +#else +#define MAGNUM_PLUGINS_IMAGECONVERTER_DIR "${MAGNUM_PLUGINS_IMAGECONVERTER_DIR}" +#endif + +#define PNG_OUTPUT_FILE "${PNG_OUTPUT_FILE}" diff --git a/src/MagnumPlugins/AnyImageConverter/pluginRegistration.cpp b/src/MagnumPlugins/AnyImageConverter/pluginRegistration.cpp new file mode 100644 index 000000000..f396c5095 --- /dev/null +++ b/src/MagnumPlugins/AnyImageConverter/pluginRegistration.cpp @@ -0,0 +1,29 @@ +/* + This file is part of Magnum. + + Copyright © 2010, 2011, 2012, 2013, 2014, 2015 + 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 "MagnumPlugins/AnyImageConverter/AnyImageConverter.h" + +CORRADE_PLUGIN_REGISTER(AnyImageConverter, Magnum::Trade::AnyImageConverter, + "cz.mosra.magnum.Trade.AbstractImageConverter/0.2.1")