Bsoft  2.1.0
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->scale();
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  write_img(sumfile, psum, 0);
147  }
148 
149  if ( fomfile.length() )
150  write_img(fomfile, psum->next, 0);
151 
152  delete psum;
153  string_kill(file_list);
154 
155  if ( verbose & VERB_TIME )
156  timer_report(ti);
157 
158  bexit(0);
159 }
Boption
A class for a command line input option.
Definition: options.h:26
use
const char * use[]
Definition: badd.cpp:19
Boption::scale
Vector3< double > scale()
Definition: options.h:190
string_kill
int string_kill(Bstring *list)
Kills a string linked list.
Definition: Bstring.cpp:768
Bstring
Definition: Bstring.h:29
Bimage::next
Bimage * next
Definition: Bimage.h:179
Boption::datatype
DataType datatype()
Definition: options.h:93
Boption::next
Boption * next
Definition: options.h:28
Vector3< double >
bexit
int bexit(int value)
Exit function for cleanup and debugging.
Definition: utilities.cpp:280
Bimage::default_origin
Vector3< double > default_origin()
Definition: Bimage.h:592
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
options.h
Header file for option handlers.
Boption::origin
Vector3< double > origin()
Definition: options.h:180
Boption::tag
Bstring tag
Definition: options.h:29
Boption::values
long values(T1 &v1, T2 &v2)
Definition: options.h:115
write_img
int write_img(const char *filename, Bimage *p, int compression)
General driver function to write multiple image formats.
Definition: rwimg.cpp:281
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:291
Boption::value
Bstring value
Definition: options.h:30
Bimage
General image parameter class.
Definition: Bimage.h:177
timer.h
Utilities for timing functions.
main
int main(int argc, char **argv)
Definition: badd.cpp:54
Unknown_Type
@ Unknown_Type
Definition: utilities.h:151
Bstring::split_into_doubles
vector< double > split_into_doubles(const Bstring &delim)
Definition: Bstring.cpp:673
VERB_TIME
#define VERB_TIME
Definition: utilities.h:114
timer_start
double timer_start()
Starts timer and prints the time.
Definition: timer.cpp:61
utilities.h
Header file for general utilities.
Bstring::length
long length() const
Definition: Bstring.cpp:263
timer_report
double timer_report(double lasttime)
Reports the time elapsed since a given time.
Definition: timer.cpp:76
Bimage::origin
void origin(double ox, double oy, double oz)
Definition: Bimage.h:570
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1765
Vector3::volume
double volume()
Definition: Vector3.h:237
rwimg.h
Header file for 2D and 3D image I/O.
Boption::filename
Bstring filename()
Definition: options.h:85
option_kill
int option_kill(Boption *option)
Deallocates a linked list of option structures.
Definition: options.cpp:111
img_combine.h
Header file for combining two images in various ways.
Bimage::sampling
void sampling(long nn, Vector3< double > u)
Definition: Bimage.h:514
DataType
DataType
Base data type specifier.
Definition: utilities.h:150
string_add
Bstring * string_add(Bstring **list, const char *string)
Adds a string to a linked list.
Definition: Bstring.cpp:713
verbose
int verbose