From 18f9581d4e0787b0a31a3a512df7d355f8a88ff4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Fri, 14 Feb 2020 11:32:01 +0100 Subject: [PATCH] Trade: extract reusable code out of magnum-imageconverter. Will be needed by magnum-sceneconverter as well. --- src/Magnum/Trade/CMakeLists.txt | 3 +- .../Trade/Implementation/converterUtilities.h | 68 +++++++++++++++++++ src/Magnum/Trade/imageconverter.cpp | 32 +-------- 3 files changed, 73 insertions(+), 30 deletions(-) create mode 100644 src/Magnum/Trade/Implementation/converterUtilities.h diff --git a/src/Magnum/Trade/CMakeLists.txt b/src/Magnum/Trade/CMakeLists.txt index 09a34b12c..698ac1e35 100644 --- a/src/Magnum/Trade/CMakeLists.txt +++ b/src/Magnum/Trade/CMakeLists.txt @@ -69,7 +69,8 @@ set(MagnumTrade_HEADERS visibility.h) set(MagnumTrade_PRIVATE_HEADERS - Implementation/arrayUtilities.h) + Implementation/arrayUtilities.h + Implementation/converterUtilities.h) if(MAGNUM_BUILD_DEPRECATED) list(APPEND MagnumTrade_GracefulAssert_SRCS diff --git a/src/Magnum/Trade/Implementation/converterUtilities.h b/src/Magnum/Trade/Implementation/converterUtilities.h new file mode 100644 index 000000000..df9ee94c3 --- /dev/null +++ b/src/Magnum/Trade/Implementation/converterUtilities.h @@ -0,0 +1,68 @@ +#ifndef Magnum_Trade_Implementation_converterUtilities_h +#define Magnum_Trade_Implementation_converterUtilities_h +/* + This file is part of Magnum. + + Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 + 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 + +#include "Magnum/Magnum.h" + +namespace Magnum { namespace Trade { namespace Implementation { + +/* Used only in executables where we don't want it to be exported */ +namespace { + +void setOptions(PluginManager::AbstractPlugin& plugin, const std::string& options) { + for(const std::string& option: Utility::String::splitWithoutEmptyParts(options, ',')) { + auto keyValue = Utility::String::partition(option, '='); + Utility::String::trimInPlace(keyValue[0]); + Utility::String::trimInPlace(keyValue[2]); + + /* Provide a warning message in case the plugin doesn't define given + option in its default config. The plugin is not *required* to have + those tho (could be backward compatibility entries, for example), so + not an error. */ + if(!plugin.configuration().valueCount(keyValue[0])) + Warning{} << "Option" << keyValue[0] << "not recognized by" << plugin.plugin(); + + /* If the option doesn't have an =, treat it as a boolean flag that's + set to true. While there's no similar way to do an inverse, it's + still nicer than causing a fatal error with those. */ + if(keyValue[1].empty()) + plugin.configuration().setValue(keyValue[0], true); + else + plugin.configuration().setValue(keyValue[0], keyValue[2]); + } +} + +} + +}}} + +#endif diff --git a/src/Magnum/Trade/imageconverter.cpp b/src/Magnum/Trade/imageconverter.cpp index bdbbdc971..d29a050e1 100644 --- a/src/Magnum/Trade/imageconverter.cpp +++ b/src/Magnum/Trade/imageconverter.cpp @@ -36,6 +36,7 @@ #include "Magnum/Trade/AbstractImporter.h" #include "Magnum/Trade/AbstractImageConverter.h" #include "Magnum/Trade/ImageData.h" +#include "Magnum/Trade/Implementation/converterUtilities.h" namespace Magnum { @@ -124,33 +125,6 @@ magnum-imageconverter image.dds --converter raw data.dat using namespace Magnum; -namespace { - -void setOptions(PluginManager::AbstractPlugin& plugin, const std::string& options) { - for(const std::string& option: Utility::String::splitWithoutEmptyParts(options, ',')) { - auto keyValue = Utility::String::partition(option, '='); - Utility::String::trimInPlace(keyValue[0]); - Utility::String::trimInPlace(keyValue[2]); - - /* Provide a warning message in case the plugin doesn't define given - option in its default config. The plugin is not *required* to have - those tho (could be backward compatibility entries, for example), so - not an error. */ - if(!plugin.configuration().valueCount(keyValue[0])) - Warning{} << "Option" << keyValue[0] << "not recognized by" << plugin.plugin(); - - /* If the option doesn't have an =, treat it as a boolean flag that's - set to true. While there's no similar way to do an inverse, it's - still nicer than causing a fatal error with those. */ - if(keyValue[1].empty()) - plugin.configuration().setValue(keyValue[0], true); - else - plugin.configuration().setValue(keyValue[0], keyValue[2]); - } -} - -} - int main(int argc, char** argv) { Utility::Arguments args; args.addArgument("input").setHelp("input", "input image") @@ -214,7 +188,7 @@ key=true.)") } /* Set options, if passed */ - setOptions(*importer, args.value("importer-options")); + Trade::Implementation::setOptions(*importer, args.value("importer-options")); /* Open input file */ if(!importer->openFile(args.value("input")) || !(image = importer->image2D(0))) { @@ -252,7 +226,7 @@ key=true.)") } /* Set options, if passed */ - setOptions(*converter, args.value("converter-options")); + Trade::Implementation::setOptions(*converter, args.value("converter-options")); /* Save output file */ if(!converter->exportToFile(*image, args.value("output"))) {