[go: up one dir, main page]

Menu

[r999]: / mia2 / src / 3dimagefilter.cc  Maximize  Restore  History

Download this file

150 lines (113 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
/*
* Copyright (c) 2007 Gert Wollny <gert.wollny at web.de>
*
* 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: 3dimagefilter.cc,v 1.12 2006-07-12 13:44:23 wollny Exp $
/*! \brief mia-3dimagefilter
\sa mia-3dimagefilter.cc
\file mask.cc
\author G. Wollny, wollny eva.mpg.de, 2005
*/
#include <miaconfig.h>
#include <iostream>
#include <string>
#include <stdexcept>
#include <sstream>
#include <mia/3d/3dfilter.hh>
#include <mia/3d/3dimageio.hh>
#include <mia/core.hh>
using namespace std;
NS_MIA_USE;
int main( int argc, const char *argv[] )
{
string in_filename;
string out_filename;
string out_type;
bool help_plugins = false;
try {
const C3DFilterPluginHandler::Instance& filter_plugins = C3DFilterPluginHandler::instance();
const C3DImageIOPluginHandler::Instance& imageio = C3DImageIOPluginHandler::instance();
stringstream filter_names;
filter_names << "filters in the order to be applied (out of: " << filter_plugins.get_plugin_names() << ")";
CCmdOptionList options;
options.push_back(make_opt( in_filename, "in-file", 'i', "input image(s) to be filtered", "input", true));
options.push_back(make_opt( out_filename, "out-file", 'o', "output image(s) that have been filtered", "output", true));
options.push_back(make_opt( out_type, imageio.get_set(), "type", 't',"output file type" , "type", false));
options.push_back(make_opt( help_plugins, "help-plugins", 0, "give some help about the filter plugins", NULL));
options.parse(argc, argv);
vector<const char *> filter_chain = options.get_remaining();
cvdebug() << "IO supported types: " << imageio.get_plugin_names() << "\n";
cvdebug() << "supported filters: " << filter_plugins.get_plugin_names() << "\n";
if (help_plugins) {
filter_plugins.print_help(cout);
return EXIT_SUCCESS;
}
if ( filter_chain.empty() )
cvwarn() << "no filters given, just copy\n";
if ( in_filename.empty() )
throw runtime_error("'--in-image' ('i') option required");
if ( out_filename.empty() )
throw runtime_error("'--out-image' ('o') option required");
//CHistory::instance().append(argv[0], "unknown", options);
std::list<C3DFilterPlugin::ProductPtr> filters;
for (vector<const char *>::const_iterator i = filter_chain.begin();
i != filter_chain.end(); ++i) {
cvdebug() << "Prepare filter " << *i << std::endl;
C3DFilterPlugin::ProductPtr filter = filter_plugins.produce(*i);
if (!filter){
std::stringstream error;
error << "Filter " << *i << " not found";
throw invalid_argument(error.str());
}
filters.push_back(filter);
}
// read image
C3DImageIOPluginHandler::Instance::PData in_image_list = imageio.load(in_filename);
if (in_image_list.get() && in_image_list->size()) {
vector<const char *>::const_iterator filter_name = filter_chain.begin();
for (std::list<C3DFilterPlugin::ProductPtr>::const_iterator f = filters.begin();
f != filters.end(); ++f, ++filter_name) {
cvmsg() << "Run filter: " << *filter_name << "\n";
for (C3DImageIOPluginHandler::Instance::Data::iterator i = in_image_list->begin();
i != in_image_list->end(); ++i)
*i = (*f)->filter(**i);
}
if ( !imageio.save(out_type, out_filename, *in_image_list) ){
string not_save = ("unable to save result to ") + out_filename;
throw runtime_error(not_save);
};
}
return EXIT_SUCCESS;
}
catch (const runtime_error &e){
cerr << argv[0] << " runtime: " << e.what() << endl;
}
catch (const invalid_argument &e){
if (help_plugins) {
C3DFilterPluginHandler::instance().print_help(cout);
return EXIT_SUCCESS;
}
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;
}