[go: up one dir, main page]

Menu

[r999]: / libmona / src / 3d22dimage.cc  Maximize  Restore  History

Download this file

180 lines (146 with data), 5.1 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/* -*- 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;
}