/* -*- 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
*
*/
#include <libmona/monaIOStream.hh>
#include <libmona/monaException.hh>
#include <libmona/2DImageWrap.hh>
#include <map>
#include <stdexcept>
#include <iostream>
#include <fstream>
#include <sstream>
using namespace mona;
typedef std::pair<int, int> CClassRange;
typedef std::map<double, CClassRange> CClassMap;
class CBinarizeImage {
public:
typedef C2DImageWrap result_type;
CBinarizeImage(unsigned int min, unsigned int max);
template <typename Image2D>
CBinarizeImage::result_type operator ()(const Image2D& image)const;
private:
int _M_min;
int _M_max;
};
CBinarizeImage::CBinarizeImage(unsigned int min, unsigned int max):
_M_min(min),
_M_max(max)
{
}
template <typename Image2D>
CBinarizeImage::result_type CBinarizeImage::operator ()(const Image2D& image) const
{
typename Image2D::const_iterator ii = image.begin();
typename Image2D::const_iterator ie = image.end();
C2DBitImage * result = new C2DBitImage(image.get_size());
if (!result)
throw std::runtime_error("Out of memory");
cvdebug() << "filter with " << _M_min << ", " << _M_max << "\n";
C2DBitImage::iterator oi = result->begin();
while (ii != ie) {
*oi = ((int)*ii <= _M_max && (int)*ii >= _M_min);
++ii;
++oi;
}
return C2DImageWrap(result);
}
int main(int argc, const char *args[])
{
std::string in_filename;
std::string out_filename;
std::string map_filename;
int min, max;
popt::COptions options;
options.push_back(popt::option( in_filename, "in-file", 'i',
"input image(s) to be remapped", NULL));
options.push_back(popt::option( out_filename, "out-file", 'o',
"output image(s) to be remapped", NULL));
options.push_back(popt::option( min, "min", 0, "minimum value", "0"));
options.push_back(popt::option( max, "max", 0, "minimum value", "65534"));
try {
std::vector<std::string> non_args;
popt::parse_options(argc, args, options, non_args);
if (!non_args.empty()) {
throw mona_fatal_error("unrecognised arguments detected, try 'eva-remap --help'");
}
if (in_filename.empty())
throw mona_fatal_error("required argument --in-image missing");
if (out_filename.empty())
throw mona_fatal_error("required argument --out-image missing");
C2DImageIOPluginHandler imageio;
std::auto_ptr<C2DImageList> in_image_list(imageio.load(in_filename));
CBinarizeImage binarizer(min,max);
if (in_image_list.get() && in_image_list->size()) {
for (C2DImageList::iterator img = in_image_list->begin();
img != in_image_list->end(); ++img)
*img = wrap_filter(binarizer, *img);
}else{
std::string no_image = ("no images found in ") + in_filename;
throw mona_runtime_error(no_image);
}
if ( !imageio.save(in_image_list->get_sourceformat(), *in_image_list, out_filename) ){
std::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){
std::cerr << args[0] << " error: " << e.what() << "\n";
}
catch (const mona_fatal_error& e){
std::cerr << args[0] << " fatal: " << e.what() << "\n";
}
catch (const mona_exception& e){
std::cerr << args[0] << " error: " << e.what() << "\n";
}
catch (const std::runtime_error &e){
std::cerr << args[0] << " runtime: " << e.what() << "\n";
}
catch (const std::invalid_argument &e){
std::cerr << args[0] << " error: " << e.what() << "\n";
}
catch (const std::exception& e){
std::cerr << args[0] << " error: " << e.what() << "\n";
}
catch (...){
std::cerr << args[0] << " unknown exception" << "\n";
}
return EXIT_FAILURE;
}