From eb0e4b26cb315efab0415c93f1684ab24885ae0a Mon Sep 17 00:00:00 2001 From: Git Merciol Date: Sun, 18 Feb 2018 08:26:02 +0100 Subject: [PATCH] =?UTF-8?q?=09nouveau=20fichier=C2=A0:=20include/Appli/Opt?= =?UTF-8?q?ion.hpp=20=09nouveau=20fichier=C2=A0:=20src/Appli/Option.cpp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/Appli/Option.hpp | 37 ++++++++ src/Appli/Option.cpp | 183 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 220 insertions(+) create mode 100644 include/Appli/Option.hpp create mode 100644 src/Appli/Option.cpp diff --git a/include/Appli/Option.hpp b/include/Appli/Option.hpp new file mode 100644 index 0000000..9401488 --- /dev/null +++ b/include/Appli/Option.hpp @@ -0,0 +1,37 @@ +#ifndef _OTB_TRISKELE_OPTION_HPP +#define _OTB_TRISKELE_OPTION_HPP + +#include +#include +#include + +#include "triskeleBase.hpp" +#include "IImage.hpp" + +namespace otb { + namespace triskele { + using namespace ::triskele; + + // ======================================== + + class Option { + public: + bool debugFlag; + IImage inputImage; + IImage outputImage; + Point topLeft = NullPoint; + Size size = NullSize; + DimChanel chanel = 0; + unsigned int treeCoreCount = boost::thread::hardware_concurrency (); + + Option (); + Option (int argc, char** argv); + + void usage (string msg = "", bool hidden = false); + void parse (int argc, char** argv); + }; + + }//namespace triskele +}//namespace otb + +#endif //_OTB_TRISKELE_OPTION_HPP diff --git a/src/Appli/Option.cpp b/src/Appli/Option.cpp new file mode 100644 index 0000000..31a48ba --- /dev/null +++ b/src/Appli/Option.cpp @@ -0,0 +1,183 @@ +#define LAST_VERSION "2018-02-17 (Debian Stretch)" + + +#include +#include + +#include +#include +#include + +#include "triskeleDebug.hpp" +#include "triskeleBase.hpp" +#include "IImage.hpp" +#include "Appli/Option.hpp" + +#ifndef TRISKELE_ERROR +#define TRISKELE_ERROR(expr) std::cerr << expr << std::endl << std::flush, std::exit (1) +#endif + +using namespace std; +using namespace triskele; +using namespace otb::triskele; + +// ======================================== +Option::Option () { +} + +Option::Option (int argc, char** argv) { + parse (argc, argv); +} + +// ======================================== +namespace po = boost::program_options; +po::options_description desc ("Allowed options"); +po::options_description hide ("Hidded options"); +char *prog = NULL; + +// ======================================== +void +Option::usage (string msg, bool hidden) { + if (!msg.empty ()) + cout << msg << endl; + cout << endl + <<"Usage: " << endl + <<" " << prog << " [options] [-i] inputFileName [-o] outputFileName" << endl << endl + << endl << desc << endl; + if (hidden) + cout << hide << endl; + exit (1); +} + +void +version () { + cout << endl << prog << " version " << LAST_VERSION << endl << endl + << " GDAL : read and write image (http://www.gdal.org/)" << endl + << " cct : inspired by jirihavel library to build trees (https://github.com/jirihavel/libcct)" << endl + << " Boost : C++ libraries (http://www.boost.org/)" << endl + << endl << " This software is a Obelix team production (http://www-obelix.irisa.fr/)" << endl << endl; + exit (0); +} + +// ======================================== +static const string inputFile = "input-file"; +static const char *const inputFileC = inputFile.c_str (); + +void +Option::parse (int argc, char** argv) { + prog = argv [0]; + bool helpFlag = false, versionFlag = false, useTheForceLuke = false; + string inputFileName, outputFileName; + long left = -1, top = -1, width = -1, height = -1; + try { + desc.add_options () + ("help", po::bool_switch (&helpFlag), "produce this help message") + ("version", po::bool_switch (&versionFlag), "display version information") + ("debug", po::bool_switch (&debugFlag), "debug mode") + ("band,b", po::value (&chanel), (string ("select input band (first band is 0) (default ")+ + boost::lexical_cast (chanel)+")").c_str ()) + ("left,x", po::value (&left), "left crop (default center)") + ("top,y", po::value (&top), "top crop (default middle)") + ("width,w", po::value (&width), "width crop (default input width)") + ("height,h", po::value (&height), "height crop (default input height)") + ("input,i", po::value (&inputFileName), "input file name image") + ("output,o", po::value (&outputFileName), "output file name hyperbands image (contains attributs profiles)") + //("min-tree", po::bool_switch (&options.noTreeFlag[minE]), "build min-tree") + //("area,A", po::value (&areaThresholdsName), "produce area attributs") + ; + hide.add_options () + ("use-the-force-luke", po::bool_switch (&useTheForceLuke), "display hidded options") + ("tree-core-count", po::value (&treeCoreCount), "thread used to build tree (default hardware value)") + // ("max-tree", po::bool_switch (&options.noTreeFlag[maxE]), "build max-tree") + // ("tos-tree", po::bool_switch (&options.noTreeFlag[tosE]), "build tree-of-shape") + // ("standard-deviation,S", po::value (&sdThresholdsName), "produce standard deviation attributs") + // ("moment-of-inertia,I", po::value (&moiThresholdsName), "produce moment of inertia attributs") + // ("no-border", po::bool_switch (&options.noBorder), "build tree with all pixels (included no-data)") + // ("dap", po::bool_switch (&options.dapFlag), "produce DAP rather than AP") + // ("use-all-orig", po::bool_switch (&options.useAllOrigFlag), "force use all original band") + // ("dap-no-orig", po::bool_switch (&options.dapNoOrigFlag), "force don't use original AP band") + // ("dap-orig-pos", po::value (&apOrigPos), (string ("position of origin on DAP (default ")+ + // boost::lexical_cast (apOrigPos)+ + // " = "+apOrigPosName [apOrigPos]+")").c_str ()) + // ("dap-orig-weight", po::bool_switch (&options.dapOrigWeightFlag), "use origin weight for dap") + ; + + po::options_description cmd ("All options"); + cmd.add (desc).add (hide).add_options () + (inputFileC, po::value > (), "input thresholds output") + ; + + po::positional_options_description p; + p.add (inputFileC, -1); + po::variables_map vm; + po::store (po::command_line_parser (argc, argv).options (cmd).positional (p).run (), vm); + po::store (po::command_line_parser (argc, argv).options (desc).positional (p).run (), vm); + po::notify (vm); + + if (useTheForceLuke) + usage ("", true); + if (versionFlag) + version (); + if (helpFlag) + usage (); + + int required = 2; + if (vm.count ("input")) + required--; + if (vm.count ("output")) + required--; + + int nbArgs = 0; + if (vm.count (inputFileC)) + nbArgs = vm[inputFileC].as > ().size (); + if (required-nbArgs != 0) + usage ("Need one input and one output"); + if (required) { + vector var = vm[inputFileC].as > (); + if (outputFileName.empty ()) + outputFileName = var [--required]; + if (inputFileName.empty ()) + inputFileName = var [--required]; + } + + if (debugFlag) { +#ifndef ENABLE_LOG + cout << "No debug option available. You must compile without: -DENABLE_LOG" << endl; +#endif + debug = true; + } + + } catch (...) { + usage ("Bad options"); + } + inputImage.setFileName (inputFileName); + outputImage.setFileName (outputFileName); + inputImage.readImage (); + + GDALDataType inputType = inputImage.getDataType (); + + int bandInputCount = inputImage.getBandCount (); + if (!(chanel < bandInputCount)) + TRISKELE_ERROR ("chanel " << chanel << " out of " << bandInputCount << " bands!"); + + Size orgSize = inputImage.getSize (); + if (width < 0 || width > orgSize.width) + width = left <= 0 ? orgSize.width : min (1L, orgSize.width-left); + if (height < 0 || height > orgSize.height) + height = top <= 0 ? orgSize.height : min (1L, orgSize.height-top); + if (left < 0) + left = (orgSize.width - width)/2; + if (top < 0) + top = (orgSize.height - height)/2; + if (left > orgSize.width - width) + left = orgSize.width - width; + if (top > orgSize.height - height) + top = orgSize.height - height; + topLeft = Point ((DimSideImg) left, (DimSideImg) top); + size = Size ((DimSideImg) width, (DimSideImg) height); + + cout + << "Input :" << inputFileName << " " << orgSize << " (" << bandInputCount << " chanels of " << GDALGetDataTypeName (inputType) << ")" << endl + << "Crop topLeft:" << topLeft << " size:" << size << " band:" << chanel << endl + << "Output :" << outputFileName << endl; +}