Bsoft  2.1.0
Bernard's software package
bconvert.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 // Usage assistance
18 const char* use[] = {
19 " ",
20 "Usage: bconvert [options] input.images",
21 "--------------------------------------",
22 "Converts a list of images into a new format, using the original names and replacing the extension.",
23 "Any number of input images may be given and may include the wild card \"*\".",
24 "(However, VMS does not support Unix style usage of wild cards).",
25 " ",
26 "Actions:",
27 "-rescale -0.1,5.2 Rescale input images to average and standard deviation.",
28 "-extension zyx Change the file extension after writing.",
29 " ",
30 "Parameters:",
31 "-verbose 3 Verbosity of output.",
32 "-datatype u Force writing of a new data type.",
33 "-sampling 1.5,1.5,1.5 Sampling (A/pixel; a single value can be given).",
34 "-origin 0.8,-10,15.7 Set the origin (default from input image).",
35 "-raw d=f#x=120,120,1 Formatting to reinterpret image files.",
36 " ",
37 "Output:",
38 "-output mrc Output format (default pif).",
39 " ",
40 NULL
41 };
42 
43 int main(int argc, char **argv)
44 {
45  // Initialize variables
46  DataType nudatatype(Unknown_Type); // Conversion to new type
47  Vector3<double> sam; // Units for the three axes (A/pixel)
48  Vector3<double> origin; // New image origin
49  int set_origin(0); // Flag to set origin
50  Bstring nuformat("pif"); // New image file format
51  Bstring nuext; // New extension after writing
52  Bstring rawstring;
53  int setrescale(0);
54  double nuavg(0), nustd(0); // Rescale image
55 
56  int optind;
57  Boption* option = get_option_list(use, argc, argv, optind);
58  Boption* curropt;
59  for ( curropt = option; curropt; curropt = curropt->next ) {
60  if ( curropt->tag == "rescale" ) {
61  if ( curropt->values(nuavg, nustd) < 2 )
62  cerr << "-rescale: Both averge and standard deviation must be specified!" << endl;
63  else
64  setrescale = 1;
65  if ( nustd <= 0 ) setrescale = 0;
66  }
67  if ( curropt->tag == "extension" ) nuext = curropt->value;
68  if ( curropt->tag == "datatype" )
69  nudatatype = curropt->datatype();
70  if ( curropt->tag == "sampling" )
71  sam = curropt->scale();
72  if ( curropt->tag == "origin" ) {
73  if ( curropt->value[0] == 'c' ) {
74  set_origin = 2;
75  } else {
76  origin = curropt->origin();
77  set_origin = 1;
78  }
79  }
80  if ( curropt->tag == "raw" ) {
81  rawstring = curropt->value;
82  if ( rawstring.length() < 1 )
83  cerr << "-raw: A valid format reinterpretation string must be specified!" << endl;
84  }
85  if ( curropt->tag == "output" ) {
86  nuformat = curropt->value;
87  if ( nuformat.length() < 1 )
88  cerr << "-output: An output format must be specified!" << endl;
89  }
90  }
91  option_kill(option);
92 
93  if ( verbose && argc < 3 ) {
94  cerr << "Error: No input files given!" << endl;
95  bexit(-1);
96  }
97 
98  double ti = timer_start();
99 
100  // Read the images and convert them
101  Bimage* p = NULL;
102  Bstring filename, nuname;
103 
104  if ( nuformat[0] != '.' ) nuformat = "." + nuformat;
105  if ( nuext.length() && nuext[0] != '.' ) nuext = "." + nuext;
106  if ( rawstring.length() > 0 && rawstring[0] != '#' ) rawstring = "#" + rawstring;
107 
108  while ( optind < argc ) {
109  filename = argv[optind++];
110  filename += rawstring;
111  p = read_img(filename, 1, -1);
112  if ( p != NULL ) {
113  if ( nudatatype == Unknown_Type )
114  nudatatype = p->data_type();
115  else if ( nudatatype > p->data_type() )
116  p->change_type(nudatatype);
117  if ( setrescale ) p->rescale_to_avg_std(nuavg, nustd);
118  p->statistics();
120  if ( sam.volume() > 0 ) p->sampling(sam);
121  if ( set_origin ) {
122  if ( set_origin == 2 ) p->origin(p->default_origin());
123  else p->origin(origin);
124  }
125  filename = p->file_name();
126  filename = filename.pre_rev('.') + nuformat;
127  p->change_type(nudatatype);
128  write_img(filename, p, 0);
129  delete p;
130  if ( nuext.length() ) {
131  nuname = filename.pre_rev('.') + nuext;
132  if ( rename(filename.c_str(), nuname.c_str()) )
133  cerr << "File " << filename << " not renamed to " << nuname << endl;
134  }
135  }
136  }
137 
138  if ( verbose & VERB_TIME )
139  timer_report(ti);
140 
141  bexit(0);
142 }
143 
Boption
A class for a command line input option.
Definition: options.h:26
Boption::scale
Vector3< double > scale()
Definition: options.h:190
Bstring
Definition: Bstring.h:29
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
main
int main(int argc, char **argv)
Definition: bconvert.cpp:43
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
Boption::origin
Vector3< double > origin()
Definition: options.h:180
Bstring::c_str
const char * c_str()
Definition: Bstring.h:70
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
write_img
int write_img(const char *filename, Bimage *p, int compression)
General driver function to write multiple image formats.
Definition: rwimg.cpp:281
Boption::value
Bstring value
Definition: options.h:30
Bstring::pre_rev
Bstring pre_rev(char c)
Definition: Bstring.cpp:421
Bimage
General image parameter class.
Definition: Bimage.h:177
timer.h
Utilities for timing functions.
Unknown_Type
@ Unknown_Type
Definition: utilities.h:151
VERB_TIME
#define VERB_TIME
Definition: utilities.h:114
use
const char * use[]
Definition: bconvert.cpp:18
Bimage::file_name
void file_name(string s)
Definition: Bimage.h:245
timer_start
double timer_start()
Starts timer and prints the time.
Definition: timer.cpp:61
Bimage::data_type
DataType data_type()
Definition: Bimage.h:362
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.
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
verbose
int verbose
DataType
DataType
Base data type specifier.
Definition: utilities.h:150
Bimage::statistics
long statistics()
Calculates the statistics for an image.
Definition: Bimage_stats.cpp:22
Bimage::rescale_to_avg_std
int rescale_to_avg_std(double nuavg, double nustd)
Rescales the image data to a given average and standard deviation.
Definition: Bimage_rescale.cpp:163