| Bsoft 2.0.5 Bernard's software package

badd.cpp
Go to the documentation of this file.
1
9 #include "rwimg.h"
10 #include "img_combine.h"
11 #include "utilities.h"
12 #include "options.h"
13 #include "timer.h"
14
15 // Declaration of global variables
16 extern int verbose; // Level of output to the screen
17
18 // Usage assistance
19 const char* use[] = {
20 " ",
21 "Usage: badd [options] input.images",
22 "----------------------------------",
23 "Adds multiple images to generate a new composite image.",
24 "Any number of input images may be given and may include the wild card \"*\".",
25 "All images are converted to floating point.",
26 " ",
27 "Actions:",
28 "-rescale -0.1,5.2 Rescale input images to average and standard deviation.",
29 "-average Output average image rather than the sum.",
30 "-std Output standard deviation image rather than variance image.",
31 "-center Center images before summation.",
32 " ",
33 "Parameters:",
34 "-verbose 7 Verbosity of output.",
35 "-datatype u Force writing of a new data type.",
36 "-sampling 1.5,1.5,1.5 Sampling (A/pixel; a single value can be given).",
37 "-origin 0.8,-10,15.7 Set the origin (default from input image).",
38 "-weights 0.1,0.3,0.9 Weights for input files (default 1 for every file).",
39 " ",
40 "Output:",
41 "-output output.img Output image (default sum.map).",
42 "-fom fom.img Output variance/standard deviation image.",
43 " ",
44 "Examples:",
45 "To sum three images with different weights:",
46 "badd -v 7 -rescale 0,1 -weights 0.5,1.2,0.87 -output new.pif in1.map in2.map in3.map",
47 " ",
48 "To output the standard deviation of the summed input images:",
49 "badd -v 7 -fom stdev.map -output sum.pif *.spi",
50 " ",
51 NULL
52 };
53
54 int main(int argc, char **argv)
55 {
56 // Initialize variables
57 DataType nudatatype(Unknown_Type); // Conversion to new type
58 Vector3<double> sam; // Units for the three axes (A/pixel)
59 Vector3<double> origin; // New image origin
60 int set_origin(0); // Flag to set origin
61 Bstring sumfile("sum.map"); // Default output image file name
62 double nuavg(0), nustd(0); // Average and standard deviation for rescaling
63 Bstring weight_string;
64 int setweigh(0); // Default weights are 1
65 int flags(0); // Flags to modify summation
66 Bstring fomfile; // Output FOM file
67
68 int optind;
69 Boption* option = get_option_list(use, argc, argv, optind);
70 Boption* curropt;
71 for ( curropt = option; curropt; curropt = curropt->next ) {
72 if ( curropt->tag == "rescale" )
73 if ( curropt->values(nuavg, nustd) < 2 )
74 cerr << "-rescale: Both average and standard deviation must be specified" << endl;
75 if ( curropt->tag == "average" ) flags |= 1;
76 if ( curropt->tag == "std" ) flags |= 4;
77 if ( curropt->tag == "center" ) flags |= 8;
78 if ( curropt->tag == "datatype" )
79 nudatatype = curropt->datatype();
80 if ( curropt->tag == "sampling" )
81 sam = curropt->sampling();
82 if ( curropt->tag == "origin" ) {
83 if ( curropt->value[0] == 'c' ) {
84 set_origin = 2;
85 } else {
86 origin = curropt->origin();
87 set_origin = 1;
88 }
89 }
90 if ( curropt->tag == "weights" ) {
91 weight_string = curropt->value;
92 if ( weight_string.length() < 1 )
93 cerr << "-weights: Weights for all files must be specified" << endl;
94 else
95 setweigh = 1;
96 }
97 if ( curropt->tag == "output" )
98 sumfile = curropt->filename();
99 if ( curropt->tag == "fom" ) {
100 fomfile = curropt->filename();
101 flags |= 2;
102 }
103 }
104 option_kill(option);
105
106 if ( verbose && argc < 3 ) {
107 cerr << "Error: No input files given!" << endl;
108 bexit(-1);
109 }
110
111 double ti = timer_start();
112
113 if ( optind >= argc ) {
114 cerr << "No input files!" << endl;
115 bexit(-1);
116 }
117
118 // Set up image file names and weights
119 long nfiles(0);
120 Bstring* file_list = NULL;
121 while ( optind < argc ) {
122 string_add(&file_list, argv[optind++]);
123 nfiles++;
124 }
125
126 vector<double> weight;
127 if ( setweigh ) {
128 weight = weight_string.split_into_doubles(",");
129 if ( weight.size() < nfiles ) {
130 cerr << "Error: The number of weights must equal the number of input files" << endl;
131 bexit(-1);
132 }
133 }
134
135 Bimage* psum = img_add_weighed(file_list, weight, nuavg, nustd, flags);
136
137 if ( sam.volume() > 0 ) psum->sampling(sam);
138
139 if ( set_origin ) {
140 if ( set_origin == 2 ) psum->origin(psum->default_origin());
141 else psum->origin(origin);
142 }
143
144 if ( sumfile.length() ) {
145 psum->change_type(nudatatype);
146 psum->label(get_cmd_line(argc, argv));
147 write_img(sumfile, psum, 0);
148 }
149
150 if ( fomfile.length() )
151 write_img(fomfile, psum->next, 0);
152
153 delete psum;
154 string_kill(file_list);
155
156 if ( verbose & VERB_TIME )
157 timer_report(ti);
158
159 bexit(0);
160 }
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1743
string_add
Bstring * string_add(Bstring **list, const char *string)
Adds a string to a linked list.
Definition: Bstring.cpp:705
DataType
DataType
Base data type specifier.
Definition: utilities.h:146
Bimage::sampling
Vector3< double > sampling()
Definition: Bimage.h:505
verbose
int verbose
utilities.h
Header file for general utilities.
Boption::sampling
Vector3< double > sampling()
Definition: options.h:202
Boption::value
Bstring value
Definition: options.h:30
Boption::tag
Bstring tag
Definition: options.h:29
Bstring::length
long length() const
Definition: Bstring.cpp:263
get_option_list
Boption * get_option_list(const char *use[], int argc, char *argv[], int &optind)
Parses command line arguments based on a template.
Definition: options.cpp:40
get_cmd_line
string get_cmd_line(int argc, char **argv)
Returns the command line as a string.
Definition: utilities.cpp:36
Boption::filename
Bstring filename()
Definition: options.h:85
timer.h
Utilities for timing functions.
img_add_weighed
Bimage * img_add_weighed(Bstring *file_list, vector< double > weight, double newavg=0, double newstd=0, int flags=0)
Adds multiple images together with given weights.
Definition: img_combine.cpp:288
Bimage
General image parameter class.
Definition: Bimage.h:163
Boption
A class for a command line input option.
Definition: options.h:26
Bimage::label
void label(string s)
Definition: Bimage.h:235
VERB_TIME
#define VERB_TIME
Definition: utilities.h:110
img_combine.h
Header file for combining two images in various ways.
write_img
int write_img(const char *filename, Bimage *p, int compression)
General driver function to write multiple image formats.
Definition: rwimg.cpp:280
Bstring::split_into_doubles
vector< double > split_into_doubles(const Bstring &delim)
Definition: Bstring.cpp:665
Bimage::next
Bimage * next
Definition: Bimage.h:165
Boption::datatype
DataType datatype()
Definition: options.h:93
Boption::next
Boption * next
Definition: options.h:28
Bstring
Definition: Bstring.h:29
Bimage::default_origin
Vector3< double > default_origin()
Definition: Bimage.h:585
Vector3< double >
Boption::origin
Vector3< double > origin()
Definition: options.h:180
timer_report
double timer_report(double lasttime)
Reports the time elapsed since a given time.
Definition: timer.cpp:67
option_kill
int option_kill(Boption *option)
Deallocates a linked list of option structures.
Definition: options.cpp:109
Unknown_Type
Definition: utilities.h:147
main
int main(int argc, char **argv)
Definition: badd.cpp:54
rwimg.h
Header file for 2D and 3D image I/O.
options.h
Header file for option handlers.
use
const char * use[]
Definition: badd.cpp:19
Vector3::volume
double volume()
Definition: Vector3.h:235
Boption::values
long values(T1 &v1, T2 &v2)
Definition: options.h:115
bexit
int bexit(int value)
Exit function for cleanup and debugging.
Definition: utilities.cpp:273
timer_start
double timer_start()
Starts timer and prints the time.
Definition: timer.cpp:52
Bimage::origin
void origin(double ox, double oy, double oz)
Definition: Bimage.h:563
string_kill
int string_kill(Bstring *list)
Kills a string linked list.
Definition: Bstring.cpp:760