mirror of https://github.com/mosra/magnum.git
Browse Source
Similarly to AnyImageImporter and AnySceneImporter detects file type based on extension and then delegates the work to appropriate plugin.pull/205/head
8 changed files with 403 additions and 0 deletions
@ -0,0 +1,80 @@
|
||||
/*
|
||||
This file is part of Magnum. |
||||
|
||||
Copyright © 2010, 2011, 2012, 2013, 2014, 2015 |
||||
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 "AnyImageConverter.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 { |
||||
|
||||
AnyImageConverter::AnyImageConverter(PluginManager::Manager<AbstractImageConverter>& 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<PluginManager::Manager<AbstractImageConverter>*>(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; |
||||
} |
||||
|
||||
}} |
||||
@ -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š <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 @ref Magnum::Trade::AnyImageConverter |
||||
*/ |
||||
|
||||
#include <Magnum/Trade/AbstractImageConverter.h> |
||||
|
||||
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<AbstractImageConverter>& 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 |
||||
@ -0,0 +1,57 @@
|
||||
# |
||||
# This file is part of Magnum. |
||||
# |
||||
# Copyright © 2010, 2011, 2012, 2013, 2014, 2015 |
||||
# 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(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 |
||||
$<TARGET_OBJECTS:AnyImageConverterObjects> |
||||
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_OBJECTS:AnyImageConverterObjects>) |
||||
target_link_libraries(MagnumAnyImageConverterTestLib ${MAGNUM_LIBRARIES}) |
||||
add_subdirectory(Test) |
||||
endif() |
||||
@ -0,0 +1,33 @@
|
||||
# |
||||
# This file is part of Magnum. |
||||
# |
||||
# Copyright © 2010, 2011, 2012, 2013, 2014, 2015 |
||||
# 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(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) |
||||
@ -0,0 +1,92 @@
|
||||
/*
|
||||
This file is part of Magnum. |
||||
|
||||
Copyright © 2010, 2011, 2012, 2013, 2014, 2015 |
||||
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/Utility/Directory.h> |
||||
#include <Corrade/PluginManager/Manager.h> |
||||
|
||||
#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<AbstractImageConverter> _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) |
||||
@ -0,0 +1,32 @@
|
||||
/* |
||||
This file is part of Magnum. |
||||
|
||||
Copyright © 2010, 2011, 2012, 2013, 2014, 2015 |
||||
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_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}" |
||||
@ -0,0 +1,29 @@
|
||||
/*
|
||||
This file is part of Magnum. |
||||
|
||||
Copyright © 2010, 2011, 2012, 2013, 2014, 2015 |
||||
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 "MagnumPlugins/AnyImageConverter/AnyImageConverter.h" |
||||
|
||||
CORRADE_PLUGIN_REGISTER(AnyImageConverter, Magnum::Trade::AnyImageConverter, |
||||
"cz.mosra.magnum.Trade.AbstractImageConverter/0.2.1") |
||||
Loading…
Reference in new issue