/* -*- 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: dist2ascii.cc,v 1.1 2006-08-22 10:15:15 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 <stack>
#include <stdexcept>
#include <iostream>
#include <fstream>
#include <libmona/monaHistory.hh>
#include <libmona/monaException.hh>
#include <libmona/monaPopt.hh>
#include <libmona/3DVector.hh>
#include <libmona/filter.hh>
#include <libmona/fnameana.hh>
#include <libmona/2DImageWrap.hh>
#include <libmona/filter2dstack.hh>
#include <libmona/filetools.hh>
using namespace std;
using namespace mona;
void append_data(const C2DImageWrap& image, size_t z, float scale, ostream& outfile)
{
const C2DUSImage *src = image.getC2DUSImage();
if (!src) {
stringstream msg;
msg << "input image " << z << "is not result of a distance transform";
throw runtime_error(msg.str());
}
float iscale = 1.0 / scale;
C2DUSImage::const_iterator p = src->begin();
for (size_t y = 0; y < src->get_size().y; ++y)
for (size_t x = 0; x < src->get_size().x; ++x, ++p)
if (*p > 0)
outfile << x << " " << y << " " << z << " " << *p * iscale << "\n";
}
int main(int argc, const char *args[])
{
string in_filename;
string out_filename;
float scale;
C2DImageIOPluginHandler imageio;
// 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-file", 'o', "output file name", "output.txt"));
options.push_back(popt::option( scale, "scale", 's', "fixed point scale of distcance transform", "256.0"));
vector<string> unknown_args;
popt::parse_options(argc, args, options, unknown_args);
if (!unknown_args.empty()) {
cverr() << "unknown arguments given\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;
}
try {
ofstream os( out_filename.c_str(), ios::out );
C2DImageList in_imagebuffer;
size_t start_filenum = 0;
size_t end_filenum = 0;
size_t format_width = 0;
std::string src_basename = get_filename_pattern_and_range(in_filename, start_filenum, end_filenum, format_width);
if (start_filenum >= end_filenum)
throw invalid_argument(string("no files match pattern ") + src_basename);
// read all the files
for (size_t i = start_filenum; i < end_filenum; ++i) {
cvmsg() << "read " << i << " out of [" << start_filenum << ", " << end_filenum << "]\n";
string src_name = create_filename(src_basename.c_str(), i);
auto_ptr<C2DImageList> in_image_list(imageio.load(src_name));
if (!in_image_list.get() || !in_image_list->size()) {
cverr() << "expected " << end_filenum - start_filenum << " images, got only" << i - start_filenum <<"\n";
break;
}
append_data(*in_image_list->begin(), i, scale, os );
}
cvmsg() << '\n';
return EXIT_SUCCESS;
}
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;
}