Proper parsing of the CLI, adding filter according to it; catch some CLI error (unknown filter, no output file given, ...)

This commit is contained in:
Némunaire 2012-07-16 01:39:26 +02:00
parent 86ba74fe35
commit 7ef166e2bd
7 changed files with 79 additions and 22 deletions

1
TODO
View File

@ -1,3 +1,2 @@
- Proper parsing of the CLI, adding filter according to it.
- Moar filters !
- Kikoo stuff.

View File

@ -15,6 +15,7 @@ AC_CONFIG_MACRO_DIR([m4])
AC_PROG_CXX
AC_PROG_RANLIB
CXXFLAGS="$CXXFLAGS_save -std=c++11 -g -O0"
AM_SILENT_RULES([yes])
AC_ARG_ENABLE(debug,
[ --enable-debug compile with debugging support],

View File

@ -1 +1,28 @@
#include "configuration.hh"
#include <sstream>
#include <boost/tokenizer.hpp>
void
Configuration::add_filter(const std::string param)
{
std::pair<std::string, std::vector<int> > pair;
boost::char_separator<char> sep(",-;|. =");
boost::tokenizer<boost::char_separator<char> > tokens(param, sep);
for (auto tok : tokens)
{
if (pair.first.empty())
pair.first = tok;
else
{
int p = -1;
std::stringstream ss (tok);
ss >> p;
pair.second.push_back(p);
}
}
filters_.push_back (pair);
}

View File

@ -3,6 +3,7 @@
# include <string>
# include <list>
# include <vector>
class Configuration
{
@ -10,8 +11,12 @@ class Configuration
std::list<std::string> input;
std::string output;
std::string codec;
std::list<std::string> filters;
void add_filter(std::string param);
inline std::list<std::pair<std::string, std::vector<int> > > filters_get();
private:
std::list<std::pair<std::string, std::vector<int> > > filters_;
};
# include "configuration.hxx"
#endif /* !CONFIGURATION_HH_ */

10
src/configuration.hxx Normal file
View File

@ -0,0 +1,10 @@
#ifndef CONFIGURATION_HXX_
# define CONFIGURATION_HXX_
std::list<std::pair<std::string, std::vector<int> > >
Configuration::filters_get()
{
return filters_;
}
#endif /* !CONFIGURATION_HXX_ */

View File

@ -49,24 +49,18 @@ bool parse_cli(Configuration& conf, int argc, char** argv)
return false;
}
// GO!
std::cout << "Given filters: ";
if (vm.count("filter"))
std::cout << vm["filter"].as<std::vector<std::string> > ().size()
<< std::endl;
else
std::cout << 0 << std::endl;
std::cout << "Given files: ";
if (vm.count("input-file"))
std::cout << vm["input-file"].as<std::vector<std::string> > ().size()
<< std::endl;
else
std::cout << 0 << std::endl;
/* Store configuration */
conf.output = std::string(vm["output"].as<std::string>());
conf.codec = std::string(vm["codec"].as<std::string>());
if (vm.count("output"))
conf.output = std::string(vm["output"].as<std::string>());
else
{
std::cerr << "Please provide an output file" << std::endl;
return false;
}
if (vm.count("codec"))
conf.codec = std::string(vm["codec"].as<std::string>());
else
conf.codec = "MP42";
std::vector<std::string> vs;
if (vm.count("input-file"))
@ -80,7 +74,7 @@ bool parse_cli(Configuration& conf, int argc, char** argv)
{
vs = vm["filter"].as<std::vector<std::string>> ();
for (auto it = vs.begin(); it != vs.end(); ++it)
conf.filters.push_back(std::string(*it));
conf.add_filter(std::string(*it));
}
return true;
@ -102,6 +96,18 @@ int main (int argc, char** argv)
input_vr.front().width_get(), input_vr.front().height_get());
Pipeline pipe(input_vr, vw);
// Add filters to pipeline
for (auto filter : conf.filters_get())
{
if (!pipe.add_filter(filter.first, filter.second))
{
std::cerr << argv[0] << ": Unknown filter `"
<< filter.first << "'" << std::endl;
return 2;
}
}
// add filters here
//pipe.add_filter("grey", std::vector<int>());
//pipe.add_filter("count", std::vector<int>());
@ -111,6 +117,7 @@ int main (int argc, char** argv)
//pipe.add_filter("interlace", std::vector<int>());
//pipe.add_filter("merge", std::vector<int>());
//pipe.add_filter("inlay", std::vector<int> { 0, 0, 255 });
pipe.run();
return 0;

View File

@ -58,7 +58,11 @@ bool Pipeline::add_filter(std::string filter, std::vector<int> args)
this->pipeline_.add_filter(*(new Compose(*this->input_it_)));
this->input_it_++;
this->pipeline_.add_filter(
*(new Inlay(cv::Vec3b { args[0], args[1], args[2] }, 0)));
*(new Inlay(cv::Vec3b {
(unsigned char) args[0],
(unsigned char) args[1],
(unsigned char) args[2]
}, 0)));
}
else if (filter == "strictinlay" && this->input_it_ != this->input_.end()
&& args.size() >= 3)
@ -66,7 +70,11 @@ bool Pipeline::add_filter(std::string filter, std::vector<int> args)
this->pipeline_.add_filter(*(new Compose(*this->input_it_)));
this->input_it_++;
this->pipeline_.add_filter(
*(new Inlay(cv::Vec3b { args[0], args[1], args[2] }, 1)));
*(new Inlay(cv::Vec3b {
(unsigned char) args[0],
(unsigned char) args[1],
(unsigned char) args[2]
}, 1)));
}
else
return false;