/*
* Copyright (c) 2004 Max-Planck-Institute of Cognitive NeuroScience
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
// $Id: image_filter.cc 683 2005-06-30 12:06:41Z wollny $
/*! \brief mona-image-filter
\sa getmtr.cc
\file mask.cc
\author G. Wollny, wollny cbs.mpg.de, 2004
*/
// $Id: image_filter.cc 683 2005-06-30 12:06:41Z wollny $
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <iostream>
#include <string>
#include <dlfcn.h>
#include <mona.hh>
#include <libmona/filter_plugin.hh>
using namespace std;
using namespace mona;
/* Revision string */
static const char *revision = "$Revision: 683 $";
namespace options {
}
int main( int argc, const char *argv[] )
{
using namespace mona;
using namespace std;
string in_filename;
string out_filename;
vector<string> filter_chain;
C3DImageFilterHandler filter_plugins;
stringstream filter_names;
bool help_plugins;
filter_names << "filters in the order to be applied (out of: " << filter_plugins.plugin_names() << ")";
popt::COptions opts;
opts.push_back(popt::option( in_filename, "in-image", 'i', "input image(s) to be filtered", NULL));
opts.push_back(popt::option( out_filename, "out-image", 'o', "output image(s) that have been filtered", NULL));
opts.push_back(popt::option(help_plugins, "help-plugins", '?', "give some help about the filter plugins"));
try {
popt::parse_options(argc, argv, opts, filter_chain);
if (help_plugins) {
filter_plugins.print_help();
return EXIT_SUCCESS;
}
if ( filter_chain.empty() )
throw invalid_argument("no filters given");
if ( in_filename.empty() )
throw mona_runtime_error("'--in-image' ('i') option required");
if ( out_filename.empty() )
throw mona_runtime_error("'--out-image' ('o') option required");
// initialize the plugin handler
#ifdef DEBUG
cverb.set_verbosity( vstream::ml_debug );
#endif
C3DImageIOPluginHandler imageio;
CHistory::instance().append(argv[0], revision, opts, &filter_chain);
// read image
auto_ptr<C3DImageList> in_image_list(imageio.load(in_filename));
std::list<C3DImageFilterFactory::ProductPtr> filters;
for (std::vector<string>::const_iterator i = filter_chain.begin();
i != filter_chain.end(); ++i) {
cvdebug() << "Prepare filter " << *i << std::endl;
C3DImageFilterFactory::ProductPtr filter = C3DImageFilterFactory::produce(i->c_str(), filter_plugins);
if (!filter){
std::stringstream error;
error << "Filter " << *i << " not found";
throw mona_fatal_error(error.str());
}
filters.push_back(filter);
}
if (in_image_list.get() && in_image_list->size()) {
for (std::list<C3DImageFilterFactory::ProductPtr>::const_iterator f = filters.begin();
f != filters.end(); ++f) {
(*f)->filter(*in_image_list);
}
if ( !imageio.save(in_image_list->get_sourceformat(), *in_image_list, out_filename) ){
string not_save = ("unable to save result to ") + out_filename;
throw mona_runtime_error(not_save);
};
}
return EXIT_SUCCESS;
}
catch (const mona_runtime_error& e){
cerr << argv[0] << " error: " << e.what() << endl;
}
catch (const mona_fatal_error& e){
cerr << argv[0] << " fatal: " << e.what() << endl;
}
catch (const mona_exception& e){
cerr << argv[0] << " error: " << e.what() << endl;
}
catch (const invalid_argument &e){
cerr << argv[0] << " error: " << e.what() << endl;
}
catch (const exception& e){
cerr << argv[0] << " error: " << e.what() << endl;
}
catch (...){
cerr << argv[0] << " unknown exception" << endl;
}
return EXIT_FAILURE;
}
/*
$Log$
Revision 1.1 2005/06/30 12:06:41 wollny
add missing file
Revision 1.2 2005/06/02 13:33:23 gerddie
adapt code to new plugin handling
Revision 1.1 2005/05/02 14:04:17 gerddie
add a general image filter program
*/