/* -*- mona-c++ -*-
* Copyright (c) Leipzig, Madrid 2004 - 2008
* Max-Planck-Institute for Human Cognitive and Brain Science
* Max-Planck-Institute for Evolutionary Anthropology
* BIT, ETSI Telecomunicacion, UPM
*
* 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: imageselect.cc 938 2006-07-11 11:57:01Z write1 $
/*! \brief mona-imageselect -- select an image from a bundeled set
\par Description
mona-imageselect selects or excludes a single image from bundeled input images
\par Usage
<code>mona-imageselect --usage</code> (or <code>mona-imageselect --help</code>)
\param --in-image input file holding the image
\param --out-image the converted image
\param --number image to be seletced
\param --not erase image of given number from input
\param --verbose give some verbose information (field size, etc.)
\par Examples
-# Select image no 2 from a set of n images
\code
user> mona-imgageselect -i images.v -o image.v -n 2
\endcode
-# Remove image no 2 from a set of n images (result has n-1 images)
\code
user> mona-imgageselect -i images.v -o image.v -n 2 --not
\endcode
Note, the input format can be anything covered by the io plugins included in \a libmona.
\par Known bugs
None known yet.
\todo writing to stdout and verbosity level
\file imageselect.cc
\author G. Wollny, wollnyATcbs.mpg.de, 2004
*/
#if HAVE_CONFIG_H
#include <config.h>
#endif
#include <string>
// MONA specific
#include <mona.hh>
static const char *revision = "$Revision: 938 $";
using namespace std;
using namespace mona;
int main(int argc, const char *argv[])
{
string in_filename;
string out_filename;
long number=0;
bool erase=false;
using namespace std;
using namespace mona;
try {
// initialize the plugin handler
C3DImageIOPluginHandler imageio;
popt::COptions opts;
opts.push_back(popt::option( in_filename, "in-image", 'i', "input images", "" ));
opts.push_back(popt::option( out_filename, "out-image", 'o', "output image", "-" ));
opts.push_back(popt::option( number, "number", 'n', "select image number (starting with 0)", "0" ));
opts.push_back(popt::option( erase, "not", 'e', "select all images except of the given number"));
vector<string> non_options;
popt::parse_options(argc, argv, opts, non_options);
if ( non_options.size() > 0 )
throw invalid_argument("unknown options");
if (in_filename.empty()) {
cvfatal() << argv[0] << " in-image required" << endl;
return 1;
}
CHistory::instance().append(argv[0], revision, opts);
auto_ptr<C3DImageList> images(imageio.load(in_filename));
C3DImageList::iterator i = images->begin();
if (erase) {
while( number-- ) ++i;
images->erase(i);
imageio.save(images->get_sourceformat(), *images, out_filename);
} else {
if (images.get()) {
if (images->size() > (unsigned long)number) {
C3DImageList out_list;
out_list.push_back( (*images)[number]);
imageio.save(images->get_sourceformat(), out_list, out_filename);
} else {
stringstream s;
s << " no image number " << number << " found!";
throw mona_runtime_error(s.str());
}
} else {
string not_found = ("unable to load image from ") + in_filename;
throw mona_runtime_error(not_found);
}
}
return EXIT_SUCCESS;
} // try
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;
}
/*
*/