[go: up one dir, main page]

Menu

[r999]: / libmona / src / imagefilter.cc  Maximize  Restore  History

Download this file

177 lines (127 with data), 4.6 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
* 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
*/