Bsoft  2.1.0
Bernard's software package
bar.cpp
Go to the documentation of this file.
1 
9 #include "rwimg.h"
10 #include "utilities.h"
11 #include "options.h"
12 #include "timer.h"
13 
14 // Declaration of global variables
15 extern int verbose; // Level of output to the screen
16 
17 #define MAXOP 16
18 
19 // Usage assistance
20 const char* use[] = {
21 " ",
22 "Usage: bar [options] input.img output.img",
23 "-----------------------------------------",
24 "Does simple arithmetic on images.",
25 "Operations are done in the order given on the command line.",
26 " ",
27 "Actions:",
28 "-add -5.78 Add a constant to each voxel.",
29 "-multiply 6.123 Multiply each voxel with a constant.",
30 "-power 0.75 Raise to the power (must be positive).",
31 " ",
32 "Parameters:",
33 "-verbose 7 Verbosity of output.",
34 "-datatype u Force writing of a new data type.",
35 " ",
36 NULL
37 };
38 
39 int main(int argc, char **argv)
40 {
41  // Initialize variables
42  DataType nudatatype(Unknown_Type); // Conversion to new type
43  double add[MAXOP]; // Additions
44  double multiply[MAXOP]; // Multipliers
45  double power[MAXOP]; // Powers
46 
47  int i;
48  for ( i=0; i<MAXOP; i++ ) add[i] = multiply[i] = power[i] = 0;
49 
50  int n(0), optind;
51  Boption* option = get_option_list(use, argc, argv, optind);
52  Boption* curropt;
53  for ( curropt = option; curropt; curropt = curropt->next ) {
54  if ( curropt->tag == "datatype" )
55  nudatatype = curropt->datatype();
56  if ( curropt->tag == "add" ) {
57  if ( ( add[n] = curropt->value.real() ) == 0 )
58  cerr << "-add: A constant must be specified!" << endl;
59  else
60  n++;
61  }
62  if ( curropt->tag == "multiply" ) {
63  if ( ( multiply[n] = curropt->value.real() ) ==0 )
64  cerr << "-multiply: A constant must be specified!" << endl;
65  else
66  n++;
67  }
68  if ( curropt->tag == "power" ) {
69  if ( ( power[n] = curropt->value.real() ) == 0 )
70  cerr << "-power: A constant must be specified!" << endl;
71  else
72  n++;
73  }
74  }
75  option_kill(option);
76 
77  double ti = timer_start();
78 
79  Bimage* p = read_img(argv[optind++], 1, -1);
80  if ( p == NULL ) bexit(-1);
81 
82  if ( optind >= argc ) bexit(0);
83 
84  if ( nudatatype > p->data_type() ) p->change_type(nudatatype);
85 
86  for ( i=0; i<MAXOP && i<n; i++ ) {
87  if ( fabs(add[i]) > SMALLFLOAT ) p->add(add[i]);
88  if ( fabs(multiply[i]) > SMALLFLOAT ) p->multiply(multiply[i]);
89  if ( fabs(power[i]) > SMALLFLOAT ) p->power(power[i]);
90  }
91 
92  p->change_type(nudatatype);
93  write_img(argv[optind], p, 0);
94 
95  delete p;
96 
97  if ( verbose & VERB_TIME )
98  timer_report(ti);
99 
100  bexit(0);
101 }
102 
Boption
A class for a command line input option.
Definition: options.h:26
Boption::datatype
DataType datatype()
Definition: options.h:93
Boption::next
Boption * next
Definition: options.h:28
bexit
int bexit(int value)
Exit function for cleanup and debugging.
Definition: utilities.cpp:280
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
main
int main(int argc, char **argv)
Definition: bar.cpp:39
Bstring::real
double real()
Definition: Bstring.cpp:553
options.h
Header file for option handlers.
Boption::tag
Bstring tag
Definition: options.h:29
read_img
Bimage * read_img(char *filename, int readdata, int img_select)
General driver function to read multiple image formats.
Definition: rwimg.cpp:77
Bimage::multiply
void multiply(long j, Complex< double > cv)
Definition: Bimage.h:325
write_img
int write_img(const char *filename, Bimage *p, int compression)
General driver function to write multiple image formats.
Definition: rwimg.cpp:281
Bimage::add
void add(long j, double v)
Definition: Bimage.h:321
SMALLFLOAT
#define SMALLFLOAT
Definition: utilities.h:46
Boption::value
Bstring value
Definition: options.h:30
Bimage
General image parameter class.
Definition: Bimage.h:177
timer.h
Utilities for timing functions.
Bimage::power
void power(double v)
Calculates the power of an image.
Definition: Bimage.cpp:3291
Unknown_Type
@ Unknown_Type
Definition: utilities.h:151
VERB_TIME
#define VERB_TIME
Definition: utilities.h:114
timer_start
double timer_start()
Starts timer and prints the time.
Definition: timer.cpp:61
use
const char * use[]
Definition: bar.cpp:20
Bimage::data_type
DataType data_type()
Definition: Bimage.h:362
utilities.h
Header file for general utilities.
timer_report
double timer_report(double lasttime)
Reports the time elapsed since a given time.
Definition: timer.cpp:76
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1765
rwimg.h
Header file for 2D and 3D image I/O.
option_kill
int option_kill(Boption *option)
Deallocates a linked list of option structures.
Definition: options.cpp:111
DataType
DataType
Base data type specifier.
Definition: utilities.h:150
verbose
int verbose
MAXOP
#define MAXOP
Definition: bar.cpp:17