| Bsoft 2.0.5 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 p->label(get_cmd_line(argc, argv));
94 write_img(argv[optind], p, 0);
95
96 delete p;
97
98 if ( verbose & VERB_TIME )
99 timer_report(ti);
100
101 bexit(0);
102 }
103
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1743
Bstring::real
double real()
Definition: Bstring.cpp:545
DataType
DataType
Base data type specifier.
Definition: utilities.h:146
Bimage::add
void add(long j, double v)
Definition: Bimage.h:308
SMALLFLOAT
#define SMALLFLOAT
Definition: utilities.h:45
utilities.h
Header file for general utilities.
Boption::value
Bstring value
Definition: options.h:30
Boption::tag
Bstring tag
Definition: options.h:29
Bimage::multiply
void multiply(long j, Complex< double > cv)
Definition: Bimage.h:312
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
read_img
Bimage * read_img(char *filename, int readdata, int img_select)
General driver function to read multiple image formats.
Definition: rwimg.cpp:76
get_cmd_line
string get_cmd_line(int argc, char **argv)
Returns the command line as a string.
Definition: utilities.cpp:36
timer.h
Utilities for timing functions.
Bimage
General image parameter class.
Definition: Bimage.h:163
Bimage::power
void power(double v)
Calculates the power of an image.
Definition: Bimage.cpp:3288
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
write_img
int write_img(const char *filename, Bimage *p, int compression)
General driver function to write multiple image formats.
Definition: rwimg.cpp:280
Boption::datatype
DataType datatype()
Definition: options.h:93
Boption::next
Boption * next
Definition: options.h:28
verbose
int verbose
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
rwimg.h
Header file for 2D and 3D image I/O.
options.h
Header file for option handlers.
MAXOP
#define MAXOP
Definition: bar.cpp:17
main
int main(int argc, char **argv)
Definition: bar.cpp:39
Bimage::data_type
DataType data_type()
Definition: Bimage.h:349
use
const char * use[]
Definition: bar.cpp:20
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