[go: up one dir, main page]

Menu

[r994]: / libmona / src / borderpixel.cc  Maximize  Restore  History

Download this file

152 lines (114 with data), 4.9 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
149
150
151
/* -*- 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
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <memory>
#include <vector>
#include <numeric>
#include <libmona/filter_plugin2d.hh>
#include <libmona/2DImageWrap.hh>
#include <libmona/monaException.hh>
#include <libmona/monaHistory.hh>
using namespace mona;
using namespace std;
C2DImageWrap and_image(const C2DBitImage& image_1, const C2DBitImage& image_2, bool & nempty)
{
if (image_1.get_size() != image_2.get_size())
throw invalid_argument("input images need to be of the same size");
C2DBitImage *result = new C2DBitImage(image_1.get_size());
transform(image_1.begin(), image_1.end(), image_2.begin(), result->begin(), logical_and<bool>());
nempty = accumulate(result->begin(), result->end(), false, logical_or<bool>());
return C2DImageWrap(result);
}
int main(int argc, const char *argv[])
{
string a_filename;
string b_filename;
string out_filename;
string dilate_shape;
string out_type;
try {
C2DImageIOPluginHandler imageio;
popt::COptions options;
options.push_back(popt::option( a_filename, "file1", '1', "input image 1", NULL ));
options.push_back(popt::option( b_filename, "file2", '2', "input image 2", NULL ));
options.push_back(popt::option( out_filename, "out-file", 'o', "border image", NULL ));
options.push_back(popt::option( dilate_shape, "dilate", 'd', "dilate shape", "4n"));
options.push_back(popt::option( out_type, imageio.get_set(), "type", 't',"output file type" , NULL));
vector<string> unknown_opts;
popt::parse_options(argc, argv, options, unknown_opts);
if (!unknown_opts.empty())
throw mona_fatal_error("Unknown options given");
if (a_filename.empty() || b_filename.empty() ||out_filename.empty())
throw mona_fatal_error("2 input files and an output file required");
C2DImageFilterHandler filter_plugins;
auto_ptr<C2DImageList> image_list_1(imageio.load(a_filename));
if (!image_list_1.get() || image_list_1->empty())
throw mona_fatal_error(string("No images found in ") + a_filename);
auto_ptr<C2DImageList> image_list_2(imageio.load(b_filename));
if (!image_list_2.get() || image_list_2->empty())
throw mona_fatal_error(string("No images found in ") + b_filename);
if (out_type.empty())
out_type = image_list_1->get_sourceformat();
stringstream filter_descr;
filter_descr << "dilate:shape=" << dilate_shape;
C2DImageFilterFactory::ProductPtr filter = C2DImageFilterFactory::produce(filter_descr.str().c_str(), filter_plugins);
if (!filter)
throw invalid_argument(string("Unable to create dilate filter from:") +filter_descr.str());
C2DImageWrap image_wrap_1 = filter->filter(*image_list_1->begin());
C2DImageWrap image_wrap_2 = filter->filter(*image_list_2->begin());
C2DBitImage *image_1 = image_wrap_1.getC2DBitImage();
if (!image_1)
throw invalid_argument("Input image 1 needs to be in bit representation");
C2DBitImage *image_2 = image_wrap_2.getC2DBitImage();
if (!image_2)
throw invalid_argument("Input image 2 needs to be in bit representation");
C2DImageList out_list;
bool nempty = false;
out_list.push_back(and_image(*image_1, *image_2, nempty));
if ( !imageio.save(out_type, out_list, out_filename) ){
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){
cverr() << argv[0] << " error: " << e.what() << endl;
}
catch (const mona_fatal_error& e){
cverr() << argv[0] << " fatal: " << e.what() << endl;
}
catch (const mona_exception& e){
cverr() << argv[0] << " error: " << e.what() << endl;
}
catch (const invalid_argument &e){
cverr() << argv[0] << " error: " << e.what() << endl;
}
catch (const exception& e){
cverr() << argv[0] << " error: " << e.what() << endl;
}
catch (...){
cverr() << argv[0] << " unknown exception" << endl;
}
return EXIT_FAILURE;
}