Bsoft  2.1.0
Bernard's software package
b3v.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 
18 // Usage assistance
19 const char* use[] = {
20 " ",
21 "Usage: b3v [options] input.img output.img",
22 "-----------------------------------------",
23 "Generates 3 orthogonal views of a map.",
24 " ",
25 "Actions:",
26 "-orthogonal 24,67,45 Extract orthogonal slices around this voxel.",
27 " The voxel can be specified as center or origin.",
28 "-montage 2 Montage the slices into a number of columns.",
29 " ",
30 "Parameters:",
31 "-verbose 7 Verbosity of output.",
32 "-datatype u Force writing of a new data type.",
33 "-select 5 Image number.",
34 "-size 100,80,120 Size of orthogonal slices to extract.",
35 "-pad 5 Padding to add around slices or images (default 0).",
36 "-fill 127 Fill value for padding (default average).",
37 " ",
38 NULL
39 };
40 
41 int main(int argc, char **argv)
42 {
43  // Initialize variables
44  DataType nudatatype(Unknown_Type); // Conversion to new type
45  int orth_type(0); // Type of voxel specification: 1=center, 2=origin, 3=coor
46  long montage_cols(0); // Number of montage columns
47  Vector3<long> voxel; // Intersection of slices
48  long img_num(0); // Image number
49  Vector3<long> size; // Size of slices to extract
50  int pad(0); // Padding around images
51  double fill(0); // Fill value for resizing
52  int fill_type(FILL_BACKGROUND); // Fill type for resizing
53 
54  int optind;
55  Boption* option = get_option_list(use, argc, argv, optind);
56  Boption* curropt;
57  for ( curropt = option; curropt; curropt = curropt->next ) {
58  if ( curropt->tag == "datatype" )
59  nudatatype = curropt->datatype();
60  if ( curropt->tag == "montage" )
61  if ( ( montage_cols = curropt->value.integer() ) < 1 )
62  cerr << "-montage: A number of columns must be specified!" << endl;
63  if ( curropt->tag == "orthogonal" ) {
64  if ( curropt->value[0] == 'c' ) orth_type = 1;
65  else if ( curropt->value[0] == 'o' ) orth_type = 2;
66  else {
67  if ( curropt->values(voxel[0], voxel[1], voxel[2]) < 3 )
68  cerr << "-orthogonal: A voxel must be specified!" << endl;
69  else orth_type = 3;
70  }
71  }
72  if ( curropt->tag == "select" )
73  if ( ( img_num = curropt->value.integer() ) < 0 )
74  cerr << "-select: Image numbers start from 0!" << endl;
75  if ( curropt->tag == "size" )
76  size = curropt->size();
77  if ( curropt->tag == "pad" )
78  if ( ( pad = curropt->value.integer() ) < 1 )
79  cerr << "-pad: A number of pixels must be specified!" << endl;
80  if ( curropt->tag == "fill" )
81  fill = curropt->fill(fill_type);
82  }
83  option_kill(option);
84 
85  double ti = timer_start();
86 
87  Bimage* p = read_img(argv[optind++], 1, -1);
88  if ( p == NULL ) bexit(-1);
89 
90  if ( optind >= argc ) {
91  delete p;
92  bexit(0);
93  }
94 
95  Bimage* p3 = NULL;
96 
97  if ( fill_type == FILL_AVERAGE ) fill = p->average();
98  if ( fill_type == FILL_BACKGROUND ) {
99  if ( fabs(p->background(long(0))) < 1e-6 )
101  fill = p->background(long(0));
102  }
103  if ( fill_type == FILL_MIN ) fill = p->minimum();
104  if ( fill_type == FILL_MAX ) fill = p->maximum();
105 
106  p->background(fill);
107  fill_type = FILL_BACKGROUND;
108 
109  if ( orth_type ) {
110  if ( orth_type < 3 ) {
111  Vector3<double> vori;
112  if ( orth_type == 1 )
113  vori = p->default_origin();
114  else if ( orth_type == 2 )
115  vori = p->image[img_num].origin();
116  voxel = Vector3<long>(vori[0], vori[1], vori[2]);
117  }
118  p3 = p->orthogonal_slices(img_num, voxel, size);
119  delete p;
120  p = p3;
121 
122  if ( pad ) {
123  Vector3<long> newsize = {p->sizeX()+2*pad, p->sizeY()+2*pad, p->sizeZ()};
124  Vector3<long> translate = {pad, pad, 0};
125  p->resize(newsize, translate, fill_type, fill);
126  }
127 
128  if ( montage_cols ) {
129  p3 = p->montage(0, montage_cols, 4 - montage_cols, 0, 1);
130  delete p;
131  p = p3;
132  }
133  }
134 
135  p->change_type(nudatatype);
136  write_img(argv[optind], p, 0);
137 
138  delete p;
139 
140  if ( verbose & VERB_TIME )
141  timer_report(ti);
142 
143  bexit(0);
144 }
145 
Boption
A class for a command line input option.
Definition: options.h:26
Bimage::sizeZ
void sizeZ(long nz)
Definition: Bimage.h:392
Bstring::integer
long integer()
Definition: Bstring.cpp:547
Boption::datatype
DataType datatype()
Definition: options.h:93
Boption::next
Boption * next
Definition: options.h:28
Vector3< long >
Bimage::sizeX
void sizeX(long nx)
Definition: Bimage.h:390
use
const char * use[]
Definition: b3v.cpp:19
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
Bimage::background
double background(long nn)
Definition: Bimage.h:549
FILL_AVERAGE
#define FILL_AVERAGE
Definition: utilities.h:177
options.h
Header file for option handlers.
Bimage::calculate_background
int calculate_background(long nn, int flag)
Calculates the background for one sub-image.
Definition: Bimage_background.cpp:27
Bimage::image
Bsub_image * image
Definition: Bimage.h:202
Bimage::maximum
double maximum()
Definition: Bimage.h:542
Boption::tag
Bstring tag
Definition: options.h:29
Boption::values
long values(T1 &v1, T2 &v2)
Definition: options.h:115
read_img
Bimage * read_img(char *filename, int readdata, int img_select)
General driver function to read multiple image formats.
Definition: rwimg.cpp:77
Bsub_image::origin
void origin(double x, double y, double z)
Definition: Bimage.h:136
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::sizeY
void sizeY(long ny)
Definition: Bimage.h:391
Boption::value
Bstring value
Definition: options.h:30
Bimage
General image parameter class.
Definition: Bimage.h:177
Bimage::average
double average(long cc, double xf, double yf, double zf, long nn, double iscale)
Averages in 3D when scaling is less than 1.
Definition: Bimage.cpp:1371
timer.h
Utilities for timing functions.
Unknown_Type
@ Unknown_Type
Definition: utilities.h:151
main
int main(int argc, char **argv)
Definition: b3v.cpp:41
verbose
int verbose
VERB_TIME
#define VERB_TIME
Definition: utilities.h:114
Bimage::montage
Bimage * montage(int first, int cols, int rows, int skip=0, int flipy=0)
Rearranges an image into a montage of 2D slices for display.
Definition: Bimage_montage.cpp:49
timer_start
double timer_start()
Starts timer and prints the time.
Definition: timer.cpp:61
utilities.h
Header file for general utilities.
Boption::size
Vector3< long > size()
Definition: options.h:163
Boption::fill
double fill(int &fill_type)
Definition: options.h:138
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
FILL_BACKGROUND
#define FILL_BACKGROUND
Definition: utilities.h:178
Bimage::minimum
double minimum()
Definition: Bimage.h:541
FILL_MIN
#define FILL_MIN
Definition: utilities.h:179
DataType
DataType
Base data type specifier.
Definition: utilities.h:150
Bimage::orthogonal_slices
Bimage * orthogonal_slices(long nn, Vector3< long > voxel, Vector3< long > ext_size)
Extracts orthogonal views around a voxel.
Definition: Bimage_extract.cpp:616
FILL_MAX
#define FILL_MAX
Definition: utilities.h:180
Bimage::resize
int resize(Vector3< long > nusize, Vector3< long > translate, int fill_type=0, double fill=0)
Resizes without interpolation or rescaling.
Definition: Bimage_resize.cpp:28