commit dccba881324172846917be1d87b67dda814b2e85 Author: Vladimír Vondruš Date: Thu Jul 24 23:03:55 2014 +0200 Added AnyImageImporter. Detects file type based on extension and delegates the operation to specialized importer plugin. diff --git a/src/MagnumPlugins/AnyImageImporter/AnyImageImporter.conf b/src/MagnumPlugins/AnyImageImporter/AnyImageImporter.conf new file mode 100644 index 000000000..e69de29bb diff --git a/src/MagnumPlugins/AnyImageImporter/AnyImageImporter.cpp b/src/MagnumPlugins/AnyImageImporter/AnyImageImporter.cpp new file mode 100644 index 000000000..a11564c52 --- /dev/null +++ b/src/MagnumPlugins/AnyImageImporter/AnyImageImporter.cpp @@ -0,0 +1,85 @@ +/* + This file is part of Magnum. + + Copyright © 2010, 2011, 2012, 2013, 2014 + 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 "AnyImageImporter.h" + +#include +#include +#include + +#include "Magnum/Trade/ImageData.h" + +namespace Magnum { namespace Trade { + +AnyImageImporter::AnyImageImporter(PluginManager::Manager& manager): AbstractImporter{manager} {} + +AnyImageImporter::AnyImageImporter(PluginManager::AbstractManager& manager, std::string plugin): AbstractImporter{manager, std::move(plugin)} {} + +AnyImageImporter::~AnyImageImporter() = default; + +auto AnyImageImporter::doFeatures() const -> Features { return {}; } + +bool AnyImageImporter::doIsOpened() const { return !!_in; } + +void AnyImageImporter::doClose() { + _in = nullptr; +} + +void AnyImageImporter::doOpenFile(const std::string& filename) { + CORRADE_INTERNAL_ASSERT(manager()); + + /* Detect type from extension */ + std::string plugin; + if(Utility::String::endsWith(filename, ".tga")) + plugin = "TgaImporter"; + else if(Utility::String::endsWith(filename, ".png")) + plugin = "PngImporter"; + else if(Utility::String::endsWith(filename, ".jpg") || Utility::String::endsWith(filename, ".jpeg")) + plugin = "JpegImporter"; + else { + Error() << "Trade::AnyImageImporter::openFile(): cannot determine type of file" << filename; + return; + } + + /* Try to load the plugin */ + if(!(manager()->load(plugin) & PluginManager::LoadState::Loaded)) { + Error() << "Trade::AnyImageImporter::openFile(): cannot load" << plugin << "plugin"; + return; + } + + /* Try to open the file (error output should be printed by the plugin + itself) */ + std::unique_ptr importer = static_cast*>(manager())->instance(plugin); + if(!importer->openFile(filename)) return; + + /* Success, save the instance */ + _in = std::move(importer); +} + +UnsignedInt AnyImageImporter::doImage2DCount() const { return _in->image2DCount(); } + +std::optional AnyImageImporter::doImage2D(const UnsignedInt id) { return _in->image2D(id); } + +}} diff --git a/src/MagnumPlugins/AnyImageImporter/AnyImageImporter.h b/src/MagnumPlugins/AnyImageImporter/AnyImageImporter.h new file mode 100644 index 000000000..5e1432523 --- /dev/null +++ b/src/MagnumPlugins/AnyImageImporter/AnyImageImporter.h @@ -0,0 +1,84 @@ +#ifndef Magnum_Trade_AnyImageImporter_h +#define Magnum_Trade_AnyImageImporter_h +/* + This file is part of Magnum. + + Copyright © 2010, 2011, 2012, 2013, 2014 + 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::AnyImageImporter + */ + +#include +#include + +#ifndef DOXYGEN_GENERATING_OUTPUT + #if defined(AnyImageImporter_EXPORTS) || defined(AnyImageImporterObjects_EXPORTS) + #define MAGNUM_ANYIMAGEIMPORTER_EXPORT CORRADE_VISIBILITY_EXPORT + #else + #define MAGNUM_ANYIMAGEIMPORTER_EXPORT CORRADE_VISIBILITY_IMPORT + #endif + #define MAGNUM_ANYIMAGEIMPORTER_LOCAL CORRADE_VISIBILITY_LOCAL +#endif + +namespace Magnum { namespace Trade { + +/** +@brief Any image importer plugin + +Detects file type based on file extension, then loads either +@ref TgaImporter, @ref PngImporter or @ref JpegImporter and tries to open the +file with it. See documentation of each particular plugin for more information. + +Only loading from files is supported. +*/ +class MAGNUM_ANYIMAGEIMPORTER_EXPORT AnyImageImporter: public AbstractImporter { + public: + /** + * @brief Constructor + * + * The plugin needs access to plugin manager to function properly. + */ + explicit AnyImageImporter(PluginManager::Manager& manager); + + /** @brief Plugin manager constructor */ + explicit AnyImageImporter(PluginManager::AbstractManager& manager, std::string plugin); + + ~AnyImageImporter(); + + private: + MAGNUM_ANYIMAGEIMPORTER_LOCAL Features doFeatures() const override; + MAGNUM_ANYIMAGEIMPORTER_LOCAL bool doIsOpened() const override; + MAGNUM_ANYIMAGEIMPORTER_LOCAL void doClose() override; + MAGNUM_ANYIMAGEIMPORTER_LOCAL void doOpenFile(const std::string& filename) override; + + MAGNUM_ANYIMAGEIMPORTER_LOCAL UnsignedInt doImage2DCount() const override; + MAGNUM_ANYIMAGEIMPORTER_LOCAL std::optional doImage2D(UnsignedInt id) override; + + private: + std::unique_ptr _in; +}; + +}} + +#endif diff --git a/src/MagnumPlugins/AnyImageImporter/CMakeLists.txt b/src/MagnumPlugins/AnyImageImporter/CMakeLists.txt new file mode 100644 index 000000000..5fbe7c61e --- /dev/null +++ b/src/MagnumPlugins/AnyImageImporter/CMakeLists.txt @@ -0,0 +1,59 @@ +# +# This file is part of Magnum. +# +# Copyright © 2010, 2011, 2012, 2013, 2014 +# 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(AnyImageImporter_SRCS + AnyImageImporter.cpp) + +set(AnyImageImporter_HEADERS + AnyImageImporter.h) + +add_library(AnyImageImporterObjects OBJECT ${AnyImageImporter_SRCS}) +set_target_properties(AnyImageImporterObjects PROPERTIES COMPILE_FLAGS "${CMAKE_SHARED_LIBRARY_CXX_FLAGS}") + +add_plugin(AnyImageImporter ${MAGNUM_PLUGINS_IMPORTER_DEBUG_INSTALL_DIR} ${MAGNUM_PLUGINS_IMPORTER_RELEASE_INSTALL_DIR} + AnyImageImporter.conf + $ + pluginRegistration.cpp) +target_link_libraries(AnyImageImporter + ${MAGNUM_LIBRARIES}) + +install(FILES ${AnyImageImporter_HEADERS} DESTINATION ${MAGNUM_PLUGINS_INCLUDE_INSTALL_DIR}/AnyImageImporter) + +if(BUILD_TESTS) + add_library(MagnumAnyImageImporterTestLib STATIC $) + set_target_properties(MagnumAnyImageImporterTestLib PROPERTIES DEBUG_POSTFIX "-d") + target_link_libraries(MagnumAnyImageImporterTestLib ${MAGNUM_LIBRARIES}) + + # On Windows we need to install first and then run the tests to avoid "DLL + # not found" hell, thus we need to install this too + if(CORRADE_TARGET_WINDOWS AND NOT CMAKE_CROSSCOMPILING) + install(TARGETS MagnumAnyImageImporterTestLib + RUNTIME DESTINATION ${MAGNUM_BINARY_INSTALL_DIR} + LIBRARY DESTINATION ${MAGNUM_LIBRARY_INSTALL_DIR} + ARCHIVE DESTINATION ${MAGNUM_LIBRARY_INSTALL_DIR}) + endif() + + add_subdirectory(Test) +endif() diff --git a/src/MagnumPlugins/AnyImageImporter/Test/CMakeLists.txt b/src/MagnumPlugins/AnyImageImporter/Test/CMakeLists.txt new file mode 100644 index 000000000..16c88cb9b --- /dev/null +++ b/src/MagnumPlugins/AnyImageImporter/Test/CMakeLists.txt @@ -0,0 +1,35 @@ +# +# This file is part of Magnum. +# +# Copyright © 2010, 2011, 2012, 2013, 2014 +# 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(TGA_FILE ${CMAKE_SOURCE_DIR}/src/MagnumPlugins/ColladaImporter/Test/ColladaImporterTestFiles/image.tga) +set(JPEG_FILE ${CMAKE_SOURCE_DIR}/src/MagnumPlugins/JpegImporter/Test/rgb.jpg) +set(PNG_FILE ${CMAKE_SOURCE_DIR}/src/MagnumPlugins/PngImporter/Test/rgb.png) + +configure_file(${CMAKE_CURRENT_SOURCE_DIR}/configure.h.cmake + ${CMAKE_CURRENT_BINARY_DIR}/configure.h) + +corrade_add_test(AnyImageImporterTest Test.cpp LIBRARIES MagnumAnyImageImporterTestLib) diff --git a/src/MagnumPlugins/AnyImageImporter/Test/Test.cpp b/src/MagnumPlugins/AnyImageImporter/Test/Test.cpp new file mode 100644 index 000000000..71a14e69d --- /dev/null +++ b/src/MagnumPlugins/AnyImageImporter/Test/Test.cpp @@ -0,0 +1,111 @@ +/* + This file is part of Magnum. + + Copyright © 2010, 2011, 2012, 2013, 2014 + 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 "Magnum/Trade/ImageData.h" + +#include "MagnumPlugins/AnyImageImporter/AnyImageImporter.h" + +#include "configure.h" + +namespace Magnum { namespace Trade { namespace Test { + +class AnyImageImporterTest: public TestSuite::Tester { + public: + explicit AnyImageImporterTest(); + + void tga(); + void jpeg(); + void png(); + + void unknown(); + + private: + PluginManager::Manager _manager; +}; + +AnyImageImporterTest::AnyImageImporterTest(): _manager{MAGNUM_PLUGINS_IMPORTER_DIR} { + addTests({&AnyImageImporterTest::tga, + &AnyImageImporterTest::jpeg, + &AnyImageImporterTest::png, + + &AnyImageImporterTest::unknown}); +} + +void AnyImageImporterTest::tga() { + if(_manager.loadState("TgaImporter") == PluginManager::LoadState::NotFound) + CORRADE_SKIP("TgaImporter plugin not found, cannot test"); + + AnyImageImporter importer{_manager}; + CORRADE_VERIFY(importer.openFile(TGA_FILE)); + + /* Check only size, as it is good enough proof that it is working */ + std::optional image = importer.image2D(0); + CORRADE_VERIFY(image); + CORRADE_COMPARE(image->size(), Vector2i(2, 3)); +} + +void AnyImageImporterTest::jpeg() { + if(_manager.loadState("JpegImporter") == PluginManager::LoadState::NotFound) + CORRADE_SKIP("JpegImporter plugin not found, cannot test"); + + AnyImageImporter importer{_manager}; + CORRADE_VERIFY(importer.openFile(JPEG_FILE)); + + /* Check only size, as it is good enough proof that it is working */ + std::optional image = importer.image2D(0); + CORRADE_VERIFY(image); + CORRADE_COMPARE(image->size(), Vector2i(3, 2)); +} + +void AnyImageImporterTest::png() { + if(_manager.loadState("PngImporter") == PluginManager::LoadState::NotFound) + CORRADE_SKIP("PngImporter plugin not found, cannot test"); + + AnyImageImporter importer{_manager}; + CORRADE_VERIFY(importer.openFile(PNG_FILE)); + + /* Check only size, as it is good enough proof that it is working */ + std::optional image = importer.image2D(0); + CORRADE_VERIFY(image); + CORRADE_COMPARE(image->size(), Vector2i(3, 2)); +} + +void AnyImageImporterTest::unknown() { + std::ostringstream output; + Error::setOutput(&output); + + AnyImageImporter importer{_manager}; + CORRADE_VERIFY(!importer.openFile("image.xcf")); + + CORRADE_COMPARE(output.str(), "Trade::AnyImageImporter::openFile(): cannot determine type of file image.xcf\n"); +} + +}}} + +CORRADE_TEST_MAIN(Magnum::Trade::Test::AnyImageImporterTest) diff --git a/src/MagnumPlugins/AnyImageImporter/Test/configure.h.cmake b/src/MagnumPlugins/AnyImageImporter/Test/configure.h.cmake new file mode 100644 index 000000000..538430d39 --- /dev/null +++ b/src/MagnumPlugins/AnyImageImporter/Test/configure.h.cmake @@ -0,0 +1,34 @@ +/* + This file is part of Magnum. + + Copyright © 2010, 2011, 2012, 2013, 2014 + 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_IMPORTER_DIR "${MAGNUM_PLUGINS_IMPORTER_DEBUG_DIR}" +#else +#define MAGNUM_PLUGINS_IMPORTER_DIR "${MAGNUM_PLUGINS_IMPORTER_DIR}" +#endif + +#define TGA_FILE "${TGA_FILE}" +#define JPEG_FILE "${JPEG_FILE}" +#define PNG_FILE "${PNG_FILE}" diff --git a/src/MagnumPlugins/AnyImageImporter/pluginRegistration.cpp b/src/MagnumPlugins/AnyImageImporter/pluginRegistration.cpp new file mode 100644 index 000000000..c92852bc8 --- /dev/null +++ b/src/MagnumPlugins/AnyImageImporter/pluginRegistration.cpp @@ -0,0 +1,29 @@ +/* + This file is part of Magnum. + + Copyright © 2010, 2011, 2012, 2013, 2014 + 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 "AnyImageImporter.h" + +CORRADE_PLUGIN_REGISTER(AnyImageImporter, Magnum::Trade::AnyImageImporter, + "cz.mosra.magnum.Trade.AbstractImporter/0.3")