nouveau fichier : include/Appli/Option.hpp

nouveau fichier : src/Appli/Option.cpp
This commit is contained in:
Git Merciol 2018-02-18 08:26:02 +01:00
parent 4b4bbe889d
commit eb0e4b26cb
2 changed files with 220 additions and 0 deletions

37
include/Appli/Option.hpp Normal file
View File

@ -0,0 +1,37 @@
#ifndef _OTB_TRISKELE_OPTION_HPP
#define _OTB_TRISKELE_OPTION_HPP
#include <iostream>
#include <vector>
#include <boost/thread.hpp>
#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

183
src/Appli/Option.cpp Normal file
View File

@ -0,0 +1,183 @@
#define LAST_VERSION "2018-02-17 (Debian Stretch)"
#include <iostream>
#include <string.h>
#include <boost/program_options/options_description.hpp>
#include <boost/program_options/variables_map.hpp>
#include <boost/program_options/parsers.hpp>
#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<DimChanel> (&chanel), (string ("select input band (first band is 0) (default ")+
boost::lexical_cast<std::string> (chanel)+")").c_str ())
("left,x", po::value<long> (&left), "left crop (default center)")
("top,y", po::value<long> (&top), "top crop (default middle)")
("width,w", po::value<long> (&width), "width crop (default input width)")
("height,h", po::value<long> (&height), "height crop (default input height)")
("input,i", po::value<string> (&inputFileName), "input file name image")
("output,o", po::value<string> (&outputFileName), "output file name hyperbands image (contains attributs profiles)")
//("min-tree", po::bool_switch (&options.noTreeFlag[minE]), "build min-tree")
//("area,A", po::value<string> (&areaThresholdsName), "produce area attributs")
;
hide.add_options ()
("use-the-force-luke", po::bool_switch (&useTheForceLuke), "display hidded options")
("tree-core-count", po::value<unsigned int> (&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<string> (&sdThresholdsName), "produce standard deviation attributs")
// ("moment-of-inertia,I", po::value<string> (&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<unsigned int> (&apOrigPos), (string ("position of origin on DAP (default ")+
// boost::lexical_cast<std::string> (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<vector<string> > (), "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<vector<string> > ().size ();
if (required-nbArgs != 0)
usage ("Need one input and one output");
if (required) {
vector<string> var = vm[inputFileC].as<vector<string> > ();
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;
}