/* -*- 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: 3d22dimage.cc,v 1.7 2006-07-12 13:44:25 wollny Exp $
/*! \brief eva-downscale
\sa eva-downscale3d.cc
\file downscale.cc
\author G. Wollny, wollny eva.mpg.de, 2005
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <string>
#include <stdexcept>
#include <iomanip>
#include <libmona/monaHistory.hh>
#include <libmona/monaException.hh>
#include <libmona/monaPopt.hh>
#include <libmona/3DVector.hh>
#include <libmona/filter.hh>
#include <libmona/filter_plugin2d.hh>
#include <libmona/2DImageWrap.hh>
#include <libmona/3DImagePtr.hh>
#include <libmona/fnameana.hh>
#include <libmona/filetools.hh>
using namespace std;
using namespace mona;
class C2DImageSaver: public TUnaryImageFilter<bool> {
public:
C2DImageSaver(const string& basename, const string& type, const C2DImageIOPluginHandler& imageio2d);
template <class Image3D>
bool operator () (const Image3D& image)const;
private:
string _M_basename;
string _M_type;
const C2DImageIOPluginHandler& _M_imageio2d;
};
C2DImageSaver::C2DImageSaver(const string& basename, const string& type, const C2DImageIOPluginHandler& imageio2d):
_M_basename(basename),
_M_type(type),
_M_imageio2d(imageio2d)
{
}
template <class Image3D>
bool C2DImageSaver::operator () (const Image3D& image)const
{
typedef T2DImage<typename Image3D::value_type> OutImage;
for (size_t z = 0; z < image.get_size().z; ++z) {
OutImage *r = new OutImage(C2DBounds( image.get_size().x, image.get_size().y));
C2DImageList out_images;
out_images.push_back(C2DImageWrap(r));
image.get_data_plane_xy(z, &(*r)(0,0), r->size());
for (typename OutImage::iterator i= r->begin();
i != r->end(); ++i)
if (*i < 0)
*i = 0;
stringstream fname;
fname << _M_basename << setfill ('0') << setw (4) << z << "." <<_M_type;
cvmsg() << "save " << fname.str() << '\n';
if (!_M_imageio2d.save(_M_type, out_images, fname.str(), NULL))
return false;
}
return true;
}
int main(int argc, const char *args[])
{
string in_filename;
string out_filename;
string type;
vector<int> new_size;
try {
C2DImageIOPluginHandler imageio2d;
string types = string("data type to convert to ( ") + imageio2d.supported_formats() + ")";
// define options
popt::COptions options;
options.push_back(popt::option( in_filename, "in-file", 'i', "input file name", ""));
options.push_back(popt::option( out_filename, "out-basename", 'o', "output base name", "basename"));
options.push_back(popt::option( type, imageio2d.get_set(), "type", 't',"output file type" , "png"));
vector<string> unknows_args;
popt::parse_options(argc, args, options, unknows_args);
if (!unknows_args.empty()) {
cverr() << "Unknown arguments: ";
for (vector<string>::const_iterator i = unknows_args.begin(); i != unknows_args.end();
++i)
cverb << *i << " ";
cverb << "\n";
return -1;
}
if (in_filename.empty()) {
cverr() << "No input file given, run '" << args[0] << " --help' to get help\n";
return -1;
}
if (out_filename.empty()) {
cverr() << "No output filename given, run '" << args[0] << " --help' to get help\n";
return -1;
}
// now start the fun part
//first count the number of slices
C3DImageIOPluginHandler imageio3d;
auto_ptr<C3DImageList> in_image(imageio3d.load(in_filename));
if (in_image.get() && !in_image->empty()) {
C2DImageSaver saver(out_filename, type, imageio2d);
return !wrap_filter(saver, *in_image->begin());
}
}
catch (const mona_runtime_error& e){
cerr << args[0] << " error: " << e.what() << endl;
}
catch (const mona_fatal_error& e){
cerr << args[0] << " fatal: " << e.what() << endl;
}
catch (const mona_exception& e){
cerr << args[0] << " error: " << e.what() << endl;
}
catch (const runtime_error &e){
cerr << args[0] << " runtime: " << e.what() << endl;
}
catch (const invalid_argument &e){
cerr << args[0] << " error: " << e.what() << endl;
}
catch (const exception& e){
cerr << args[0] << " error: " << e.what() << endl;
}
catch (...){
cerr << args[0] << " unknown exception" << endl;
}
return EXIT_FAILURE;
}