Bsoft  2.1.0
Bernard's software package
bhead.cpp
Go to the documentation of this file.
1 
9 #include "rwimg.h"
10 #include "utilities.h"
11 #include "options.h"
12 #include "versions.h"
13 #include "timer.h"
14 
15 #include <iostream>
16 using namespace std;
17 
18 // Declaration of global variables
19 extern int verbose; // Level of output to the screen
20 
21 // Usage assistance
22 const char* use[] = {
23 " ",
24 "Usage: bhead [options] input.img output.img",
25 "-------------------------------------------",
26 "Reports on and modifies image parameters in the image header.",
27 "Image format extensions supported:",
28 "BioRad (pic), Brix (brx), CCP4 (map), Digital Instruments (di)",
29 "EM (em), Goodford (pot), GRD (grd), Imagic (img), MFF (mff)",
30 "Image Magick (miff), MRC (mrc), PIC (bp), PIF (pif), Spider (spi)",
31 "Suprim (spm), TIFF (tif), RAW (raw)",
32 "Background calculation flag:",
33 " 1=outside enclosing circle",
34 " 2=inside enclosing circle",
35 " 3=inside half-radius circle",
36 " ",
37 "Actions:",
38 "-information Show sub-image information.",
39 "-moments 6 Show sub-image moments up to the indicated order.",
40 "-get size Show selected information.",
41 "-recalculate Force recalculation of statistics.",
42 "-slices Interpret multiple 2D images as z-slices of a single 3D image.",
43 "-images Interpret slices of a single 3D image as 2D images.",
44 "-background 1 Calculate new background values (image not modified).",
45 "-Background 2 Calculate new background values and mask the background.",
46 "-subtractbackground Subtract the background.",
47 "-shiftbackground 1.6 Shift the background to this value.",
48 " ",
49 "Parameters:",
50 "-verbose 7 Verbosity of output.",
51 "-datatype u Force writing of a new data type.",
52 "-select 1 Select an image (first image = 0; default: all images).",
53 "-origin 0,-10.5,30 Set the origin.",
54 "-sampling 1.5,1.5,1.5 Sampling (A/pixel; a single value can be given).",
55 "-Sampling 1.5,1.5,1.5 Sets sampling and unit cell dimensions to fill image (A/pixel).",
56 "-setbackground 0.5 Set the background to this value.",
57 "-symmetry D6 Point group identifier.",
58 "-spacegroup 90 Set space group.",
59 "-unitcell 82,82.3,100,90,66.7,70 Set unit cell parameters (A and degrees).",
60 "-View 0.3,0.5,-0.2,35 Sets the view parameters for every image.",
61 "-label \"New title\" Give a new title or label in the header.",
62 "-fixtype 8 or 16 bit integer types are switched between signed and unsigned.",
63 " ",
64 NULL
65 };
66 
67 int main(int argc, char **argv)
68 {
69  // Initialize variables
70  DataType nudatatype(Unknown_Type); // Conversion to new type
71  int show_sub_images(0); // Flag to show sub-image info
72  int show_moments(0); // Flag to show sub-image moments
73  Bstring get; // Tag for selected information
74  long setimg(-1); // Select all images
75  Vector3<double> origin; // Origin
76  int set_origin(0); // Flag to set origin
77  int set_stats(0);
78  int set_sampling(0); // 1=set sampling, 2=set sampling and reset unitcell
79  Vector3<double> sam; // Units for the three axes (A/pixel)
80  Bstring symmetry_string; // No symmetry specified
81  int spacegroup(0); // Space group
82  UnitCell uc; // Unit cell parameters
83  int calc_background(0); // Flag to calculate background values
84  int correct_background(0); // Flag to correct backgrounds
85  int set_background(0); // Flag to set backgrounds
86  int subtract_background(0); // Flag to subtract backgrounds
87  int shift_background(0); // Flag to shift backgrounds
88  double newbackground(0); // New background
89  View view; // View parameters
90  int set_view(0); // Flag to set the view
91  Bstring label; // Header title or label
92  int znswitch(0); // 0=not, 1=n2z, 2=z2n
93  int fixtype(0); // Flag to switch between signed and unsigned
94  int dataflag(0); // Flag to set if data must be read
95 
96  int optind;
97  Boption* option = get_option_list(use, argc, argv, optind);
98  Boption* curropt;
99  for ( curropt = option; curropt; curropt = curropt->next ) {
100  if ( curropt->tag == "datatype" )
101  nudatatype = curropt->datatype();
102  if ( curropt->tag == "information" )
103  show_sub_images = 1;
104  if ( curropt->tag == "moments" )
105  if ( ( show_moments = curropt->value.integer() ) < 0 )
106  cerr << "-moments: A number of orders must be specified!" << endl;
107  if ( curropt->tag == "get" )
108  get = curropt->value.lower();
109  if ( curropt->tag == "recalculate" )
110  set_stats = dataflag = 1;
111  if ( curropt->tag == "select" )
112  if ( ( setimg = curropt->value.integer() ) < 0 )
113  cerr << "-select: An image number must be specified!" << endl;
114  if ( curropt->tag == "origin" ) {
115  if ( curropt->value[0] == 'c' ) {
116  set_origin = 2;
117  } else {
118  origin = curropt->origin();
119  set_origin = 1;
120  }
121  }
122  if ( curropt->tag == "sampling" ) {
123  sam = curropt->scale();
124  set_sampling = 1;
125  }
126  if ( curropt->tag == "Sampling" ) {
127  sam = curropt->scale();
128  if ( sam[0] > 0 ) set_sampling = 2;
129  }
130  if ( curropt->tag == "symmetry" )
131  symmetry_string = curropt->symmetry_string();
132  if ( curropt->tag == "spacegroup" )
133  if ( ( spacegroup = curropt->value.integer() ) < 1 )
134  cerr << "-spacegroup: A valid space group must be specified!" << endl;
135  if ( curropt->tag == "unitcell" )
136  uc = curropt->unit_cell();
137  if ( curropt->tag == "background" ) {
138  dataflag = 1;
139  if ( ( calc_background = curropt->value.integer() ) < 1 )
140  cerr << "-background: A calculation type must be specified!" << endl;
141  }
142  if ( curropt->tag == "Background" ) {
143  dataflag = 1;
144  if ( ( correct_background = curropt->value.integer() ) < 1 )
145  cerr << "-Background: A calculation type must be specified!" << endl;
146  }
147  if ( curropt->tag == "setbackground" ) {
148  newbackground = curropt->value.real();
149  set_background = 1;
150  }
151  if ( curropt->tag == "subtractbackground" )
152  subtract_background = dataflag = 1;
153  if ( curropt->tag == "shiftbackground" ) {
154  newbackground = curropt->value.real();
155  shift_background = dataflag = 1;
156  }
157  if ( curropt->tag == "View" ) {
158  view = curropt->view();
159  set_view = 1;
160  }
161  if ( curropt->tag == "label" )
162  label = curropt->value;
163  if ( curropt->tag == "slices" )
164  znswitch = 1;
165  if ( curropt->tag == "images" )
166  znswitch = 2;
167  if ( curropt->tag == "fixtype" )
168  fixtype = 1;
169  }
170  option_kill(option);
171 
172  double ti = timer_start();
173 
174  if ( verbose & VERB_PROCESS ) systype(1);
175 
176  if ( verbose & VERB_PROCESS ) cout << "Number of processors: " << system_processors() << endl;
177 
178  if ( get.contains("lib") || get.contains("ver") )
180  if ( optind >= argc ) bexit(0);
181 
182  // Read image file
183  if ( set_stats || show_moments || calc_background || optind < argc - 1 ) dataflag = 1;
184  Bimage* p = read_img(argv[optind++], dataflag, setimg);
185  if ( !p ) bexit(-1);
186 
187  if ( fixtype ) p->fix_type();
188 
189  if ( znswitch == 1 )
190  if ( p->images_to_slices() < 0 ) bexit(-1);
191 
192  if ( znswitch == 2 )
193  if ( p->slices_to_images() < 0 ) bexit(-1);
194 
195  if ( set_stats ) {
196  p->statistics();
197  if ( verbose ) show_sub_images = 1;
198  }
199 
200  if ( calc_background ) {
201  p->calculate_background(calc_background);
202  if (verbose ) show_sub_images = 1;
203  }
204 
205  if ( optind < argc ) {
206  if ( correct_background ) {
207  p->correct_background(correct_background);
208  if (verbose ) show_sub_images = 1;
209  }
210 
211  if ( set_background ) {
212  p->background(newbackground);
213  if (verbose ) show_sub_images = 1;
214  }
215 
216  if ( shift_background ) {
217  p->shift_background(newbackground);
218  if (verbose ) show_sub_images = 1;
219  }
220 
221  if ( subtract_background ) {
222  p->subtract_background();
223  if (verbose ) show_sub_images = 1;
224  }
225 
226  if ( set_view ) p->view(view);
227 
228 /* if ( set_minmax ) {
229  p->min = p->smin = min;
230  p->max = p->smax = max;
231  }
232 */
233  if ( set_sampling ) {
234  p->sampling(sam);
235  if ( set_sampling > 1 ) p->unit_cell(UnitCell());
236  }
237 
238  if ( set_origin ) {
239  if ( set_origin == 2 ) p->origin(p->default_origin());
240  else p->origin(origin);
241  }
242 
243  if ( symmetry_string.length() ) p->symmetry(symmetry_string.str());
244 
245  if ( uc.check() ) p->unit_cell(uc);
246 
247  if ( spacegroup ) p->space_group(spacegroup);
248 
249  if ( nudatatype != Unknown_Type ) p->change_type(nudatatype);
250 
251  if ( label.length() ) p->label(label.str());
252 
253  write_img(argv[optind], p, 0);
254  }
255 
256  if ( show_sub_images ) p->subimage_information();
257 
258  if ( show_moments ) p->moments(show_moments);
259 
260  if ( get.length() ) p->get(get);
261 
262  delete p;
263 
264  if ( verbose & VERB_TIME )
265  timer_report(ti);
266 
267  bexit(0);
268 }
Boption
A class for a command line input option.
Definition: options.h:26
versions.h
Library functions to encode versions of all included packages.
Bimage::symmetry
string & symmetry()
Definition: Bimage.h:603
Boption::unit_cell
UnitCell unit_cell()
Definition: options.h:279
Boption::scale
Vector3< double > scale()
Definition: options.h:190
Bimage::get
double get(long nn, long xx, long yy, long zz, long cc=0)
Returns the data value at the given coordinates.
Definition: Bimage.cpp:960
system_processors
size_t system_processors()
Returns the number of processors.
Definition: utilities.cpp:193
Bstring
Definition: Bstring.h:29
Bstring::integer
long integer()
Definition: Bstring.cpp:547
systype
SysType systype(int show)
Finds the system type - mostly just for byte order.
Definition: utilities.cpp:151
Boption::datatype
DataType datatype()
Definition: options.h:93
Boption::next
Boption * next
Definition: options.h:28
Vector3< double >
Bimage::unit_cell
void unit_cell(UnitCell uc)
Definition: Bimage.h:608
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
Bstring::real
double real()
Definition: Bstring.cpp:553
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
View
Definition: View.h:26
Boption::origin
Vector3< double > origin()
Definition: options.h:180
Boption::view
View view()
Definition: options.h:247
Bimage::correct_background
int correct_background(long nn, int flag)
Corrects the background for one sub-image.
Definition: Bimage_background.cpp:202
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:111
Bimage::images_to_slices
int images_to_slices()
Changes the 2D images to slices in a 3D image.
Definition: Bimage.cpp:2115
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
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::shift_background
int shift_background(double bkg)
Sets the background for each sub-image to a given value.
Definition: Bimage_background.cpp:351
Bstring::str
string str()
Definition: Bstring.h:42
Bimage::slices_to_images
int slices_to_images()
Changes the slices in a 3D image into a set of 2D images.
Definition: Bimage.cpp:2074
Boption::value
Bstring value
Definition: options.h:30
Bimage
General image parameter class.
Definition: Bimage.h:177
timer.h
Utilities for timing functions.
Unknown_Type
@ Unknown_Type
Definition: utilities.h:151
main
int main(int argc, char **argv)
Definition: bhead.cpp:67
Bimage::fix_type
void fix_type()
Determines the replacement data type.
Definition: Bimage.cpp:1740
Boption::symmetry_string
Bstring symmetry_string()
Definition: options.h:233
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.
UnitCell::check
bool check()
Definition: UnitCell.h:34
use
const char * use[]
Definition: bhead.cpp:22
Bimage::moments
int moments(long max_order)
Prints out moments for all sub-images.
Definition: Bimage.cpp:2607
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
rwimg.h
Header file for 2D and 3D image I/O.
Bimage::space_group
long space_group()
Definition: Bimage.h:598
Bimage::subimage_information
int subimage_information()
Prints out header information for all sub-images.
Definition: Bimage.cpp:2571
option_kill
int option_kill(Boption *option)
Deallocates a linked list of option structures.
Definition: options.cpp:111
Bimage::sampling
void sampling(long nn, Vector3< double > u)
Definition: Bimage.h:514
UnitCell
Definition: UnitCell.h:22
Bimage::subtract_background
int subtract_background()
Subtracts the background for each sub-image.
Definition: Bimage_background.cpp:337
DataType
DataType
Base data type specifier.
Definition: utilities.h:150
Bstring::contains
bool contains(const Bstring &s)
Definition: Bstring.cpp:592
show_library_versions
int show_library_versions()
Displaying library versions.
Definition: versions.h:22
Bstring::lower
Bstring lower()
Definition: Bstring.cpp:316
Bimage::view
void view(double vx, double vy, double vz, double va)
Definition: Bimage.h:596
Bimage::statistics
long statistics()
Calculates the statistics for an image.
Definition: Bimage_stats.cpp:22
Bimage::label
void label(string s)
Definition: Bimage.h:250
verbose
int verbose