Browse Source

Added AnyImageImporter.

Detects file type based on extension and delegates the operation to
specialized importer plugin.
pull/205/head
Vladimír Vondruš 12 years ago
commit
dccba88132
  1. 0
      src/MagnumPlugins/AnyImageImporter/AnyImageImporter.conf
  2. 85
      src/MagnumPlugins/AnyImageImporter/AnyImageImporter.cpp
  3. 84
      src/MagnumPlugins/AnyImageImporter/AnyImageImporter.h
  4. 59
      src/MagnumPlugins/AnyImageImporter/CMakeLists.txt
  5. 35
      src/MagnumPlugins/AnyImageImporter/Test/CMakeLists.txt
  6. 111
      src/MagnumPlugins/AnyImageImporter/Test/Test.cpp
  7. 34
      src/MagnumPlugins/AnyImageImporter/Test/configure.h.cmake
  8. 29
      src/MagnumPlugins/AnyImageImporter/pluginRegistration.cpp

0
src/MagnumPlugins/AnyImageImporter/AnyImageImporter.conf

85
src/MagnumPlugins/AnyImageImporter/AnyImageImporter.cpp

@ -0,0 +1,85 @@
/*
This file is part of Magnum.
Copyright © 2010, 2011, 2012, 2013, 2014
Vladimír Vondruš <mosra@centrum.cz>
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 <Corrade/PluginManager/Manager.h>
#include <Corrade/Utility/Assert.h>
#include <Corrade/Utility/String.h>
#include "Magnum/Trade/ImageData.h"
namespace Magnum { namespace Trade {
AnyImageImporter::AnyImageImporter(PluginManager::Manager<AbstractImporter>& 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<AbstractImporter> importer = static_cast<PluginManager::Manager<AbstractImporter>*>(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<ImageData2D> AnyImageImporter::doImage2D(const UnsignedInt id) { return _in->image2D(id); }
}}

84
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š <mosra@centrum.cz>
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 <Corrade/Containers/Array.h>
#include <Magnum/Trade/AbstractImporter.h>
#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<AbstractImporter>& 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<ImageData2D> doImage2D(UnsignedInt id) override;
private:
std::unique_ptr<AbstractImporter> _in;
};
}}
#endif

59
src/MagnumPlugins/AnyImageImporter/CMakeLists.txt

@ -0,0 +1,59 @@
#
# This file is part of Magnum.
#
# Copyright © 2010, 2011, 2012, 2013, 2014
# Vladimír Vondruš <mosra@centrum.cz>
#
# 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
$<TARGET_OBJECTS:AnyImageImporterObjects>
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 $<TARGET_OBJECTS:AnyImageImporterObjects>)
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()

35
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š <mosra@centrum.cz>
#
# 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)

111
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š <mosra@centrum.cz>
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 <sstream>
#include <Corrade/TestSuite/Tester.h>
#include <Corrade/PluginManager/Manager.h>
#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<AbstractImporter> _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<ImageData2D> 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<ImageData2D> 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<ImageData2D> 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)

34
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š <mosra@centrum.cz>
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}"

29
src/MagnumPlugins/AnyImageImporter/pluginRegistration.cpp

@ -0,0 +1,29 @@
/*
This file is part of Magnum.
Copyright © 2010, 2011, 2012, 2013, 2014
Vladimír Vondruš <mosra@centrum.cz>
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")
Loading…
Cancel
Save